X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.layer0.utils%2Fsrc%2Forg%2Fsimantics%2Flayer0%2Futils%2FbinaryPredicates%2FCompositePredicate.java;fp=bundles%2Forg.simantics.layer0.utils%2Fsrc%2Forg%2Fsimantics%2Flayer0%2Futils%2FbinaryPredicates%2FCompositePredicate.java;h=88000f680cb630e581d2ca9aaad3a8d5f3b044a9;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/CompositePredicate.java b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/CompositePredicate.java new file mode 100644 index 000000000..88000f680 --- /dev/null +++ b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/CompositePredicate.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * 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.layer0.utils.binaryPredicates; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.utils.datastructures.Pair; + +public class CompositePredicate extends BinaryPredicate { + IBinaryPredicate first; + IBinaryPredicate second; + + int hasStatementDirection; + int getStatementsDirection; + + public CompositePredicate(IBinaryPredicate first, IBinaryPredicate second) { + this.first = first; + this.second = second; + if(first.supportsGetStatements() && second.supportsGetObjects()) + getStatementsDirection = 1; + else if(second.supportsGetStatements() && first.supportsGetSubjects()) + getStatementsDirection = 2; + else + this.getStatementsDirection = 0; + if(first.supportsGetObjects()) + hasStatementDirection = 1; + else if(second.supportsGetSubjects()) + hasStatementDirection = 2; + else + throw new IllegalArgumentException("Cannot compose binary predicates such that the first one does not support getObjects and the second one does not support getSubjects."); + } + + @Override + public Collection getObjects(ReadGraph g, Resource subject) throws DatabaseException { + Set result = new HashSet(); + for(Resource r : first.getObjects(g, subject)) + result.addAll(second.getObjects(g, r)); + return result; + } + + @Override + public Collection> getStatements(ReadGraph g) throws DatabaseException { + Set> result = + new HashSet>(); + if(getStatementsDirection == 1) + for(Pair p : first.getStatements(g)) + for(Resource r : second.getObjects(g, p.second)) + result.add(new Pair(p.first, r)); + else if(getStatementsDirection == 2) + for(Pair p : second.getStatements(g)) + for(Resource r : second.getSubjects(g, p.first)) + result.add(new Pair(r, p.second)); + else + throw new UnsupportedOperationException(); + return result; + } + + @Override + public Collection getSubjects(ReadGraph g, Resource object) throws DatabaseException { + Set result = new HashSet(); + for(Resource r : second.getSubjects(g, object)) + result.addAll(first.getSubjects(g, r)); + return result; + } + + @Override + public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException { + if(hasStatementDirection == 1) { + for(Resource r : first.getObjects(g, subject)) + if(second.has(g, r, object)) + return true; + } + else { + for(Resource r : second.getSubjects(g, object)) + if(second.has(g, subject, r)) + return true; + } + return false; + } + + @Override + public void add(WriteGraph g, Resource subject, Resource object) { + throw new UnsupportedOperationException(); + } + + @Override + public void remove(WriteGraph g, Resource subject, Resource object) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean supportsAdditions() { + return false; + } + + @Override + public boolean supportsGetObjects() { + return first.supportsGetObjects() && second.supportsGetObjects(); + } + + @Override + public boolean supportsGetStatements() { + return getStatementsDirection != 0; + } + + @Override + public boolean supportsGetSubjects() { + return first.supportsGetSubjects() && second.supportsGetSubjects(); + } + + @Override + public boolean supportsRemovals() { + return false; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((first == null) ? 0 : first.hashCode()); + result = prime * result + ((second == null) ? 0 : second.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CompositePredicate other = (CompositePredicate) obj; + if (first == null) { + if (other.first != null) + return false; + } else if (!first.equals(other.first)) + return false; + if (second == null) { + if (other.second != null) + return false; + } else if (!second.equals(other.second)) + return false; + return true; + } + +}