]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/StructuralRVIResolver.java
Merge "Editing of texts inside SVG elements"
[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     @Override
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);
55         }
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;
61         }
62         RVIPart part = getRVIPart(graph, variable);
63         if(part == null) return null;
64         return new RVIBuilder(base.getRVI(graph)).append(part).toRVI();
65     }
66
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);
74         }
75         return false;
76     }
77
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);
84         return represents;
85     }
86
87     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, Resource resource)
88             throws DatabaseException {
89         Resource base = getBase(graph, variable, resource);
90         if (base == null)
91             return null;
92         LinkedList<Resource> result = new LinkedList<Resource>();
93         result.add(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;
101         }
102         return result;
103     }
104     
105     protected Collection<Resource> getRVIPath(ReadGraph graph, Variable variable, GuidRVIPart part)
106             throws DatabaseException {
107
108         if(part.resource != null) {
109                 return getRVIPath(graph, variable, part.resource);
110         } else {
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());
117         }
118         
119     }
120
121     @Override
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);
126         return variable;
127     }
128     
129     @Override
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);
134         return variable;
135     }
136
137 }