]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleSessionVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PossibleSessionVariable.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.request;\r
13 \r
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.common.ResourceArray;\r
18 import org.simantics.db.common.exception.DebugException;\r
19 import org.simantics.db.common.request.BinaryRead;\r
20 import org.simantics.db.exception.AdaptionException;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.exception.ResourceNotFoundException;\r
23 import org.simantics.db.layer0.exception.MissingVariableException;\r
24 import org.simantics.db.layer0.variable.Variable;\r
25 import org.simantics.db.layer0.variable.VariableInterface;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.scl.reflection.OntologyVersions;\r
28 import org.simantics.simulation.ontology.SimulationResource;\r
29 \r
30 public class PossibleSessionVariable extends BinaryRead<ResourceArray, String, Variable> {\r
31 \r
32     public PossibleSessionVariable(ResourceArray resource, String identifier) {\r
33         super(resource, identifier);\r
34     }\r
35 \r
36     @Override\r
37     public Variable perform(ReadGraph graph) throws DatabaseException {\r
38 \r
39         try {\r
40 \r
41                 String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/CompositeToDiagram");\r
42                 Resource map = graph.getResource(uri);\r
43 \r
44             Resource tail = parameter.tail();\r
45             Resource head = parameter.head();\r
46 \r
47             if(!graph.hasStatement(tail)) return null;\r
48 \r
49             Layer0 l0 = Layer0.getInstance(graph);\r
50 \r
51             String valuationName = null;\r
52 \r
53             Resource diagram = graph.syncRequest(new PossibleDiagram(tail));\r
54             if(diagram == null) {\r
55                 Resource d = diagram;\r
56                 while((d = graph.getPossibleObject(d, l0.PartOf)) != null) {\r
57                     diagram = graph.getPossibleObject(d, map);\r
58                     if(diagram != null) break;\r
59                 }\r
60             }\r
61 \r
62             if(diagram != null) {\r
63                 SimulationResource SIMU = SimulationResource.getInstance(graph);\r
64                 for(Resource run : graph.getObjects(diagram, l0.ConsistsOf)) {\r
65                     String name = graph.getPossibleRelatedValue(run, l0.HasName, Bindings.STRING);\r
66                     if(parameter2.equals(name)) {\r
67                         valuationName = graph.getPossibleRelatedValue(run, SIMU.HasActiveExperiment);\r
68                         break;\r
69                     }\r
70                 }\r
71             }\r
72 \r
73 \r
74             Resource model = graph.syncRequest(new PossibleModel(head));\r
75             if (model == null) return null;\r
76 \r
77 //            if(valuationName == null) {\r
78 //              String valuation = graph.syncRequest(new PossibleActiveExperiment(model));\r
79 //              if(valuation != null) valuationName = valuation;\r
80 //            }\r
81 \r
82             if (valuationName == null) {\r
83                 valuationName = "/ConfigurationValues";\r
84             }\r
85 \r
86             VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);\r
87             Variable modelVariable = graph.adapt(model, Variable.class);\r
88             Variable experimentVariable = modelVariable.browse(graph, valuationName);\r
89             return variableInterface.getVariable(graph, experimentVariable, parameter);\r
90 \r
91         } catch (MissingVariableException e) {\r
92             if (DebugException.DEBUG)\r
93                 e.printStackTrace();\r
94             return null;\r
95         } catch (ResourceNotFoundException e) {\r
96             if (DebugException.DEBUG)\r
97                 e.printStackTrace();\r
98             return null;\r
99         } catch (AdaptionException e) {\r
100             if (DebugException.DEBUG)\r
101                 e.printStackTrace();\r
102             return null;\r
103         } catch (Throwable e) {\r
104             if (DebugException.DEBUG)\r
105                 e.printStackTrace();\r
106             return null;\r
107         }\r
108 \r
109     }\r
110 \r
111 \r
112 //    @Override\r
113 //    public Variable perform(ReadGraph graph) throws DatabaseException {\r
114 //\r
115 //      try {\r
116 //\r
117 //              Builtins b = graph.getBuiltins();\r
118 //              String valuationName = null;\r
119 //              //Valuations valuations = graph.adapt(parameter, Valuations.class);\r
120 //              Resource map = graph.getResource("http://www.simantics.org/Modeling-1.1/CompositeToDiagram");\r
121 //\r
122 //              Resource tail = parameter.resources[parameter.size() - 1];\r
123 //\r
124 //            Resource diagram = graph.syncRequest(new PossibleDiagram(tail));\r
125 //            if(diagram == null) {\r
126 //              Resource d = diagram;\r
127 //                              while((d = graph.getPossibleObject(d, b.PartOf)) != null) {\r
128 //                                      diagram = graph.getPossibleObject(d, map);\r
129 //                                      if(diagram != null) break;\r
130 //                              }\r
131 //            }\r
132 //\r
133 //            if(diagram != null) {\r
134 //                      for(Resource run : graph.getObjects(diagram, b.ConsistsOf)) {\r
135 //                              String name = graph.getPossibleRelatedValue(run, b.HasName, StringJavaBinding.INSTANCE);\r
136 //                              if(parameter2.equals(name)) {\r
137 //                      valuationName = graph.getPossibleRelatedValue(run, b.HasActiveExperiment);\r
138 //                      break;\r
139 //                              }\r
140 //                      }\r
141 //            }\r
142 //\r
143 //            Resource model = graph.syncRequest(new PossibleModel(head));\r
144 //            if (model == null) return null;\r
145 //\r
146 //            if(valuationName == null) {\r
147 //              String valuation = graph.syncRequest(new PossibleActiveExperiment(model));\r
148 //              if(valuation != null) valuationName = valuation;\r
149 //            }\r
150 //\r
151 //            if (valuationName == null) {\r
152 //                valuationName = "/ConfigurationValues";\r
153 //            }\r
154 //\r
155 //            VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);\r
156 //            Variable modelVariable = graph.adapt(model, Variable.class);\r
157 //            Variable experimentVariable = modelVariable.browse(graph, valuationName);\r
158 //            return variableInterface.getVariable(graph, experimentVariable, parameter);\r
159 //\r
160 ////            return null;\r
161 //\r
162 ////            Resource value = valuations.getValue(graph, null, valuationName, parameter3);\r
163 ////            return graph.getSingleObject(value, b.ConsistsOf);\r
164 //\r
165 //        } catch (ResourceNotFoundException e) {\r
166 //            if (DebugException.DEBUG)\r
167 //                e.printStackTrace();\r
168 //            return null;\r
169 //        } catch (AdaptionException e) {\r
170 //            if (DebugException.DEBUG)\r
171 //                e.printStackTrace();\r
172 //            return null;\r
173 //        }\r
174 //\r
175 //    }\r
176 \r
177 }\r