1 package org.simantics.modeling.rules;
3 import gnu.trove.set.hash.THashSet;
5 import java.util.Collection;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.structural.stubs.StructuralResource2;
15 public class RelatedDiagramConnections {
18 StructuralResource2 sr;
21 THashSet<Resource> connections = new THashSet<Resource>();
22 THashSet<Resource> connectionJoins = new THashSet<Resource>();
23 THashSet<Resource> outputConnectors = new THashSet<Resource>();
25 public RelatedDiagramConnections(ReadGraph g) {
27 this.sr = StructuralResource2.getInstance(g);
28 this.dr = DiagramResource.getInstance(g);
31 public void addConnection(Resource connection) throws DatabaseException {
32 if(connections.add(connection)) {
33 for(Statement hasConnector : g.getStatements(connection, dr.HasConnector)) {
34 Resource connector = hasConnector.getObject();
35 for(Resource flag : g.getObjects(connector, sr.Connects)) {
36 if(g.isInstanceOf(flag, dr.Flag)) {
37 for(Resource join : g.getObjects(flag, dr.FlagIsJoinedBy))
38 addConnectionJoin(join);
41 if (g.isSubrelationOf(hasConnector.getPredicate(), dr.HasTailConnector)) {
42 outputConnectors.add(connector);
48 public void addConnectionJoin(Resource join) throws DatabaseException {
49 if(connectionJoins.add(join)) {
50 for(Resource flag : g.getObjects(join, dr.JoinsFlag))
51 for(Resource connector : g.getObjects(flag, sr.IsConnectedTo))
52 for(Resource connection : g.getObjects(connector, dr.IsConnectorOf))
53 addConnection(connection);
57 public static Collection<Resource> getRelatedDiagramConnections(ReadGraph g, Resource connection) throws DatabaseException {
58 RelatedDiagramConnections p = new RelatedDiagramConnections(g);
59 p.addConnection(connection);
63 public Set<Resource> getDiagramConnections() {
67 public Set<Resource> getOutputConnectors() {
68 return outputConnectors;