1 package org.simantics.structural2;
3 import java.util.ArrayList;
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;
24 * @deprecated Don't use this class anymore. It contains nothing worth using.
27 final public class StructuralVariables {
29 private static final boolean DEBUG = false;
32 public static Resource getCompositeParent(Session session, Resource component) throws DatabaseException {
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;
43 public static Resource getCompositeParent(ReadGraph graph, Resource component) throws DatabaseException {
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;
54 public static ResourceArray getCompositeArray(Session session, Resource composite) throws DatabaseException {
56 ArrayList<Resource> path = new ArrayList<Resource>();
57 while(composite != null) {
59 composite = getCompositeParent(session, composite);
61 return new ResourceArray(path).reversed();
65 public static ResourceArray getCompositeArray(ReadGraph graph, Resource composite) throws DatabaseException {
67 ArrayList<Resource> path = new ArrayList<Resource>();
68 while(composite != null) {
70 composite = getCompositeParent(graph, composite);
72 return new ResourceArray(path).reversed();
76 public static ResourceArray getComponentArray(ReadGraph graph, Resource component) throws DatabaseException {
78 Resource composite = getCompositeParent(graph, component);
79 return getCompositeArray(graph, composite).appended(component);
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);
88 // return new ResourceArray(path).reversed();
92 public static ResourceArray getComponentArray(ReadGraph graph, ResourceArray prefix, Resource component) throws DatabaseException {
94 return prefix.appended(getComponentArray(graph, component));
98 static Resource getModel(ReadGraph graph, ResourceArray path) throws DatabaseException {
100 // The first element is the configuration composite
101 return graph.getPossibleObject(path.resources[0], SimulationResource.getInstance(graph).IsConfigurationOf);
105 public static Resource getRootComposite(ReadGraph graph, Resource component) throws DatabaseException {
107 ResourceArray fullPath = getComponentArray(graph, ResourceArray.EMPTY, component);
108 return fullPath.head();
112 public static Variable getVariable(ReadGraph graph, ResourceArray path, Resource component) throws DatabaseException {
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));
125 * @return RVI, never <code>null</code>
126 * @throws DatabaseException
128 public static String getRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
129 Layer0 L0 = Layer0.getInstance(graph);
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;
140 public static String getRVI(Session session, ResourceArray array) throws DatabaseException {
141 Layer0 L0 = Layer0.getInstance(session);
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;
157 * @return RVI or <code>null</code> if it can't be resolved
158 * @throws DatabaseException
160 public static String getPossibleRVI(ReadGraph graph, ResourceArray array) throws DatabaseException {
161 Layer0 L0 = Layer0.getInstance(graph);
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);
168 String segment = URIStringUtils.escape(name);
169 result += "/" + segment;
174 public static Resource getModel(ReadGraph graph, Resource composite) throws DatabaseException {
175 return graph.syncRequest(new PossibleTypedParent(composite, SimulationResource.getInstance(graph).Model));
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);