]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Enhancements to district functionalities and code
[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.DatabaseException;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.diagram.synchronization.graph.AddElement;
14 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
15 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
16 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
17 import org.simantics.district.network.ontology.DistrictNetworkResource;
18 import org.simantics.layer0.Layer0;
19
20 public class DistrictNetworkUtil {
21
22     public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
23         Layer0 L0 = Layer0.getInstance(graph);
24         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
25         
26         Resource edge = graph.newResource();
27         graph.claim(edge, L0.InstanceOf, DN.Edge);
28         
29         Resource defaultEdgeMapping = graph.getPossibleObject(composite, DN.EdgeDefaultMapping);
30         graph.claim(edge, DN.HasMapping, defaultEdgeMapping);
31         
32         OrderedSetUtils.add(graph, composite, edge);
33         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
34         
35         AddElement.claimFreshElementName(graph, composite, edge);
36         return edge;
37     }
38     
39     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
40         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
41         return createVertex(graph, composite, coords, defaultVertexMapping);
42     }
43     
44     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
45         Layer0 L0 = Layer0.getInstance(graph);
46         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
47         DiagramResource DIA = DiagramResource.getInstance(graph);
48         Resource vertex = graph.newResource();
49         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
50         graph.claimLiteral(vertex, DIA.HasLocation, coords);
51         
52         graph.claim(vertex, DN.HasMapping, mapping);
53         
54         OrderedSetUtils.add(graph, composite, vertex);
55         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
56         
57         AddElement.claimFreshElementName(graph, composite, vertex);
58         
59         // We need to put GraphLayer to newLayers so...
60         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
61             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
62             
63             GraphLayer gl = layerUtil.loadLayer(graph, layer);
64             gl.forEachTag(tag -> {
65                 DiagramGraphUtil.tag(graph, vertex, tag, true);
66             });
67         }
68         
69         return vertex;
70     }
71     
72     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
73         if (vertices.isEmpty())
74             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
75         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
76         Iterator<Resource> verticeIterator = vertices.iterator();
77         Resource master = verticeIterator.next();
78         while (verticeIterator.hasNext()) {
79             Resource slave = verticeIterator.next();
80             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
81             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
82             for (Resource startVertexEdge : startVertexEdges) {
83                 graph.deny(startVertexEdge, DN.HasStartVertex);
84                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
85             }
86             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
87             for (Resource endVertexEdge : endVertexEdges) {
88                 graph.deny(endVertexEdge, DN.HasEndVertex);
89                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
90             }
91             OrderedSetUtils.remove(graph, composite, slave);
92         }
93         return master;
94     }
95     
96     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
97         Layer0 L0 = Layer0.getInstance(graph);
98         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
99         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
100         if (!startComposite.equalsResource(endComposite)) {
101             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
102         }
103         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
104         
105         CRS crsClass = graph.adapt(crs, CRS.class);
106         
107         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
108         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
109         
110         return crsClass.calculateDistance(startCoords, endCoords);
111     }
112 }