diff options
author | 2021-10-24 23:07:20 +0200 | |
---|---|---|
committer | 2021-10-24 23:07:20 +0200 | |
commit | ca851685b7a0f0e1c9cac7329866123a2d066000 (patch) | |
tree | 60dc9e347bee0ed9dd00792541dd3ef6e17881a5 /store/src | |
parent | QueryTest update to binery relations (diff) | |
download | refinery-ca851685b7a0f0e1c9cac7329866123a2d066000.tar.gz refinery-ca851685b7a0f0e1c9cac7329866123a2d066000.tar.zst refinery-ca851685b7a0f0e1c9cac7329866123a2d066000.zip |
Fixed query parameter list undeterministic order
Diffstat (limited to 'store/src')
-rw-r--r-- | store/src/main/java/tools/refinery/store/query/internal/DNF2PQuery.java | 6 | ||||
-rw-r--r-- | store/src/test/java/tools/refinery/store/query/test/QueryTest.java | 160 |
2 files changed, 117 insertions, 49 deletions
diff --git a/store/src/main/java/tools/refinery/store/query/internal/DNF2PQuery.java b/store/src/main/java/tools/refinery/store/query/internal/DNF2PQuery.java index 7558424b..aa797cde 100644 --- a/store/src/main/java/tools/refinery/store/query/internal/DNF2PQuery.java +++ b/store/src/main/java/tools/refinery/store/query/internal/DNF2PQuery.java | |||
@@ -46,7 +46,11 @@ public class DNF2PQuery { | |||
46 | Map<Variable, PParameter> parameters = new HashMap<>(); | 46 | Map<Variable, PParameter> parameters = new HashMap<>(); |
47 | 47 | ||
48 | predicate.getVariables().forEach(variable -> parameters.put(variable, new PParameter(variable.getName()))); | 48 | predicate.getVariables().forEach(variable -> parameters.put(variable, new PParameter(variable.getName()))); |
49 | query.setParameter(new ArrayList<>(parameters.values())); | 49 | List<PParameter> parameterList = new ArrayList<>(); |
50 | for(var param : predicate.getVariables()) { | ||
51 | parameterList.add(parameters.get(param)); | ||
52 | } | ||
53 | query.setParameter(parameterList); | ||
50 | for (DNFAnd clause : predicate.getClauses()) { | 54 | for (DNFAnd clause : predicate.getClauses()) { |
51 | PBody body = new PBody(query); | 55 | PBody body = new PBody(query); |
52 | List<ExportedParameter> symbolicParameters = new ArrayList<>(); | 56 | List<ExportedParameter> symbolicParameters = new ArrayList<>(); |
diff --git a/store/src/test/java/tools/refinery/store/query/test/QueryTest.java b/store/src/test/java/tools/refinery/store/query/test/QueryTest.java index 3649ed0c..629ecc14 100644 --- a/store/src/test/java/tools/refinery/store/query/test/QueryTest.java +++ b/store/src/test/java/tools/refinery/store/query/test/QueryTest.java | |||
@@ -110,7 +110,8 @@ class QueryTest { | |||
110 | } | 110 | } |
111 | translatedMatchSet.add(elementToTranslatedMatchSet); | 111 | translatedMatchSet.add(elementToTranslatedMatchSet); |
112 | } | 112 | } |
113 | assertEquals(translatedMatchSet, expected); | 113 | |
114 | assertEquals(expected, translatedMatchSet); | ||
114 | } | 115 | } |
115 | 116 | ||
116 | @Test | 117 | @Test |
@@ -177,6 +178,116 @@ class QueryTest { | |||
177 | 178 | ||
178 | model.flushChanges(); | 179 | model.flushChanges(); |
179 | assertEquals(3, model.countResults(predicate)); | 180 | assertEquals(3, model.countResults(predicate)); |
181 | compareMatchSets(model.allResults(predicate), Set.of( | ||
182 | List.of(Tuple.of(0), Tuple.of(1)), | ||
183 | List.of(Tuple.of(1), Tuple.of(0)), | ||
184 | List.of(Tuple.of(1), Tuple.of(2)) | ||
185 | )); | ||
186 | } | ||
187 | |||
188 | @Test | ||
189 | void andTest() { | ||
190 | Relation<Boolean> person = new Relation<Boolean>("Person", 1, false); | ||
191 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
192 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
193 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
194 | |||
195 | Variable p1 = new Variable("p1"); | ||
196 | Variable p2 = new Variable("p2"); | ||
197 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
198 | |||
199 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
200 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
201 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
202 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p2, p1)); | ||
203 | DNFAnd clause = new DNFAnd(new HashSet<>(parameters), | ||
204 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom1, friendRelationAtom2)); | ||
205 | DNFPredicate predicate = new DNFPredicate("RelationConstraint", parameters, Arrays.asList(clause)); | ||
206 | |||
207 | QueriableModelStore store = new QueriableModelStoreImpl(Set.of(person, friend), Set.of(persionView,friendMustView), Set.of(predicate)); | ||
208 | QueriableModel model = store.createModel(); | ||
209 | |||
210 | assertEquals(0, model.countResults(predicate)); | ||
211 | |||
212 | model.put(person, Tuple.of(0), true); | ||
213 | model.put(person, Tuple.of(1), true); | ||
214 | model.put(person, Tuple.of(2), true); | ||
215 | |||
216 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
217 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
218 | |||
219 | model.flushChanges(); | ||
220 | assertEquals(0, model.countResults(predicate)); | ||
221 | |||
222 | model.put(friend, Tuple.of(1, 0), TruthValue.TRUE); | ||
223 | model.flushChanges(); | ||
224 | assertEquals(2, model.countResults(predicate)); | ||
225 | compareMatchSets(model.allResults(predicate), Set.of( | ||
226 | List.of(Tuple.of(0), Tuple.of(1)), | ||
227 | List.of(Tuple.of(1), Tuple.of(0)) | ||
228 | )); | ||
229 | |||
230 | model.put(friend, Tuple.of(2, 0), TruthValue.TRUE); | ||
231 | model.flushChanges(); | ||
232 | assertEquals(4, model.countResults(predicate)); | ||
233 | compareMatchSets(model.allResults(predicate), Set.of( | ||
234 | List.of(Tuple.of(0), Tuple.of(1)), | ||
235 | List.of(Tuple.of(1), Tuple.of(0)), | ||
236 | List.of(Tuple.of(0), Tuple.of(2)), | ||
237 | List.of(Tuple.of(2), Tuple.of(0)) | ||
238 | )); | ||
239 | |||
240 | } | ||
241 | |||
242 | // existTest | ||
243 | |||
244 | @Test | ||
245 | @Disabled | ||
246 | void orTest() { | ||
247 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
248 | Relation<Boolean> animal = new Relation<>("Animal", 1, false); | ||
249 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
250 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
251 | RelationView<Boolean> animalView = new KeyOnlyRelationView(animal); | ||
252 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
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 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
261 | DNFAnd clause1 = new DNFAnd(new HashSet<>(parameters), | ||
262 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom1)); | ||
263 | |||
264 | RelationAtom animalRelationAtom1 = new RelationAtom(animalView, Arrays.asList(p1)); | ||
265 | RelationAtom animalRelationAtom2 = new RelationAtom(animalView, Arrays.asList(p2)); | ||
266 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
267 | DNFAnd clause2 = new DNFAnd(new HashSet<>(parameters), | ||
268 | Arrays.asList(animalRelationAtom1, animalRelationAtom2, friendRelationAtom2)); | ||
269 | |||
270 | DNFPredicate predicate = new DNFPredicate("Or", parameters, Arrays.asList(clause1, clause2)); | ||
271 | GenericQuerySpecification<RawPatternMatcher> query = DNF2PQuery.translate(predicate).build(); | ||
272 | |||
273 | ModelStore store = new ModelStoreImpl(Set.of(person, animal, friend)); | ||
274 | Model model = store.createModel(); | ||
275 | |||
276 | model.put(person, Tuple.of(0), true); | ||
277 | model.put(person, Tuple.of(1), true); | ||
278 | model.put(animal, Tuple.of(2), true); | ||
279 | model.put(animal, Tuple.of(3), true); | ||
280 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
281 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
282 | model.put(friend, Tuple.of(2, 3), TruthValue.TRUE); | ||
283 | model.put(friend, Tuple.of(3, 0), TruthValue.TRUE); | ||
284 | |||
285 | RelationalScope scope = new RelationalScope(model, Set.of(persionView, animalView, friendMustView)); | ||
286 | |||
287 | ViatraQueryEngine engine = AdvancedViatraQueryEngine.on(scope); | ||
288 | GenericPatternMatcher matcher = engine.getMatcher(query); | ||
289 | |||
290 | assertEquals(2, matcher.countMatches()); | ||
180 | } | 291 | } |
181 | 292 | ||
182 | @Test | 293 | @Test |
@@ -402,52 +513,5 @@ class QueryTest { | |||
402 | assertEquals(3, matcher.countMatches()); | 513 | assertEquals(3, matcher.countMatches()); |
403 | } | 514 | } |
404 | 515 | ||
405 | @Test | ||
406 | @Disabled | ||
407 | void orTest() { | ||
408 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
409 | Relation<Boolean> animal = new Relation<>("Animal", 1, false); | ||
410 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.FALSE); | ||
411 | RelationView<Boolean> persionView = new KeyOnlyRelationView(person); | ||
412 | RelationView<Boolean> animalView = new KeyOnlyRelationView(animal); | ||
413 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(friend, (k, v) -> v.must()); | ||
414 | |||
415 | Variable p1 = new Variable("p1"); | ||
416 | Variable p2 = new Variable("p2"); | ||
417 | List<Variable> parameters = Arrays.asList(p1, p2); | ||
418 | |||
419 | RelationAtom personRelationAtom1 = new RelationAtom(persionView, Arrays.asList(p1)); | ||
420 | RelationAtom personRelationAtom2 = new RelationAtom(persionView, Arrays.asList(p2)); | ||
421 | RelationAtom friendRelationAtom1 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
422 | DNFAnd clause1 = new DNFAnd(new HashSet<>(parameters), | ||
423 | Arrays.asList(personRelationAtom1, personRelationAtom2, friendRelationAtom1)); | ||
424 | |||
425 | RelationAtom animalRelationAtom1 = new RelationAtom(animalView, Arrays.asList(p1)); | ||
426 | RelationAtom animalRelationAtom2 = new RelationAtom(animalView, Arrays.asList(p2)); | ||
427 | RelationAtom friendRelationAtom2 = new RelationAtom(friendMustView, Arrays.asList(p1, p2)); | ||
428 | DNFAnd clause2 = new DNFAnd(new HashSet<>(parameters), | ||
429 | Arrays.asList(animalRelationAtom1, animalRelationAtom2, friendRelationAtom2)); | ||
430 | |||
431 | DNFPredicate predicate = new DNFPredicate("Or", parameters, Arrays.asList(clause1, clause2)); | ||
432 | GenericQuerySpecification<RawPatternMatcher> query = DNF2PQuery.translate(predicate).build(); | ||
433 | 516 | ||
434 | ModelStore store = new ModelStoreImpl(Set.of(person, animal, friend)); | ||
435 | Model model = store.createModel(); | ||
436 | |||
437 | model.put(person, Tuple.of(0), true); | ||
438 | model.put(person, Tuple.of(1), true); | ||
439 | model.put(animal, Tuple.of(2), true); | ||
440 | model.put(animal, Tuple.of(3), true); | ||
441 | model.put(friend, Tuple.of(0, 1), TruthValue.TRUE); | ||
442 | model.put(friend, Tuple.of(0, 2), TruthValue.TRUE); | ||
443 | model.put(friend, Tuple.of(2, 3), TruthValue.TRUE); | ||
444 | model.put(friend, Tuple.of(3, 0), TruthValue.TRUE); | ||
445 | |||
446 | RelationalScope scope = new RelationalScope(model, Set.of(persionView, animalView, friendMustView)); | ||
447 | |||
448 | ViatraQueryEngine engine = AdvancedViatraQueryEngine.on(scope); | ||
449 | GenericPatternMatcher matcher = engine.getMatcher(query); | ||
450 | |||
451 | assertEquals(2, matcher.countMatches()); | ||
452 | } | ||
453 | } \ No newline at end of file | 517 | } \ No newline at end of file |