]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/StructuralRVIResolver.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / 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         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();
47     }
48
49     protected boolean isPartOfComponentType(ReadGraph graph, Resource resource)
50             throws DatabaseException {
51         Layer0 L0 = Layer0.getInstance(graph);
52         StructuralResource2 STR = StructuralResource2.getInstance(graph);
53         Resource container = graph.getPossibleObject(resource, L0.PartOf);
54         if(container != null && graph.isInstanceOf(container, STR.Composite)) {
55             return graph.hasStatement(container, STR.Defines);
56         }
57         return false;
58     }
59
60     protected Resource getBase(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
61         Layer0 L0 = Layer0.getInstance(graph);
62         SimulationResource SIMU = SimulationResource.getInstance(graph);
63         Resource represents = variable.getPossibleRepresents(graph);
64         if(represents != null && graph.isInstanceOf(represents, SIMU.Run)) return Variables.getPossibleConfigurationContextResource(graph, represents);
65         else if(isPartOfComponentType(graph, resource)) return graph.getPossibleObject(resource, L0.PartOf);
66         return represents;
67     }
68
69     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, Resource resource)
70             throws DatabaseException {
71         Resource base = getBase(graph, variable, resource);
72         if (base == null)
73             return null;
74         LinkedList<Resource> result = new LinkedList<Resource>();
75         result.add(resource);
76         Layer0 L0 = Layer0.getInstance(graph);
77         resource = graph.getPossibleObject(resource, L0.PartOf);
78         if(resource == null) return null;
79         while(!base.equals(resource)) {
80             result.addFirst(resource);
81             resource = graph.getPossibleObject(resource, L0.PartOf);
82             if(resource == null) return null;
83         }
84         return result;
85     }
86     
87     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, GuidRVIPart part)
88             throws DatabaseException {
89
90         if(part.resource != null) {
91                 return getRVIPath(graph, variable, part.resource);
92         } else {
93                 Resource indexRoot = variable.getIndexRoot(graph);
94                 Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
95                 GUID guid = new GUID(part.mostSignificant, part.leastSignificant);
96                 Collection<Resource> queryResult = instances.find(graph, indexRoot, "GUID:"+guid.indexString());
97                 if(queryResult.size() != 1) return null;
98                 return getRVIPath(graph, variable, queryResult.iterator().next());
99         }
100         
101     }
102
103     @Override
104     protected Variable resolveChild(ReadGraph graph, Variable variable, Resource resource) throws DatabaseException {
105         Collection<Resource> path = getRVIPath(graph, variable, resource);
106         if(path == null) throw new MissingVariableException("Didn't find a variable related to " + resource + ".", resource);
107         for(Resource r : path) variable = variable.browse(graph, r);
108         return variable;
109     }
110     
111     @Override
112     protected Variable resolveChild(ReadGraph graph, Variable variable, GuidRVIPart part) throws DatabaseException {
113         Collection<Resource> path = getRVIPath(graph, variable, part);
114         if(path == null) throw new MissingVariableException("Didn't find a variable related to " + part + ".");
115         for(Resource r : path) variable = variable.browse(graph, r);
116         return variable;
117     }
118
119 }