diff options
author | 2023-11-07 02:20:54 +0100 | |
---|---|---|
committer | 2023-11-17 12:41:35 +0100 | |
commit | cb3fa67e8ae934070f914300d2bbb0b604cb25bb (patch) | |
tree | 47176fb021cf1db37235825e55998001acf4cdc5 /subprojects/store-reasoning-scope/src/test | |
parent | refactor(language): opposite content assist (diff) | |
download | refinery-cb3fa67e8ae934070f914300d2bbb0b604cb25bb.tar.gz refinery-cb3fa67e8ae934070f914300d2bbb0b604cb25bb.tar.zst refinery-cb3fa67e8ae934070f914300d2bbb0b604cb25bb.zip |
fix: scope upper bounds
Diffstat (limited to 'subprojects/store-reasoning-scope/src/test')
-rw-r--r-- | subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/PredicateScopeTest.java | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/PredicateScopeTest.java b/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/PredicateScopeTest.java new file mode 100644 index 00000000..d21bd12e --- /dev/null +++ b/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/PredicateScopeTest.java | |||
@@ -0,0 +1,155 @@ | |||
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.scope; | ||
7 | |||
8 | import org.junit.jupiter.api.Tag; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | import org.junit.jupiter.params.ParameterizedTest; | ||
11 | import org.junit.jupiter.params.provider.ValueSource; | ||
12 | import tools.refinery.store.dse.propagation.PropagationAdapter; | ||
13 | import tools.refinery.store.dse.strategy.BestFirstStoreManager; | ||
14 | import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter; | ||
15 | import tools.refinery.store.model.ModelStore; | ||
16 | import tools.refinery.store.query.dnf.Query; | ||
17 | import tools.refinery.store.query.interpreter.QueryInterpreterAdapter; | ||
18 | import tools.refinery.store.query.term.Variable; | ||
19 | import tools.refinery.store.reasoning.ReasoningAdapter; | ||
20 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
21 | import tools.refinery.store.reasoning.interpretation.PartialInterpretation; | ||
22 | import tools.refinery.store.reasoning.literal.Concreteness; | ||
23 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
24 | import tools.refinery.store.reasoning.seed.ModelSeed; | ||
25 | import tools.refinery.store.reasoning.translator.PartialRelationTranslator; | ||
26 | import tools.refinery.store.reasoning.translator.containment.ContainmentHierarchyTranslator; | ||
27 | import tools.refinery.store.reasoning.translator.metamodel.Metamodel; | ||
28 | import tools.refinery.store.reasoning.translator.metamodel.MetamodelTranslator; | ||
29 | import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator; | ||
30 | import tools.refinery.store.reasoning.translator.multiplicity.ConstrainedMultiplicity; | ||
31 | import tools.refinery.store.representation.TruthValue; | ||
32 | import tools.refinery.store.representation.cardinality.CardinalityIntervals; | ||
33 | import tools.refinery.store.statecoding.StateCoderAdapter; | ||
34 | import tools.refinery.store.tuple.Tuple; | ||
35 | |||
36 | import static org.hamcrest.MatcherAssert.assertThat; | ||
37 | import static org.hamcrest.Matchers.is; | ||
38 | import static tools.refinery.store.query.literal.Literals.not; | ||
39 | |||
40 | class PredicateScopeTest { | ||
41 | private static final PartialRelation index = new PartialRelation("Index", 1); | ||
42 | private static final PartialRelation next = new PartialRelation("next", 2); | ||
43 | private static final PartialRelation nextInvalidMultiplicity = new PartialRelation("next::invalidMultiplicity", 1); | ||
44 | private static final PartialRelation prev = new PartialRelation("prev", 2); | ||
45 | private static final PartialRelation prevInvalidMultiplicity = new PartialRelation("prev::invalidMultiplicity", 1); | ||
46 | private static final PartialRelation loop = new PartialRelation("loop", 1); | ||
47 | private static final PartialRelation first = new PartialRelation("first", 1); | ||
48 | private static final PartialRelation last = new PartialRelation("last", 1); | ||
49 | |||
50 | @Tag("slow") | ||
51 | @ParameterizedTest | ||
52 | @ValueSource(ints = {1, 2, 3, 4, 5}) | ||
53 | void generateTest(int randomSeed) { | ||
54 | var store = createStore(); | ||
55 | var newIndex = Tuple.of(0); | ||
56 | var modelSeed = ModelSeed.builder(1) | ||
57 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder | ||
58 | .reducedValue(CardinalityIntervals.ONE) | ||
59 | .put(newIndex, CardinalityIntervals.SET)) | ||
60 | .seed(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, | ||
61 | builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
62 | .seed(index, builder -> builder | ||
63 | .reducedValue(TruthValue.UNKNOWN) | ||
64 | .put(newIndex, TruthValue.TRUE)) | ||
65 | .seed(next, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
66 | .seed(prev, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
67 | .build(); | ||
68 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed); | ||
69 | var initialVersion = model.commit(); | ||
70 | var bestFistSearch = new BestFirstStoreManager(store, 1); | ||
71 | bestFistSearch.startExploration(initialVersion, randomSeed); | ||
72 | model.restore(bestFistSearch.getSolutionStore().getSolutions().get(0).version()); | ||
73 | var reasoningAdapter = model.getAdapter(ReasoningAdapter.class); | ||
74 | var firstInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, first); | ||
75 | assertSize(firstInterpretation, 1); | ||
76 | var lastInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, last); | ||
77 | assertSize(lastInterpretation, 1); | ||
78 | } | ||
79 | |||
80 | @Test | ||
81 | void invalidResultTest() { | ||
82 | var store = createStore(); | ||
83 | var modelSeed = ModelSeed.builder(8) | ||
84 | .seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder.reducedValue(CardinalityIntervals.ONE)) | ||
85 | .seed(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, | ||
86 | builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
87 | .seed(index, builder -> builder.reducedValue(TruthValue.TRUE)) | ||
88 | .seed(next, builder -> builder | ||
89 | .reducedValue(TruthValue.UNKNOWN) | ||
90 | .put(Tuple.of(1, 2), TruthValue.TRUE) | ||
91 | .put(Tuple.of(2, 3), TruthValue.TRUE) | ||
92 | .put(Tuple.of(3, 4), TruthValue.TRUE) | ||
93 | .put(Tuple.of(4, 5), TruthValue.TRUE) | ||
94 | .put(Tuple.of(6, 1), TruthValue.TRUE)) | ||
95 | .seed(prev, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
96 | .build(); | ||
97 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed); | ||
98 | var reasoningAdapter = model.getAdapter(ReasoningAdapter.class); | ||
99 | var firstInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, first); | ||
100 | assertSize(firstInterpretation, 3); | ||
101 | var lastInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, last); | ||
102 | assertSize(lastInterpretation, 3); | ||
103 | var designSpaceAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
104 | assertThat(designSpaceAdapter.checkAccept(), is(false)); | ||
105 | } | ||
106 | |||
107 | private ModelStore createStore() { | ||
108 | var metamodel = Metamodel.builder() | ||
109 | .type(index) | ||
110 | .reference(next, index, false, | ||
111 | ConstrainedMultiplicity.of(CardinalityIntervals.LONE, nextInvalidMultiplicity), index, prev) | ||
112 | .reference(prev, index, false, | ||
113 | ConstrainedMultiplicity.of(CardinalityIntervals.LONE, prevInvalidMultiplicity), index, next) | ||
114 | .build(); | ||
115 | return ModelStore.builder() | ||
116 | .with(QueryInterpreterAdapter.builder()) | ||
117 | .with(StateCoderAdapter.builder()) | ||
118 | .with(PropagationAdapter.builder()) | ||
119 | .with(DesignSpaceExplorationAdapter.builder()) | ||
120 | .with(ReasoningAdapter.builder()) | ||
121 | .with(new MultiObjectTranslator()) | ||
122 | .with(new MetamodelTranslator(metamodel)) | ||
123 | .with(PartialRelationTranslator.of(loop) | ||
124 | .query(Query.of("loop", (builder, p1) -> builder.clause( | ||
125 | index.call(p1), | ||
126 | next.callTransitive(p1, p1) | ||
127 | ))) | ||
128 | .mayNever()) | ||
129 | .with(PartialRelationTranslator.of(first) | ||
130 | .query(Query.of("first", (builder, p1) -> builder.clause( | ||
131 | index.call(p1), | ||
132 | not(prev.call(p1, Variable.of())) | ||
133 | )))) | ||
134 | .with(PartialRelationTranslator.of(last) | ||
135 | .query(Query.of("last", (builder, p1) -> builder.clause( | ||
136 | index.call(p1), | ||
137 | not(next.call(p1, Variable.of())) | ||
138 | )))) | ||
139 | .with(new ScopePropagator() | ||
140 | .scope(index, CardinalityIntervals.exactly(8)) | ||
141 | .scope(first, CardinalityIntervals.ONE) | ||
142 | .scope(last, CardinalityIntervals.ONE)) | ||
143 | .build(); | ||
144 | } | ||
145 | |||
146 | private void assertSize(PartialInterpretation<TruthValue, Boolean> partialInterpretation, int expected) { | ||
147 | int size = 0; | ||
148 | var cursor = partialInterpretation.getAll(); | ||
149 | while (cursor.move()) { | ||
150 | assertThat(cursor.getValue(), is(TruthValue.TRUE)); | ||
151 | size++; | ||
152 | } | ||
153 | assertThat(size, is(expected)); | ||
154 | } | ||
155 | } | ||