diff options
author | 2023-01-29 02:06:57 +0100 | |
---|---|---|
committer | 2023-01-29 02:06:57 +0100 | |
commit | 1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef (patch) | |
tree | ee5cfb52db47c10de7e86bb44651e815526acb0b /subprojects/store-query-viatra/src/test | |
parent | refactor: Model store and query API (diff) | |
download | refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.tar.gz refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.tar.zst refinery-1d87633ef1dcf924f27949ff4dc2fedb4a8b67ef.zip |
feat: negative and transitive RelationViewAtom
Use PVisibility.EMBEDDED helper patterns to avoid superfluous production
nodes in the Rete net.
Diffstat (limited to 'subprojects/store-query-viatra/src/test')
-rw-r--r-- | subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java index 72e8d7e5..ba0abca0 100644 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java +++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/QueryTest.java | |||
@@ -391,6 +391,48 @@ class QueryTest { | |||
391 | } | 391 | } |
392 | 392 | ||
393 | @Test | 393 | @Test |
394 | void negativeRelationViewTest() { | ||
395 | var person = new Symbol<>("Person", 1, Boolean.class, false); | ||
396 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | ||
397 | var personView = new KeyOnlyRelationView<>(person); | ||
398 | var friendMustView = new FilteredRelationView<>(friend, "must", TruthValue::must); | ||
399 | |||
400 | var p1 = new Variable("p1"); | ||
401 | var p2 = new Variable("p2"); | ||
402 | var predicate = DNF.builder("NegativePatternCall") | ||
403 | .parameters(p1, p2) | ||
404 | .clause( | ||
405 | new RelationViewAtom(personView, p1), | ||
406 | new RelationViewAtom(personView, p2), | ||
407 | new RelationViewAtom(false, friendMustView, p1, p2) | ||
408 | ) | ||
409 | .build(); | ||
410 | |||
411 | var store = ModelStore.builder() | ||
412 | .symbols(person, friend) | ||
413 | .with(ViatraModelQuery.ADAPTER) | ||
414 | .queries(predicate) | ||
415 | .build(); | ||
416 | |||
417 | var model = store.createModel(); | ||
418 | var personInterpretation = model.getInterpretation(person); | ||
419 | var friendInterpretation = model.getInterpretation(friend); | ||
420 | var queryEngine = model.getAdapter(ModelQuery.ADAPTER); | ||
421 | var predicateResultSet = queryEngine.getResultSet(predicate); | ||
422 | |||
423 | personInterpretation.put(Tuple.of(0), true); | ||
424 | personInterpretation.put(Tuple.of(1), true); | ||
425 | personInterpretation.put(Tuple.of(2), true); | ||
426 | |||
427 | friendInterpretation.put(Tuple.of(0, 1), TruthValue.TRUE); | ||
428 | friendInterpretation.put(Tuple.of(1, 0), TruthValue.TRUE); | ||
429 | friendInterpretation.put(Tuple.of(1, 2), TruthValue.TRUE); | ||
430 | |||
431 | queryEngine.flushChanges(); | ||
432 | assertEquals(6, predicateResultSet.countResults()); | ||
433 | } | ||
434 | |||
435 | @Test | ||
394 | void negativePatternCallTest() { | 436 | void negativePatternCallTest() { |
395 | var person = new Symbol<>("Person", 1, Boolean.class, false); | 437 | var person = new Symbol<>("Person", 1, Boolean.class, false); |
396 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | 438 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); |
@@ -444,6 +486,47 @@ class QueryTest { | |||
444 | } | 486 | } |
445 | 487 | ||
446 | @Test | 488 | @Test |
489 | void negativeRelationViewWithQuantificationTest() { | ||
490 | var person = new Symbol<>("Person", 1, Boolean.class, false); | ||
491 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | ||
492 | var personView = new KeyOnlyRelationView<>(person); | ||
493 | var friendMustView = new FilteredRelationView<>(friend, "must", TruthValue::must); | ||
494 | |||
495 | var p1 = new Variable("p1"); | ||
496 | var p2 = new Variable("p2"); | ||
497 | |||
498 | var predicate = DNF.builder("Count") | ||
499 | .parameters(p1) | ||
500 | .clause( | ||
501 | new RelationViewAtom(personView, p1), | ||
502 | new RelationViewAtom(false, friendMustView, p1, p2) | ||
503 | ) | ||
504 | .build(); | ||
505 | |||
506 | var store = ModelStore.builder() | ||
507 | .symbols(person, friend) | ||
508 | .with(ViatraModelQuery.ADAPTER) | ||
509 | .queries(predicate) | ||
510 | .build(); | ||
511 | |||
512 | var model = store.createModel(); | ||
513 | var personInterpretation = model.getInterpretation(person); | ||
514 | var friendInterpretation = model.getInterpretation(friend); | ||
515 | var queryEngine = model.getAdapter(ModelQuery.ADAPTER); | ||
516 | var predicateResultSet = queryEngine.getResultSet(predicate); | ||
517 | |||
518 | personInterpretation.put(Tuple.of(0), true); | ||
519 | personInterpretation.put(Tuple.of(1), true); | ||
520 | personInterpretation.put(Tuple.of(2), true); | ||
521 | |||
522 | friendInterpretation.put(Tuple.of(0, 1), TruthValue.TRUE); | ||
523 | friendInterpretation.put(Tuple.of(0, 2), TruthValue.TRUE); | ||
524 | |||
525 | queryEngine.flushChanges(); | ||
526 | assertEquals(2, predicateResultSet.countResults()); | ||
527 | } | ||
528 | |||
529 | @Test | ||
447 | void negativeWithQuantificationTest() { | 530 | void negativeWithQuantificationTest() { |
448 | var person = new Symbol<>("Person", 1, Boolean.class, false); | 531 | var person = new Symbol<>("Person", 1, Boolean.class, false); |
449 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | 532 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); |
@@ -494,6 +577,47 @@ class QueryTest { | |||
494 | } | 577 | } |
495 | 578 | ||
496 | @Test | 579 | @Test |
580 | void transitiveRelationViewTest() { | ||
581 | var person = new Symbol<>("Person", 1, Boolean.class, false); | ||
582 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | ||
583 | var personView = new KeyOnlyRelationView<>(person); | ||
584 | var friendMustView = new FilteredRelationView<>(friend, "must", TruthValue::must); | ||
585 | |||
586 | var p1 = new Variable("p1"); | ||
587 | var p2 = new Variable("p2"); | ||
588 | var predicate = DNF.builder("TransitivePatternCall") | ||
589 | .parameters(p1, p2) | ||
590 | .clause( | ||
591 | new RelationViewAtom(personView, p1), | ||
592 | new RelationViewAtom(personView, p2), | ||
593 | new RelationViewAtom(CallPolarity.TRANSITIVE, friendMustView, p1, p2) | ||
594 | ) | ||
595 | .build(); | ||
596 | |||
597 | var store = ModelStore.builder() | ||
598 | .symbols(person, friend) | ||
599 | .with(ViatraModelQuery.ADAPTER) | ||
600 | .queries(predicate) | ||
601 | .build(); | ||
602 | |||
603 | var model = store.createModel(); | ||
604 | var personInterpretation = model.getInterpretation(person); | ||
605 | var friendInterpretation = model.getInterpretation(friend); | ||
606 | var queryEngine = model.getAdapter(ModelQuery.ADAPTER); | ||
607 | var predicateResultSet = queryEngine.getResultSet(predicate); | ||
608 | |||
609 | personInterpretation.put(Tuple.of(0), true); | ||
610 | personInterpretation.put(Tuple.of(1), true); | ||
611 | personInterpretation.put(Tuple.of(2), true); | ||
612 | |||
613 | friendInterpretation.put(Tuple.of(0, 1), TruthValue.TRUE); | ||
614 | friendInterpretation.put(Tuple.of(1, 2), TruthValue.TRUE); | ||
615 | |||
616 | queryEngine.flushChanges(); | ||
617 | assertEquals(3, predicateResultSet.countResults()); | ||
618 | } | ||
619 | |||
620 | @Test | ||
497 | void transitivePatternCallTest() { | 621 | void transitivePatternCallTest() { |
498 | var person = new Symbol<>("Person", 1, Boolean.class, false); | 622 | var person = new Symbol<>("Person", 1, Boolean.class, false); |
499 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); | 623 | var friend = new Symbol<>("friend", 2, TruthValue.class, TruthValue.FALSE); |