]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
ChangeMappingType SCL function to change district UC type for scripting
[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 import java.util.List;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.datatypes.literal.RGB;
9 import org.simantics.datatypes.literal.RGB.Integer;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
14 import org.simantics.db.common.request.ResourceRead;
15 import org.simantics.db.common.utils.OrderedSetUtils;
16 import org.simantics.db.exception.BindingException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
19 import org.simantics.db.exception.ServiceException;
20 import org.simantics.db.layer0.request.PossibleVariable;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
24 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
25 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.adapters.NewCompositeActionFactory;
30 import org.simantics.operation.Layer0X;
31
32 public class DistrictNetworkUtil {
33
34     public static Resource createEdge(WriteGraph graph, Resource composite, double[] detailedGeometryCoords) throws DatabaseException {
35         return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping), detailedGeometryCoords);
36     }
37
38     public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
39         Layer0 L0 = Layer0.getInstance(graph);
40         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
41         if (mapping == null) {
42             mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping);
43         }
44         
45         Resource edge = graph.newResource();
46         graph.claim(edge, L0.InstanceOf, DN.Edge);
47         
48         graph.claim(edge, DN.HasMapping, null, mapping);
49         
50         OrderedSetUtils.addFirst(graph, composite, edge);
51         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
52         
53         claimFreshElementName(graph, composite, edge);
54         
55         // We need to put GraphLayer to newLayers so...
56         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
57             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
58             
59             GraphLayer gl = layerUtil.loadLayer(graph, layer);
60             gl.forEachTag(tag -> {
61                 DiagramGraphUtil.tag(graph, edge, tag, true);
62             });
63         }
64         
65         // add detailed geometry (if any)
66         graph.claimLiteral(edge, DN.Edge_HasGeometry, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
67         return edge;
68     }
69
70     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation) throws DatabaseException {
71         Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
72         return createVertex(graph, composite, coords, elevation, defaultVertexMapping);
73     }
74
75     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation, Resource mapping) throws DatabaseException {
76         Layer0 L0 = Layer0.getInstance(graph);
77         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
78         DiagramResource DIA = DiagramResource.getInstance(graph);
79         Resource vertex = graph.newResource();
80         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
81         graph.claimLiteral(vertex, DIA.HasLocation, coords);
82         graph.claimLiteral(vertex, DN.Vertex_HasElevation, elevation, Bindings.DOUBLE);
83         
84         graph.claim(vertex, DN.HasMapping, null, mapping);
85         
86         OrderedSetUtils.add(graph, composite, vertex);
87         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
88         
89         claimFreshElementName(graph, composite, vertex);
90         
91         // We need to put GraphLayer to newLayers so...
92         for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) {
93             IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
94             
95             GraphLayer gl = layerUtil.loadLayer(graph, layer);
96             gl.forEachTag(tag -> {
97                 DiagramGraphUtil.tag(graph, vertex, tag, true);
98             });
99         }
100         
101         return vertex;
102     }
103     
104     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
105         if (vertices.isEmpty())
106             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
107         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
108         Iterator<Resource> verticeIterator = vertices.iterator();
109         Resource master = verticeIterator.next();
110         while (verticeIterator.hasNext()) {
111             Resource slave = verticeIterator.next();
112             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
113             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
114             for (Resource startVertexEdge : startVertexEdges) {
115                 graph.deny(startVertexEdge, DN.HasStartVertex);
116                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
117             }
118             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
119             for (Resource endVertexEdge : endVertexEdges) {
120                 graph.deny(endVertexEdge, DN.HasEndVertex);
121                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
122             }
123             OrderedSetUtils.remove(graph, composite, slave);
124             // Remove ConsistsOf statement
125             graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave);
126         }
127         return master;
128     }
129     
130     public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException {
131         Layer0 L0 = Layer0.getInstance(graph);
132         Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf);
133         Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf);
134         if (!startComposite.equalsResource(endComposite)) {
135             throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex);
136         }
137         Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem);
138         
139         CRS crsClass = graph.adapt(crs, CRS.class);
140         
141         double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
142         double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
143         
144         return crsClass.calculateDistance(startCoords, endCoords);
145     }
146     
147     public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
148         Layer0 L0 = Layer0.getInstance(graph);
149         DiagramResource DIA = DiagramResource.getInstance(graph);
150         // Get name prefix from diagram
151         String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix);
152         if (namePrefix == null)
153             namePrefix = "";
154         // Give running name to element and increment the counter attached to the diagram.
155         Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
156         if (l == null)
157             l = Long.valueOf(0L);
158         String name = namePrefix + l.toString();
159         graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
160         graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
161         return name;
162     }
163
164     public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException {
165         if (component == null)
166             return null;
167         DiagramResource DIA = DiagramResource.getInstance(graph);
168         if (graph.isInstanceOf(component, DIA.Element))
169             return component;
170         ModelingResources MOD = ModelingResources.getInstance(graph);
171         Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
172         return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null;
173     }
174
175     public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException {
176         if (element == null)
177             return null;
178         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
179         return graph.getPossibleObject(element, DN.MappedComponent);
180     }
181
182     public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException {
183         if (element == null)
184             return null;
185         Resource mappedElement = getMappedElement(graph, element);
186         if (mappedElement == null)
187             return null;
188         ModelingResources MOD = ModelingResources.getInstance(graph);
189         return graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
190     }
191     
192     public static Resource getMappedComponentCached(ReadGraph graph, Resource vertex) throws DatabaseException {
193         return graph.syncRequest(new MappedComponentRequest(vertex), TransientCacheListener.instance());
194     }
195
196     public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException {
197         if (element == null)
198             return null;
199         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
200         return graph.getPossibleObject(element, DN.MappedFromElement);
201     }
202
203     public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException {
204         if (input == null)
205             return null;
206
207         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
208         if (graph.isInstanceOf(input, DN.Element)) {
209             Resource mappedElement = getMappedElement(graph, input);
210             if (mappedElement == null)
211                 return null;
212
213             ModelingResources MOD = ModelingResources.getInstance(graph);
214             Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent);
215             if (mappedComponent == null)
216                 return null;
217
218             return graph.syncRequest(new PossibleVariable(mappedComponent));
219         }
220         return null;
221     }
222
223     public static void toggleDrawMap(WriteGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
224         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
225         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
226         if (current == null)
227             current = true;
228         graph.claimLiteral(diagram, DN.Diagram_drawMapEnabled, !current, Bindings.BOOLEAN);
229     }
230
231     public static Boolean drawMapEnabled(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
232         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
233         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
234         return current != null ? current : true;
235     }
236
237     public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
238         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
239         graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
240     }
241     
242     public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
243         if (diagram != null && graph.hasStatement(diagram)) {
244             return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
245                 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
246         } else {
247             return false;
248         }
249     }
250
251     public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
252         return graph.getPossibleRelatedValue(diagram,
253                 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
254                 Bindings.getBindingUnchecked(RGB.Integer.class));
255     }
256     
257     public static Resource createNetworkDiagram(WriteGraph graph, Resource target, Resource compositeType, String defaultName, Resource defaultEdgeMapping, Resource defaultVertexMapping, Resource rightClickVertexMapping, Resource leftClickVertexMapping, Resource crs) throws DatabaseException {
258         Resource composite = NewCompositeActionFactory.createComposite(graph, target, defaultName, compositeType);
259
260         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
261         Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
262         graph.claim(diagram, DN.EdgeDefaultMapping, defaultEdgeMapping);
263         graph.claim(diagram, DN.VertexDefaultMapping, defaultVertexMapping);
264         graph.claim(diagram, DN.RightClickDefaultMapping, rightClickVertexMapping);
265         graph.claim(diagram, DN.LeftClickDefaultMapping, leftClickVertexMapping);
266         graph.claim(diagram, DN.HasSpatialRefSystem, crs);
267         
268         // Generated name prefix from composite name
269         String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
270         graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
271         
272         return composite;
273     }
274
275     public static final class MappedComponentRequest extends ResourceRead<Resource> {
276         public MappedComponentRequest(Resource element) {
277             super(element);
278         }
279
280         @Override
281         public Resource perform(ReadGraph graph) throws DatabaseException {
282             return getMappedComponent(graph, resource);
283         }
284     }
285
286     public static class ResourceVertex {
287         
288         public final boolean isConsumer;
289         public final Resource vertex;
290         public final double[] coords;
291         
292         public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
293             this.vertex = vertex;
294             this.coords = coords;
295             this.isConsumer = isConsumer;
296         }
297     }
298     
299     public static void changeMappingType(WriteGraph graph, Resource newMapping, List<Resource> elements) throws DatabaseException {
300         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
301         for (Resource element : elements) {
302             graph.deny(element, DN.HasMapping);
303             graph.claim(element, DN.HasMapping, newMapping);
304         }
305     }
306 }