]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/rules/RelatedDiagramConnections.java
Find SCL references in SCLModuleEditor with Ctrl+Shift+G
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / rules / RelatedDiagramConnections.java
1 package org.simantics.modeling.rules;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.util.Collection;
6 import java.util.Set;
7
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;
14
15 public class RelatedDiagramConnections {
16
17         ReadGraph g;
18         StructuralResource2 sr;
19         DiagramResource dr;
20         
21         THashSet<Resource> connections = new THashSet<Resource>();
22         THashSet<Resource> connectionJoins = new THashSet<Resource>();
23         THashSet<Resource> outputConnectors = new THashSet<Resource>();
24                 
25         public RelatedDiagramConnections(ReadGraph g) {
26                 this.g = g;
27                 this.sr = StructuralResource2.getInstance(g);
28                 this.dr = DiagramResource.getInstance(g);
29         }
30
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);
39                                         }
40                                 }
41                                 if (g.isSubrelationOf(hasConnector.getPredicate(), dr.HasTailConnector)) {
42                                         outputConnectors.add(connector);
43                                 }
44                         }
45                 }
46         }
47         
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);
54                 }
55         }
56
57         public static Collection<Resource> getRelatedDiagramConnections(ReadGraph g, Resource connection) throws DatabaseException {
58                 RelatedDiagramConnections p = new RelatedDiagramConnections(g);
59                 p.addConnection(connection);
60                 return p.connections;
61         }
62
63         public Set<Resource> getDiagramConnections() {
64                 return connections;
65         }
66
67         public Set<Resource> getOutputConnectors() {
68                 return outputConnectors;
69         }
70
71 }