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) throws DatabaseException {
67 Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
68 return createVertex(graph, composite, coords, defaultVertexMapping);
71 public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, 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);
79 graph.claim(vertex, DN.HasMapping, null, mapping);
81 OrderedSetUtils.add(graph, composite, vertex);
82 graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
84 claimFreshElementName(graph, composite, vertex);
86 // We need to put GraphLayer to newLayers so...
87 for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
88 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
90 GraphLayer gl = layerUtil.loadLayer(graph, layer);
91 gl.forEachTag(tag -> {
92 DiagramGraphUtil.tag(graph, vertex, tag, true);
99 public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
100 if (vertices.isEmpty())
101 throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
102 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
103 Iterator<Resource> verticeIterator = vertices.iterator();
104 Resource master = verticeIterator.next();
105 while (verticeIterator.hasNext()) {
106 Resource slave = verticeIterator.next();
107 Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
108 Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
109 for (Resource startVertexEdge : startVertexEdges) {
110 graph.deny(startVertexEdge, DN.HasStartVertex);
111 graph.claim(startVertexEdge, DN.HasStartVertex, master);
113 Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
114 for (Resource endVertexEdge : endVertexEdges) {
115 graph.deny(endVertexEdge, DN.HasEndVertex);
116 graph.claim(endVertexEdge, DN.HasEndVertex, master);
118 OrderedSetUtils.remove(graph, composite, slave);
119 // Remove ConsistsOf statement
120 graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
125 public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
126 Layer0 L0 = Layer0.getInstance(graph);
127 Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
128 Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
129 if (!startComposite.equalsResource(endComposite)) {
130 throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
132 Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
134 CRS crsClass = graph.adapt(crs, CRS.class);
136 double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
137 double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
139 return crsClass.calculateDistance(startCoords, endCoords);
142 public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
143 Layer0 L0 = Layer0.getInstance(graph);
144 DiagramResource DIA = DiagramResource.getInstance(graph);
145 // Get name prefix from diagram
146 String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
147 if (namePrefix == null)
149 // Give running name to element and increment the counter attached to the diagram.
150 Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
152 l = Long.valueOf(0L);
153 String name = namePrefix + l.toString();
154 graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
155 graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
159 public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
160 if (component == null)
162 DiagramResource DIA = DiagramResource.getInstance(graph);
163 if (graph.isInstanceOf(component, DIA.Element))
165 ModelingResources MOD = ModelingResources.getInstance(graph);
166 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
167 return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
170 public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
173 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
174 return graph.getPossibleObject(element, DN.MappedComponent);
177 public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
180 Resource mappedElement = getMappedElement(graph, element);
181 if (mappedElement == null)
183 ModelingResources MOD = ModelingResources.getInstance(graph);
184 return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
187 public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
188 return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
191 public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
194 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
195 return graph.getPossibleObject(element, DN.MappedFromElement);
198 public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
202 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
203 if (graph.isInstanceOf(input, DN.Element)) {
204 Resource mappedElement = getMappedElement(graph, input);
205 if (mappedElement == null)
208 ModelingResources MOD = ModelingResources.getInstance(graph);
209 Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
210 if (mappedComponent == null)
213 return graph.syncRequest(new PossibleVariable(mappedComponent));
218 public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
219 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
220 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
223 graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
226 public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
227 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
228 Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
229 return current != null ? current : true;
232 public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
233 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
234 graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
237 public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
238 if (diagram != null && graph.hasStatement(diagram)) {
239 return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
240 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
246 public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
247 return graph.getPossibleRelatedValue(diagram,
248 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
249 Bindings.getBindingUnchecked(RGB.Integer.class));
252 public static final class MappedComponentRequest extends ResourceRead<Resource> {
253 public MappedComponentRequest(Resource element) {
258 public Resource perform(ReadGraph graph) throws DatabaseException {
259 return getMappedComponent(graph, resource);