1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.sysdyn.ui.elements.connections;
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Shape;
17 import java.awt.Stroke;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Rectangle2D;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
28 import org.eclipse.jface.resource.StringConverter;
29 import org.eclipse.swt.graphics.RGB;
30 import org.simantics.databoard.Bindings;
31 import org.simantics.db.AsyncReadGraph;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.Session;
35 import org.simantics.db.Statement;
36 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
37 import org.simantics.db.common.utils.NameUtils;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.procedure.AsyncProcedure;
40 import org.simantics.diagram.G2DUtils;
41 import org.simantics.diagram.adapter.SyncElementFactory;
42 import org.simantics.diagram.connection.ConnectionVisuals;
43 import org.simantics.diagram.connection.RouteGraph;
44 import org.simantics.diagram.connection.RouteGraphConnectionClass;
45 import org.simantics.diagram.connection.RouteLine;
46 import org.simantics.diagram.connection.RouteNode;
47 import org.simantics.diagram.connection.RouteTerminal;
48 import org.simantics.diagram.connection.rendering.ConnectionStyle;
49 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
50 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
51 import org.simantics.diagram.content.EdgeResource;
52 import org.simantics.diagram.content.ResourceTerminal;
53 import org.simantics.diagram.content.TerminalMap;
54 import org.simantics.diagram.query.DiagramRequests;
55 import org.simantics.diagram.stubs.DiagramResource;
56 import org.simantics.diagram.stubs.G2DResource;
57 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
58 import org.simantics.diagram.synchronization.graph.RouteGraphConnection;
59 import org.simantics.diagram.ui.DiagramModelHints;
60 import org.simantics.g2d.canvas.ICanvasContext;
61 import org.simantics.g2d.connection.ConnectionEntity;
62 import org.simantics.g2d.diagram.DiagramHints;
63 import org.simantics.g2d.diagram.IDiagram;
64 import org.simantics.g2d.diagram.handler.DataElementMap;
65 import org.simantics.g2d.diagram.handler.Topology.Connection;
66 import org.simantics.g2d.diagram.handler.Topology.Terminal;
67 import org.simantics.g2d.element.ElementClass;
68 import org.simantics.g2d.element.ElementHints;
69 import org.simantics.g2d.element.ElementUtils;
70 import org.simantics.g2d.element.IElement;
71 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
72 import org.simantics.g2d.element.handler.TerminalTopology;
73 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
74 import org.simantics.g2d.routing.algorithm2.Router4;
75 import org.simantics.g2d.utils.TopologicalSelectionExpander;
76 import org.simantics.layer0.Layer0;
77 import org.simantics.modeling.ModelingResources;
78 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;
79 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent;
80 import org.simantics.structural.stubs.StructuralResource2;
81 import org.simantics.structural2.modelingRules.CPTerminal;
82 import org.simantics.structural2.modelingRules.IAttachmentRelationMap;
83 import org.simantics.structural2.modelingRules.IModelingRules;
84 import org.simantics.sysdyn.SysdynResource;
85 import org.simantics.sysdyn.ui.elements.ValveFactory.ValveSceneGraph;
86 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPreferences;
87 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPropertyExternalRead;
88 import org.simantics.utils.datastructures.Pair;
90 * An element class for Sysdyn Flow elements.
91 * Copied from RouteGraphConnectionClassFactory and adapted to Flow needs
93 * @author Teemu Lempinen
96 public class RouteFlowConnectionFactory extends SyncElementFactory {
98 public static final ElementClass CLASS = RouteFlowEdgeClass.FLOW_CLASS;
102 StructuralResource2 STR;
103 ModelingResources MOD;
105 public RouteFlowConnectionFactory(ReadGraph graph) {
106 this.L0 = Layer0.getInstance(graph);
107 this.DIA = DiagramResource.getInstance(graph);
108 this.STR = StructuralResource2.getInstance(graph);
109 this.MOD = ModelingResources.getInstance(graph);
113 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
114 procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
118 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
119 return graph.getService(SysdynResource.class).FlowConnection;
123 public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource connection,
124 final IElement element) throws DatabaseException {
127 element.setHint(DiagramHints.ROUTE_ALGORITHM, new Router4(false));
128 IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
131 DiagramResource DR = DiagramResource.getInstance(graph);
132 G2DResource G2D = G2DResource.getInstance(graph);
133 if (graph.isInstanceOf(connection, DR.ColorProvider)) {
134 Statement colorStatement = graph.getPossibleStatement(connection, G2D.HasColor);
135 if(colorStatement != null && !colorStatement.isAsserted(connection)) {
136 element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, colorStatement.getObject()));
138 String colorString = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(connection, SysdynDiagramPreferences.getColorPreferenceName(graph, connection))));
139 if(colorString != null) {
140 RGB rgb = StringConverter.asRGB(colorString, null);
142 color = new Color(rgb.red, rgb.green, rgb.blue);
143 element.setHint(ElementHints.KEY_TEXT_COLOR, color);
150 RouteGraph rg = new RouteGraph();
152 Set<Resource> nodes = new HashSet<Resource>();
153 Set<EdgeResource> links = new HashSet<EdgeResource>();
154 Map<Object, RouteNode> nodeByData = new HashMap<Object, RouteNode>();
156 // Needed to support ConnectionEntity#getTerminalConnections
157 Set<BackendConnection> backendonnections = new HashSet<BackendConnection>();
159 // Load all route graph interior RouteNodes: route lines and points
160 for (Resource interiorNode : graph.getObjects(connection, DIA.HasInteriorRouteNode)) {
161 if (graph.isInstanceOf(interiorNode, DIA.RouteLine)) {
162 Boolean isHorizontal = graph.getRelatedValue(interiorNode, DIA.IsHorizontal, Bindings.BOOLEAN);
163 Double position = graph.getRelatedValue(interiorNode, DIA.HasPosition, Bindings.DOUBLE);
164 RouteLine line = rg.addLine(isHorizontal, position);
165 line.setData( RouteGraphConnection.serialize(graph, interiorNode) );
167 nodes.add( interiorNode );
168 nodeByData.put( interiorNode, line );
170 for (Resource connectedTo : graph.getObjects(interiorNode, DIA.AreConnected)) {
171 links.add( new EdgeResource(interiorNode, connectedTo) );
173 } else if (graph.isInstanceOf(interiorNode, DIA.RoutePoint)) {
174 // Not supported yet. Ignore.
178 Rectangle2D bounds = new Rectangle2D.Double();
180 // Load all node terminal connections as RouteTerminals
181 for (Statement toConnector : graph.getStatements(connection, DIA.HasConnector)) {
182 Resource connector = toConnector.getObject();
183 Resource attachmentRelation = toConnector.getPredicate();
185 Statement terminalStm = findTerminalStatement(graph, STR, connection, connector);
186 if (terminalStm == null)
187 // Ignore broken connector: attached to the connection but not to any terminal.
190 Resource terminalElement = terminalStm.getObject();
191 Resource terminalElementType = graph.getPossibleType(terminalElement, DIA.Element);
192 if (terminalElementType == null)
193 // Ignore non-element terminal elements
196 Resource connectionRelation = graph.getInverse(terminalStm.getPredicate());
198 // Discover node and terminal this connector is connected to.
199 TerminalMap terminals = graph.syncRequest(DiagramRequests.elementTypeTerminals(terminalElementType),
200 TransientCacheListener.<TerminalMap> instance());
201 Resource terminal = terminals.getTerminal(connectionRelation);
202 if (terminal == null) {
203 System.err.println(getClass().getSimpleName()
204 + ": Could not find terminal for connection point "
205 + NameUtils.getSafeName(graph, connectionRelation, true)
207 + NameUtils.getSafeName(graph, terminalElement, true));
211 double[] position = graph.getRelatedValue(connector, DIA.HasRelativeLocation, Bindings.DOUBLE_ARRAY);
212 if (position.length != 2)
213 position = new double[] { 0, 0 };
215 //System.out.println("terminalStm: " + NameUtils.toString(graph, terminalStm));
216 AffineTransform terminalElementTr = getWorldTransform(graph, terminalElement);
218 double x = terminalElementTr.getTranslateX();
219 double y = terminalElementTr.getTranslateY();
220 double minx = x-1, miny = y-1, maxx = x+1, maxy = y+1;
223 // Use modelingRules to ascertain the proper attachmentRelation
224 // for this terminal connection, if available.
225 if (modelingRules != null) {
226 // Get attachmentRelation from modelingRules if possible.
227 IAttachmentRelationMap map = modelingRules.getAttachmentRelations(graph, connection);
228 Resource att = map.get(graph, new CPTerminal(terminalElement, terminal));
230 //System.out.println("modeling rules attachment: " + NameUtils.getSafeLabel(graph, att));
231 attachmentRelation = att;
234 //System.out.println("attachment: " + NameUtils.getSafeLabel(graph, attachmentRelation));
236 // Get element bounds to decide allowed terminal direction(s)
237 IElement te = graph.syncRequest(DiagramRequests.getElement(canvas, diagram, terminalElement, null));
239 // Fetch the flow width
240 float lw = FlowConnectionStyle.DEFAULT_LINE_WIDTH;
241 SysdynResource sr = SysdynResource.getInstance(graph);
242 Float width = graph.getPossibleRelatedValue(connection, sr.FlowConnection_width, Bindings.FLOAT);
246 if(te.getElementClass().containsClass(ValveSceneGraph.class)) {
247 // Valve behaves differently. The flow must start inside the valve bounds
248 ValveSceneGraph vs = te.getElementClass().getSingleItem(ValveSceneGraph.class);
249 Rectangle2D size = new Rectangle2D.Double();
250 vs.getValveBounds(te, size);
251 Shape shp = org.simantics.g2d.utils.GeometryUtils.transformShape(size, terminalElementTr);
252 size = (Rectangle2D) shp;
253 bounds.setFrame(new Rectangle2D.Double(size.getCenterX() - (lw/2), size.getCenterY() - (lw/2), lw, lw));
256 bounds = ElementUtils.getElementShape(te).getBounds2D();
257 Shape shp = org.simantics.g2d.utils.GeometryUtils.transformShape(bounds, terminalElementTr);
258 bounds.setFrame(shp.getBounds2D());
261 x = bounds.getCenterX();
262 y = bounds.getCenterY();
264 // Expand bounds by 4mm to make the connections enter the terminals
265 // at a straight angle and from a distance instead of coming in
267 //GeometryUtils.expandRectangle(bounds, 4);
269 minx = bounds.getMinX();
270 miny = bounds.getMinY();
271 maxx = bounds.getMaxX();
272 maxy = bounds.getMaxY();
274 Integer allowedDirections = graph.getPossibleRelatedValue(terminal, DIA.Terminal_AllowedDirections, Bindings.INTEGER);
276 // Valve behaves differently. Allowed directions depend on the orientation of the valve
277 if(te.getElementClass().containsClass(ValveSceneGraph.class)) {
278 if(graph.hasStatement(terminalElement, sr.ValveSymbol_orientation, sr.Vertical)) {
279 allowedDirections = 10; // Directions up and down (1010)
281 allowedDirections = 5; // Directions left and right (0101)
284 if (allowedDirections != null) {
285 direction |= allowedDirections;
287 direction |= RouteGraphConnectionClass.shortestDirectionOutOfBounds(x, y, bounds);
290 backendonnections.add(
291 new BackendConnection(
292 toEdgeEnd(graph, attachmentRelation, EdgeEnd.Begin),
298 // Accept any horizontal/vertical direction if nothing is defined
301 //System.out.println("load line style: " + NameUtils.getSafeLabel(graph, attachmentRelation));
302 ILineEndStyle endStyle = loadLineEndStyle(graph, te, attachmentRelation, color, lw);
304 RouteTerminal routeTerminal = rg.addBigTerminal(/*x, y,*/ minx, miny, maxx, maxy, /*direction,*/ endStyle);
305 routeTerminal.setData( RouteGraphConnection.serialize(graph, connector) );
307 nodes.add( connector );
308 nodeByData.put( connector, routeTerminal );
310 for (Resource connectedTo : graph.getObjects(connector, DIA.AreConnected)) {
311 links.add( new EdgeResource(connectedTo, connector) );
315 // Finish route graph loading by Linking route nodes together
316 for (EdgeResource link : links) {
317 RouteNode n1 = nodeByData.get(link.first());
318 RouteNode n2 = nodeByData.get(link.second());
319 if (n1 == null || n2 == null) {
320 System.err.println("Stray connection link found: " + link.toString(graph));
326 // Load connection line style
327 ConnectionStyle style = readConnectionStyle(graph, modelingRules, connection, element);
328 StyledRouteGraphRenderer renderer = new StyledRouteGraphRenderer(style);
330 // Finish element load
331 element.setHint(RouteGraphConnectionClass.KEY_ROUTEGRAPH, rg);
332 element.setHint(RouteGraphConnectionClass.KEY_RENDERER, renderer);
333 element.setHint(RouteGraphConnectionClass.KEY_PICK_TOLERANCE, 0.5);
335 // Initialize ConnectionEntity in element
336 // NOTE: MUST use the mapped element with class CE, not the connection (element) were loading into.
337 // GDS will synchronize element into mappedElement in a controlled manner.
338 element.setHint(ElementHints.KEY_CONNECTION_ENTITY, new CE(diagram, connection, element, backendonnections));
340 // Setup graph writeback support for route graph modifications
341 final Session session = graph.getSession();
342 element.setHint(RouteGraphConnectionClass.KEY_RG_LISTENER, new IRouteGraphListener() {
344 public void routeGraphChanged(RouteGraphChangeEvent event) {
345 scheduleSynchronize(session, connection, event);
349 final Map<String, Pair<Resource, Object>> properties = new HashMap<>();
350 for (Resource predicate : graph.getPredicates(connection)) {
351 if (graph.isSubrelationOf(predicate, L0.HasProperty)) {
352 String name = graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);
353 Object value = graph.getPossibleRelatedValue(connection, predicate);
354 if (name != null && value != null) {
355 properties.put(name, Pair.make(predicate, value));
359 element.setHint(DiagramHints.PROPERTIES, properties);
362 private EdgeEnd toEdgeEnd(ReadGraph graph, Resource attachmentRelation, EdgeEnd defaultValue)
363 throws DatabaseException {
364 if (graph.isSubrelationOf(attachmentRelation, DIA.IsTailConnectorOf))
365 return EdgeEnd.Begin;
366 if (graph.isSubrelationOf(attachmentRelation, DIA.IsHeadConnectorOf))
371 private ConnectionStyle readConnectionStyle(ReadGraph graph, IModelingRules modelingRules, Resource connection,
372 IElement element) throws DatabaseException {
373 Resource connectionType = null;
374 if (modelingRules != null)
375 connectionType = modelingRules.getConnectionType(graph, connection);
376 if (connectionType == null)
377 connectionType = graph.getPossibleObject(connection, STR.HasConnectionType);
379 ConnectionVisuals cv = null;
380 if (connectionType != null)
381 cv = graph.syncRequest(DiagramRequests.getConnectionVisuals(connectionType),
382 TransientCacheListener.<ConnectionVisuals> instance());
385 Color lineColor = element.getHint(ElementHints.KEY_TEXT_COLOR);
386 if (lineColor == null)
387 lineColor = (cv != null && cv.toColor() != null) ? cv.toColor() : Color.DARK_GRAY;
389 Stroke lineStroke = cv != null ? cv.stroke : null;
390 if (lineStroke == null)
391 lineStroke = new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10, null, 0);
393 return new FlowConnectionStyle(
404 * @return connection relation statement from diagram connection connector
406 * @throws DatabaseException
408 private static Statement findTerminalStatement(ReadGraph graph, StructuralResource2 STR, Resource connection,
409 Resource connector) throws DatabaseException {
410 for (Statement stm : graph.getStatements(connector, STR.Connects)) {
411 if (connection.equals(stm.getObject()))
418 public ILineEndStyle loadLineEndStyle(ReadGraph graph, IElement te, Resource attachmentRelation, Color color, float lineWidth)
419 throws DatabaseException {
421 // TODO: change bounds according to terminal type: Very small rectangle for Valves, Text box size for Stocks and Clouds
422 if(te.getElementClass().containsClass(ValveSceneGraph.class)) {
423 style = new FlowArrowLineStyle("none 0 0 0", color);
425 if (graph.isSubrelationOf(attachmentRelation, DIA.HasHeadConnector)) {
426 float arrowSize = lineWidth * 1.3f;
427 style = new FlowArrowLineStyle("fill " + arrowSize + " " + arrowSize + " 0", color);
429 style = new FlowArrowLineStyle("none 0 0 0", color);
439 * @throws DatabaseException
441 private static AffineTransform getWorldTransform(ReadGraph graph, Resource element) throws DatabaseException {
442 ModelingResources MOD = ModelingResources.getInstance(graph);
443 AffineTransform result = DiagramGraphUtil.getAffineTransform(graph, element);
445 Resource parentComponent = graph.getPossibleObject(element, MOD.HasParentComponent);
446 if (parentComponent == null)
448 element = graph.getPossibleObject(parentComponent, MOD.ComponentToElement);
451 AffineTransform tr = DiagramGraphUtil.getAffineTransform(graph, element);
452 tr.setToTranslation(tr.getTranslateX(), tr.getTranslateY());
453 result.preConcatenate(tr);
458 protected void scheduleSynchronize(Session session, Resource connection, RouteGraphChangeEvent event) {
459 session.asyncRequest(RouteGraphConnection.synchronizer(connection, event));
463 * Must have this in order for {@link TopologicalSelectionExpander} to work.
464 * Otherwise this is pretty useless and should be deprecated altogether.
466 * @see ElementHints#KEY_CONNECTION_ENTITY
468 static class CE implements ConnectionEntity {
471 private IDiagram diagram;
473 private transient DataElementMap dataMap;
476 * The connection instance resource in the graph backend.
478 final Resource connection;
481 * The connection entity element which is a part of the diagram.
483 IElement connectionElement;
486 * @see #getTerminalConnections(Collection)
488 final Set<BackendConnection> backendConnections;
493 Set<Connection> terminalConnections;
496 public CE(IDiagram diagram, Resource connection, IElement connectionElement, Set<BackendConnection> backendConnections) {
497 if (connectionElement == null)
498 throw new NullPointerException("null connection element");
499 this.diagram = diagram;
500 this.dataMap = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
501 this.connection = connection;
502 this.connectionElement = connectionElement;
503 this.backendConnections = backendConnections;
504 IElement ce = getConnection0();
506 this.connectionElement = ce;
509 public IElement getConnection0() {
510 IElement connectionElement = dataMap.getElement(diagram, connection);
511 return connectionElement;
515 public IElement getConnection() {
516 IElement c = getConnection0();
518 c = this.connectionElement;
522 public Object getConnectionObject() {
526 public IElement getConnectionElement() {
527 return connectionElement;
531 public Collection<IElement> getBranchPoints(Collection<IElement> result) {
532 return result != null ? result : Collections.<IElement> emptyList();
536 public Collection<IElement> getSegments(Collection<IElement> result) {
537 return result != null ? result : Collections.<IElement> emptyList();
541 public Collection<Connection> getTerminalConnections(Collection<Connection> result) {
542 if (terminalConnections == null)
543 terminalConnections = calculateTerminalConnections();
545 result = new ArrayList<Connection>(terminalConnections);
547 result.addAll(terminalConnections);
548 return terminalConnections;
551 private Set<Connection> calculateTerminalConnections() {
552 DataElementMap dem = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
553 Set<Connection> result = new HashSet<Connection>();
554 ArrayList<Terminal> ts = new ArrayList<Terminal>();
555 for (BackendConnection bc : backendConnections) {
556 IElement e = dem.getElement(diagram, bc.node);
559 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
560 tt.getTerminals(e, ts);
561 for (Terminal t : ts) {
562 if (t instanceof ResourceTerminal) {
563 ResourceTerminal rt = (ResourceTerminal) t;
564 if (bc.terminal.equals(rt.getResource())) {
565 result.add(new Connection(connectionElement, bc.end, e, t));
575 public void setListener(ConnectionListener listener) {
576 throw new UnsupportedOperationException();
580 public String toString() {
581 return getClass().getSimpleName() + "[resource=" + connection + ", connectionElement=" + connectionElement
587 public static class BackendConnection {
588 public final Resource node;
589 public final Resource terminal;
590 public final EdgeEnd end;
591 public BackendConnection(EdgeEnd end, Resource node, Resource terminal) {
594 assert terminal != null;
597 this.terminal = terminal;
600 public boolean equals(Object obj) {
603 if (!(obj instanceof Connection))
605 Connection other = (Connection) obj;
606 return other.terminal == terminal
607 && other.node == node
611 public int hashCode() {
612 final int prime = 31;
614 result = prime * result + end.hashCode();
615 result = prime * result + ((node == null) ? 0 : node.hashCode());
616 result = prime * result + ((terminal == null) ? 0 : terminal.hashCode());
620 public String toString() {
621 return "BackendConnection[node=" + node + ", terminal=" + terminal + ", end=" + end + "]";