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.db.layer0.request.PossibleVariable;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
16 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
17 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingResources;
21 import org.simantics.operation.Layer0X;
23 public class DistrictNetworkUtil {
25 public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
26 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
29 public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
30 Layer0 L0 = Layer0.getInstance(graph);
31 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
32 if (mapping == null) {
33 mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
36 Resource edge = graph.newResource();
37 graph.claim(edge, L0.InstanceOf, DN.Edge);
39 graph.claim(edge, DN.HasMapping, mapping);
41 OrderedSetUtils.addFirst(graph, composite, edge);
42 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
44 claimFreshElementName(graph, composite, edge);
46 // We need to put GraphLayer to newLayers so...
47 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
48 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
50 GraphLayer gl = layerUtil.loadLayer(graph, layer);
51 gl.forEachTag(tag -> {
52 DiagramGraphUtil.tag(graph, edge, tag, true);
59 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
60 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
61 return createVertex(graph, composite, coords, defaultVertexMapping);
64 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
65 Layer0 L0 = Layer0.getInstance(graph);
66 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
67 DiagramResource DIA = DiagramResource.getInstance(graph);
68 Resource vertex = graph.newResource();
69 graph.claim(vertex, L0.InstanceOf, DN.Vertex);
70 graph.claimLiteral(vertex, DIA.HasLocation, coords);
72 graph.claim(vertex, DN.HasMapping, mapping);
74 OrderedSetUtils.add(graph, composite, vertex);
75 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
77 claimFreshElementName(graph, composite, vertex);
79 // We need to put GraphLayer to newLayers so...
80 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
81 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
83 GraphLayer gl = layerUtil.loadLayer(graph, layer);
84 gl.forEachTag(tag -> {
85 DiagramGraphUtil.tag(graph, vertex, tag, true);
92 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
93 if (vertices.isEmpty())
94 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
95 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
96 Iterator<Resource> verticeIterator = vertices.iterator();
97 Resource master = verticeIterator.next();
98 while (verticeIterator.hasNext()) {
99 Resource slave = verticeIterator.next();
100 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
101 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
102 for (Resource startVertexEdge : startVertexEdges) {
103 graph.deny(startVertexEdge, DN.HasStartVertex);
104 graph.claim(startVertexEdge, DN.HasStartVertex, master);
106 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
107 for (Resource endVertexEdge : endVertexEdges) {
108 graph.deny(endVertexEdge, DN.HasEndVertex);
109 graph.claim(endVertexEdge, DN.HasEndVertex, master);
111 OrderedSetUtils.remove(graph, composite, slave);
112 // Remove ConsistsOf statement
113 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
118 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
119 Layer0 L0 = Layer0.getInstance(graph);
120 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
121 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
122 if (!startComposite.equalsResource(endComposite)) {
123 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
125 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
127 CRS crsClass = graph.adapt(crs, CRS.class);
129 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
130 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
132 return crsClass.calculateDistance(startCoords, endCoords);
135 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
136 Layer0 L0 = Layer0.getInstance(graph);
137 DiagramResource DIA = DiagramResource.getInstance(graph);
138 // Get name prefix from diagram
139 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
140 if (namePrefix == null)
142 // Give running name to element and increment the counter attached to the diagram.
143 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
145 l = Long.valueOf(0L);
146 String name = namePrefix + l.toString();
147 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
148 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
152 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
153 if (component == null)
155 DiagramResource DIA = DiagramResource.getInstance(graph);
156 if (graph.isInstanceOf(component, DIA.Element))
158 ModelingResources MOD = ModelingResources.getInstance(graph);
159 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
160 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
163 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
166 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
167 return graph.getPossibleObject(element, DN.MappedComponent);
170 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
173 Resource mappedElement = getMappedElement(graph, element);
174 if (mappedElement == null)
176 ModelingResources MOD = ModelingResources.getInstance(graph);
177 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
180 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
183 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
184 return graph.getPossibleObject(element, DN.MappedFromElement);
187 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
191 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
192 if (graph.isInstanceOf(input, DN.Element)) {
193 Resource mappedElement = getMappedElement(graph, input);
194 if (mappedElement == null)
197 ModelingResources MOD = ModelingResources.getInstance(graph);
198 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
199 if (mappedComponent == null)
202 return graph.syncRequest(new PossibleVariable(mappedComponent));