diff options
author | 2022-09-26 00:39:23 +0200 | |
---|---|---|
committer | 2022-10-03 20:06:52 +0200 | |
commit | aac386f0d8c4e4585026b11bfeca20f378f7f261 (patch) | |
tree | cad56fc2c88a1abd56258d64b5ce3a16baaff011 /subprojects/store/src/test/java | |
parent | chore: fix some warnings (diff) | |
download | refinery-aac386f0d8c4e4585026b11bfeca20f378f7f261.tar.gz refinery-aac386f0d8c4e4585026b11bfeca20f378f7f261.tar.zst refinery-aac386f0d8c4e4585026b11bfeca20f378f7f261.zip |
refactor: move viatra into a separate subproject
Diffstat (limited to 'subprojects/store/src/test/java')
-rw-r--r-- | subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTest.java | 445 | ||||
-rw-r--r-- | subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTransactionTest.java | 58 |
2 files changed, 0 insertions, 503 deletions
diff --git a/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTest.java b/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTest.java deleted file mode 100644 index 02381bcd..00000000 --- a/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTest.java +++ /dev/null | |||
@@ -1,445 +0,0 @@ | |||
1 | package tools.refinery.store.query.test; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | |||
5 | import java.util.ArrayList; | ||
6 | import java.util.Arrays; | ||
7 | import java.util.Collections; | ||
8 | import java.util.HashSet; | ||
9 | import java.util.List; | ||
10 | import java.util.Set; | ||
11 | import java.util.stream.Stream; | ||
12 | |||
13 | import org.junit.jupiter.api.Test; | ||
14 | |||
15 | import tools.refinery.store.model.Tuple; | ||
16 | import tools.refinery.store.model.representation.Relation; | ||
17 | import tools.refinery.store.model.representation.TruthValue; | ||
18 | import tools.refinery.store.query.QueriableModel; | ||
19 | import tools.refinery.store.query.QueriableModelStore; | ||
20 | import tools.refinery.store.query.QueriableModelStoreImpl; | ||
21 | import tools.refinery.store.query.building.DNFAnd; | ||
22 | import tools.refinery.store.query.building.DNFPredicate; | ||
23 | import tools.refinery.store.query.building.EquivalenceAtom; | ||
24 | import tools.refinery.store.query.building.PredicateAtom; | ||
25 | import tools.refinery.store.query.building.RelationAtom; | ||
26 | import tools.refinery.store.query.building.Variable; | ||
27 | import tools.refinery.store.query.view.FilteredRelationView; | ||
28 | import tools.refinery.store.query.view.KeyOnlyRelationView; | ||
29 | import tools.refinery.store.query.view.RelationView; | ||
30 | |||
31 | class QueryTest { | ||
32 | |||
33 | static void compareMatchSets(Stream<Object[]> matchSet, Set<List<Tuple>> expected) { | ||
34 | Set<List<Tuple>> translatedMatchSet = new HashSet<>(); | ||
35 | var interator = matchSet.iterator(); | ||
36 | while (interator.hasNext()) { | ||
37 | var element = interator.next(); | ||
38 | List<Tuple> elementToTranslatedMatchSet = new ArrayList<>(); | ||
39 | for (int i = 0; i < element.length; i++) { | ||
40 | elementToTranslatedMatchSet.add((Tuple) element[i]); | ||
41 | } | ||
42 | translatedMatchSet.add(elementToTranslatedMatchSet); | ||
43 | } | ||
44 | |||
45 | assertEquals(expected, translatedMatchSet); | ||
46 | } | ||
47 | |||
48 | @Test | ||
49 | void typeConstraintTest() { | ||
50 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
51 | Relation<Boolean> asset = new Relation<>("Asset", 1, false); | ||
52 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
53 | |||
54 | List<Variable> parameters = Arrays.asList(new Variable("p1")); | ||
55 | RelationAtom personRelationAtom = new RelationAtom(persionView, parameters); | ||
56 | DNFAnd clause = new DNFAnd(Collections.emptySet(), Arrays.asList(personRelationAtom)); | ||
57 | DNFPredicate predicate = new DNFPredicate("TypeConstraint", parameters, Arrays.asList(clause)); | ||
58 | |||
59 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, asset), Set.of(persionView), | ||
60 | Set.of(predicate)); | ||
61 | QueriableModel model = store.createModel(); | ||
62 | |||
63 | model.put(person, Tuple.of(0), true); | ||
64 | model.put(person, Tuple.of(1), true); | ||
65 | model.put(asset, Tuple.of(1), true); | ||
66 | model.put(asset, Tuple.of(2), true); | ||
67 | |||
68 | model.flushChanges(); | ||
69 | assertEquals(2, model.countResults(predicate)); | ||
70 | compareMatchSets(model.allResults(predicate), Set.of(List.of(Tuple.of(0)), List.of(Tuple.of(1)))); | ||
71 | } | ||
72 | |||
73 | @Test | ||
74 | void relationConstraintTest() { | ||
75 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
76 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
77 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
78 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
79 | |||
80 | Variable p1 = new Variable("p1"); | ||
81 | Variable p2 = new Variable("p2"); | ||
82 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
83 | |||
84 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
85 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
86 | RelationAtom friendRelationAtom = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
87 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
88 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom)); | ||
89 | DNFPredicate predicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
90 | |||
91 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
92 | Set.of(persionView, friendMustView), Set.of(predicate)); | ||
93 | QueriableModel model = store.createModel(); | ||
94 | |||
95 | assertEquals(0, model.countResults(predicate)); | ||
96 | |||
97 | model.put(person, Tuple.of(0), true); | ||
98 | model.put(person, Tuple.of(1), true); | ||
99 | model.put(person, Tuple.of(2), true); | ||
100 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
101 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
102 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
103 | |||
104 | assertEquals(0, model.countResults(predicate)); | ||
105 | |||
106 | model.flushChanges(); | ||
107 | assertEquals(3, model.countResults(predicate)); | ||
108 | compareMatchSets(model.allResults(predicate), Set.of(List.of(Tuple.of(0), Tuple.of(1)), | ||
109 | List.of(Tuple.of(1), Tuple.of(0)), List.of(Tuple.of(1), Tuple.of(2)))); | ||
110 | } | ||
111 | |||
112 | @Test | ||
113 | void andTest() { | ||
114 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
115 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
116 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
117 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
118 | |||
119 | Variable p1 = new Variable("p1"); | ||
120 | Variable p2 = new Variable("p2"); | ||
121 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
122 | |||
123 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
124 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
125 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
126 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p2, p1)); | ||
127 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
128 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom1, friendRelationAtom2)); | ||
129 | DNFPredicate predicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
130 | |||
131 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
132 | Set.of(persionView, friendMustView), Set.of(predicate)); | ||
133 | QueriableModel model = store.createModel(); | ||
134 | |||
135 | assertEquals(0, model.countResults(predicate)); | ||
136 | |||
137 | model.put(person, Tuple.of(0), true); | ||
138 | model.put(person, Tuple.of(1), true); | ||
139 | model.put(person, Tuple.of(2), true); | ||
140 | |||
141 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
142 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
143 | |||
144 | model.flushChanges(); | ||
145 | assertEquals(0, model.countResults(predicate)); | ||
146 | |||
147 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
148 | model.flushChanges(); | ||
149 | assertEquals(2, model.countResults(predicate)); | ||
150 | compareMatchSets(model.allResults(predicate), | ||
151 | Set.of(List.of(Tuple.of(0), Tuple.of(1)), List.of(Tuple.of(1), Tuple.of(0)))); | ||
152 | |||
153 | model.put(friend, Tuple.of(2, 0), TruthValue.TRUE); | ||
154 | model.flushChanges(); | ||
155 | assertEquals(4, model.countResults(predicate)); | ||
156 | compareMatchSets(model.allResults(predicate), | ||
157 | Set.of(List.of(Tuple.of(0), Tuple.of(1)), List.of(Tuple.of(1), Tuple.of(0)), | ||
158 | List.of(Tuple.of(0), Tuple.of(2)), List.of(Tuple.of(2), Tuple.of(0)))); | ||
159 | } | ||
160 | |||
161 | @Test | ||
162 | void existTest() { | ||
163 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
164 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
165 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
166 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
167 | |||
168 | Variable p1 = new Variable("p1"); | ||
169 | Variable p2 = new Variable("p2"); | ||
170 | List<Variable> parameters = Arrays.asList(p1); | ||
171 | |||
172 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
173 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
174 | RelationAtom friendRelationAtom = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
175 | DNFAnd clause = new DNFAnd(Set.of(p2), | ||
176 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom)); | ||
177 | DNFPredicate predicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
178 | |||
179 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
180 | Set.of(persionView, friendMustView), Set.of(predicate)); | ||
181 | QueriableModel model = store.createModel(); | ||
182 | |||
183 | assertEquals(0, model.countResults(predicate)); | ||
184 | |||
185 | model.put(person, Tuple.of(0), true); | ||
186 | model.put(person, Tuple.of(1), true); | ||
187 | model.put(person, Tuple.of(2), true); | ||
188 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
189 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
190 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
191 | |||
192 | assertEquals(0, model.countResults(predicate)); | ||
193 | |||
194 | model.flushChanges(); | ||
195 | assertEquals(2, model.countResults(predicate)); | ||
196 | compareMatchSets(model.allResults(predicate), Set.of(List.of(Tuple.of(0)), List.of(Tuple.of(1)))); | ||
197 | } | ||
198 | |||
199 | @Test | ||
200 | void orTest() { | ||
201 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
202 | Relation<Boolean> animal = new Relation<>("Animal", 1, false); | ||
203 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
204 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
205 | RelationView<Boolean> animalView = new KeyOnlyRelationView(animal); | ||
206 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
207 | |||
208 | Variable p1 = new Variable("p1"); | ||
209 | Variable p2 = new Variable("p2"); | ||
210 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
211 | |||
212 | // Person-Person friendship | ||
213 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
214 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
215 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
216 | DNFAnd clause1 = new DNFAnd(Collections.emptySet(), | ||
217 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom1)); | ||
218 | |||
219 | // Animal-Animal friendship | ||
220 | RelationAtom animalRelationAtom1 = new RelationAtom(animalView, Arrays.asList(p1)); | ||
221 | RelationAtom animalRelationAtom2 = new RelationAtom(animalView, Arrays.asList(p2)); | ||
222 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
223 | DNFAnd clause2 = new DNFAnd(Collections.emptySet(), | ||
224 | Arrays.asList(animalRelationAtom1, animalRelationAtom2, friendRelationAtom2)); | ||
225 | |||
226 | // No inter-species friendship | ||
227 | |||
228 | DNFPredicate predicate = new DNFPredicate("Or", parameters, Arrays.asList(clause1, clause2)); | ||
229 | |||
230 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, animal, friend), | ||
231 | Set.of(persionView, animalView, friendMustView), Set.of(predicate)); | ||
232 | QueriableModel model = store.createModel(); | ||
233 | |||
234 | model.put(person, Tuple.of(0), true); | ||
235 | model.put(person, Tuple.of(1), true); | ||
236 | model.put(animal, Tuple.of(2), true); | ||
237 | model.put(animal, Tuple.of(3), true); | ||
238 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
239 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
240 | model.put(friend, Tuple.of(2, 3), TruthValue.TRUE); | ||
241 | model.put(friend, Tuple.of(3, 0), TruthValue.TRUE); | ||
242 | |||
243 | model.flushChanges(); | ||
244 | assertEquals(2, model.countResults(predicate)); | ||
245 | compareMatchSets(model.allResults(predicate), | ||
246 | Set.of(List.of(Tuple.of(0), Tuple.of(1)), List.of(Tuple.of(2), Tuple.of(3)))); | ||
247 | } | ||
248 | |||
249 | @Test | ||
250 | void equalityTest() { | ||
251 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
252 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
253 | |||
254 | Variable p1 = new Variable("p1"); | ||
255 | Variable p2 = new Variable("p2"); | ||
256 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
257 | |||
258 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
259 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
260 | EquivalenceAtom equivalenceAtom = new EquivalenceAtom(true, p1, p2); | ||
261 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
262 | Arrays.asList(personRelationAtom1, personRelationAtom2, equivalenceAtom)); | ||
263 | DNFPredicate predicate = new DNFPredicate("Equality", parameters, Arrays.asList(clause)); | ||
264 | |||
265 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person), Set.of(persionView), Set.of(predicate)); | ||
266 | QueriableModel model = store.createModel(); | ||
267 | |||
268 | model.put(person, Tuple.of(0), true); | ||
269 | model.put(person, Tuple.of(1), true); | ||
270 | model.put(person, Tuple.of(2), true); | ||
271 | |||
272 | model.flushChanges(); | ||
273 | assertEquals(3, model.countResults(predicate)); | ||
274 | compareMatchSets(model.allResults(predicate), Set.of(List.of(Tuple.of(0), Tuple.of(0)), | ||
275 | List.of(Tuple.of(1), Tuple.of(1)), List.of(Tuple.of(2), Tuple.of(2)))); | ||
276 | } | ||
277 | |||
278 | @Test | ||
279 | void inequalityTest() { | ||
280 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
281 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
282 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
283 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
284 | |||
285 | Variable p1 = new Variable("p1"); | ||
286 | Variable p2 = new Variable("p2"); | ||
287 | Variable p3 = new Variable("p3"); | ||
288 | List<Variable> parameters = Arrays.asList(p1, p2, p3); | ||
289 | |||
290 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
291 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
292 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p3)); | ||
293 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p2, p3)); | ||
294 | EquivalenceAtom inequivalenceAtom = new EquivalenceAtom(false, p1, p2); | ||
295 | DNFAnd clause = new DNFAnd(Collections.emptySet(), Arrays.asList(personRelationAtom1, personRelationAtom2, | ||
296 | friendRelationAtom1, friendRelationAtom2, inequivalenceAtom)); | ||
297 | DNFPredicate predicate = new DNFPredicate("Inequality", parameters, Arrays.asList(clause)); | ||
298 | |||
299 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
300 | Set.of(persionView, friendMustView), Set.of(predicate)); | ||
301 | QueriableModel model = store.createModel(); | ||
302 | |||
303 | model.put(person, Tuple.of(0), true); | ||
304 | model.put(person, Tuple.of(1), true); | ||
305 | model.put(person, Tuple.of(2), true); | ||
306 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
307 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
308 | |||
309 | model.flushChanges(); | ||
310 | assertEquals(2, model.countResults(predicate)); | ||
311 | compareMatchSets(model.allResults(predicate), | ||
312 | Set.of(List.of(Tuple.of(0), Tuple.of(1), Tuple.of(2)), List.of(Tuple.of(1), Tuple.of(0), Tuple.of(2)))); | ||
313 | } | ||
314 | |||
315 | @Test | ||
316 | void patternCallTest() { | ||
317 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
318 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
319 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
320 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
321 | |||
322 | Variable p1 = new Variable("p1"); | ||
323 | Variable p2 = new Variable("p2"); | ||
324 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
325 | |||
326 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
327 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
328 | RelationAtom friendRelationAtom = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
329 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
330 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom)); | ||
331 | DNFPredicate friendPredicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
332 | |||
333 | Variable p3 = new Variable("p3"); | ||
334 | Variable p4 = new Variable("p4"); | ||
335 | List<Variable> substitution = Arrays.asList(p3, p4); | ||
336 | RelationAtom personRelationAtom3 = new RelationAtom(persionView, Arrays.asList(p3)); | ||
337 | RelationAtom personRelationAtom4 = new RelationAtom(persionView, Arrays.asList(p4)); | ||
338 | PredicateAtom friendPredicateAtom = new PredicateAtom(true, false, friendPredicate, substitution); | ||
339 | DNFAnd patternCallClause = new DNFAnd(Collections.emptySet(), | ||
340 | Arrays.asList(personRelationAtom3, personRelationAtom4, friendPredicateAtom)); | ||
341 | DNFPredicate predicate = new DNFPredicate("PatternCall", substitution, Arrays.asList(patternCallClause)); | ||
342 | |||
343 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
344 | Set.of(persionView, friendMustView), Set.of(friendPredicate, predicate)); | ||
345 | QueriableModel model = store.createModel(); | ||
346 | |||
347 | model.put(person, Tuple.of(0), true); | ||
348 | model.put(person, Tuple.of(1), true); | ||
349 | model.put(person, Tuple.of(2), true); | ||
350 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
351 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
352 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
353 | |||
354 | model.flushChanges(); | ||
355 | |||
356 | assertEquals(3, model.countResults(friendPredicate)); | ||
357 | } | ||
358 | |||
359 | @Test | ||
360 | void negativePatternCallTest() { | ||
361 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
362 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
363 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
364 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
365 | |||
366 | Variable p1 = new Variable("p1"); | ||
367 | Variable p2 = new Variable("p2"); | ||
368 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
369 | |||
370 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
371 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
372 | RelationAtom friendRelationAtom = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
373 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
374 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom)); | ||
375 | DNFPredicate friendPredicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
376 | |||
377 | Variable p3 = new Variable("p3"); | ||
378 | Variable p4 = new Variable("p4"); | ||
379 | List<Variable> substitution = Arrays.asList(p3, p4); | ||
380 | RelationAtom personRelationAtom3 = new RelationAtom(persionView, Arrays.asList(p3)); | ||
381 | RelationAtom personRelationAtom4 = new RelationAtom(persionView, Arrays.asList(p4)); | ||
382 | PredicateAtom friendPredicateAtom = new PredicateAtom(false, false, friendPredicate, substitution); | ||
383 | DNFAnd negativePatternCallClause = new DNFAnd(Collections.emptySet(), | ||
384 | Arrays.asList(personRelationAtom3, personRelationAtom4, friendPredicateAtom)); | ||
385 | DNFPredicate predicate = new DNFPredicate("NegativePatternCall", substitution, | ||
386 | Arrays.asList(negativePatternCallClause)); | ||
387 | |||
388 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
389 | Set.of(persionView, friendMustView), Set.of(friendPredicate, predicate)); | ||
390 | QueriableModel model = store.createModel(); | ||
391 | |||
392 | model.put(person, Tuple.of(0), true); | ||
393 | model.put(person, Tuple.of(1), true); | ||
394 | model.put(person, Tuple.of(2), true); | ||
395 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
396 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
397 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
398 | |||
399 | model.flushChanges(); | ||
400 | assertEquals(6, model.countResults(predicate)); | ||
401 | } | ||
402 | |||
403 | @Test | ||
404 | void transitivePatternCallTest() { | ||
405 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
406 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
407 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
408 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
409 | |||
410 | Variable p1 = new Variable("p1"); | ||
411 | Variable p2 = new Variable("p2"); | ||
412 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
413 | |||
414 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
415 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
416 | RelationAtom friendRelationAtom = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
417 | DNFAnd clause = new DNFAnd(Collections.emptySet(), | ||
418 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom)); | ||
419 | DNFPredicate friendPredicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
420 | |||
421 | Variable p3 = new Variable("p3"); | ||
422 | Variable p4 = new Variable("p4"); | ||
423 | List<Variable> substitution = Arrays.asList(p3, p4); | ||
424 | RelationAtom personRelationAtom3 = new RelationAtom(persionView, Arrays.asList(p3)); | ||
425 | RelationAtom personRelationAtom4 = new RelationAtom(persionView, Arrays.asList(p4)); | ||
426 | PredicateAtom friendPredicateAtom = new PredicateAtom(true, true, friendPredicate, substitution); | ||
427 | DNFAnd patternCallClause = new DNFAnd(Collections.emptySet(), | ||
428 | Arrays.asList(personRelationAtom3, personRelationAtom4, friendPredicateAtom)); | ||
429 | DNFPredicate predicate = new DNFPredicate("TransitivePatternCall", substitution, | ||
430 | Arrays.asList(patternCallClause)); | ||
431 | |||
432 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), | ||
433 | Set.of(persionView, friendMustView), Set.of(friendPredicate, predicate)); | ||
434 | QueriableModel model = store.createModel(); | ||
435 | |||
436 | model.put(person, Tuple.of(0), true); | ||
437 | model.put(person, Tuple.of(1), true); | ||
438 | model.put(person, Tuple.of(2), true); | ||
439 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
440 | model.put(friend, Tuple.of(1, 2), TruthValue.TRUE); | ||
441 | |||
442 | model.flushChanges(); | ||
443 | assertEquals(3, model.countResults(predicate)); | ||
444 | } | ||
445 | } \ No newline at end of file | ||
diff --git a/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTransactionTest.java b/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTransactionTest.java deleted file mode 100644 index e72186b9..00000000 --- a/subprojects/store/src/test/java/tools/refinery/store/query/test/QueryTransactionTest.java +++ /dev/null | |||
@@ -1,58 +0,0 @@ | |||
1 | package tools.refinery.store.query.test; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | |||
5 | import java.util.Arrays; | ||
6 | import java.util.Collections; | ||
7 | import java.util.List; | ||
8 | import java.util.Set; | ||
9 | |||
10 | import org.junit.jupiter.api.Test; | ||
11 | |||
12 | import tools.refinery.store.model.Tuple; | ||
13 | import tools.refinery.store.model.representation.Relation; | ||
14 | import tools.refinery.store.query.QueriableModel; | ||
15 | import tools.refinery.store.query.QueriableModelStore; | ||
16 | import tools.refinery.store.query.QueriableModelStoreImpl; | ||
17 | import tools.refinery.store.query.building.DNFAnd; | ||
18 | import tools.refinery.store.query.building.DNFPredicate; | ||
19 | import tools.refinery.store.query.building.RelationAtom; | ||
20 | import tools.refinery.store.query.building.Variable; | ||
21 | import tools.refinery.store.query.view.KeyOnlyRelationView; | ||
22 | import tools.refinery.store.query.view.RelationView; | ||
23 | |||
24 | class QueryTransactionTest { | ||
25 | @Test | ||
26 | void flushTest() { | ||
27 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
28 | Relation<Boolean> asset = new Relation<>("Asset", 1, false); | ||
29 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
30 | |||
31 | List<Variable> parameters = Arrays.asList(new Variable("p1")); | ||
32 | RelationAtom personRelationAtom = new RelationAtom(persionView, parameters); | ||
33 | DNFAnd clause = new DNFAnd(Collections.emptySet(), Arrays.asList(personRelationAtom)); | ||
34 | DNFPredicate predicate = new DNFPredicate("TypeConstraint", parameters, Arrays.asList(clause)); | ||
35 | |||
36 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, asset), Set.of(persionView), | ||
37 | Set.of(predicate)); | ||
38 | QueriableModel model = store.createModel(); | ||
39 | |||
40 | assertEquals(0, model.countResults(predicate)); | ||
41 | |||
42 | model.put(person, Tuple.of(0), true); | ||
43 | model.put(person, Tuple.of(1), true); | ||
44 | model.put(asset, Tuple.of(1), true); | ||
45 | model.put(asset, Tuple.of(2), true); | ||
46 | |||
47 | assertEquals(0, model.countResults(predicate)); | ||
48 | |||
49 | model.flushChanges(); | ||
50 | assertEquals(2, model.countResults(predicate)); | ||
51 | |||
52 | model.put(person, Tuple.of(4), true); | ||
53 | assertEquals(2, model.countResults(predicate)); | ||
54 | |||
55 | model.flushChanges(); | ||
56 | assertEquals(3, model.countResults(predicate)); | ||
57 | } | ||
58 | } | ||