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.procedure.adapter.TransientCacheListener;
13 import org.simantics.db.common.request.ResourceRead;
14 import org.simantics.db.common.utils.OrderedSetUtils;
15 import org.simantics.db.exception.BindingException;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
18 import org.simantics.db.exception.ServiceException;
19 import org.simantics.db.layer0.request.PossibleVariable;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.diagram.stubs.DiagramResource;
22 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
23 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
24 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
25 import org.simantics.district.network.ontology.DistrictNetworkResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.operation.Layer0X;
30 public class DistrictNetworkUtil {
32 public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
33 return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
36 public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
37 Layer0 L0 = Layer0.getInstance(graph);
38 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
39 if (mapping == null) {
40 mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
43 Resource edge = graph.newResource();
44 graph.claim(edge, L0.InstanceOf, DN.Edge);
46 graph.claim(edge, DN.HasMapping, null, mapping);
48 OrderedSetUtils.addFirst(graph, composite, edge);
49 graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
51 claimFreshElementName(graph, composite, edge);
53 // We need to put GraphLayer to newLayers so...
54 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
55 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
57 GraphLayer gl = layerUtil.loadLayer(graph, layer);
58 gl.forEachTag(tag -> {
59 DiagramGraphUtil.tag(graph, edge, tag, true);
66 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation) throws DatabaseException {
67 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
68 return createVertex(graph, composite, coords, elevation, defaultVertexMapping);
71 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation, Resource mapping) throws DatabaseException {
72 Layer0 L0 = Layer0.getInstance(graph);
73 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
74 DiagramResource DIA = DiagramResource.getInstance(graph);
75 Resource vertex = graph.newResource();
76 graph.claim(vertex, L0.InstanceOf, DN.Vertex);
77 graph.claimLiteral(vertex, DIA.HasLocation, coords);
78 graph.claimLiteral(vertex, DN.Vertex_HasElevation, elevation, Bindings.DOUBLE);
80 graph.claim(vertex, DN.HasMapping, null, mapping);
82 OrderedSetUtils.add(graph, composite, vertex);
83 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
85 claimFreshElementName(graph, composite, vertex);
87 // We need to put GraphLayer to newLayers so...
88 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
89 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
91 GraphLayer gl = layerUtil.loadLayer(graph, layer);
92 gl.forEachTag(tag -> {
93 DiagramGraphUtil.tag(graph, vertex, tag, true);
100 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
101 if (vertices.isEmpty())
102 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
103 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
104 Iterator<Resource> verticeIterator = vertices.iterator();
105 Resource master = verticeIterator.next();
106 while (verticeIterator.hasNext()) {
107 Resource slave = verticeIterator.next();
108 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
109 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
110 for (Resource startVertexEdge : startVertexEdges) {
111 graph.deny(startVertexEdge, DN.HasStartVertex);
112 graph.claim(startVertexEdge, DN.HasStartVertex, master);
114 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
115 for (Resource endVertexEdge : endVertexEdges) {
116 graph.deny(endVertexEdge, DN.HasEndVertex);
117 graph.claim(endVertexEdge, DN.HasEndVertex, master);
119 OrderedSetUtils.remove(graph, composite, slave);
120 // Remove ConsistsOf statement
121 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
126 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
127 Layer0 L0 = Layer0.getInstance(graph);
128 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
129 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
130 if (!startComposite.equalsResource(endComposite)) {
131 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
133 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
135 CRS crsClass = graph.adapt(crs, CRS.class);
137 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
138 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
140 return crsClass.calculateDistance(startCoords, endCoords);
143 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
144 Layer0 L0 = Layer0.getInstance(graph);
145 DiagramResource DIA = DiagramResource.getInstance(graph);
146 // Get name prefix from diagram
147 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
148 if (namePrefix == null)
150 // Give running name to element and increment the counter attached to the diagram.
151 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
153 l = Long.valueOf(0L);
154 String name = namePrefix + l.toString();
155 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
156 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
160 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
161 if (component == null)
163 DiagramResource DIA = DiagramResource.getInstance(graph);
164 if (graph.isInstanceOf(component, DIA.Element))
166 ModelingResources MOD = ModelingResources.getInstance(graph);
167 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
168 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
171 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
174 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
175 return graph.getPossibleObject(element, DN.MappedComponent);
178 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
181 Resource mappedElement = getMappedElement(graph, element);
182 if (mappedElement == null)
184 ModelingResources MOD = ModelingResources.getInstance(graph);
185 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
188 public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
189 return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
192 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
195 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
196 return graph.getPossibleObject(element, DN.MappedFromElement);
199 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
203 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
204 if (graph.isInstanceOf(input, DN.Element)) {
205 Resource mappedElement = getMappedElement(graph, input);
206 if (mappedElement == null)
209 ModelingResources MOD = ModelingResources.getInstance(graph);
210 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
211 if (mappedComponent == null)
214 return graph.syncRequest(new PossibleVariable(mappedComponent));
219 public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
220 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
221 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
224 graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
227 public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
228 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
229 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
230 return current != null ? current : true;
233 public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
234 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
235 graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
238 public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
239 if (diagram != null && graph.hasStatement(diagram)) {
240 return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
241 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
247 public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
248 return graph.getPossibleRelatedValue(diagram,
249 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
250 Bindings.getBindingUnchecked(RGB.Integer.class));
253 public static final class MappedComponentRequest extends ResourceRead<Resource> {
254 public MappedComponentRequest(Resource element) {
259 public Resource perform(ReadGraph graph) throws DatabaseException {
260 return getMappedComponent(graph, resource);