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 Layer0 L0 = Layer0.getInstance(graph);
24 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
26 Resource edge = graph.newResource();
27 graph.claim(edge, L0.InstanceOf, DN.Edge);
29 Resource defaultEdgeMapping = graph.getPossibleObject(composite, DN.EdgeDefaultMapping);
30 graph.claim(edge, DN.HasMapping, defaultEdgeMapping);
32 OrderedSetUtils.add(graph, composite, edge);
33 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
35 claimFreshElementName(graph, composite, edge);
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);
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);
52 graph.claim(vertex, DN.HasMapping, mapping);
54 OrderedSetUtils.add(graph, composite, vertex);
55 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
57 claimFreshElementName(graph, composite, vertex);
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);
63 GraphLayer gl = layerUtil.loadLayer(graph, layer);
64 gl.forEachTag(tag -> {
65 DiagramGraphUtil.tag(graph, vertex, tag, true);
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);
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);
91 OrderedSetUtils.remove(graph, composite, slave);
92 // Remove ConsistsOf statement
93 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
98 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
99 Layer0 L0 = Layer0.getInstance(graph);
100 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
101 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
102 if (!startComposite.equalsResource(endComposite)) {
103 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
105 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
107 CRS crsClass = graph.adapt(crs, CRS.class);
109 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
110 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
112 return crsClass.calculateDistance(startCoords, endCoords);
115 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
116 Layer0 L0 = Layer0.getInstance(graph);
117 DiagramResource DIA = DiagramResource.getInstance(graph);
118 // Get name prefix from diagram
119 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
120 if (namePrefix == null)
122 // Give running name to element and increment the counter attached to the diagram.
123 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
125 l = Long.valueOf(0L);
126 String name = namePrefix + l.toString();
127 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
128 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);