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