]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/internal/queries/ConnectedTo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / internal / queries / ConnectedTo.java
1 package org.simantics.structural2.internal.queries;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
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.common.request.ResourceRead2;
12 import org.simantics.db.common.utils.NameUtils;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.structural.stubs.StructuralResource2;
15 import org.simantics.structural2.queries.Terminal;
16 import org.simantics.structural2.utils.StructuralUtils;
17
18 /**
19  * @author Tuukka Lehtonen
20  */
21 public class ConnectedTo extends ResourceRead2<Set<Terminal>> {
22
23     private static final boolean DEBUG = false;
24
25     public ConnectedTo(Resource component, Resource connectionPoint) {
26         super(component, connectionPoint);
27     }
28
29     @Override
30     public Set<Terminal> perform(ReadGraph graph) throws DatabaseException {
31         if (DEBUG)
32             System.out.println("connectedTo(" + NameUtils.getSafeName(graph, resource) + ", " + NameUtils.getSafeName(graph, resource2) + ")");
33
34         Collection<Resource> connections = graph.getObjects(resource, resource2);
35         if (connections.isEmpty())
36             return Collections.emptySet();
37
38         Set<Resource> reachableConnections = new HashSet<Resource>();
39         for (Resource connection : connections) {
40             reachableConnections.addAll(StructuralUtils.getRelatedConnections(graph, connection));
41         }
42
43         StructuralResource2 sr = StructuralResource2.getInstance(graph);
44
45         Set<Terminal> connectedTo = new HashSet<Terminal>();
46
47         for (Resource connection : reachableConnections) {
48             if (DEBUG)
49                 System.out.println("  checking connection: " + NameUtils.getSafeName(graph, connection));
50
51             for (Statement stm : graph.getStatements(connection, sr.Connects)) {
52                 Resource connectionPoint2 = graph.getInverse(stm.getPredicate());
53                 if (resource2.equals(connectionPoint2) && resource.equals(stm.getObject()))
54                     continue;
55
56                 if (DEBUG)
57                     System.out.println("    connected to (" + NameUtils.getSafeName(graph, stm.getObject()) + ", " + NameUtils.getSafeName(graph, connectionPoint2) + ")");
58
59                 connectedTo.add(new Terminal(stm.getObject(), connectionPoint2));
60             }
61         }
62
63         return connectedTo;
64     }
65
66 }