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