]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/StructuralVariables.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / StructuralVariables.java
1 package org.simantics.structural2;
2
3 import java.util.ArrayList;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.util.URIStringUtils;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.Session;
10 import org.simantics.db.common.ResourceArray;
11 import org.simantics.db.common.primitiverequest.PossibleObject;
12 import org.simantics.db.common.request.PossibleTypedParent;
13 import org.simantics.db.common.request.Queries;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.layer0.variable.VariableInterface;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.operation.Layer0X;
20 import org.simantics.simulation.ontology.SimulationResource;
21 import org.simantics.structural.stubs.StructuralResource2;
22
23 /**
24  * @deprecated Don't use this class anymore. It contains nothing worth using.
25  */
26 @Deprecated
27 final public class StructuralVariables {
28
29         private static final boolean DEBUG = false;
30
31         
32         public static Resource getCompositeParent(Session session, Resource component) throws DatabaseException {
33
34                 Layer0 L0 = Layer0.getInstance(session);
35                 StructuralResource2 sr = StructuralResource2.getInstance(session);
36                 Resource parent = session.syncRequest( new PossibleObject(component, L0.PartOf) );
37                 if(parent == null) return null;
38                 if ( session.syncRequest( Queries.isInstanceOf(parent, sr.Component) )) return parent;
39                 return null;
40                 
41         }
42         
43         public static Resource getCompositeParent(ReadGraph graph, Resource component) throws DatabaseException {
44
45                 Layer0 L0 = Layer0.getInstance(graph);
46                 StructuralResource2 sr = StructuralResource2.getInstance(graph);
47                 Resource parent = graph.getPossibleObject(component, L0.PartOf);
48                 if(parent == null) return null;
49                 if(graph.isInstanceOf(parent, sr.Composite)) return parent;
50                 return null;
51                 
52         }
53
54         public static ResourceArray getCompositeArray(Session session, Resource composite) throws DatabaseException {
55                 
56                 ArrayList<Resource> path = new ArrayList<Resource>();
57                 while(composite != null) {
58                         path.add(composite);
59                         composite = getCompositeParent(session, composite);
60                 }
61                 return new ResourceArray(path).reversed();
62                 
63         }
64         
65         public static ResourceArray getCompositeArray(ReadGraph graph, Resource composite) throws DatabaseException {
66                 
67                 ArrayList<Resource> path = new ArrayList<Resource>();
68                 while(composite != null) {
69                         path.add(composite);
70                         composite = getCompositeParent(graph, composite);
71                 }
72                 return new ResourceArray(path).reversed();
73                 
74         }
75         
76         public static ResourceArray getComponentArray(ReadGraph graph, Resource component) throws DatabaseException {
77                 
78                 Resource composite = getCompositeParent(graph, component);
79                 return getCompositeArray(graph, composite).appended(component);
80                 
81 //              ArrayList<Resource> path = new ArrayList<Resource>();
82 //              path.add(component);
83 //              component = getCompositeParent(graph, component);
84 //              while(component != null) {
85 //                      path.add(component);
86 //                      component = getCompositeParent(graph, component);
87 //              }
88 //              return new ResourceArray(path).reversed();
89                 
90         }
91
92         public static ResourceArray getComponentArray(ReadGraph graph, ResourceArray prefix, Resource component) throws DatabaseException {
93                 
94                 return prefix.appended(getComponentArray(graph, component));
95                 
96         }
97
98         static Resource getModel(ReadGraph graph, ResourceArray path) throws DatabaseException {
99                 
100                 // The first element is the configuration composite
101                 return graph.getPossibleObject(path.resources[0], SimulationResource.getInstance(graph).IsConfigurationOf);
102                 
103         }
104         
105         public static Resource getRootComposite(ReadGraph graph, Resource component) throws DatabaseException {
106
107                 ResourceArray fullPath = getComponentArray(graph, ResourceArray.EMPTY, component);
108                 return fullPath.head();
109                 
110         }
111         
112         public static Variable getVariable(ReadGraph graph, ResourceArray path, Resource component) throws DatabaseException {
113                 
114                 ResourceArray fullPath = getComponentArray(graph, path, component);
115                 Resource model = getModel(graph, fullPath);
116                 if(model == null) return null;
117                 VariableInterface variables = graph.adapt(model, VariableInterface.class);
118                 return variables.getVariable(graph, fullPath.removeFromBeginning(1));
119                 
120         }
121
122     /**
123      * @param graph
124      * @param array
125      * @return RVI, never <code>null</code>
126      * @throws DatabaseException
127      */
128     public static String getRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
129         Layer0 L0 = Layer0.getInstance(graph);
130         String result = "";
131         for(Resource r : array) {
132             if(DEBUG) System.out.println("Variables.getRVI " + NameUtils.getSafeName(graph, r) );
133             String name = graph.getRelatedValue(r, L0.HasName, Bindings.STRING);
134             String segment = URIStringUtils.escape(name);
135             result += "/" + segment;
136         }
137         return result;
138     }
139     
140     public static String getRVI(Session session, ResourceArray array) throws DatabaseException {
141         Layer0 L0 = Layer0.getInstance(session);
142         String result = "";
143         for(Resource r : array) {
144                 String safeName = session.syncRequest( Queries.safeName(r) );
145             if(DEBUG) System.out.println("Variables.getRVI " + safeName );            
146             String name = (String) session.syncRequest( Queries.getRelatedValue(r, L0.HasName, Bindings.STRING) );
147             String segment = URIStringUtils.escape(name);
148             result += "/" + segment;
149         }
150         return result;
151     }
152     
153
154     /**
155      * @param graph
156      * @param array
157      * @return RVI or <code>null</code> if it can't be resolved
158      * @throws DatabaseException
159      */
160     public static String getPossibleRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
161         Layer0 L0 = Layer0.getInstance(graph);
162         String result = "";
163         for(Resource r : array) {
164             if(DEBUG) System.out.println("Variables.getRVI " + NameUtils.getSafeName(graph, r) );
165             String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
166             if (name == null)
167                 return null;
168             String segment = URIStringUtils.escape(name);
169             result += "/" + segment;
170         }
171         return result;
172     }
173
174         public static Resource getModel(ReadGraph graph, Resource composite) throws DatabaseException {
175                 return graph.syncRequest(new PossibleTypedParent(composite, SimulationResource.getInstance(graph).Model));
176         }
177
178         public static Resource getBaseRealization(ReadGraph graph, Resource composite) throws DatabaseException {
179                 Layer0X L0X = Layer0X.getInstance(graph);
180                 Resource model = getModel(graph, composite);
181                 if(model == null) return null;
182                 return graph.getPossibleObject(model, L0X.HasBaseRealization);
183         }
184         
185 }