]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleSessionVariableFromURI.java
New implementation NearestOwnerFinder of CommonDBUtils.getNearestOwner
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PossibleSessionVariableFromURI.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.TernaryRead;
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 PossibleSessionVariableFromURI extends TernaryRead<ResourceArray, String, String, Variable> {
31
32     public PossibleSessionVariableFromURI(ResourceArray resource, String identifier, String sessionId) {
33         super(resource, identifier, sessionId);
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             Resource model = graph.syncRequest(new PossibleModel(head));
74             if (model == null) return null;
75             if (valuationName == null) {
76                 valuationName = "/ConfigurationValues";
77             }
78
79             VariableInterface variableInterface = graph.adapt(model, VariableInterface.class);
80             Variable modelVariable = graph.adapt(model, Variable.class);
81             Variable experimentVariable = modelVariable.browse(graph, valuationName+parameter2);
82             return variableInterface.getVariable(graph, experimentVariable, parameter3);
83         } catch (MissingVariableException e) {
84             if (DebugException.DEBUG)
85                 e.printStackTrace();
86             return null;
87         } catch (ResourceNotFoundException e) {
88             if (DebugException.DEBUG)
89                 e.printStackTrace();
90             return null;
91         } catch (AdaptionException e) {
92             if (DebugException.DEBUG)
93                 e.printStackTrace();
94             return null;
95         } catch (Throwable e) {
96             if (DebugException.DEBUG)
97                 e.printStackTrace();
98             return null;
99         }
100     }
101 }