]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/CompositePredicate.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / binaryPredicates / CompositePredicate.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.layer0.utils.binaryPredicates;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17
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;
23
24 public class CompositePredicate extends BinaryPredicate {
25         IBinaryPredicate first;
26         IBinaryPredicate second;
27         
28         int hasStatementDirection;
29         int getStatementsDirection;
30         
31         public CompositePredicate(IBinaryPredicate first, IBinaryPredicate second) {
32                 this.first = first;
33                 this.second = second;           
34                 if(first.supportsGetStatements() && second.supportsGetObjects())
35                         getStatementsDirection = 1;
36                 else if(second.supportsGetStatements() && first.supportsGetSubjects())
37                         getStatementsDirection = 2;
38                 else
39                         this.getStatementsDirection = 0;
40                 if(first.supportsGetObjects())
41                         hasStatementDirection = 1;
42                 else if(second.supportsGetSubjects())
43                         hasStatementDirection = 2;
44                 else
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.");
46         }
47
48         @Override
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));
53                 return result;
54         }
55
56         @Override
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));
68                 else
69                         throw new UnsupportedOperationException();
70                 return result;
71         }
72
73         @Override
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));
78                 return result;
79         }
80
81         @Override
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))
86                                         return true;
87                 }
88                 else {
89                         for(Resource r : second.getSubjects(g, object))
90                                 if(second.has(g, subject, r))
91                                         return true;
92                 }
93                 return false;
94         }
95
96         @Override
97         public void add(WriteGraph g, Resource subject, Resource object) {
98                 throw new UnsupportedOperationException();
99         }
100
101         @Override
102         public void remove(WriteGraph g, Resource subject, Resource object) {
103                 throw new UnsupportedOperationException();
104         }
105
106         @Override
107         public boolean supportsAdditions() {
108                 return false;
109         }
110
111         @Override
112         public boolean supportsGetObjects() {
113                 return first.supportsGetObjects() && second.supportsGetObjects();
114         }
115
116         @Override
117         public boolean supportsGetStatements() {
118                 return getStatementsDirection != 0;
119         }
120
121         @Override
122         public boolean supportsGetSubjects() {
123                 return first.supportsGetSubjects() && second.supportsGetSubjects();
124         }
125
126         @Override
127         public boolean supportsRemovals() {             
128                 return false;
129         }
130
131         @Override
132         public int hashCode() {
133                 final int prime = 31;
134                 int result = 1;
135                 result = prime * result + ((first == null) ? 0 : first.hashCode());
136                 result = prime * result + ((second == null) ? 0 : second.hashCode());
137                 return result;
138         }
139
140         @Override
141         public boolean equals(Object obj) {
142                 if (this == obj)
143                         return true;
144                 if (obj == null)
145                         return false;
146                 if (getClass() != obj.getClass())
147                         return false;
148                 CompositePredicate other = (CompositePredicate) obj;
149                 if (first == null) {
150                         if (other.first != null)
151                                 return false;
152                 } else if (!first.equals(other.first))
153                         return false;
154                 if (second == null) {
155                         if (other.second != null)
156                                 return false;
157                 } else if (!second.equals(other.second))
158                         return false;
159                 return true;
160         }       
161         
162 }