]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Some more added functionality to simantics district editor etc
[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.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.utils.OrderedSetUtils;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.util.RemoverUtil;
11 import org.simantics.diagram.stubs.DiagramResource;
12 import org.simantics.district.network.ontology.DistrictNetworkResource;
13 import org.simantics.layer0.Layer0;
14
15 public class DistrictNetworkUtil {
16
17     public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
18         Layer0 L0 = Layer0.getInstance(graph);
19         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
20         
21         Resource edge = graph.newResource();
22         graph.claim(edge, L0.InstanceOf, DN.Edge);
23         OrderedSetUtils.add(graph, composite, edge);
24         graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
25         return edge;
26     }
27     
28     public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {
29         Layer0 L0 = Layer0.getInstance(graph);
30         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
31         DiagramResource DIA = DiagramResource.getInstance(graph);
32         Resource vertex = graph.newResource();
33         graph.claim(vertex, L0.InstanceOf, DN.Vertex);
34         graph.claimLiteral(vertex, DIA.HasLocation, coords);
35         OrderedSetUtils.add(graph, composite, vertex);
36         graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
37         
38         return vertex;
39     }
40     
41     public static Resource joinVertices(WriteGraph graph, Collection<Resource> vertices) throws DatabaseException {
42         if (vertices.isEmpty())
43             throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!");
44         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
45         Iterator<Resource> verticeIterator = vertices.iterator();
46         Resource master = verticeIterator.next();
47         while (verticeIterator.hasNext()) {
48             Resource slave = verticeIterator.next();
49             Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf);
50             Collection<Resource> startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse);
51             for (Resource startVertexEdge : startVertexEdges) {
52                 graph.deny(startVertexEdge, DN.HasStartVertex);
53                 graph.claim(startVertexEdge, DN.HasStartVertex, master);
54             }
55             Collection<Resource> endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse);
56             for (Resource endVertexEdge : endVertexEdges) {
57                 graph.deny(endVertexEdge, DN.HasEndVertex);
58                 graph.claim(endVertexEdge, DN.HasEndVertex, master);
59             }
60             OrderedSetUtils.remove(graph, composite, slave);
61         }
62         return master;
63     }
64 }