1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.structural2.queries;
14 import gnu.trove.set.hash.THashSet;
16 import java.util.Collection;
17 import java.util.Collections;
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;
26 public class ConnectionSet {
28 final Set<Resource> connections;
29 THashSet<Resource> joins;
30 THashSet<Resource> excluded;
31 THashSet<Resource> excludedJoins;
33 StructuralResource2 sr;
35 // public ConnectionSet() {
39 public ConnectionSet(ReadGraph graph) {
40 sr = StructuralResource2.getInstance(graph);
41 connections = graph.getService(CollectionSupport.class).createSet();
44 public boolean excludeConnection(Resource connection) {
46 excluded = new THashSet<Resource>();
47 return excluded.add(connection);
50 public boolean excludeJoin(Resource join) {
51 if (excludedJoins == null)
52 excludedJoins = new THashSet<Resource>();
53 return excludedJoins.add(join);
56 public boolean excludeConnections(Collection<Resource> connections) {
57 if (connections.isEmpty())
60 excluded = new THashSet<Resource>();
61 return excluded.addAll(connections);
64 public boolean isExcluded(Resource connection) {
65 return excluded != null && excluded.contains(connection);
68 public boolean isJoinExcluded(Resource join) {
69 return excludedJoins != null && excludedJoins.contains(join);
72 public void addConnection(ReadGraph g, Resource connection) throws DatabaseException {
73 if(isExcluded(connection))
75 if(connections.add(connection)) {
76 if(sr == null) sr = StructuralResource2.getInstance(g);
77 for(Resource join : g.getObjects(connection, sr.IsJoinedBy))
82 public void addJoin(ReadGraph g, Resource join) throws DatabaseException {
83 if(isJoinExcluded(join))
86 joins = new THashSet<Resource>();
88 if(sr == null) sr = StructuralResource2.getInstance(g);
89 for(Resource connection : g.getObjects(join, sr.Joins))
90 addConnection(g, connection);
94 public Set<Resource> getConnections() {
98 public Set<Resource> getJoins() {
99 if(joins == null) return Collections.emptySet();