]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleSessionVariable.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PossibleSessionVariable.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.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.ResourceArray;
18 import org.simantics.db.common.exception.DebugException;
19 import org.simantics.db.common.request.BinaryRead;
20 import org.simantics.db.exception.AdaptionException;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ResourceNotFoundException;
23 import org.simantics.db.layer0.exception.MissingVariableException;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.simantics.db.layer0.variable.VariableInterface;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.scl.reflection.OntologyVersions;
28 import org.simantics.simulation.ontology.SimulationResource;
29
30 public class PossibleSessionVariable extends BinaryRead<ResourceArray, String, Variable> {
31
32     public PossibleSessionVariable(ResourceArray resource, String identifier) {
33         super(resource, identifier);
34     }
35
36     @Override
37     public Variable perform(ReadGraph graph) throws DatabaseException {
38
39         try {
40
41                 String uri = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Modeling-0.0/CompositeToDiagram");
42                 Resource map = graph.getResource(uri);
43
44             Resource tail = parameter.tail();
45             Resource head = parameter.head();
46
47             if(!graph.hasStatement(tail)) return null;
48
49             Layer0 l0 = Layer0.getInstance(graph);
50
51             String valuationName = null;
52
53             Resource diagram = graph.syncRequest(new PossibleDiagram(tail));
54             if(diagram == null) {
55                 Resource d = diagram;
56                 while((d = graph.getPossibleObject(d, l0.PartOf)) != null) {
57                     diagram = graph.getPossibleObject(d, map);
58                     if(diagram != null) break;
59                 }
60             }
61
62             if(diagram != null) {
63                 SimulationResource SIMU = SimulationResource.getInstance(graph);
64                 for(Resource run : graph.getObjects(diagram, l0.ConsistsOf)) {
65                     String name = graph.getPossibleRelatedValue(run, l0.HasName, Bindings.STRING);
66                     if(parameter2.equals(name)) {
67                         valuationName = graph.getPossibleRelatedValue(run, SIMU.HasActiveExperiment);
68                         break;
69                     }
70                 }
71             }
72
73
74             Resource model = graph.syncRequest(new PossibleModel(head));
75             if (model == null) return null;
76
77 //            if(valuationName == null) {
78 //              String valuation = graph.syncRequest(new PossibleActiveExperiment(model));
79 //              if(valuation != null) valuationName = valuation;
80 //            }
81
82             if (valuationName == null) {
83                 valuationName = "/ConfigurationValues";
84             }
85
86             VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);
87             Variable modelVariable = graph.adapt(model, Variable.class);
88             Variable experimentVariable = modelVariable.browse(graph, valuationName);
89             return variableInterface.getVariable(graph, experimentVariable, parameter);
90
91         } catch (MissingVariableException e) {
92             if (DebugException.DEBUG)
93                 e.printStackTrace();
94             return null;
95         } catch (ResourceNotFoundException e) {
96             if (DebugException.DEBUG)
97                 e.printStackTrace();
98             return null;
99         } catch (AdaptionException e) {
100             if (DebugException.DEBUG)
101                 e.printStackTrace();
102             return null;
103         } catch (Throwable e) {
104             if (DebugException.DEBUG)
105                 e.printStackTrace();
106             return null;
107         }
108
109     }
110
111
112 //    @Override
113 //    public Variable perform(ReadGraph graph) throws DatabaseException {
114 //
115 //      try {
116 //
117 //              Builtins b = graph.getBuiltins();
118 //              String valuationName = null;
119 //              //Valuations valuations = graph.adapt(parameter, Valuations.class);
120 //              Resource map = graph.getResource("http://www.simantics.org/Modeling-1.1/CompositeToDiagram");
121 //
122 //              Resource tail = parameter.resources[parameter.size() - 1];
123 //
124 //            Resource diagram = graph.syncRequest(new PossibleDiagram(tail));
125 //            if(diagram == null) {
126 //              Resource d = diagram;
127 //                              while((d = graph.getPossibleObject(d, b.PartOf)) != null) {
128 //                                      diagram = graph.getPossibleObject(d, map);
129 //                                      if(diagram != null) break;
130 //                              }
131 //            }
132 //
133 //            if(diagram != null) {
134 //                      for(Resource run : graph.getObjects(diagram, b.ConsistsOf)) {
135 //                              String name = graph.getPossibleRelatedValue(run, b.HasName, StringJavaBinding.INSTANCE);
136 //                              if(parameter2.equals(name)) {
137 //                      valuationName = graph.getPossibleRelatedValue(run, b.HasActiveExperiment);
138 //                      break;
139 //                              }
140 //                      }
141 //            }
142 //
143 //            Resource model = graph.syncRequest(new PossibleModel(head));
144 //            if (model == null) return null;
145 //
146 //            if(valuationName == null) {
147 //              String valuation = graph.syncRequest(new PossibleActiveExperiment(model));
148 //              if(valuation != null) valuationName = valuation;
149 //            }
150 //
151 //            if (valuationName == null) {
152 //                valuationName = "/ConfigurationValues";
153 //            }
154 //
155 //            VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);
156 //            Variable modelVariable = graph.adapt(model, Variable.class);
157 //            Variable experimentVariable = modelVariable.browse(graph, valuationName);
158 //            return variableInterface.getVariable(graph, experimentVariable, parameter);
159 //
160 ////            return null;
161 //
162 ////            Resource value = valuations.getValue(graph, null, valuationName, parameter3);
163 ////            return graph.getSingleObject(value, b.ConsistsOf);
164 //
165 //        } catch (ResourceNotFoundException e) {
166 //            if (DebugException.DEBUG)
167 //                e.printStackTrace();
168 //            return null;
169 //        } catch (AdaptionException e) {
170 //            if (DebugException.DEBUG)
171 //                e.printStackTrace();
172 //            return null;
173 //        }
174 //
175 //    }
176
177 }