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