]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/queries/ConnectionSet.java
Paster.getGraph() method for overriding classes.
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / queries / ConnectionSet.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.structural2.queries;
13
14 import gnu.trove.set.hash.THashSet;
15
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Set;
19
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.service.CollectionSupport;
24 import org.simantics.structural.stubs.StructuralResource2;
25
26 public class ConnectionSet {
27         
28     final Set<Resource> connections;
29     THashSet<Resource> joins;
30     THashSet<Resource> excluded;
31     THashSet<Resource> excludedJoins;
32     
33     StructuralResource2 sr;
34     
35 //    public ConnectionSet() {
36 //      
37 //    }
38     
39     public ConnectionSet(ReadGraph graph) {
40         sr = StructuralResource2.getInstance(graph);
41         connections = graph.getService(CollectionSupport.class).createSet();
42     }
43
44     public boolean excludeConnection(Resource connection) {
45         if (excluded == null)
46             excluded = new THashSet<Resource>();
47         return excluded.add(connection);
48     }
49
50     public boolean excludeJoin(Resource join) {
51         if (excludedJoins == null)
52                 excludedJoins = new THashSet<Resource>();
53         return excludedJoins.add(join);
54     }
55     
56     public boolean excludeConnections(Collection<Resource> connections) {
57         if (connections.isEmpty())
58             return false;
59         if (excluded == null)
60             excluded = new THashSet<Resource>();
61         return excluded.addAll(connections);
62     }
63
64     public boolean isExcluded(Resource connection) {
65         return excluded != null && excluded.contains(connection);
66     }
67
68     public boolean isJoinExcluded(Resource join) {
69         return excludedJoins != null && excludedJoins.contains(join);
70     }
71
72     public void addConnection(ReadGraph g, Resource connection) throws DatabaseException {
73         if(isExcluded(connection))
74             return;
75         if(connections.add(connection)) {
76                 if(sr == null) sr = StructuralResource2.getInstance(g);
77             for(Resource join : g.getObjects(connection, sr.IsJoinedBy))
78                 addJoin(g, join);
79         }
80     }
81
82     public void addJoin(ReadGraph g, Resource join) throws DatabaseException {
83         if(isJoinExcluded(join))
84             return;
85         if (joins == null)
86                 joins = new THashSet<Resource>();
87         if(joins.add(join)) {
88                 if(sr == null) sr = StructuralResource2.getInstance(g);
89             for(Resource connection : g.getObjects(join, sr.Joins))
90                 addConnection(g, connection);
91         }
92     }
93
94     public Set<Resource> getConnections() {
95         return connections;
96     }
97
98     public Set<Resource> getJoins() {
99         if(joins == null) return Collections.emptySet();
100         return joins;
101     }
102 }