1 package org.simantics.structural2;
\r
3 import java.util.ArrayList;
\r
5 import org.simantics.databoard.Bindings;
\r
6 import org.simantics.databoard.util.URIStringUtils;
\r
7 import org.simantics.db.ReadGraph;
\r
8 import org.simantics.db.Resource;
\r
9 import org.simantics.db.Session;
\r
10 import org.simantics.db.common.ResourceArray;
\r
11 import org.simantics.db.common.primitiverequest.PossibleObject;
\r
12 import org.simantics.db.common.request.PossibleTypedParent;
\r
13 import org.simantics.db.common.request.Queries;
\r
14 import org.simantics.db.common.utils.NameUtils;
\r
15 import org.simantics.db.exception.DatabaseException;
\r
16 import org.simantics.db.layer0.variable.Variable;
\r
17 import org.simantics.db.layer0.variable.VariableInterface;
\r
18 import org.simantics.layer0.Layer0;
\r
19 import org.simantics.operation.Layer0X;
\r
20 import org.simantics.simulation.ontology.SimulationResource;
\r
21 import org.simantics.structural.stubs.StructuralResource2;
\r
24 * @deprecated Don't use this class anymore. It contains nothing worth using.
\r
27 final public class StructuralVariables {
\r
29 private static final boolean DEBUG = false;
\r
32 public static Resource getCompositeParent(Session session, Resource component) throws DatabaseException {
\r
34 Layer0 L0 = Layer0.getInstance(session);
\r
35 StructuralResource2 sr = StructuralResource2.getInstance(session);
\r
36 Resource parent = session.syncRequest( new PossibleObject(component, L0.PartOf) );
\r
37 if(parent == null) return null;
\r
38 if ( session.syncRequest( Queries.isInstanceOf(parent, sr.Component) )) return parent;
\r
43 public static Resource getCompositeParent(ReadGraph graph, Resource component) throws DatabaseException {
\r
45 Layer0 L0 = Layer0.getInstance(graph);
\r
46 StructuralResource2 sr = StructuralResource2.getInstance(graph);
\r
47 Resource parent = graph.getPossibleObject(component, L0.PartOf);
\r
48 if(parent == null) return null;
\r
49 if(graph.isInstanceOf(parent, sr.Composite)) return parent;
\r
54 public static ResourceArray getCompositeArray(Session session, Resource composite) throws DatabaseException {
\r
56 ArrayList<Resource> path = new ArrayList<Resource>();
\r
57 while(composite != null) {
\r
58 path.add(composite);
\r
59 composite = getCompositeParent(session, composite);
\r
61 return new ResourceArray(path).reversed();
\r
65 public static ResourceArray getCompositeArray(ReadGraph graph, Resource composite) throws DatabaseException {
\r
67 ArrayList<Resource> path = new ArrayList<Resource>();
\r
68 while(composite != null) {
\r
69 path.add(composite);
\r
70 composite = getCompositeParent(graph, composite);
\r
72 return new ResourceArray(path).reversed();
\r
76 public static ResourceArray getComponentArray(ReadGraph graph, Resource component) throws DatabaseException {
\r
78 Resource composite = getCompositeParent(graph, component);
\r
79 return getCompositeArray(graph, composite).appended(component);
\r
81 // ArrayList<Resource> path = new ArrayList<Resource>();
\r
82 // path.add(component);
\r
83 // component = getCompositeParent(graph, component);
\r
84 // while(component != null) {
\r
85 // path.add(component);
\r
86 // component = getCompositeParent(graph, component);
\r
88 // return new ResourceArray(path).reversed();
\r
92 public static ResourceArray getComponentArray(ReadGraph graph, ResourceArray prefix, Resource component) throws DatabaseException {
\r
94 return prefix.appended(getComponentArray(graph, component));
\r
98 static Resource getModel(ReadGraph graph, ResourceArray path) throws DatabaseException {
\r
100 // The first element is the configuration composite
\r
101 return graph.getPossibleObject(path.resources[0], SimulationResource.getInstance(graph).IsConfigurationOf);
\r
105 public static Resource getRootComposite(ReadGraph graph, Resource component) throws DatabaseException {
\r
107 ResourceArray fullPath = getComponentArray(graph, ResourceArray.EMPTY, component);
\r
108 return fullPath.head();
\r
112 public static Variable getVariable(ReadGraph graph, ResourceArray path, Resource component) throws DatabaseException {
\r
114 ResourceArray fullPath = getComponentArray(graph, path, component);
\r
115 Resource model = getModel(graph, fullPath);
\r
116 if(model == null) return null;
\r
117 VariableInterface variables = graph.adapt(model, VariableInterface.class);
\r
118 return variables.getVariable(graph, fullPath.removeFromBeginning(1));
\r
125 * @return RVI, never <code>null</code>
\r
126 * @throws DatabaseException
\r
128 public static String getRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
\r
129 Layer0 L0 = Layer0.getInstance(graph);
\r
130 String result = "";
\r
131 for(Resource r : array) {
\r
132 if(DEBUG) System.out.println("Variables.getRVI " + NameUtils.getSafeName(graph, r) );
\r
133 String name = graph.getRelatedValue(r, L0.HasName, Bindings.STRING);
\r
134 String segment = URIStringUtils.escape(name);
\r
135 result += "/" + segment;
\r
140 public static String getRVI(Session session, ResourceArray array) throws DatabaseException {
\r
141 Layer0 L0 = Layer0.getInstance(session);
\r
142 String result = "";
\r
143 for(Resource r : array) {
\r
144 String safeName = session.syncRequest( Queries.safeName(r) );
\r
145 if(DEBUG) System.out.println("Variables.getRVI " + safeName );
\r
146 String name = (String) session.syncRequest( Queries.getRelatedValue(r, L0.HasName, Bindings.STRING) );
\r
147 String segment = URIStringUtils.escape(name);
\r
148 result += "/" + segment;
\r
157 * @return RVI or <code>null</code> if it can't be resolved
\r
158 * @throws DatabaseException
\r
160 public static String getPossibleRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
\r
161 Layer0 L0 = Layer0.getInstance(graph);
\r
162 String result = "";
\r
163 for(Resource r : array) {
\r
164 if(DEBUG) System.out.println("Variables.getRVI " + NameUtils.getSafeName(graph, r) );
\r
165 String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
\r
168 String segment = URIStringUtils.escape(name);
\r
169 result += "/" + segment;
\r
174 public static Resource getModel(ReadGraph graph, Resource composite) throws DatabaseException {
\r
175 return graph.syncRequest(new PossibleTypedParent(composite, SimulationResource.getInstance(graph).Model));
\r
178 public static Resource getBaseRealization(ReadGraph graph, Resource composite) throws DatabaseException {
\r
179 Layer0X L0X = Layer0X.getInstance(graph);
\r
180 Resource model = getModel(graph, composite);
\r
181 if(model == null) return null;
\r
182 return graph.getPossibleObject(model, L0X.HasBaseRealization);
\r