]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Disconnected subgraph analysis for district network diagrams
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / DistrictNetworkUtil.java
1 package org.simantics.district.network;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5
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.DatabaseException;
12 import org.simantics.db.layer0.request.PossibleVariable;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
16 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
17 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingResources;
21 import org.simantics.operation.Layer0X;
22
23 public class DistrictNetworkUtil {
24
25     public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
26         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
27     }
28
29     public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
30         Layer0 L0 = Layer0.getInstance(graph);
31         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
32         if (mapping == null) {
33             mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
34         }
35         
36         Resource edge = graph.newResource();
37         graph.claim(edge, L0.InstanceOf, DN.Edge);
38         
39         graph.claim(edge, DN.HasMapping, mapping);
40         
41         OrderedSetUtils.addFirst(graph, composite, edge);
42         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
43         
44         claimFreshElementName(graph, composite, edge);
45         
46         // We need to put GraphLayer to newLayers so...
47         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
48             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
49             
50             GraphLayer gl = layerUtil.loadLayer(graph, layer);
51             gl.forEachTag(tag -> {
52                 DiagramGraphUtil.tag(graph, edge, tag, true);
53             });
54         }
55         
56         return edge;
57     }
58
59     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
60         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
61         return createVertex(graph, composite, coords, defaultVertexMapping);
62     }
63
64     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException {
65         Layer0 L0 = Layer0.getInstance(graph);
66         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
67         DiagramResource DIA = DiagramResource.getInstance(graph);
68         Resource vertex = graph.newResource();
69         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
70         graph.claimLiteral(vertex, DIA.HasLocation, coords);
71         
72         graph.claim(vertex, DN.HasMapping, mapping);
73         
74         OrderedSetUtils.add(graph, composite, vertex);
75         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
76         
77         claimFreshElementName(graph, composite, vertex);
78         
79         // We need to put GraphLayer to newLayers so...
80         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
81             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
82             
83             GraphLayer gl = layerUtil.loadLayer(graph, layer);
84             gl.forEachTag(tag -> {
85                 DiagramGraphUtil.tag(graph, vertex, tag, true);
86             });
87         }
88         
89         return vertex;
90     }
91     
92     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
93         if (vertices.isEmpty())
94             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
95         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
96         Iterator<Resource> verticeIterator = vertices.iterator();
97         Resource master = verticeIterator.next();
98         while (verticeIterator.hasNext()) {
99             Resource slave = verticeIterator.next();
100             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
101             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
102             for (Resource startVertexEdge : startVertexEdges) {
103                 graph.deny(startVertexEdge, DN.HasStartVertex);
104                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
105             }
106             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
107             for (Resource endVertexEdge : endVertexEdges) {
108                 graph.deny(endVertexEdge, DN.HasEndVertex);
109                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
110             }
111             OrderedSetUtils.remove(graph, composite, slave);
112             // Remove ConsistsOf statement
113             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
114         }
115         return master;
116     }
117     
118     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
119         Layer0 L0 = Layer0.getInstance(graph);
120         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
121         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
122         if (!startComposite.equalsResource(endComposite)) {
123             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
124         }
125         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
126         
127         CRS crsClass = graph.adapt(crs, CRS.class);
128         
129         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
130         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
131         
132         return crsClass.calculateDistance(startCoords, endCoords);
133     }
134     
135     public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
136         Layer0 L0 = Layer0.getInstance(graph);
137         DiagramResource DIA = DiagramResource.getInstance(graph);
138         // Get name prefix from diagram
139         String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
140         if (namePrefix == null)
141             namePrefix = "";
142         // Give running name to element and increment the counter attached to the diagram.
143         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
144         if (l == null)
145             l = Long.valueOf(0L);
146         String name = namePrefix + l.toString();
147         graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
148         graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
149         return name;
150     }
151
152     public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
153         if (component == null)
154             return null;
155         DiagramResource DIA = DiagramResource.getInstance(graph);
156         if (graph.isInstanceOf(component, DIA.Element))
157             return component;
158         ModelingResources MOD = ModelingResources.getInstance(graph);
159         Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
160         return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
161     }
162
163     public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
164         if (element == null)
165             return null;
166         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
167         return graph.getPossibleObject(element, DN.MappedComponent);
168     }
169
170     public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
171         if (element == null)
172             return null;
173         Resource mappedElement = getMappedElement(graph, element);
174         if (mappedElement == null)
175             return null;
176         ModelingResources MOD = ModelingResources.getInstance(graph);
177         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
178     }
179
180     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
181         if (element == null)
182             return null;
183         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
184         return graph.getPossibleObject(element, DN.MappedFromElement);
185     }
186
187     public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
188         if (input == null)
189             return null;
190
191         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
192         if (graph.isInstanceOf(input, DN.Element)) {
193             Resource mappedElement = getMappedElement(graph, input);
194             if (mappedElement == null)
195                 return null;
196
197             ModelingResources MOD = ModelingResources.getInstance(graph);
198             Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
199             if (mappedComponent == null)
200                 return null;
201
202             return graph.syncRequest(new PossibleVariable(mappedComponent));
203         }
204         return null;
205     }
206
207 }