diff options
3 files changed, 24 insertions, 351 deletions
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsWorker.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsWorker.java index 8470bb99..c745d86e 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsWorker.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsWorker.java | |||
@@ -22,6 +22,7 @@ import tools.refinery.language.semantics.metadata.MetadataCreator; | |||
22 | import tools.refinery.language.semantics.model.ModelInitializer; | 22 | import tools.refinery.language.semantics.model.ModelInitializer; |
23 | import tools.refinery.language.semantics.model.SemanticsUtils; | 23 | import tools.refinery.language.semantics.model.SemanticsUtils; |
24 | import tools.refinery.language.semantics.model.TracedException; | 24 | import tools.refinery.language.semantics.model.TracedException; |
25 | import tools.refinery.store.dse.propagation.PropagationAdapter; | ||
25 | import tools.refinery.store.map.Cursor; | 26 | import tools.refinery.store.map.Cursor; |
26 | import tools.refinery.store.model.Model; | 27 | import tools.refinery.store.model.Model; |
27 | import tools.refinery.store.model.ModelStore; | 28 | import tools.refinery.store.model.ModelStore; |
@@ -29,9 +30,7 @@ import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | |||
29 | import tools.refinery.store.reasoning.ReasoningAdapter; | 30 | import tools.refinery.store.reasoning.ReasoningAdapter; |
30 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | 31 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; |
31 | import tools.refinery.store.reasoning.literal.Concreteness; | 32 | import tools.refinery.store.reasoning.literal.Concreteness; |
32 | import tools.refinery.store.reasoning.refinement.RefinementResult; | ||
33 | import tools.refinery.store.reasoning.representation.PartialRelation; | 33 | import tools.refinery.store.reasoning.representation.PartialRelation; |
34 | import tools.refinery.store.reasoning.scope.ScopePropagatorAdapter; | ||
35 | import tools.refinery.store.reasoning.translator.TranslationException; | 34 | import tools.refinery.store.reasoning.translator.TranslationException; |
36 | import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator; | 35 | import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator; |
37 | import tools.refinery.store.tuple.Tuple; | 36 | import tools.refinery.store.tuple.Tuple; |
@@ -77,9 +76,9 @@ class SemanticsWorker implements Callable<SemanticsResult> { | |||
77 | var builder = ModelStore.builder() | 76 | var builder = ModelStore.builder() |
78 | .with(ViatraModelQueryAdapter.builder() | 77 | .with(ViatraModelQueryAdapter.builder() |
79 | .cancellationToken(cancellationToken)) | 78 | .cancellationToken(cancellationToken)) |
79 | .with(PropagationAdapter.builder()) | ||
80 | .with(ReasoningAdapter.builder() | 80 | .with(ReasoningAdapter.builder() |
81 | .requiredInterpretations(Concreteness.PARTIAL)) | 81 | .requiredInterpretations(Concreteness.PARTIAL)); |
82 | .with(ScopePropagatorAdapter.builder()); | ||
83 | cancellationToken.checkCancelled(); | 82 | cancellationToken.checkCancelled(); |
84 | try { | 83 | try { |
85 | var modelSeed = initializer.createModel(problem, builder); | 84 | var modelSeed = initializer.createModel(problem, builder); |
@@ -94,9 +93,6 @@ class SemanticsWorker implements Callable<SemanticsResult> { | |||
94 | cancellationToken.checkCancelled(); | 93 | cancellationToken.checkCancelled(); |
95 | var cancellableModelSeed = CancellableSeed.wrap(cancellationToken, modelSeed); | 94 | var cancellableModelSeed = CancellableSeed.wrap(cancellationToken, modelSeed); |
96 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(cancellableModelSeed); | 95 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(cancellableModelSeed); |
97 | if (model.getAdapter(ScopePropagatorAdapter.class).propagate() == RefinementResult.REJECTED) { | ||
98 | return new SemanticsInternalErrorResult("Scopes are unsatisfiable"); | ||
99 | } | ||
100 | cancellationToken.checkCancelled(); | 96 | cancellationToken.checkCancelled(); |
101 | var partialInterpretation = getPartialInterpretation(initializer, model); | 97 | var partialInterpretation = getPartialInterpretation(initializer, model); |
102 | 98 | ||
diff --git a/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/MultiObjectTest.java b/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/MultiObjectTest.java index 5fc70ae1..0132b3f9 100644 --- a/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/MultiObjectTest.java +++ b/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/MultiObjectTest.java | |||
@@ -27,11 +27,13 @@ import tools.refinery.store.tuple.Tuple; | |||
27 | 27 | ||
28 | import static org.hamcrest.MatcherAssert.assertThat; | 28 | import static org.hamcrest.MatcherAssert.assertThat; |
29 | import static org.hamcrest.Matchers.is; | 29 | import static org.hamcrest.Matchers.is; |
30 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
30 | 31 | ||
31 | class MultiObjectTest { | 32 | class MultiObjectTest { |
32 | private static final PartialRelation person = new PartialRelation("Person", 1); | 33 | private static final PartialRelation person = new PartialRelation("Person", 1); |
33 | 34 | ||
34 | private ModelStore store; | 35 | private ModelStore store; |
36 | private ReasoningStoreAdapter reasoningStoreAdapter; | ||
35 | private Model model; | 37 | private Model model; |
36 | private Interpretation<CardinalityInterval> countStorage; | 38 | private Interpretation<CardinalityInterval> countStorage; |
37 | 39 | ||
@@ -47,6 +49,7 @@ class MultiObjectTest { | |||
47 | .with(new ScopePropagator() | 49 | .with(new ScopePropagator() |
48 | .scope(person, CardinalityIntervals.between(5, 15))) | 50 | .scope(person, CardinalityIntervals.between(5, 15))) |
49 | .build(); | 51 | .build(); |
52 | reasoningStoreAdapter = store.getAdapter(ReasoningStoreAdapter.class); | ||
50 | model = null; | 53 | model = null; |
51 | countStorage = null; | 54 | countStorage = null; |
52 | } | 55 | } |
@@ -59,7 +62,6 @@ class MultiObjectTest { | |||
59 | .put(Tuple.of(0), CardinalityIntervals.SET)) | 62 | .put(Tuple.of(0), CardinalityIntervals.SET)) |
60 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 63 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
61 | .build()); | 64 | .build()); |
62 | assertThat(propagate(), is(PropagationResult.PROPAGATED)); | ||
63 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(2, 12))); | 65 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(2, 12))); |
64 | } | 66 | } |
65 | 67 | ||
@@ -71,19 +73,18 @@ class MultiObjectTest { | |||
71 | .put(Tuple.of(0), CardinalityIntervals.between(5, 20))) | 73 | .put(Tuple.of(0), CardinalityIntervals.between(5, 20))) |
72 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 74 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
73 | .build()); | 75 | .build()); |
74 | assertThat(propagate(), is(PropagationResult.PROPAGATED)); | ||
75 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(5, 12))); | 76 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(5, 12))); |
76 | } | 77 | } |
77 | 78 | ||
78 | @Test | 79 | @Test |
79 | void oneMultiObjectUnsatisfiableUpperTest() { | 80 | void oneMultiObjectUnsatisfiableUpperTest() { |
80 | createModel(ModelSeed.builder(21) | 81 | var seed = ModelSeed.builder(21) |
81 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | 82 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder |
82 | .reducedValue(CardinalityIntervals.ONE) | 83 | .reducedValue(CardinalityIntervals.ONE) |
83 | .put(Tuple.of(0), CardinalityIntervals.SET)) | 84 | .put(Tuple.of(0), CardinalityIntervals.SET)) |
84 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 85 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
85 | .build()); | 86 | .build(); |
86 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 87 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
87 | } | 88 | } |
88 | 89 | ||
89 | @Test | 90 | @Test |
@@ -97,33 +98,33 @@ class MultiObjectTest { | |||
97 | 98 | ||
98 | @Test | 99 | @Test |
99 | void noMultiObjectUnsatisfiableTest() { | 100 | void noMultiObjectUnsatisfiableTest() { |
100 | createModel(ModelSeed.builder(2) | 101 | var seed = ModelSeed.builder(2) |
101 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder.reducedValue(CardinalityIntervals.ONE)) | 102 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder.reducedValue(CardinalityIntervals.ONE)) |
102 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 103 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
103 | .build()); | 104 | .build(); |
104 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 105 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
105 | } | 106 | } |
106 | 107 | ||
107 | @Test | 108 | @Test |
108 | void oneMultiObjectExistingBoundUnsatisfiableLowerTest() { | 109 | void oneMultiObjectExistingBoundUnsatisfiableLowerTest() { |
109 | createModel(ModelSeed.builder(4) | 110 | var seed = ModelSeed.builder(4) |
110 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | 111 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder |
111 | .reducedValue(CardinalityIntervals.ONE) | 112 | .reducedValue(CardinalityIntervals.ONE) |
112 | .put(Tuple.of(0), CardinalityIntervals.atLeast(20))) | 113 | .put(Tuple.of(0), CardinalityIntervals.atLeast(20))) |
113 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 114 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
114 | .build()); | 115 | .build(); |
115 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 116 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
116 | } | 117 | } |
117 | 118 | ||
118 | @Test | 119 | @Test |
119 | void oneMultiObjectExistingBoundUnsatisfiableUpperTest() { | 120 | void oneMultiObjectExistingBoundUnsatisfiableUpperTest() { |
120 | createModel(ModelSeed.builder(4) | 121 | var seed = ModelSeed.builder(4) |
121 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | 122 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder |
122 | .reducedValue(CardinalityIntervals.ONE) | 123 | .reducedValue(CardinalityIntervals.ONE) |
123 | .put(Tuple.of(0), CardinalityIntervals.atMost(1))) | 124 | .put(Tuple.of(0), CardinalityIntervals.atMost(1))) |
124 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 125 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
125 | .build()); | 126 | .build(); |
126 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 127 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
127 | } | 128 | } |
128 | 129 | ||
129 | @Test | 130 | @Test |
@@ -135,7 +136,6 @@ class MultiObjectTest { | |||
135 | .put(Tuple.of(1), CardinalityIntervals.SET)) | 136 | .put(Tuple.of(1), CardinalityIntervals.SET)) |
136 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 137 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
137 | .build()); | 138 | .build()); |
138 | assertThat(propagate(), is(PropagationResult.PROPAGATED)); | ||
139 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.atMost(12))); | 139 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.atMost(12))); |
140 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.atMost(12))); | 140 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.atMost(12))); |
141 | } | 141 | } |
@@ -149,33 +149,32 @@ class MultiObjectTest { | |||
149 | .put(Tuple.of(1), CardinalityIntervals.atMost(11))) | 149 | .put(Tuple.of(1), CardinalityIntervals.atMost(11))) |
150 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 150 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
151 | .build()); | 151 | .build()); |
152 | assertThat(propagate(), is(PropagationResult.PROPAGATED)); | ||
153 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(7, 12))); | 152 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.between(7, 12))); |
154 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.atMost(5))); | 153 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.atMost(5))); |
155 | } | 154 | } |
156 | 155 | ||
157 | @Test | 156 | @Test |
158 | void twoMultiObjectsExistingBoundUnsatisfiableUpperTest() { | 157 | void twoMultiObjectsExistingBoundUnsatisfiableUpperTest() { |
159 | createModel(ModelSeed.builder(5) | 158 | var seed = ModelSeed.builder(5) |
160 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | 159 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder |
161 | .reducedValue(CardinalityIntervals.ONE) | 160 | .reducedValue(CardinalityIntervals.ONE) |
162 | .put(Tuple.of(0), CardinalityIntervals.between(7, 20)) | 161 | .put(Tuple.of(0), CardinalityIntervals.between(7, 20)) |
163 | .put(Tuple.of(1), CardinalityIntervals.exactly(11))) | 162 | .put(Tuple.of(1), CardinalityIntervals.exactly(11))) |
164 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 163 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
165 | .build()); | 164 | .build(); |
166 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 165 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
167 | } | 166 | } |
168 | 167 | ||
169 | @Test | 168 | @Test |
170 | void twoMultiObjectsExistingBoundUnsatisfiableLowerTest() { | 169 | void twoMultiObjectsExistingBoundUnsatisfiableLowerTest() { |
171 | createModel(ModelSeed.builder(3) | 170 | var seed = ModelSeed.builder(3) |
172 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | 171 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder |
173 | .reducedValue(CardinalityIntervals.ONE) | 172 | .reducedValue(CardinalityIntervals.ONE) |
174 | .put(Tuple.of(0), CardinalityIntervals.LONE) | 173 | .put(Tuple.of(0), CardinalityIntervals.LONE) |
175 | .put(Tuple.of(1), CardinalityIntervals.atMost(2))) | 174 | .put(Tuple.of(1), CardinalityIntervals.atMost(2))) |
176 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 175 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
177 | .build()); | 176 | .build(); |
178 | assertThat(propagate(), is(PropagationResult.REJECTED)); | 177 | assertThrows(IllegalArgumentException.class, () -> reasoningStoreAdapter.createInitialModel(seed)); |
179 | } | 178 | } |
180 | 179 | ||
181 | @Test | 180 | @Test |
@@ -187,7 +186,6 @@ class MultiObjectTest { | |||
187 | .put(Tuple.of(1), CardinalityIntervals.SET)) | 186 | .put(Tuple.of(1), CardinalityIntervals.SET)) |
188 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | 187 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) |
189 | .build()); | 188 | .build()); |
190 | assertThat(propagate(), is(PropagationResult.PROPAGATED)); | ||
191 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.LONE)); | 189 | assertThat(countStorage.get(Tuple.of(0)), is(CardinalityIntervals.LONE)); |
192 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.between(1, 12))); | 190 | assertThat(countStorage.get(Tuple.of(1)), is(CardinalityIntervals.between(1, 12))); |
193 | countStorage.put(Tuple.of(0), CardinalityIntervals.ONE); | 191 | countStorage.put(Tuple.of(0), CardinalityIntervals.ONE); |
diff --git a/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/multiobject/CandidateCountTest.java b/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/multiobject/CandidateCountTest.java deleted file mode 100644 index 28391ec7..00000000 --- a/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/multiobject/CandidateCountTest.java +++ /dev/null | |||
@@ -1,321 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.reasoning.translator.multiobject; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.store.model.ModelStore; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.dnf.Query; | ||
12 | import tools.refinery.store.query.resultset.ResultSet; | ||
13 | import tools.refinery.store.query.term.Variable; | ||
14 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
15 | import tools.refinery.store.reasoning.ReasoningAdapter; | ||
16 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
17 | import tools.refinery.store.reasoning.literal.CountCandidateLowerBoundLiteral; | ||
18 | import tools.refinery.store.reasoning.literal.CountCandidateUpperBoundLiteral; | ||
19 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
20 | import tools.refinery.store.reasoning.seed.ModelSeed; | ||
21 | import tools.refinery.store.reasoning.translator.PartialRelationTranslator; | ||
22 | import tools.refinery.store.representation.Symbol; | ||
23 | import tools.refinery.store.representation.TruthValue; | ||
24 | import tools.refinery.store.representation.cardinality.CardinalityIntervals; | ||
25 | import tools.refinery.store.tuple.Tuple; | ||
26 | |||
27 | import java.util.List; | ||
28 | |||
29 | import static org.hamcrest.MatcherAssert.assertThat; | ||
30 | import static org.hamcrest.Matchers.is; | ||
31 | import static tools.refinery.store.query.literal.Literals.not; | ||
32 | import static tools.refinery.store.reasoning.literal.PartialLiterals.must; | ||
33 | |||
34 | class CandidateCountTest { | ||
35 | private static final PartialRelation person = new PartialRelation("Person", 1); | ||
36 | private static final PartialRelation friend = new PartialRelation("friend", 2); | ||
37 | |||
38 | @Test | ||
39 | void lowerBoundZeroTest() { | ||
40 | var query = Query.of("LowerBound", Integer.class, (builder, p1, p2, output) -> builder.clause( | ||
41 | must(person.call(p1)), | ||
42 | must(person.call(p2)), | ||
43 | new CountCandidateLowerBoundLiteral(output, friend, List.of(p1, p2)) | ||
44 | )); | ||
45 | |||
46 | var modelSeed = ModelSeed.builder(2) | ||
47 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
48 | .put(Tuple.of(0), CardinalityIntervals.atLeast(3)) | ||
49 | .put(Tuple.of(1), CardinalityIntervals.atMost(7))) | ||
50 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
51 | .seed(friend, builder -> builder | ||
52 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
53 | .put(Tuple.of(1, 0), TruthValue.UNKNOWN) | ||
54 | .put(Tuple.of(1, 1), TruthValue.ERROR)) | ||
55 | .build(); | ||
56 | |||
57 | var resultSet = getResultSet(query, modelSeed); | ||
58 | assertThat(resultSet.get(Tuple.of(0, 0)), is(0)); | ||
59 | assertThat(resultSet.get(Tuple.of(0, 1)), is(1)); | ||
60 | assertThat(resultSet.get(Tuple.of(1, 0)), is(0)); | ||
61 | assertThat(resultSet.get(Tuple.of(1, 1)), is(1)); | ||
62 | } | ||
63 | |||
64 | @Test | ||
65 | void upperBoundZeroTest() { | ||
66 | var query = Query.of("UpperBound", Integer.class, (builder, p1, p2, output) -> builder.clause( | ||
67 | must(person.call(p1)), | ||
68 | must(person.call(p2)), | ||
69 | new CountCandidateUpperBoundLiteral(output, friend, List.of(p1, p2)) | ||
70 | )); | ||
71 | |||
72 | var modelSeed = ModelSeed.builder(2) | ||
73 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
74 | .put(Tuple.of(0), CardinalityIntervals.atLeast(3)) | ||
75 | .put(Tuple.of(1), CardinalityIntervals.atMost(7))) | ||
76 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
77 | .seed(friend, builder -> builder | ||
78 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
79 | .put(Tuple.of(1, 0), TruthValue.UNKNOWN) | ||
80 | .put(Tuple.of(1, 1), TruthValue.ERROR)) | ||
81 | .build(); | ||
82 | |||
83 | var resultSet = getResultSet(query, modelSeed); | ||
84 | assertThat(resultSet.get(Tuple.of(0, 0)), is(0)); | ||
85 | assertThat(resultSet.get(Tuple.of(0, 1)), is(1)); | ||
86 | assertThat(resultSet.get(Tuple.of(1, 0)), is(0)); | ||
87 | assertThat(resultSet.get(Tuple.of(1, 1)), is(1)); | ||
88 | } | ||
89 | |||
90 | @Test | ||
91 | void lowerBoundOneTest() { | ||
92 | var query = Query.of("LowerBound", Integer.class, (builder, p1, output) -> builder.clause( | ||
93 | must(person.call(p1)), | ||
94 | new CountCandidateLowerBoundLiteral(output, friend, List.of(p1, Variable.of())) | ||
95 | )); | ||
96 | |||
97 | var modelSeed = ModelSeed.builder(4) | ||
98 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
99 | .reducedValue(CardinalityIntervals.ONE) | ||
100 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
101 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
102 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
103 | .seed(friend, builder -> builder | ||
104 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
105 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
106 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
107 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
108 | .put(Tuple.of(1, 2), TruthValue.UNKNOWN) | ||
109 | .put(Tuple.of(1, 3), TruthValue.UNKNOWN) | ||
110 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
111 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
112 | .build(); | ||
113 | |||
114 | var resultSet = getResultSet(query, modelSeed); | ||
115 | assertThat(resultSet.get(Tuple.of(0)), is(2)); | ||
116 | assertThat(resultSet.get(Tuple.of(1)), is(1)); | ||
117 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
118 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
119 | } | ||
120 | |||
121 | @Test | ||
122 | void upperBoundOneTest() { | ||
123 | var query = Query.of("UpperBound", Integer.class, (builder, p1, output) -> builder.clause( | ||
124 | must(person.call(p1)), | ||
125 | new CountCandidateUpperBoundLiteral(output, friend, List.of(p1, Variable.of())) | ||
126 | )); | ||
127 | |||
128 | var modelSeed = ModelSeed.builder(4) | ||
129 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
130 | .reducedValue(CardinalityIntervals.ONE) | ||
131 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
132 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
133 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
134 | .seed(friend, builder -> builder | ||
135 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
136 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
137 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
138 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
139 | .put(Tuple.of(1, 2), TruthValue.UNKNOWN) | ||
140 | .put(Tuple.of(1, 3), TruthValue.UNKNOWN) | ||
141 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
142 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
143 | .build(); | ||
144 | |||
145 | var resultSet = getResultSet(query, modelSeed); | ||
146 | assertThat(resultSet.get(Tuple.of(0)), is(2)); | ||
147 | assertThat(resultSet.get(Tuple.of(1)), is(1)); | ||
148 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
149 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
150 | } | ||
151 | |||
152 | @Test | ||
153 | void lowerBoundTwoTest() { | ||
154 | var subQuery = Query.of("SubQuery", (builder, p1, p2, p3) -> builder.clause( | ||
155 | friend.call(p1, p2), | ||
156 | friend.call(p1, p3), | ||
157 | friend.call(p2, p3) | ||
158 | )); | ||
159 | var query = Query.of("LowerBound", Integer.class, (builder, p1, output) -> builder.clause( | ||
160 | must(person.call(p1)), | ||
161 | new CountCandidateLowerBoundLiteral(output, subQuery.getDnf(), | ||
162 | List.of(p1, Variable.of(), Variable.of())) | ||
163 | )); | ||
164 | |||
165 | var modelSeed = ModelSeed.builder(4) | ||
166 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
167 | .reducedValue(CardinalityIntervals.ONE) | ||
168 | .put(Tuple.of(0), CardinalityIntervals.between(5, 9)) | ||
169 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
170 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
171 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
172 | .seed(friend, builder -> builder | ||
173 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
174 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
175 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
176 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
177 | .put(Tuple.of(1, 2), TruthValue.TRUE) | ||
178 | .put(Tuple.of(1, 3), TruthValue.TRUE) | ||
179 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
180 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
181 | .build(); | ||
182 | |||
183 | var resultSet = getResultSet(query, modelSeed); | ||
184 | assertThat(resultSet.get(Tuple.of(0)), is(1)); | ||
185 | assertThat(resultSet.get(Tuple.of(1)), is(1)); | ||
186 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
187 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
188 | } | ||
189 | |||
190 | @Test | ||
191 | void upperBoundTwoTest() { | ||
192 | var subQuery = Query.of("SubQuery", (builder, p1, p2, p3) -> builder.clause( | ||
193 | friend.call(p1, p2), | ||
194 | friend.call(p1, p3), | ||
195 | friend.call(p2, p3) | ||
196 | )); | ||
197 | var query = Query.of("UpperBound", Integer.class, (builder, p1, output) -> builder.clause( | ||
198 | must(person.call(p1)), | ||
199 | new CountCandidateUpperBoundLiteral(output, subQuery.getDnf(), | ||
200 | List.of(p1, Variable.of(), Variable.of())) | ||
201 | )); | ||
202 | |||
203 | var modelSeed = ModelSeed.builder(4) | ||
204 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
205 | .reducedValue(CardinalityIntervals.ONE) | ||
206 | .put(Tuple.of(0), CardinalityIntervals.between(5, 9)) | ||
207 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
208 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
209 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
210 | .seed(friend, builder -> builder | ||
211 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
212 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
213 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
214 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
215 | .put(Tuple.of(1, 2), TruthValue.UNKNOWN) | ||
216 | .put(Tuple.of(1, 3), TruthValue.UNKNOWN) | ||
217 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
218 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
219 | .build(); | ||
220 | |||
221 | var resultSet = getResultSet(query, modelSeed); | ||
222 | assertThat(resultSet.get(Tuple.of(0)), is(0)); | ||
223 | assertThat(resultSet.get(Tuple.of(1)), is(0)); | ||
224 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
225 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
226 | } | ||
227 | |||
228 | @Test | ||
229 | void lowerBoundDiagonalTest() { | ||
230 | var subQuery = Query.of("SubQuery", (builder, p1, p2, p3) -> builder.clause( | ||
231 | friend.call(p1, p2), | ||
232 | friend.call(p1, p3), | ||
233 | not(friend.call(p2, p3)) | ||
234 | )); | ||
235 | var query = Query.of("LowerBound", Integer.class, (builder, p1, output) -> builder.clause(v1 -> List.of( | ||
236 | must(person.call(p1)), | ||
237 | new CountCandidateLowerBoundLiteral(output, subQuery.getDnf(), List.of(p1, v1, v1)) | ||
238 | ))); | ||
239 | |||
240 | var modelSeed = ModelSeed.builder(4) | ||
241 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
242 | .reducedValue(CardinalityIntervals.ONE) | ||
243 | .put(Tuple.of(0), CardinalityIntervals.between(5, 9)) | ||
244 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
245 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
246 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
247 | .seed(friend, builder -> builder | ||
248 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
249 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
250 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
251 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
252 | .put(Tuple.of(1, 2), TruthValue.UNKNOWN) | ||
253 | .put(Tuple.of(1, 3), TruthValue.UNKNOWN) | ||
254 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
255 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
256 | .build(); | ||
257 | |||
258 | var resultSet = getResultSet(query, modelSeed); | ||
259 | assertThat(resultSet.get(Tuple.of(0)), is(2)); | ||
260 | assertThat(resultSet.get(Tuple.of(1)), is(1)); | ||
261 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
262 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
263 | } | ||
264 | |||
265 | @Test | ||
266 | void upperBoundDiagonalTest() { | ||
267 | var subQuery = Query.of("SubQuery", (builder, p1, p2, p3) -> builder.clause( | ||
268 | friend.call(p1, p2), | ||
269 | friend.call(p1, p3), | ||
270 | not(friend.call(p2, p3)) | ||
271 | )); | ||
272 | var query = Query.of("UpperBound", Integer.class, (builder, p1, output) -> builder | ||
273 | .clause(v1 -> List.of( | ||
274 | must(person.call(p1)), | ||
275 | new CountCandidateUpperBoundLiteral(output, subQuery.getDnf(), List.of(p1, v1, v1)) | ||
276 | ))); | ||
277 | |||
278 | var modelSeed = ModelSeed.builder(4) | ||
279 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
280 | .reducedValue(CardinalityIntervals.ONE) | ||
281 | .put(Tuple.of(0), CardinalityIntervals.between(5, 9)) | ||
282 | .put(Tuple.of(1), CardinalityIntervals.atLeast(3)) | ||
283 | .put(Tuple.of(2), CardinalityIntervals.atMost(7))) | ||
284 | .seed(person, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
285 | .seed(friend, builder -> builder | ||
286 | .put(Tuple.of(0, 1), TruthValue.TRUE) | ||
287 | .put(Tuple.of(0, 2), TruthValue.TRUE) | ||
288 | .put(Tuple.of(0, 3), TruthValue.TRUE) | ||
289 | .put(Tuple.of(1, 0), TruthValue.TRUE) | ||
290 | .put(Tuple.of(1, 2), TruthValue.UNKNOWN) | ||
291 | .put(Tuple.of(1, 3), TruthValue.UNKNOWN) | ||
292 | .put(Tuple.of(2, 0), TruthValue.TRUE) | ||
293 | .put(Tuple.of(2, 1), TruthValue.ERROR)) | ||
294 | .build(); | ||
295 | |||
296 | var resultSet = getResultSet(query, modelSeed); | ||
297 | assertThat(resultSet.get(Tuple.of(0)), is(2)); | ||
298 | assertThat(resultSet.get(Tuple.of(1)), is(1)); | ||
299 | assertThat(resultSet.get(Tuple.of(2)), is(2)); | ||
300 | assertThat(resultSet.get(Tuple.of(3)), is(0)); | ||
301 | } | ||
302 | |||
303 | private static <T> ResultSet<T> getResultSet(Query<T> query, ModelSeed modelSeed) { | ||
304 | var personStorage = Symbol.of("Person", 1, TruthValue.class, TruthValue.FALSE); | ||
305 | var friendStorage = Symbol.of("friend", 2, TruthValue.class, TruthValue.FALSE); | ||
306 | |||
307 | var store = ModelStore.builder() | ||
308 | .with(ViatraModelQueryAdapter.builder() | ||
309 | .query(query)) | ||
310 | .with(ReasoningAdapter.builder()) | ||
311 | .with(new MultiObjectTranslator()) | ||
312 | .with(PartialRelationTranslator.of(person) | ||
313 | .symbol(personStorage)) | ||
314 | .with(PartialRelationTranslator.of(friend) | ||
315 | .symbol(friendStorage)) | ||
316 | .build(); | ||
317 | |||
318 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed); | ||
319 | return model.getAdapter(ModelQueryAdapter.class).getResultSet(query); | ||
320 | } | ||
321 | } | ||