1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.layer0.utils.binaryPredicates;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\r
16 import java.util.Set;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.utils.datastructures.Pair;
\r
24 public class CompositePredicate extends BinaryPredicate {
\r
25 IBinaryPredicate first;
\r
26 IBinaryPredicate second;
\r
28 int hasStatementDirection;
\r
29 int getStatementsDirection;
\r
31 public CompositePredicate(IBinaryPredicate first, IBinaryPredicate second) {
\r
33 this.second = second;
\r
34 if(first.supportsGetStatements() && second.supportsGetObjects())
\r
35 getStatementsDirection = 1;
\r
36 else if(second.supportsGetStatements() && first.supportsGetSubjects())
\r
37 getStatementsDirection = 2;
\r
39 this.getStatementsDirection = 0;
\r
40 if(first.supportsGetObjects())
\r
41 hasStatementDirection = 1;
\r
42 else if(second.supportsGetSubjects())
\r
43 hasStatementDirection = 2;
\r
45 throw new IllegalArgumentException("Cannot compose binary predicates such that the first one does not support getObjects and the second one does not support getSubjects.");
\r
49 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
\r
50 Set<Resource> result = new HashSet<Resource>();
\r
51 for(Resource r : first.getObjects(g, subject))
\r
52 result.addAll(second.getObjects(g, r));
\r
57 public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) throws DatabaseException {
\r
58 Set<Pair<Resource, Resource>> result =
\r
59 new HashSet<Pair<Resource, Resource>>();
\r
60 if(getStatementsDirection == 1)
\r
61 for(Pair<Resource, Resource> p : first.getStatements(g))
\r
62 for(Resource r : second.getObjects(g, p.second))
\r
63 result.add(new Pair<Resource, Resource>(p.first, r));
\r
64 else if(getStatementsDirection == 2)
\r
65 for(Pair<Resource, Resource> p : second.getStatements(g))
\r
66 for(Resource r : second.getSubjects(g, p.first))
\r
67 result.add(new Pair<Resource, Resource>(r, p.second));
\r
69 throw new UnsupportedOperationException();
\r
74 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
\r
75 Set<Resource> result = new HashSet<Resource>();
\r
76 for(Resource r : second.getSubjects(g, object))
\r
77 result.addAll(first.getSubjects(g, r));
\r
82 public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
\r
83 if(hasStatementDirection == 1) {
\r
84 for(Resource r : first.getObjects(g, subject))
\r
85 if(second.has(g, r, object))
\r
89 for(Resource r : second.getSubjects(g, object))
\r
90 if(second.has(g, subject, r))
\r
97 public void add(WriteGraph g, Resource subject, Resource object) {
\r
98 throw new UnsupportedOperationException();
\r
102 public void remove(WriteGraph g, Resource subject, Resource object) {
\r
103 throw new UnsupportedOperationException();
\r
107 public boolean supportsAdditions() {
\r
112 public boolean supportsGetObjects() {
\r
113 return first.supportsGetObjects() && second.supportsGetObjects();
\r
117 public boolean supportsGetStatements() {
\r
118 return getStatementsDirection != 0;
\r
122 public boolean supportsGetSubjects() {
\r
123 return first.supportsGetSubjects() && second.supportsGetSubjects();
\r
127 public boolean supportsRemovals() {
\r
132 public int hashCode() {
\r
133 final int prime = 31;
\r
135 result = prime * result + ((first == null) ? 0 : first.hashCode());
\r
136 result = prime * result + ((second == null) ? 0 : second.hashCode());
\r
141 public boolean equals(Object obj) {
\r
146 if (getClass() != obj.getClass())
\r
148 CompositePredicate other = (CompositePredicate) obj;
\r
149 if (first == null) {
\r
150 if (other.first != null)
\r
152 } else if (!first.equals(other.first))
\r
154 if (second == null) {
\r
155 if (other.second != null)
\r
157 } else if (!second.equals(other.second))
\r