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 | |
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')
4 files changed, 182 insertions, 3 deletions
diff --git a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java index 2be92464..86401b7e 100644 --- a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java +++ b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java | |||
@@ -39,7 +39,7 @@ class LowerTypeScopePropagator extends TypeScopePropagator { | |||
39 | constraint.setLb((lowerBound - getSingleCount())); | 39 | constraint.setLb((lowerBound - getSingleCount())); |
40 | } | 40 | } |
41 | 41 | ||
42 | public static class Factory extends TypeScopePropagator.Factory { | 42 | static class Factory extends TypeScopePropagator.Factory { |
43 | private final PartialRelation type; | 43 | private final PartialRelation type; |
44 | private final int lowerBound; | 44 | private final int lowerBound; |
45 | private final RelationalQuery allMay; | 45 | private final RelationalQuery allMay; |
diff --git a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/TypeScopePropagator.java b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/TypeScopePropagator.java index bb50656b..193c132c 100644 --- a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/TypeScopePropagator.java +++ b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/TypeScopePropagator.java | |||
@@ -59,7 +59,7 @@ abstract class TypeScopePropagator { | |||
59 | adapter.markAsChanged(); | 59 | adapter.markAsChanged(); |
60 | } | 60 | } |
61 | 61 | ||
62 | public abstract static class Factory { | 62 | abstract static class Factory { |
63 | public abstract TypeScopePropagator createPropagator(BoundScopePropagator adapter); | 63 | public abstract TypeScopePropagator createPropagator(BoundScopePropagator adapter); |
64 | 64 | ||
65 | protected abstract Collection<AnyQuery> getQueries(); | 65 | protected abstract Collection<AnyQuery> getQueries(); |
diff --git a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/UpperTypeScopePropagator.java b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/UpperTypeScopePropagator.java index 4aba5aac..634e93bc 100644 --- a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/UpperTypeScopePropagator.java +++ b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/UpperTypeScopePropagator.java | |||
@@ -5,14 +5,22 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.reasoning.scope; | 6 | package tools.refinery.store.reasoning.scope; |
7 | 7 | ||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder; | ||
9 | import tools.refinery.store.dse.transition.objectives.Criteria; | ||
10 | import tools.refinery.store.model.ModelStoreBuilder; | ||
8 | import tools.refinery.store.query.dnf.AnyQuery; | 11 | import tools.refinery.store.query.dnf.AnyQuery; |
9 | import tools.refinery.store.query.dnf.Query; | 12 | import tools.refinery.store.query.dnf.Query; |
10 | import tools.refinery.store.query.dnf.RelationalQuery; | 13 | import tools.refinery.store.query.dnf.RelationalQuery; |
14 | import tools.refinery.store.query.term.Variable; | ||
15 | import tools.refinery.store.reasoning.literal.CountCandidateUpperBoundLiteral; | ||
11 | import tools.refinery.store.reasoning.representation.PartialRelation; | 16 | import tools.refinery.store.reasoning.representation.PartialRelation; |
12 | 17 | ||
13 | import java.util.Collection; | 18 | import java.util.Collection; |
14 | import java.util.List; | 19 | import java.util.List; |
15 | 20 | ||
21 | import static tools.refinery.store.query.literal.Literals.check; | ||
22 | import static tools.refinery.store.query.term.int_.IntTerms.constant; | ||
23 | import static tools.refinery.store.query.term.int_.IntTerms.greater; | ||
16 | import static tools.refinery.store.reasoning.literal.PartialLiterals.must; | 24 | import static tools.refinery.store.reasoning.literal.PartialLiterals.must; |
17 | import static tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator.MULTI_VIEW; | 25 | import static tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator.MULTI_VIEW; |
18 | 26 | ||
@@ -30,12 +38,14 @@ class UpperTypeScopePropagator extends TypeScopePropagator { | |||
30 | constraint.setUb((upperBound - getSingleCount())); | 38 | constraint.setUb((upperBound - getSingleCount())); |
31 | } | 39 | } |
32 | 40 | ||
33 | public static class Factory extends TypeScopePropagator.Factory { | 41 | static class Factory extends TypeScopePropagator.Factory { |
42 | private final PartialRelation type; | ||
34 | private final int upperBound; | 43 | private final int upperBound; |
35 | private final RelationalQuery allMust; | 44 | private final RelationalQuery allMust; |
36 | private final RelationalQuery multiMust; | 45 | private final RelationalQuery multiMust; |
37 | 46 | ||
38 | public Factory(PartialRelation type, int upperBound) { | 47 | public Factory(PartialRelation type, int upperBound) { |
48 | this.type = type; | ||
39 | this.upperBound = upperBound; | 49 | this.upperBound = upperBound; |
40 | allMust = Query.of(type.name() + "#must", (builder, instance) -> builder.clause( | 50 | allMust = Query.of(type.name() + "#must", (builder, instance) -> builder.clause( |
41 | must(type.call(instance)) | 51 | must(type.call(instance)) |
@@ -55,5 +65,19 @@ class UpperTypeScopePropagator extends TypeScopePropagator { | |||
55 | protected Collection<AnyQuery> getQueries() { | 65 | protected Collection<AnyQuery> getQueries() { |
56 | return List.of(allMust, multiMust); | 66 | return List.of(allMust, multiMust); |
57 | } | 67 | } |
68 | |||
69 | @Override | ||
70 | public void configure(ModelStoreBuilder storeBuilder) { | ||
71 | super.configure(storeBuilder); | ||
72 | |||
73 | var tooManyObjects = Query.of(type.name() + "#tooMany", builder -> builder | ||
74 | .clause(Integer.class, candidateUpperBound -> List.of( | ||
75 | new CountCandidateUpperBoundLiteral(candidateUpperBound, type, List.of(Variable.of())), | ||
76 | check(greater(candidateUpperBound, constant(upperBound))) | ||
77 | ))); | ||
78 | |||
79 | storeBuilder.tryGetAdapter(DesignSpaceExplorationBuilder.class).ifPresent(dseBuilder -> | ||
80 | dseBuilder.accept(Criteria.whenNoMatch(tooManyObjects))); | ||
81 | } | ||
58 | } | 82 | } |
59 | } | 83 | } |
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 | } | ||