1 package org.simantics.district.network;
3 import java.util.Collection;
4 import java.util.Iterator;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.datatypes.literal.RGB;
9 import org.simantics.datatypes.literal.RGB.Integer;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
14 import org.simantics.db.common.request.ResourceRead;
15 import org.simantics.db.common.utils.OrderedSetUtils;
16 import org.simantics.db.exception.BindingException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
19 import org.simantics.db.exception.ServiceException;
20 import org.simantics.db.layer0.request.PossibleVariable;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
24 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
25 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.adapters.NewCompositeActionFactory;
30 import org.simantics.operation.Layer0X;
32 public class DistrictNetworkUtil {
34 public static Resource createEdge(WriteGraph graph, Resource composite, double[] detailedGeometryCoords) throws DatabaseException {
35 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping), detailedGeometryCoords);
38 public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
39 Layer0 L0 = Layer0.getInstance(graph);
40 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
41 if (mapping == null) {
42 mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
45 Resource edge = graph.newResource();
46 graph.claim(edge, L0.InstanceOf, DN.Edge);
48 graph.claim(edge, DN.HasMapping, null, mapping);
50 OrderedSetUtils.addFirst(graph, composite, edge);
51 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
53 claimFreshElementName(graph, composite, edge);
55 // We need to put GraphLayer to newLayers so...
56 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
57 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
59 GraphLayer gl = layerUtil.loadLayer(graph, layer);
60 gl.forEachTag(tag -> {
61 DiagramGraphUtil.tag(graph, edge, tag, true);
65 // add detailed geometry (if any)
66 graph.claimLiteral(edge, DN.Edge_HasGeometry, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
70 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation) throws DatabaseException {
71 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
72 return createVertex(graph, composite, coords, elevation, defaultVertexMapping);
75 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation, Resource mapping) throws DatabaseException {
76 Layer0 L0 = Layer0.getInstance(graph);
77 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
78 DiagramResource DIA = DiagramResource.getInstance(graph);
79 Resource vertex = graph.newResource();
80 graph.claim(vertex, L0.InstanceOf, DN.Vertex);
81 graph.claimLiteral(vertex, DIA.HasLocation, coords);
82 graph.claimLiteral(vertex, DN.Vertex_HasElevation, elevation, Bindings.DOUBLE);
84 graph.claim(vertex, DN.HasMapping, null, mapping);
86 OrderedSetUtils.add(graph, composite, vertex);
87 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
89 claimFreshElementName(graph, composite, vertex);
91 // We need to put GraphLayer to newLayers so...
92 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
93 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
95 GraphLayer gl = layerUtil.loadLayer(graph, layer);
96 gl.forEachTag(tag -> {
97 DiagramGraphUtil.tag(graph, vertex, tag, true);
104 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
105 if (vertices.isEmpty())
106 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
107 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
108 Iterator<Resource> verticeIterator = vertices.iterator();
109 Resource master = verticeIterator.next();
110 while (verticeIterator.hasNext()) {
111 Resource slave = verticeIterator.next();
112 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
113 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
114 for (Resource startVertexEdge : startVertexEdges) {
115 graph.deny(startVertexEdge, DN.HasStartVertex);
116 graph.claim(startVertexEdge, DN.HasStartVertex, master);
118 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
119 for (Resource endVertexEdge : endVertexEdges) {
120 graph.deny(endVertexEdge, DN.HasEndVertex);
121 graph.claim(endVertexEdge, DN.HasEndVertex, master);
123 OrderedSetUtils.remove(graph, composite, slave);
124 // Remove ConsistsOf statement
125 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
130 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
131 Layer0 L0 = Layer0.getInstance(graph);
132 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
133 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
134 if (!startComposite.equalsResource(endComposite)) {
135 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
137 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
139 CRS crsClass = graph.adapt(crs, CRS.class);
141 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
142 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
144 return crsClass.calculateDistance(startCoords, endCoords);
147 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
148 Layer0 L0 = Layer0.getInstance(graph);
149 DiagramResource DIA = DiagramResource.getInstance(graph);
150 // Get name prefix from diagram
151 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
152 if (namePrefix == null)
154 // Give running name to element and increment the counter attached to the diagram.
155 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
157 l = Long.valueOf(0L);
158 String name = namePrefix + l.toString();
159 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
160 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
164 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
165 if (component == null)
167 DiagramResource DIA = DiagramResource.getInstance(graph);
168 if (graph.isInstanceOf(component, DIA.Element))
170 ModelingResources MOD = ModelingResources.getInstance(graph);
171 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
172 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
175 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
178 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
179 return graph.getPossibleObject(element, DN.MappedComponent);
182 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
185 Resource mappedElement = getMappedElement(graph, element);
186 if (mappedElement == null)
188 ModelingResources MOD = ModelingResources.getInstance(graph);
189 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
192 public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
193 return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
196 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
199 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
200 return graph.getPossibleObject(element, DN.MappedFromElement);
203 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
207 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
208 if (graph.isInstanceOf(input, DN.Element)) {
209 Resource mappedElement = getMappedElement(graph, input);
210 if (mappedElement == null)
213 ModelingResources MOD = ModelingResources.getInstance(graph);
214 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
215 if (mappedComponent == null)
218 return graph.syncRequest(new PossibleVariable(mappedComponent));
223 public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
224 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
225 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
228 graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
231 public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
232 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
233 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
234 return current != null ? current : true;
237 public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
238 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
239 graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
242 public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
243 if (diagram != null && graph.hasStatement(diagram)) {
244 return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
245 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
251 public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
252 return graph.getPossibleRelatedValue(diagram,
253 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
254 Bindings.getBindingUnchecked(RGB.Integer.class));
257 public static Resource createNetworkDiagram(WriteGraph graph, Resource target, Resource compositeType, String defaultName, Resource defaultEdgeMapping, Resource defaultVertexMapping, Resource rightClickVertexMapping, Resource leftClickVertexMapping, Resource crs) throws DatabaseException {
258 Resource composite = NewCompositeActionFactory.createComposite(graph, target, defaultName, compositeType);
260 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
261 Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
262 graph.claim(diagram, DN.EdgeDefaultMapping, defaultEdgeMapping);
263 graph.claim(diagram, DN.VertexDefaultMapping, defaultVertexMapping);
264 graph.claim(diagram, DN.RightClickDefaultMapping, rightClickVertexMapping);
265 graph.claim(diagram, DN.LeftClickDefaultMapping, leftClickVertexMapping);
266 graph.claim(diagram, DN.HasSpatialRefSystem, crs);
268 // Generated name prefix from composite name
269 String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
270 graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
275 public static final class MappedComponentRequest extends ResourceRead<Resource> {
276 public MappedComponentRequest(Resource element) {
281 public Resource perform(ReadGraph graph) throws DatabaseException {
282 return getMappedComponent(graph, resource);
286 public static class ResourceVertex {
288 public final boolean isConsumer;
289 public final Resource vertex;
290 public final double[] coords;
292 public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
293 this.vertex = vertex;
294 this.coords = coords;
295 this.isConsumer = isConsumer;
299 public static void changeMappingType(WriteGraph graph, Resource newMapping, List<Resource> elements) throws DatabaseException {
300 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
301 for (Resource element : elements) {
302 graph.deny(element, DN.HasMapping);
303 graph.claim(element, DN.HasMapping, newMapping);