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.datatypes.literal.RGB;
8 import org.simantics.datatypes.literal.RGB.Integer;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.utils.OrderedSetUtils;
13 import org.simantics.db.exception.BindingException;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
16 import org.simantics.db.exception.ServiceException;
17 import org.simantics.db.layer0.request.PossibleVariable;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
21 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
22 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.ModelingResources;
26 import org.simantics.operation.Layer0X;
28 public class DistrictNetworkUtil {
30 public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
31 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
34 public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
35 Layer0 L0 = Layer0.getInstance(graph);
36 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
37 if (mapping == null) {
38 mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
41 Resource edge = graph.newResource();
42 graph.claim(edge, L0.InstanceOf, DN.Edge);
44 graph.claim(edge, DN.HasMapping, mapping);
46 OrderedSetUtils.addFirst(graph, composite, edge);
47 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
49 claimFreshElementName(graph, composite, edge);
51 // We need to put GraphLayer to newLayers so...
52 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
53 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
55 GraphLayer gl = layerUtil.loadLayer(graph, layer);
56 gl.forEachTag(tag -> {
57 DiagramGraphUtil.tag(graph, edge, tag, true);
64 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
65 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
66 return createVertex(graph, composite, coords, defaultVertexMapping);
69 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
70 Layer0 L0 = Layer0.getInstance(graph);
71 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
72 DiagramResource DIA = DiagramResource.getInstance(graph);
73 Resource vertex = graph.newResource();
74 graph.claim(vertex, L0.InstanceOf, DN.Vertex);
75 graph.claimLiteral(vertex, DIA.HasLocation, coords);
77 graph.claim(vertex, DN.HasMapping, mapping);
79 OrderedSetUtils.add(graph, composite, vertex);
80 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
82 claimFreshElementName(graph, composite, vertex);
84 // We need to put GraphLayer to newLayers so...
85 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
86 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
88 GraphLayer gl = layerUtil.loadLayer(graph, layer);
89 gl.forEachTag(tag -> {
90 DiagramGraphUtil.tag(graph, vertex, tag, true);
97 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
98 if (vertices.isEmpty())
99 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
100 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
101 Iterator<Resource> verticeIterator = vertices.iterator();
102 Resource master = verticeIterator.next();
103 while (verticeIterator.hasNext()) {
104 Resource slave = verticeIterator.next();
105 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
106 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
107 for (Resource startVertexEdge : startVertexEdges) {
108 graph.deny(startVertexEdge, DN.HasStartVertex);
109 graph.claim(startVertexEdge, DN.HasStartVertex, master);
111 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
112 for (Resource endVertexEdge : endVertexEdges) {
113 graph.deny(endVertexEdge, DN.HasEndVertex);
114 graph.claim(endVertexEdge, DN.HasEndVertex, master);
116 OrderedSetUtils.remove(graph, composite, slave);
117 // Remove ConsistsOf statement
118 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
123 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
124 Layer0 L0 = Layer0.getInstance(graph);
125 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
126 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
127 if (!startComposite.equalsResource(endComposite)) {
128 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
130 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
132 CRS crsClass = graph.adapt(crs, CRS.class);
134 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
135 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
137 return crsClass.calculateDistance(startCoords, endCoords);
140 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
141 Layer0 L0 = Layer0.getInstance(graph);
142 DiagramResource DIA = DiagramResource.getInstance(graph);
143 // Get name prefix from diagram
144 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
145 if (namePrefix == null)
147 // Give running name to element and increment the counter attached to the diagram.
148 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
150 l = Long.valueOf(0L);
151 String name = namePrefix + l.toString();
152 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
153 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
157 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
158 if (component == null)
160 DiagramResource DIA = DiagramResource.getInstance(graph);
161 if (graph.isInstanceOf(component, DIA.Element))
163 ModelingResources MOD = ModelingResources.getInstance(graph);
164 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
165 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
168 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
171 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
172 return graph.getPossibleObject(element, DN.MappedComponent);
175 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
178 Resource mappedElement = getMappedElement(graph, element);
179 if (mappedElement == null)
181 ModelingResources MOD = ModelingResources.getInstance(graph);
182 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
185 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
188 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
189 return graph.getPossibleObject(element, DN.MappedFromElement);
192 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
196 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
197 if (graph.isInstanceOf(input, DN.Element)) {
198 Resource mappedElement = getMappedElement(graph, input);
199 if (mappedElement == null)
202 ModelingResources MOD = ModelingResources.getInstance(graph);
203 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
204 if (mappedComponent == null)
207 return graph.syncRequest(new PossibleVariable(mappedComponent));
212 public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
213 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
214 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
217 graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
220 public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
221 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
222 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
223 return current != null ? current : true;
226 public static void changeMapBackgroundColor(WriteGraph graph, Resource resource, Integer integer) throws DatabaseException {
227 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
228 graph.claimLiteral(resource, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
231 public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
232 return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
233 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));