]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Enabling/Disabling map painting
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / DistrictNetworkUtil.java
1 package org.simantics.district.network;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.utils.OrderedSetUtils;
11 import org.simantics.db.exception.BindingException;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
14 import org.simantics.db.exception.ServiceException;
15 import org.simantics.db.layer0.request.PossibleVariable;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
19 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
20 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
21 import org.simantics.district.network.ontology.DistrictNetworkResource;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.modeling.ModelingResources;
24 import org.simantics.operation.Layer0X;
25
26 public class DistrictNetworkUtil {
27
28     public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
29         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
30     }
31
32     public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
33         Layer0 L0 = Layer0.getInstance(graph);
34         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
35         if (mapping == null) {
36             mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
37         }
38         
39         Resource edge = graph.newResource();
40         graph.claim(edge, L0.InstanceOf, DN.Edge);
41         
42         graph.claim(edge, DN.HasMapping, mapping);
43         
44         OrderedSetUtils.addFirst(graph, composite, edge);
45         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
46         
47         claimFreshElementName(graph, composite, edge);
48         
49         // We need to put GraphLayer to newLayers so...
50         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
51             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
52             
53             GraphLayer gl = layerUtil.loadLayer(graph, layer);
54             gl.forEachTag(tag -> {
55                 DiagramGraphUtil.tag(graph, edge, tag, true);
56             });
57         }
58         
59         return edge;
60     }
61
62     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
63         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
64         return createVertex(graph, composite, coords, defaultVertexMapping);
65     }
66
67     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
68         Layer0 L0 = Layer0.getInstance(graph);
69         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
70         DiagramResource DIA = DiagramResource.getInstance(graph);
71         Resource vertex = graph.newResource();
72         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
73         graph.claimLiteral(vertex, DIA.HasLocation, coords);
74         
75         graph.claim(vertex, DN.HasMapping, mapping);
76         
77         OrderedSetUtils.add(graph, composite, vertex);
78         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
79         
80         claimFreshElementName(graph, composite, vertex);
81         
82         // We need to put GraphLayer to newLayers so...
83         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
84             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
85             
86             GraphLayer gl = layerUtil.loadLayer(graph, layer);
87             gl.forEachTag(tag -> {
88                 DiagramGraphUtil.tag(graph, vertex, tag, true);
89             });
90         }
91         
92         return vertex;
93     }
94     
95     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
96         if (vertices.isEmpty())
97             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
98         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
99         Iterator<Resource> verticeIterator = vertices.iterator();
100         Resource master = verticeIterator.next();
101         while (verticeIterator.hasNext()) {
102             Resource slave = verticeIterator.next();
103             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
104             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
105             for (Resource startVertexEdge : startVertexEdges) {
106                 graph.deny(startVertexEdge, DN.HasStartVertex);
107                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
108             }
109             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
110             for (Resource endVertexEdge : endVertexEdges) {
111                 graph.deny(endVertexEdge, DN.HasEndVertex);
112                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
113             }
114             OrderedSetUtils.remove(graph, composite, slave);
115             // Remove ConsistsOf statement
116             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
117         }
118         return master;
119     }
120     
121     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
122         Layer0 L0 = Layer0.getInstance(graph);
123         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
124         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
125         if (!startComposite.equalsResource(endComposite)) {
126             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
127         }
128         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
129         
130         CRS crsClass = graph.adapt(crs, CRS.class);
131         
132         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
133         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
134         
135         return crsClass.calculateDistance(startCoords, endCoords);
136     }
137     
138     public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
139         Layer0 L0 = Layer0.getInstance(graph);
140         DiagramResource DIA = DiagramResource.getInstance(graph);
141         // Get name prefix from diagram
142         String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
143         if (namePrefix == null)
144             namePrefix = "";
145         // Give running name to element and increment the counter attached to the diagram.
146         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
147         if (l == null)
148             l = Long.valueOf(0L);
149         String name = namePrefix + l.toString();
150         graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
151         graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
152         return name;
153     }
154
155     public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
156         if (component == null)
157             return null;
158         DiagramResource DIA = DiagramResource.getInstance(graph);
159         if (graph.isInstanceOf(component, DIA.Element))
160             return component;
161         ModelingResources MOD = ModelingResources.getInstance(graph);
162         Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
163         return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
164     }
165
166     public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
167         if (element == null)
168             return null;
169         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
170         return graph.getPossibleObject(element, DN.MappedComponent);
171     }
172
173     public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
174         if (element == null)
175             return null;
176         Resource mappedElement = getMappedElement(graph, element);
177         if (mappedElement == null)
178             return null;
179         ModelingResources MOD = ModelingResources.getInstance(graph);
180         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
181     }
182
183     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
184         if (element == null)
185             return null;
186         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
187         return graph.getPossibleObject(element, DN.MappedFromElement);
188     }
189
190     public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
191         if (input == null)
192             return null;
193
194         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
195         if (graph.isInstanceOf(input, DN.Element)) {
196             Resource mappedElement = getMappedElement(graph, input);
197             if (mappedElement == null)
198                 return null;
199
200             ModelingResources MOD = ModelingResources.getInstance(graph);
201             Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
202             if (mappedComponent == null)
203                 return null;
204
205             return graph.syncRequest(new PossibleVariable(mappedComponent));
206         }
207         return null;
208     }
209
210     public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
211         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
212         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
213         if (current == null)
214             current = true;
215         graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
216     }
217
218     public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
219         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
220         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
221         return current != null ? current : true;
222     }
223 }