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