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