]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Make vertices smaller on map UI & CSV import performance improvements
[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) throws DatabaseException {
33         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
34     }
35
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);
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         return edge;
64     }
65
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);
69     }
70
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);
79         
80         graph.claim(vertex, DN.HasMapping, null, mapping);
81         
82         OrderedSetUtils.add(graph, composite, vertex);
83         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
84         
85         claimFreshElementName(graph, composite, vertex);
86         
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);
90             
91             GraphLayer gl = layerUtil.loadLayer(graph, layer);
92             gl.forEachTag(tag -> {
93                 DiagramGraphUtil.tag(graph, vertex, tag, true);
94             });
95         }
96         
97         return vertex;
98     }
99     
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);
113             }
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);
118             }
119             OrderedSetUtils.remove(graph, composite, slave);
120             // Remove ConsistsOf statement
121             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
122         }
123         return master;
124     }
125     
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);
132         }
133         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
134         
135         CRS crsClass = graph.adapt(crs, CRS.class);
136         
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);
139         
140         return crsClass.calculateDistance(startCoords, endCoords);
141     }
142     
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)
149             namePrefix = "";
150         // Give running name to element and increment the counter attached to the diagram.
151         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
152         if (l == null)
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);
157         return name;
158     }
159
160     public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
161         if (component == null)
162             return null;
163         DiagramResource DIA = DiagramResource.getInstance(graph);
164         if (graph.isInstanceOf(component, DIA.Element))
165             return component;
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;
169     }
170
171     public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
172         if (element == null)
173             return null;
174         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
175         return graph.getPossibleObject(element, DN.MappedComponent);
176     }
177
178     public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
179         if (element == null)
180             return null;
181         Resource mappedElement = getMappedElement(graph, element);
182         if (mappedElement == null)
183             return null;
184         ModelingResources MOD = ModelingResources.getInstance(graph);
185         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
186     }
187     
188     public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
189         return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
190     }
191
192     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
193         if (element == null)
194             return null;
195         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
196         return graph.getPossibleObject(element, DN.MappedFromElement);
197     }
198
199     public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
200         if (input == null)
201             return null;
202
203         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
204         if (graph.isInstanceOf(input, DN.Element)) {
205             Resource mappedElement = getMappedElement(graph, input);
206             if (mappedElement == null)
207                 return null;
208
209             ModelingResources MOD = ModelingResources.getInstance(graph);
210             Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
211             if (mappedComponent == null)
212                 return null;
213
214             return graph.syncRequest(new PossibleVariable(mappedComponent));
215         }
216         return null;
217     }
218
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);
222         if (current == null)
223             current = true;
224         graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
225     }
226
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;
231     }
232
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));
236     }
237     
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));
242         } else {
243             return false;
244         }
245     }
246
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));
251     }
252
253     public static final class MappedComponentRequest extends ResourceRead<Resource> {
254         public MappedComponentRequest(Resource element) {
255             super(element);
256         }
257
258         @Override
259         public Resource perform(ReadGraph graph) throws DatabaseException {
260             return getMappedComponent(graph, resource);
261         }
262     }
263
264 }