1 package org.simantics.district.network.ui;
3 import java.awt.geom.Rectangle2D;
4 import java.util.Collection;
6 import java.util.Optional;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.request.ObjectsWithType;
12 import org.simantics.db.exception.BindingException;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
15 import org.simantics.db.exception.ServiceException;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
19 import org.simantics.diagram.synchronization.SynchronizationHints;
20 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
21 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.layer0.Layer0;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.vividsolutions.jts.geom.Envelope;
30 import com.vividsolutions.jts.index.quadtree.Quadtree;
32 public class DNEdgeBuilder {
34 private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeBuilder.class);
36 private Resource diagramResource;
37 private IDiagram diagram;
38 private GraphLayerManager glm;
40 public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
41 this.diagramResource = diagramResource;
42 this.diagram = diagram;
44 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
45 glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
48 public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
49 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
50 double halfPadding = padding / 2;
52 Quadtree vv = new Quadtree();
53 for (Resource vertex : vertices) {
54 double[] coords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
55 double x1 = coords[0] - halfPadding;
56 double y1= coords[1] - halfPadding;
57 double x2 = coords[0] + halfPadding;
58 double y2= coords[1] + halfPadding;
59 Envelope e = new Envelope(x1, x2, y1, y2);
60 vv.insert(e, new ResourceVertex(vertex, coords, false));
62 return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, padding, false);
65 public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
67 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
70 Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, startElevation, padding);
71 Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, endElevation, padding);
72 if (startVertex.equals(endVertex)) {
73 LOGGER.info("Circular edges are not supported, startVertex: {}, endVertex: {}", startVertex, endVertex);
74 return Optional.empty();
77 // 1. Get diagram edge to construct
78 Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
80 if (writeElevationToEdgeFromPoints) {
81 graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE);
84 graph.claim(edge, DN.HasStartVertex, startVertex);
85 graph.claim(edge, DN.HasEndVertex, endVertex);
87 // We need to put GraphLayer to newLayers so...
88 // for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) {
89 // IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
91 // GraphLayer gl = layerUtil.loadLayer(graph, layer);
92 // gl.forEachTag(tag -> {
93 // DiagramGraphUtil.tag(graph, startVertex, tag, true);
94 // DiagramGraphUtil.tag(graph, endVertex, tag, true);
98 return Optional.of(edge);
100 private static double calculateElevationFromVertices(WriteGraph graph, Resource startVertex, Resource endVertex) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
101 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
102 Double startElevation = graph.getPossibleRelatedValue(startVertex, DN.Vertex_HasElevation, Bindings.DOUBLE);
103 Double endElevation = graph.getPossibleRelatedValue(endVertex, DN.Vertex_HasElevation, Bindings.DOUBLE);
104 if (startElevation != null && endElevation != null) {
105 return (startElevation + endElevation) / 2;
110 public void create(WriteGraph graph, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
112 Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, padding);
113 // 7. Put the element on all the currently active layers if possible.
115 putOnActiveLayer(graph, edge.get());
118 Layer0Utils.addCommentMetadata(graph, "Added edge " + edge.get());
119 graph.markUndoPoint();
122 private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
123 glm.removeFromAllLayers(graph, res);
124 glm.putElementOnVisibleLayers(diagram, graph, res);
127 private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Quadtree qtree, double[] coords, double elevation, double padding) throws DatabaseException {
128 Resource vertex = null;
129 double halfPadding = padding / 2;
130 double maxDistance = Double.MAX_VALUE;
131 double x1 = coords[0] - halfPadding;
132 double y1= coords[1] - halfPadding;
133 double x2 = coords[0] + halfPadding;
134 double y2= coords[1] + halfPadding;
135 Envelope e = new Envelope(x1, x2, y1, y2);
137 List<?> result = qtree.query(e);
138 @SuppressWarnings("unchecked")
139 List<ResourceVertex> vertices = (List<ResourceVertex>) result;
140 for (ResourceVertex vertx : vertices) {
141 Rectangle2D existing = new Rectangle2D.Double(vertx.coords[0] - halfPadding, vertx.coords[1] - halfPadding, padding, padding);
142 Rectangle2D tobecreated = new Rectangle2D.Double(x1, y1, padding, padding);
143 if (existing.intersects(tobecreated)) {
144 double dist = Math.sqrt((Math.pow(coords[0] - vertx.coords[0], 2) + (Math.pow(coords[1] - vertx.coords[1], 2))));
145 if (dist <= maxDistance) {
146 vertex = vertx.vertex;
151 if (vertex == null) {
152 vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords, elevation);
153 qtree.insert(e, new ResourceVertex(vertex, coords, false));
158 private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
159 return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
162 public static class ResourceVertex {
164 final boolean isConsumer;
165 final Resource vertex;
166 final double[] coords;
168 public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
169 this.vertex = vertex;
170 this.coords = coords;
171 this.isConsumer = isConsumer;