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.BindingException;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
14 import org.simantics.db.exception.ServiceException;
15 import org.simantics.db.layer0.request.PossibleVariable;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
19 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
20 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
21 import org.simantics.district.network.ontology.DistrictNetworkResource;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.modeling.ModelingResources;
24 import org.simantics.operation.Layer0X;
26 public class DistrictNetworkUtil {
28 public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
29 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
32 public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
33 Layer0 L0 = Layer0.getInstance(graph);
34 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
35 if (mapping == null) {
36 mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
39 Resource edge = graph.newResource();
40 graph.claim(edge, L0.InstanceOf, DN.Edge);
42 graph.claim(edge, DN.HasMapping, mapping);
44 OrderedSetUtils.addFirst(graph, composite, edge);
45 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
47 claimFreshElementName(graph, composite, edge);
49 // We need to put GraphLayer to newLayers so...
50 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
51 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
53 GraphLayer gl = layerUtil.loadLayer(graph, layer);
54 gl.forEachTag(tag -> {
55 DiagramGraphUtil.tag(graph, edge, tag, true);
62 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
63 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
64 return createVertex(graph, composite, coords, defaultVertexMapping);
67 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
68 Layer0 L0 = Layer0.getInstance(graph);
69 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
70 DiagramResource DIA = DiagramResource.getInstance(graph);
71 Resource vertex = graph.newResource();
72 graph.claim(vertex, L0.InstanceOf, DN.Vertex);
73 graph.claimLiteral(vertex, DIA.HasLocation, coords);
75 graph.claim(vertex, DN.HasMapping, mapping);
77 OrderedSetUtils.add(graph, composite, vertex);
78 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
80 claimFreshElementName(graph, composite, vertex);
82 // We need to put GraphLayer to newLayers so...
83 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
84 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
86 GraphLayer gl = layerUtil.loadLayer(graph, layer);
87 gl.forEachTag(tag -> {
88 DiagramGraphUtil.tag(graph, vertex, tag, true);
95 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
96 if (vertices.isEmpty())
97 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
98 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
99 Iterator<Resource> verticeIterator = vertices.iterator();
100 Resource master = verticeIterator.next();
101 while (verticeIterator.hasNext()) {
102 Resource slave = verticeIterator.next();
103 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
104 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
105 for (Resource startVertexEdge : startVertexEdges) {
106 graph.deny(startVertexEdge, DN.HasStartVertex);
107 graph.claim(startVertexEdge, DN.HasStartVertex, master);
109 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
110 for (Resource endVertexEdge : endVertexEdges) {
111 graph.deny(endVertexEdge, DN.HasEndVertex);
112 graph.claim(endVertexEdge, DN.HasEndVertex, master);
114 OrderedSetUtils.remove(graph, composite, slave);
115 // Remove ConsistsOf statement
116 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
121 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
122 Layer0 L0 = Layer0.getInstance(graph);
123 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
124 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
125 if (!startComposite.equalsResource(endComposite)) {
126 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
128 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
130 CRS crsClass = graph.adapt(crs, CRS.class);
132 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
133 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
135 return crsClass.calculateDistance(startCoords, endCoords);
138 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
139 Layer0 L0 = Layer0.getInstance(graph);
140 DiagramResource DIA = DiagramResource.getInstance(graph);
141 // Get name prefix from diagram
142 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
143 if (namePrefix == null)
145 // Give running name to element and increment the counter attached to the diagram.
146 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
148 l = Long.valueOf(0L);
149 String name = namePrefix + l.toString();
150 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
151 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
155 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
156 if (component == null)
158 DiagramResource DIA = DiagramResource.getInstance(graph);
159 if (graph.isInstanceOf(component, DIA.Element))
161 ModelingResources MOD = ModelingResources.getInstance(graph);
162 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
163 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
166 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
169 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
170 return graph.getPossibleObject(element, DN.MappedComponent);
173 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
176 Resource mappedElement = getMappedElement(graph, element);
177 if (mappedElement == null)
179 ModelingResources MOD = ModelingResources.getInstance(graph);
180 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
183 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
186 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
187 return graph.getPossibleObject(element, DN.MappedFromElement);
190 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
194 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
195 if (graph.isInstanceOf(input, DN.Element)) {
196 Resource mappedElement = getMappedElement(graph, input);
197 if (mappedElement == null)
200 ModelingResources MOD = ModelingResources.getInstance(graph);
201 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
202 if (mappedComponent == null)
205 return graph.syncRequest(new PossibleVariable(mappedComponent));
210 public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
211 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
212 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
215 graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
218 public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
219 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
220 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
221 return current != null ? current : true;