1 package org.simantics.district.network;
3 import java.util.Collection;
4 import java.util.Iterator;
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;
20 public class DistrictNetworkUtil {
22 public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
23 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
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);
33 Resource edge = graph.newResource();
34 graph.claim(edge, L0.InstanceOf, DN.Edge);
36 graph.claim(edge, DN.HasMapping, mapping);
38 OrderedSetUtils.addFirst(graph, composite, edge);
39 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
41 claimFreshElementName(graph, composite, edge);
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);
47 GraphLayer gl = layerUtil.loadLayer(graph, layer);
48 gl.forEachTag(tag -> {
49 DiagramGraphUtil.tag(graph, edge, tag, true);
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);
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);
69 graph.claim(vertex, DN.HasMapping, mapping);
71 OrderedSetUtils.add(graph, composite, vertex);
72 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
74 claimFreshElementName(graph, composite, vertex);
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);
80 GraphLayer gl = layerUtil.loadLayer(graph, layer);
81 gl.forEachTag(tag -> {
82 DiagramGraphUtil.tag(graph, vertex, tag, true);
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);
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);
108 OrderedSetUtils.remove(graph, composite, slave);
109 // Remove ConsistsOf statement
110 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
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);
122 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
124 CRS crsClass = graph.adapt(crs, CRS.class);
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);
129 return crsClass.calculateDistance(startCoords, endCoords);
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)
139 // Give running name to element and increment the counter attached to the diagram.
140 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
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);