]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleActiveVariable.java
New implementation NearestOwnerFinder of CommonDBUtils.getNearestOwner
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PossibleActiveVariable.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.request;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.ResourceArray;
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.common.request.UnaryRead;
19 import org.simantics.db.exception.AdaptionException;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.ResourceNotFoundException;
22 import org.simantics.db.layer0.exception.MissingVariableException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.db.layer0.variable.VariableInterface;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.simulation.ontology.SimulationResource;
27
28 public class PossibleActiveVariable extends UnaryRead<ResourceArray, Variable> {
29
30     public PossibleActiveVariable(ResourceArray resource) {
31         super(resource);
32     }
33
34     @Override
35     public Variable perform(ReadGraph graph) throws DatabaseException {
36
37         try {
38
39             Resource tail = parameter.tail();
40             Resource head = parameter.head();
41
42             if(!graph.hasStatement(tail)) return null;
43
44                 Layer0 L0 = Layer0.getInstance(graph);
45             SimulationResource SIMU = SimulationResource.getInstance(graph);
46
47             String valuationName = null;
48
49             //Valuations valuations = graph.adapt(parameter, Valuations.class);
50
51             Resource diagram = graph.syncRequest(new PossibleDiagram(tail));
52             if(diagram != null) {
53
54                 String valuation = graph.getPossibleRelatedValue(diagram, SIMU.HasActiveExperiment);
55                 if(valuation != null) valuationName = valuation;
56
57             }
58
59             Resource model = graph.syncRequest(new PossibleModel(head));
60             if (model == null) return null;
61
62             if(valuationName == null) {
63                 String valuation = graph.syncRequest(new PossibleActiveExperimentPath(model));
64                 if(valuation != null) valuationName = valuation;
65             }
66
67             if (valuationName == null) {
68                 valuationName = "/BaseRealization";
69             }
70
71             VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);
72             Variable modelVariable = graph.adapt(model, Variable.class);
73             Variable experimentVariable = modelVariable.browse(graph, valuationName);
74             return variableInterface.getVariable(graph, experimentVariable, parameter);
75
76         } catch (MissingVariableException e) {
77             if (DebugException.DEBUG)
78                 e.printStackTrace();
79             return null;
80         } catch (ResourceNotFoundException e) {
81             if (DebugException.DEBUG)
82                 e.printStackTrace();
83             return null;
84         } catch (AdaptionException e) {
85             if (DebugException.DEBUG)
86                 e.printStackTrace();
87             return null;
88         } catch (Throwable e) {
89             if (DebugException.DEBUG)
90                 e.printStackTrace();
91             return null;
92         }
93
94     }
95
96 }