diff options
Diffstat (limited to 'subprojects/store-dse/src/test/java')
-rw-r--r-- | subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/TransitionTests.java | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/TransitionTests.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/TransitionTests.java new file mode 100644 index 00000000..b89360cb --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/TransitionTests.java | |||
@@ -0,0 +1,151 @@ | |||
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.dse.transition; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.store.dse.modification.ModificationAdapter; | ||
10 | import tools.refinery.store.dse.transition.objectives.QueryCriteria; | ||
11 | import tools.refinery.store.dse.transition.objectives.QueryObjective; | ||
12 | import tools.refinery.store.model.Model; | ||
13 | import tools.refinery.store.model.ModelStore; | ||
14 | import tools.refinery.store.query.ModelQueryAdapter; | ||
15 | import tools.refinery.store.query.dnf.FunctionalQuery; | ||
16 | import tools.refinery.store.query.dnf.Query; | ||
17 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
18 | import tools.refinery.store.query.literal.CallPolarity; | ||
19 | import tools.refinery.store.query.literal.Literals; | ||
20 | import tools.refinery.store.query.term.Variable; | ||
21 | import tools.refinery.store.query.term.int_.IntTerms; | ||
22 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
23 | import tools.refinery.store.query.view.AnySymbolView; | ||
24 | import tools.refinery.store.query.view.KeyOnlyView; | ||
25 | import tools.refinery.store.representation.Symbol; | ||
26 | import tools.refinery.store.statecoding.StateCoderAdapter; | ||
27 | import tools.refinery.store.tuple.Tuple; | ||
28 | |||
29 | import java.util.List; | ||
30 | |||
31 | import static org.junit.jupiter.api.Assertions.*; | ||
32 | |||
33 | class TransitionBuildTests { | ||
34 | Symbol<Boolean> person = new Symbol<>("Person", 1, Boolean.class, false); | ||
35 | Symbol<Boolean> friend = new Symbol<>("friend", 2, Boolean.class, false); | ||
36 | AnySymbolView personView = new KeyOnlyView<>(person); | ||
37 | AnySymbolView friendView = new KeyOnlyView<>(friend); | ||
38 | |||
39 | /*RelationalQuery areNotFriends = Query.of("areNotFriends", | ||
40 | (builder, p1, p2) -> builder.clause( | ||
41 | personView.call(p1), | ||
42 | personView.call(p2), | ||
43 | not(friendView.call(p1, p2))))*/ | ||
44 | |||
45 | RelationalQuery moreThan3Friends = Query.of("moreThan3Friends", | ||
46 | (builder, tooMuchFriends) -> builder.clause(Integer.class, (numberOfFriends) -> List.of( | ||
47 | numberOfFriends.assign(friendView.count(tooMuchFriends, Variable.of())), | ||
48 | Literals.assume(IntTerms.less(IntTerms.constant(3), numberOfFriends)), | ||
49 | personView.call(tooMuchFriends) | ||
50 | ))); | ||
51 | |||
52 | RelationalQuery somebodyHasNoFriend = Query.of("somebodyHasNoFriend", | ||
53 | (builder, lonely) -> builder.clause( | ||
54 | personView.call(lonely), | ||
55 | friendView.call(CallPolarity.NEGATIVE, lonely, Variable.of()) | ||
56 | )); | ||
57 | FunctionalQuery<Integer> numberOfFriends = FunctionalQuery.of(Integer.class, | ||
58 | (builder, output) -> builder.clause(output.assign(friendView.count(Variable.of(), Variable.of())))); | ||
59 | |||
60 | @Test | ||
61 | void acceptTest() { | ||
62 | Model model = getModel(); | ||
63 | |||
64 | var dse = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
65 | var query = model.getAdapter(ModelQueryAdapter.class); | ||
66 | var personI = model.getInterpretation(person); | ||
67 | var friendI = model.getInterpretation(friend); | ||
68 | |||
69 | assertTrue(dse.checkAccept()); | ||
70 | personI.put(Tuple.of(1), true); | ||
71 | personI.put(Tuple.of(2), true); | ||
72 | |||
73 | query.flushChanges(); | ||
74 | |||
75 | assertFalse(dse.checkAccept()); | ||
76 | friendI.put(Tuple.of(1, 2), true); | ||
77 | friendI.put(Tuple.of(2, 1), true); | ||
78 | |||
79 | query.flushChanges(); | ||
80 | |||
81 | assertTrue(dse.checkAccept()); | ||
82 | } | ||
83 | |||
84 | @Test | ||
85 | void includeTest() { | ||
86 | Model model = getModel(); | ||
87 | |||
88 | var dse = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
89 | var query = model.getAdapter(ModelQueryAdapter.class); | ||
90 | var personI = model.getInterpretation(person); | ||
91 | var friendI = model.getInterpretation(friend); | ||
92 | |||
93 | assertFalse(dse.checkExclude()); | ||
94 | |||
95 | personI.put(Tuple.of(1), true); | ||
96 | friendI.put(Tuple.of(1, 2), true); | ||
97 | friendI.put(Tuple.of(1, 3), true); | ||
98 | friendI.put(Tuple.of(1, 4), true); | ||
99 | |||
100 | query.flushChanges(); | ||
101 | assertFalse(dse.checkExclude()); | ||
102 | |||
103 | personI.put(Tuple.of(5), true); | ||
104 | friendI.put(Tuple.of(1, 5), true); | ||
105 | |||
106 | query.flushChanges(); | ||
107 | assertTrue(dse.checkExclude()); | ||
108 | |||
109 | friendI.put(Tuple.of(1, 2), false); | ||
110 | |||
111 | query.flushChanges(); | ||
112 | assertFalse(dse.checkExclude()); | ||
113 | } | ||
114 | |||
115 | @Test | ||
116 | void objectiveTest() { | ||
117 | Model model = getModel(); | ||
118 | |||
119 | var dse = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
120 | var query = model.getAdapter(ModelQueryAdapter.class); | ||
121 | var friendI = model.getInterpretation(friend); | ||
122 | |||
123 | assertEquals(0.0, dse.getObjectiveValue().get(0), 0.01); | ||
124 | |||
125 | friendI.put(Tuple.of(1, 2), true); | ||
126 | |||
127 | query.flushChanges(); | ||
128 | assertEquals(1.0, dse.getObjectiveValue().get(0), 0.01); | ||
129 | |||
130 | friendI.put(Tuple.of(1, 3), true); | ||
131 | friendI.put(Tuple.of(1, 4), true); | ||
132 | |||
133 | query.flushChanges(); | ||
134 | assertEquals(3.0, dse.getObjectiveValue().get(0), 0.01); | ||
135 | } | ||
136 | |||
137 | private Model getModel() { | ||
138 | var store = ModelStore.builder() | ||
139 | .symbols(person, friend) | ||
140 | .with(ViatraModelQueryAdapter.builder()) | ||
141 | .with(StateCoderAdapter.builder()) | ||
142 | .with(ModificationAdapter.builder()) | ||
143 | .with(DesignSpaceExplorationAdapter.builder() | ||
144 | .objective(new QueryObjective(numberOfFriends)) | ||
145 | .exclude(new QueryCriteria(moreThan3Friends, true)) | ||
146 | .accept(new QueryCriteria(somebodyHasNoFriend, false))) | ||
147 | .build(); | ||
148 | |||
149 | return store.createEmptyModel(); | ||
150 | } | ||
151 | } | ||