X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2Fqueries%2FConnectionSet.java;fp=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2Fqueries%2FConnectionSet.java;h=9591d8d30dfd4ed283941a9b0244222d1596ab23;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/queries/ConnectionSet.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/queries/ConnectionSet.java new file mode 100644 index 000000000..9591d8d30 --- /dev/null +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/queries/ConnectionSet.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.structural2.queries; + +import gnu.trove.set.hash.THashSet; + +import java.util.Collection; +import java.util.Collections; +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.CollectionSupport; +import org.simantics.structural.stubs.StructuralResource2; + +public class ConnectionSet { + + final Set connections; + THashSet joins; + THashSet excluded; + THashSet excludedJoins; + + StructuralResource2 sr; + +// public ConnectionSet() { +// +// } + + public ConnectionSet(ReadGraph graph) { + sr = StructuralResource2.getInstance(graph); + connections = graph.getService(CollectionSupport.class).createSet(); + } + + public boolean excludeConnection(Resource connection) { + if (excluded == null) + excluded = new THashSet(); + return excluded.add(connection); + } + + public boolean excludeJoin(Resource join) { + if (excludedJoins == null) + excludedJoins = new THashSet(); + return excludedJoins.add(join); + } + + public boolean excludeConnections(Collection connections) { + if (connections.isEmpty()) + return false; + if (excluded == null) + excluded = new THashSet(); + return excluded.addAll(connections); + } + + public boolean isExcluded(Resource connection) { + return excluded != null && excluded.contains(connection); + } + + public boolean isJoinExcluded(Resource join) { + return excludedJoins != null && excludedJoins.contains(join); + } + + public void addConnection(ReadGraph g, Resource connection) throws DatabaseException { + if(isExcluded(connection)) + return; + if(connections.add(connection)) { + if(sr == null) sr = StructuralResource2.getInstance(g); + for(Resource join : g.getObjects(connection, sr.IsJoinedBy)) + addJoin(g, join); + } + } + + public void addJoin(ReadGraph g, Resource join) throws DatabaseException { + if(isJoinExcluded(join)) + return; + if (joins == null) + joins = new THashSet(); + if(joins.add(join)) { + if(sr == null) sr = StructuralResource2.getInstance(g); + for(Resource connection : g.getObjects(join, sr.Joins)) + addConnection(g, connection); + } + } + + public Set getConnections() { + return connections; + } + + public Set getJoins() { + if(joins == null) return Collections.emptySet(); + return joins; + } +}