]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Pushing some (very) old changes to remote.. should have done long ago
[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.DiagramGraphUtil;
14 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
15 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
16 import org.simantics.district.network.ontology.DistrictNetworkResource;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.operation.Layer0X;
19
20 public class DistrictNetworkUtil {
21
22     public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
23         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
24     }
25     
26     public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
27         Layer0 L0 = Layer0.getInstance(graph);
28         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
29         if (mapping == null) {
30             mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
31         }
32         
33         Resource edge = graph.newResource();
34         graph.claim(edge, L0.InstanceOf, DN.Edge);
35         
36         graph.claim(edge, DN.HasMapping, mapping);
37         
38         OrderedSetUtils.addFirst(graph, composite, edge);
39         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
40         
41         claimFreshElementName(graph, composite, edge);
42         return edge;
43     }
44     
45     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
46         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
47         return createVertex(graph, composite, coords, defaultVertexMapping);
48     }
49     
50     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
51         Layer0 L0 = Layer0.getInstance(graph);
52         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
53         DiagramResource DIA = DiagramResource.getInstance(graph);
54         Resource vertex = graph.newResource();
55         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
56         graph.claimLiteral(vertex, DIA.HasLocation, coords);
57         
58         graph.claim(vertex, DN.HasMapping, mapping);
59         
60         OrderedSetUtils.add(graph, composite, vertex);
61         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
62         
63         claimFreshElementName(graph, composite, vertex);
64         
65         // We need to put GraphLayer to newLayers so...
66         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
67             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
68             
69             GraphLayer gl = layerUtil.loadLayer(graph, layer);
70             gl.forEachTag(tag -> {
71                 DiagramGraphUtil.tag(graph, vertex, tag, true);
72             });
73         }
74         
75         return vertex;
76     }
77     
78     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
79         if (vertices.isEmpty())
80             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
81         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
82         Iterator<Resource> verticeIterator = vertices.iterator();
83         Resource master = verticeIterator.next();
84         while (verticeIterator.hasNext()) {
85             Resource slave = verticeIterator.next();
86             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
87             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
88             for (Resource startVertexEdge : startVertexEdges) {
89                 graph.deny(startVertexEdge, DN.HasStartVertex);
90                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
91             }
92             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
93             for (Resource endVertexEdge : endVertexEdges) {
94                 graph.deny(endVertexEdge, DN.HasEndVertex);
95                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
96             }
97             OrderedSetUtils.remove(graph, composite, slave);
98             // Remove ConsistsOf statement
99             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
100         }
101         return master;
102     }
103     
104     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
105         Layer0 L0 = Layer0.getInstance(graph);
106         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
107         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
108         if (!startComposite.equalsResource(endComposite)) {
109             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
110         }
111         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
112         
113         CRS crsClass = graph.adapt(crs, CRS.class);
114         
115         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
116         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
117         
118         return crsClass.calculateDistance(startCoords, endCoords);
119     }
120     
121     public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
122         Layer0 L0 = Layer0.getInstance(graph);
123         DiagramResource DIA = DiagramResource.getInstance(graph);
124         // Get name prefix from diagram
125         String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
126         if (namePrefix == null)
127             namePrefix = "";
128         // Give running name to element and increment the counter attached to the diagram.
129         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
130         if (l == null)
131             l = Long.valueOf(0L);
132         String name = namePrefix + l.toString();
133         graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
134         graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
135         return name;
136     }
137 }