1 package org.simantics.structural2;
3 import java.util.Collection;
4 import java.util.LinkedList;
6 import org.simantics.databoard.Databoard;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.datatypes.literal.GUID;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.Instances;
13 import org.simantics.db.layer0.exception.MissingVariableException;
14 import org.simantics.db.layer0.variable.RVI;
15 import org.simantics.db.layer0.variable.RVI.GuidRVIPart;
16 import org.simantics.db.layer0.variable.RVI.RVIPart;
17 import org.simantics.db.layer0.variable.RVIBuilder;
18 import org.simantics.db.layer0.variable.StandardRVIResolver;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.simulation.ontology.SimulationResource;
23 import org.simantics.structural.stubs.StructuralResource2;
25 public class StructuralRVIResolver extends StandardRVIResolver {
27 protected boolean isRVIBase(ReadGraph graph, Variable variable) throws DatabaseException {
28 if (Variables.isContext(graph, variable)) return true;
29 StructuralResource2 STR = StructuralResource2.getInstance(graph);
30 Resource represents = variable.getRepresents(graph);
31 if (represents == null) return false;
32 if (graph.isInstanceOf(represents, STR.Composite)) return false;
37 public RVI getRVI(ReadGraph graph, Variable variable) throws DatabaseException {
38 if (Variables.isContext(graph, variable)) {
39 Databoard databoard = graph.getService( Databoard.class );
40 Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
41 return RVI.empty(rviBinding);
44 Variable base = variable.getParent(graph);
45 while(!isRVIBase(graph, base)) base = base.getParent(graph);
46 RVIPart part = getRVIPart(graph, variable);
47 return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
52 public RVI getPossibleRVI(ReadGraph graph, Variable variable) throws DatabaseException {
53 if (Variables.isContext(graph, variable)) {
54 Databoard databoard = graph.getService( Databoard.class );
55 Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
56 return RVI.empty(rviBinding);
59 Variable base = variable.getParent(graph);
60 if(base == null) return null;
61 while(!isRVIBase(graph, base)) {
62 base = base.getParent(graph);
63 if(base == null) return null;
65 RVIPart part = getRVIPart(graph, variable);
66 if(part == null) return null;
67 RVI baseRVI = base.getPossibleRVI(graph);
68 if(baseRVI == null) return null;
69 return new RVIBuilder(baseRVI).append(part).toRVI();
73 protected boolean isPartOfComponentType(ReadGraph graph, Resource resource)
74 throws DatabaseException {
75 Layer0 L0 = Layer0.getInstance(graph);
76 StructuralResource2 STR = StructuralResource2.getInstance(graph);
77 Resource container = graph.getPossibleObject(resource, L0.PartOf);
78 if(container != null && graph.isInstanceOf(container, STR.Composite)) {
79 return graph.hasStatement(container, STR.Defines);
84 protected Resource getBase(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
85 Layer0 L0 = Layer0.getInstance(graph);
86 SimulationResource SIMU = SimulationResource.getInstance(graph);
87 Resource represents = variable.getPossibleRepresents(graph);
88 if(represents != null && graph.isInstanceOf(represents, SIMU.Run)) return Variables.getPossibleConfigurationContextResource(graph, represents);
89 else if(isPartOfComponentType(graph, resource)) return graph.getPossibleObject(resource, L0.PartOf);
93 protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, Resource resource)
94 throws DatabaseException {
95 Resource base = getBase(graph, variable, resource);
98 LinkedList<Resource> result = new LinkedList<Resource>();
100 Layer0 L0 = Layer0.getInstance(graph);
101 resource = graph.getPossibleObject(resource, L0.PartOf);
102 if(resource == null) return null;
103 while(!base.equals(resource)) {
104 result.addFirst(resource);
105 resource = graph.getPossibleObject(resource, L0.PartOf);
106 if(resource == null) return null;
111 protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, GuidRVIPart part)
112 throws DatabaseException {
114 if(part.resource != null) {
115 return getRVIPath(graph, variable, part.resource);
117 Resource indexRoot = variable.getIndexRoot(graph);
118 Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
119 GUID guid = new GUID(part.mostSignificant, part.leastSignificant);
120 Collection<Resource> queryResult = instances.find(graph, indexRoot, "GUID:"+guid.indexString());
121 if(queryResult.size() != 1) return null;
122 return getRVIPath(graph, variable, queryResult.iterator().next());
128 protected Variable resolveChild(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
129 Collection<Resource> path = getRVIPath(graph, variable, resource);
130 if(path == null) throw new MissingVariableException("Didn't find a variable related to " + resource + ".", resource);
131 for(Resource r : path) variable = variable.browse(graph, r);
136 protected Variable resolveChild(ReadGraph graph, Variable variable, GuidRVIPart part) throws DatabaseException {
137 Collection<Resource> path = getRVIPath(graph, variable, part);
138 if(path == null) throw new MissingVariableException("Didn't find a variable related to " + part + ".");
139 for(Resource r : path) variable = variable.browse(graph, r);