]> gerrit.simantics Code Review - simantics/platform.git/blob - StructuralRVIResolver.java
8cf61b3e0ade8e4dc74ed2e44ca334d655a6ee0c
[simantics/platform.git] / StructuralRVIResolver.java
1 package org.simantics.structural2;
2
3 import java.util.Collection;
4 import java.util.LinkedList;
5
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;
24
25 public class StructuralRVIResolver extends StandardRVIResolver {
26
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;
33         return true;
34     }
35     
36     @Override
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);
42         }
43         else {
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();
48         }
49     }
50     
51     @Override
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);
57         }
58         else {
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;
64             }
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();
70         }
71     }
72
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);
80         }
81         return false;
82     }
83
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);
90         return represents;
91     }
92
93     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, Resource resource)
94             throws DatabaseException {
95         Resource base = getBase(graph, variable, resource);
96         if (base == null)
97             return null;
98         LinkedList<Resource> result = new LinkedList<Resource>();
99         result.add(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;
107         }
108         return result;
109     }
110     
111     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, GuidRVIPart part)
112             throws DatabaseException {
113
114         if(part.resource != null) {
115                 return getRVIPath(graph, variable, part.resource);
116         } else {
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());
123         }
124         
125     }
126
127     @Override
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);
132         return variable;
133     }
134     
135     @Override
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);
140         return variable;
141     }
142
143 }