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.layer0.utils.binaryPredicates;
14 import java.util.Collection;
15 import java.util.HashSet;
18 import org.simantics.db.Resource;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.utils.datastructures.Pair;
24 public class CompositePredicate extends BinaryPredicate {
25 IBinaryPredicate first;
26 IBinaryPredicate second;
28 int hasStatementDirection;
29 int getStatementsDirection;
31 public CompositePredicate(IBinaryPredicate first, IBinaryPredicate second) {
34 if(first.supportsGetStatements() && second.supportsGetObjects())
35 getStatementsDirection = 1;
36 else if(second.supportsGetStatements() && first.supportsGetSubjects())
37 getStatementsDirection = 2;
39 this.getStatementsDirection = 0;
40 if(first.supportsGetObjects())
41 hasStatementDirection = 1;
42 else if(second.supportsGetSubjects())
43 hasStatementDirection = 2;
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.");
49 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
50 Set<Resource> result = new HashSet<Resource>();
51 for(Resource r : first.getObjects(g, subject))
52 result.addAll(second.getObjects(g, r));
57 public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) throws DatabaseException {
58 Set<Pair<Resource, Resource>> result =
59 new HashSet<Pair<Resource, Resource>>();
60 if(getStatementsDirection == 1)
61 for(Pair<Resource, Resource> p : first.getStatements(g))
62 for(Resource r : second.getObjects(g, p.second))
63 result.add(new Pair<Resource, Resource>(p.first, r));
64 else if(getStatementsDirection == 2)
65 for(Pair<Resource, Resource> p : second.getStatements(g))
66 for(Resource r : second.getSubjects(g, p.first))
67 result.add(new Pair<Resource, Resource>(r, p.second));
69 throw new UnsupportedOperationException();
74 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
75 Set<Resource> result = new HashSet<Resource>();
76 for(Resource r : second.getSubjects(g, object))
77 result.addAll(first.getSubjects(g, r));
82 public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
83 if(hasStatementDirection == 1) {
84 for(Resource r : first.getObjects(g, subject))
85 if(second.has(g, r, object))
89 for(Resource r : second.getSubjects(g, object))
90 if(second.has(g, subject, r))
97 public void add(WriteGraph g, Resource subject, Resource object) {
98 throw new UnsupportedOperationException();
102 public void remove(WriteGraph g, Resource subject, Resource object) {
103 throw new UnsupportedOperationException();
107 public boolean supportsAdditions() {
112 public boolean supportsGetObjects() {
113 return first.supportsGetObjects() && second.supportsGetObjects();
117 public boolean supportsGetStatements() {
118 return getStatementsDirection != 0;
122 public boolean supportsGetSubjects() {
123 return first.supportsGetSubjects() && second.supportsGetSubjects();
127 public boolean supportsRemovals() {
132 public int hashCode() {
133 final int prime = 31;
135 result = prime * result + ((first == null) ? 0 : first.hashCode());
136 result = prime * result + ((second == null) ? 0 : second.hashCode());
141 public boolean equals(Object obj) {
146 if (getClass() != obj.getClass())
148 CompositePredicate other = (CompositePredicate) obj;
150 if (other.first != null)
152 } else if (!first.equals(other.first))
154 if (second == null) {
155 if (other.second != null)
157 } else if (!second.equals(other.second))