]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
bac4707d87cc8fe627e0d24f2604503a2fc6d22d
[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.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;
29
30 public class DistrictNetworkUtil {
31
32     public static Resource createEdge(WriteGraph graph, Resource composite, double[] detailedGeometryCoords) throws DatabaseException {
33         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping), detailedGeometryCoords);
34     }
35
36     public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping, double[] detailedGeometryCoords) 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);
41         }
42         
43         Resource edge = graph.newResource();
44         graph.claim(edge, L0.InstanceOf, DN.Edge);
45         
46         graph.claim(edge, DN.HasMapping, null, mapping);
47         
48         OrderedSetUtils.addFirst(graph, composite, edge);
49         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
50         
51         claimFreshElementName(graph, composite, edge);
52         
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);
56             
57             GraphLayer gl = layerUtil.loadLayer(graph, layer);
58             gl.forEachTag(tag -> {
59                 DiagramGraphUtil.tag(graph, edge, tag, true);
60             });
61         }
62         
63         // add detailed geometry (if any)
64         graph.claimLiteral(edge, DN.Edge_HasGeometry, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
65         return edge;
66     }
67
68     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation) throws DatabaseException {
69         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
70         return createVertex(graph, composite, coords, elevation, defaultVertexMapping);
71     }
72
73     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation, Resource mapping) throws DatabaseException {
74         Layer0 L0 = Layer0.getInstance(graph);
75         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
76         DiagramResource DIA = DiagramResource.getInstance(graph);
77         Resource vertex = graph.newResource();
78         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
79         graph.claimLiteral(vertex, DIA.HasLocation, coords);
80         graph.claimLiteral(vertex, DN.Vertex_HasElevation, elevation, Bindings.DOUBLE);
81         
82         graph.claim(vertex, DN.HasMapping, null, mapping);
83         
84         OrderedSetUtils.add(graph, composite, vertex);
85         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
86         
87         claimFreshElementName(graph, composite, vertex);
88         
89         // We need to put GraphLayer to newLayers so...
90         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
91             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
92             
93             GraphLayer gl = layerUtil.loadLayer(graph, layer);
94             gl.forEachTag(tag -> {
95                 DiagramGraphUtil.tag(graph, vertex, tag, true);
96             });
97         }
98         
99         return vertex;
100     }
101     
102     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
103         if (vertices.isEmpty())
104             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
105         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
106         Iterator<Resource> verticeIterator = vertices.iterator();
107         Resource master = verticeIterator.next();
108         while (verticeIterator.hasNext()) {
109             Resource slave = verticeIterator.next();
110             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
111             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
112             for (Resource startVertexEdge : startVertexEdges) {
113                 graph.deny(startVertexEdge, DN.HasStartVertex);
114                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
115             }
116             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
117             for (Resource endVertexEdge : endVertexEdges) {
118                 graph.deny(endVertexEdge, DN.HasEndVertex);
119                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
120             }
121             OrderedSetUtils.remove(graph, composite, slave);
122             // Remove ConsistsOf statement
123             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
124         }
125         return master;
126     }
127     
128     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
129         Layer0 L0 = Layer0.getInstance(graph);
130         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
131         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
132         if (!startComposite.equalsResource(endComposite)) {
133             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
134         }
135         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
136         
137         CRS crsClass = graph.adapt(crs, CRS.class);
138         
139         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
140         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
141         
142         return crsClass.calculateDistance(startCoords, endCoords);
143     }
144     
145     public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
146         Layer0 L0 = Layer0.getInstance(graph);
147         DiagramResource DIA = DiagramResource.getInstance(graph);
148         // Get name prefix from diagram
149         String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
150         if (namePrefix == null)
151             namePrefix = "";
152         // Give running name to element and increment the counter attached to the diagram.
153         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
154         if (l == null)
155             l = Long.valueOf(0L);
156         String name = namePrefix + l.toString();
157         graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
158         graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
159         return name;
160     }
161
162     public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
163         if (component == null)
164             return null;
165         DiagramResource DIA = DiagramResource.getInstance(graph);
166         if (graph.isInstanceOf(component, DIA.Element))
167             return component;
168         ModelingResources MOD = ModelingResources.getInstance(graph);
169         Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
170         return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
171     }
172
173     public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
174         if (element == null)
175             return null;
176         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
177         return graph.getPossibleObject(element, DN.MappedComponent);
178     }
179
180     public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
181         if (element == null)
182             return null;
183         Resource mappedElement = getMappedElement(graph, element);
184         if (mappedElement == null)
185             return null;
186         ModelingResources MOD = ModelingResources.getInstance(graph);
187         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
188     }
189     
190     public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
191         return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
192     }
193
194     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
195         if (element == null)
196             return null;
197         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
198         return graph.getPossibleObject(element, DN.MappedFromElement);
199     }
200
201     public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
202         if (input == null)
203             return null;
204
205         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
206         if (graph.isInstanceOf(input, DN.Element)) {
207             Resource mappedElement = getMappedElement(graph, input);
208             if (mappedElement == null)
209                 return null;
210
211             ModelingResources MOD = ModelingResources.getInstance(graph);
212             Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
213             if (mappedComponent == null)
214                 return null;
215
216             return graph.syncRequest(new PossibleVariable(mappedComponent));
217         }
218         return null;
219     }
220
221     public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
222         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
223         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
224         if (current == null)
225             current = true;
226         graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
227     }
228
229     public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
230         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
231         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
232         return current != null ? current : true;
233     }
234
235     public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
236         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
237         graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
238     }
239     
240     public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
241         if (diagram != null && graph.hasStatement(diagram)) {
242             return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
243                 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
244         } else {
245             return false;
246         }
247     }
248
249     public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
250         return graph.getPossibleRelatedValue(diagram,
251                 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
252                 Bindings.getBindingUnchecked(RGB.Integer.class));
253     }
254
255     public static final class MappedComponentRequest extends ResourceRead<Resource> {
256         public MappedComponentRequest(Resource element) {
257             super(element);
258         }
259
260         @Override
261         public Resource perform(ReadGraph graph) throws DatabaseException {
262             return getMappedComponent(graph, resource);
263         }
264     }
265
266 }