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);
43 Variable base = variable.getParent(graph);
44 while(!isRVIBase(graph, base)) base = base.getParent(graph);
45 RVIPart part = getRVIPart(graph, variable);
46 return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
50 public RVI getPossibleRVI(ReadGraph graph, Variable variable) throws DatabaseException {
51 if (Variables.isContext(graph, variable)) {
52 Databoard databoard = graph.getService( Databoard.class );
53 Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
54 return RVI.empty(rviBinding);
56 Variable base = variable.getParent(graph);
57 if(base == null) return null;
58 while(!isRVIBase(graph, base)) {
59 base = base.getParent(graph);
60 if(base == null) return null;
62 RVIPart part = getRVIPart(graph, variable);
63 if(part == null) return null;
64 return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
67 protected boolean isPartOfComponentType(ReadGraph graph, Resource resource)
68 throws DatabaseException {
69 Layer0 L0 = Layer0.getInstance(graph);
70 StructuralResource2 STR = StructuralResource2.getInstance(graph);
71 Resource container = graph.getPossibleObject(resource, L0.PartOf);
72 if(container != null && graph.isInstanceOf(container, STR.Composite)) {
73 return graph.hasStatement(container, STR.Defines);
78 protected Resource getBase(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
79 Layer0 L0 = Layer0.getInstance(graph);
80 SimulationResource SIMU = SimulationResource.getInstance(graph);
81 Resource represents = variable.getPossibleRepresents(graph);
82 if(represents != null && graph.isInstanceOf(represents, SIMU.Run)) return Variables.getPossibleConfigurationContextResource(graph, represents);
83 else if(isPartOfComponentType(graph, resource)) return graph.getPossibleObject(resource, L0.PartOf);
87 protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, Resource resource)
88 throws DatabaseException {
89 Resource base = getBase(graph, variable, resource);
92 LinkedList<Resource> result = new LinkedList<Resource>();
94 Layer0 L0 = Layer0.getInstance(graph);
95 resource = graph.getPossibleObject(resource, L0.PartOf);
96 if(resource == null) return null;
97 while(!base.equals(resource)) {
98 result.addFirst(resource);
99 resource = graph.getPossibleObject(resource, L0.PartOf);
100 if(resource == null) return null;
105 protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, GuidRVIPart part)
106 throws DatabaseException {
108 if(part.resource != null) {
109 return getRVIPath(graph, variable, part.resource);
111 Resource indexRoot = variable.getIndexRoot(graph);
112 Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
113 GUID guid = new GUID(part.mostSignificant, part.leastSignificant);
114 Collection<Resource> queryResult = instances.find(graph, indexRoot, "GUID:"+guid.indexString());
115 if(queryResult.size() != 1) return null;
116 return getRVIPath(graph, variable, queryResult.iterator().next());
122 protected Variable resolveChild(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
123 Collection<Resource> path = getRVIPath(graph, variable, resource);
124 if(path == null) throw new MissingVariableException("Didn't find a variable related to " + resource + ".", resource);
125 for(Resource r : path) variable = variable.browse(graph, r);
130 protected Variable resolveChild(ReadGraph graph, Variable variable, GuidRVIPart part) throws DatabaseException {
131 Collection<Resource> path = getRVIPath(graph, variable, part);
132 if(path == null) throw new MissingVariableException("Didn't find a variable related to " + part + ".");
133 for(Resource r : path) variable = variable.browse(graph, r);