diff options
author | 2023-08-04 13:52:59 +0200 | |
---|---|---|
committer | 2023-08-04 13:52:59 +0200 | |
commit | 7f552ebe52ac543a9a79c26d6ae91b4a2e63751b (patch) | |
tree | 31630f9a9cf093ee22ee73d25e51afcce9b9cdbb /subprojects/store-dse | |
parent | Intermediate commit with Lazy NeighbourhoodCalculator and StateEquivalenceChe... (diff) | |
parent | Merge pull request #32 from nagilooh/design-space-exploration (diff) | |
download | refinery-7f552ebe52ac543a9a79c26d6ae91b4a2e63751b.tar.gz refinery-7f552ebe52ac543a9a79c26d6ae91b4a2e63751b.tar.zst refinery-7f552ebe52ac543a9a79c26d6ae91b4a2e63751b.zip |
Merge branch 'graphs4value:main' into datastructure
Diffstat (limited to 'subprojects/store-dse')
24 files changed, 2750 insertions, 0 deletions
diff --git a/subprojects/store-dse/build.gradle.kts b/subprojects/store-dse/build.gradle.kts new file mode 100644 index 00000000..bb1dee7d --- /dev/null +++ b/subprojects/store-dse/build.gradle.kts | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | |||
7 | plugins { | ||
8 | id("tools.refinery.gradle.java-library") | ||
9 | } | ||
10 | |||
11 | dependencies { | ||
12 | api(project(":refinery-store-query")) | ||
13 | api(project(":refinery-store-query-viatra")) | ||
14 | api(project(":refinery-store-reasoning")) | ||
15 | api(project(":refinery-visualization")) | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java new file mode 100644 index 00000000..524c2f55 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import org.eclipse.collections.api.block.procedure.Procedure; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | |||
12 | public interface ActionFactory { | ||
13 | Procedure<Tuple> prepare(Model model); | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java new file mode 100644 index 00000000..8963a496 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java | |||
@@ -0,0 +1,66 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapter; | ||
9 | import tools.refinery.store.map.Version; | ||
10 | import tools.refinery.store.dse.internal.Activation; | ||
11 | import tools.refinery.store.dse.internal.DesignSpaceExplorationBuilderImpl; | ||
12 | import tools.refinery.store.dse.objectives.Fitness; | ||
13 | import tools.refinery.store.dse.objectives.ObjectiveComparatorHelper; | ||
14 | import tools.refinery.store.tuple.Tuple; | ||
15 | import tools.refinery.store.tuple.Tuple1; | ||
16 | |||
17 | import java.util.Collection; | ||
18 | import java.util.List; | ||
19 | import java.util.Random; | ||
20 | |||
21 | public interface DesignSpaceExplorationAdapter extends ModelAdapter { | ||
22 | @Override | ||
23 | DesignSpaceExplorationStoreAdapter getStoreAdapter(); | ||
24 | |||
25 | static DesignSpaceExplorationBuilder builder() { | ||
26 | return new DesignSpaceExplorationBuilderImpl(); | ||
27 | } | ||
28 | |||
29 | Collection<Version> explore(); | ||
30 | |||
31 | public int getModelSize(); | ||
32 | |||
33 | public Tuple1 createObject(); | ||
34 | |||
35 | public Tuple deleteObject(Tuple tuple); | ||
36 | |||
37 | public boolean checkGlobalConstraints(); | ||
38 | |||
39 | public boolean backtrack(); | ||
40 | |||
41 | public Fitness calculateFitness(); | ||
42 | |||
43 | public void newSolution(); | ||
44 | |||
45 | public int getDepth(); | ||
46 | |||
47 | public Collection<Activation> getUntraversedActivations(); | ||
48 | |||
49 | public boolean fireActivation(Activation activation); | ||
50 | |||
51 | public void fireRandomActivation(); | ||
52 | |||
53 | public boolean isCurrentInTrajectory(); | ||
54 | |||
55 | public List<Version> getTrajectory(); | ||
56 | |||
57 | public boolean isCurrentStateAlreadyTraversed(); | ||
58 | |||
59 | public ObjectiveComparatorHelper getObjectiveComparatorHelper(); | ||
60 | |||
61 | public void restoreTrajectory(List<Version> trajectory); | ||
62 | |||
63 | public void setRandom(Random random); | ||
64 | |||
65 | public void setRandom(long seed); | ||
66 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationBuilder.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationBuilder.java new file mode 100644 index 00000000..4def5cb2 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationBuilder.java | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapterBuilder; | ||
9 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
10 | import tools.refinery.store.dse.internal.TransformationRule; | ||
11 | import tools.refinery.store.dse.objectives.Objective; | ||
12 | |||
13 | import java.util.Collection; | ||
14 | import java.util.List; | ||
15 | |||
16 | public interface DesignSpaceExplorationBuilder extends ModelAdapterBuilder { | ||
17 | default DesignSpaceExplorationBuilder transformations(TransformationRule... transformationRules) { | ||
18 | return transformations(List.of(transformationRules)); | ||
19 | } | ||
20 | |||
21 | default DesignSpaceExplorationBuilder transformations(Collection<? extends TransformationRule> transformationRules) { | ||
22 | transformationRules.forEach(this::transformation); | ||
23 | return this; | ||
24 | } | ||
25 | |||
26 | default DesignSpaceExplorationBuilder globalConstraints(RelationalQuery... globalConstraints) { | ||
27 | return globalConstraints(List.of(globalConstraints)); | ||
28 | } | ||
29 | |||
30 | default DesignSpaceExplorationBuilder globalConstraints(Collection<RelationalQuery> globalConstraints) { | ||
31 | globalConstraints.forEach(this::globalConstraint); | ||
32 | return this; | ||
33 | } | ||
34 | |||
35 | default DesignSpaceExplorationBuilder objectives(Objective... objectives) { | ||
36 | return objectives(List.of(objectives)); | ||
37 | } | ||
38 | |||
39 | default DesignSpaceExplorationBuilder objectives(Collection<? extends Objective> objectives) { | ||
40 | objectives.forEach(this::objective); | ||
41 | return this; | ||
42 | } | ||
43 | |||
44 | DesignSpaceExplorationBuilder transformation(TransformationRule transformationRule); | ||
45 | DesignSpaceExplorationBuilder globalConstraint(RelationalQuery globalConstraint); | ||
46 | DesignSpaceExplorationBuilder objective(Objective objective); | ||
47 | DesignSpaceExplorationBuilder strategy(Strategy strategy); | ||
48 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationStoreAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationStoreAdapter.java new file mode 100644 index 00000000..186bfebb --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationStoreAdapter.java | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelStoreAdapter; | ||
9 | |||
10 | public interface DesignSpaceExplorationStoreAdapter extends ModelStoreAdapter { | ||
11 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/Strategy.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/Strategy.java new file mode 100644 index 00000000..e240f478 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/Strategy.java | |||
@@ -0,0 +1,13 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | public interface Strategy { | ||
9 | |||
10 | public void initStrategy(DesignSpaceExplorationAdapter designSpaceExplorationAdapter); | ||
11 | |||
12 | public void explore(); | ||
13 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/Activation.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/Activation.java new file mode 100644 index 00000000..1893ce2e --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/Activation.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.internal; | ||
7 | |||
8 | import tools.refinery.store.tuple.Tuple; | ||
9 | |||
10 | public record Activation(TransformationRule transformationRule, Tuple activation) { | ||
11 | public boolean fire() { | ||
12 | return transformationRule.fireActivation(activation); | ||
13 | } | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java new file mode 100644 index 00000000..b32e9696 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java | |||
@@ -0,0 +1,292 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.internal; | ||
11 | |||
12 | import tools.refinery.store.map.Version; | ||
13 | import tools.refinery.store.model.Interpretation; | ||
14 | import tools.refinery.store.model.Model; | ||
15 | import tools.refinery.store.query.ModelQueryAdapter; | ||
16 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
17 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
18 | import tools.refinery.store.dse.DesignSpaceExplorationStoreAdapter; | ||
19 | import tools.refinery.store.dse.Strategy; | ||
20 | import tools.refinery.store.dse.objectives.Fitness; | ||
21 | import tools.refinery.store.dse.objectives.Objective; | ||
22 | import tools.refinery.store.dse.objectives.ObjectiveComparatorHelper; | ||
23 | import tools.refinery.store.query.resultset.ResultSet; | ||
24 | import tools.refinery.store.representation.Symbol; | ||
25 | import tools.refinery.store.tuple.Tuple; | ||
26 | import tools.refinery.store.tuple.Tuple1; | ||
27 | import tools.refinery.visualization.ModelVisualizerAdapter; | ||
28 | |||
29 | import java.util.*; | ||
30 | |||
31 | public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExplorationAdapter { | ||
32 | static final Symbol<Integer> NODE_COUNT_SYMBOL = Symbol.of("MODEL_SIZE", 0, Integer.class, 0); | ||
33 | private final Model model; | ||
34 | private final ModelQueryAdapter queryEngine; | ||
35 | private final DesignSpaceExplorationStoreAdapterImpl storeAdapter; | ||
36 | private final LinkedHashSet<TransformationRule> transformationRules; | ||
37 | private final LinkedHashSet<RelationalQuery> globalConstraints; | ||
38 | private final List<Objective> objectives; | ||
39 | private final LinkedHashSet<ResultSet<Boolean>> globalConstraintResultSets = new LinkedHashSet<>(); | ||
40 | private final Interpretation<Integer> sizeInterpretation; | ||
41 | private final Strategy strategy; | ||
42 | |||
43 | private ObjectiveComparatorHelper objectiveComparatorHelper; | ||
44 | private List<Version> trajectory = new LinkedList<>(); | ||
45 | private Fitness lastFitness; | ||
46 | private final LinkedHashSet<Version> solutions = new LinkedHashSet<>(); | ||
47 | private Map<Version, LinkedHashSet<Activation>> statesAndUntraversedActivations; | ||
48 | private Map<Version, LinkedHashSet<Activation>> statesAndTraversedActivations; | ||
49 | private Random random = new Random(); | ||
50 | private boolean isNewState = false; | ||
51 | private final boolean isVisualizationEnabled; | ||
52 | private final ModelVisualizerAdapter modelVisualizerAdapter; | ||
53 | |||
54 | public List<Version> getTrajectory() { | ||
55 | return new LinkedList<>(trajectory); | ||
56 | } | ||
57 | |||
58 | public DesignSpaceExplorationAdapterImpl(Model model, DesignSpaceExplorationStoreAdapterImpl storeAdapter) { | ||
59 | this.model = model; | ||
60 | this.storeAdapter = storeAdapter; | ||
61 | this.sizeInterpretation = model.getInterpretation(NODE_COUNT_SYMBOL); | ||
62 | queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
63 | |||
64 | globalConstraints = storeAdapter.getGlobalConstraints(); | ||
65 | for (var constraint : globalConstraints) { | ||
66 | globalConstraintResultSets.add(queryEngine.getResultSet(constraint)); | ||
67 | } | ||
68 | |||
69 | transformationRules = storeAdapter.getTransformationSpecifications(); | ||
70 | for (var rule : transformationRules) { | ||
71 | rule.prepare(model, queryEngine); | ||
72 | } | ||
73 | |||
74 | objectives = storeAdapter.getObjectives(); | ||
75 | statesAndUntraversedActivations = new HashMap<>(); | ||
76 | statesAndTraversedActivations = new HashMap<>(); | ||
77 | strategy = storeAdapter.getStrategy(); | ||
78 | modelVisualizerAdapter = model.tryGetAdapter(ModelVisualizerAdapter.class).orElse(null); | ||
79 | isVisualizationEnabled = modelVisualizerAdapter != null; | ||
80 | |||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public Model getModel() { | ||
85 | return model; | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public DesignSpaceExplorationStoreAdapter getStoreAdapter() { | ||
90 | return storeAdapter; | ||
91 | } | ||
92 | |||
93 | @Override | ||
94 | public LinkedHashSet<Version> explore() { | ||
95 | var state = model.commit(); | ||
96 | trajectory.add(state); | ||
97 | statesAndUntraversedActivations.put(state, getAllActivations()); | ||
98 | statesAndTraversedActivations.put(state, new LinkedHashSet<>()); | ||
99 | strategy.initStrategy(this); | ||
100 | strategy.explore(); | ||
101 | return solutions; | ||
102 | } | ||
103 | |||
104 | @Override | ||
105 | public int getModelSize() { | ||
106 | return sizeInterpretation.get(Tuple.of()); | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public Tuple1 createObject() { | ||
111 | var newNodeId = getModelSize(); | ||
112 | sizeInterpretation.put(Tuple.of(), newNodeId + 1); | ||
113 | return Tuple.of(newNodeId); | ||
114 | } | ||
115 | |||
116 | @Override | ||
117 | public Tuple deleteObject(Tuple tuple) { | ||
118 | if (tuple.getSize() != 1) { | ||
119 | throw new IllegalArgumentException("Tuple size must be 1"); | ||
120 | } | ||
121 | // TODO: implement more efficient deletion | ||
122 | // if (tuple.get(0) == getModelSize() - 1) { | ||
123 | // sizeInterpretation.put(Tuple.of(), getModelSize() - 1); | ||
124 | // } | ||
125 | return tuple; | ||
126 | } | ||
127 | |||
128 | @Override | ||
129 | public boolean checkGlobalConstraints() { | ||
130 | for (var resultSet : globalConstraintResultSets) { | ||
131 | if (resultSet.size() > 0) { | ||
132 | return false; | ||
133 | } | ||
134 | } | ||
135 | return true; | ||
136 | } | ||
137 | |||
138 | @Override | ||
139 | public boolean backtrack() { | ||
140 | if (trajectory.size() < 2) { | ||
141 | return false; | ||
142 | } | ||
143 | if (isVisualizationEnabled) { | ||
144 | modelVisualizerAdapter.addTransition(trajectory.get(trajectory.size() - 1), | ||
145 | trajectory.get(trajectory.size() - 2), "backtrack"); | ||
146 | } | ||
147 | model.restore(trajectory.get(trajectory.size() - 2)); | ||
148 | trajectory.remove(trajectory.size() - 1); | ||
149 | return true; | ||
150 | } | ||
151 | |||
152 | @Override | ||
153 | public void restoreTrajectory(List<Version> trajectory) { | ||
154 | model.restore(trajectory.get(trajectory.size() - 1)); | ||
155 | // if (isVisualizationEnabled) { | ||
156 | // modelVisualizerAdapter.addTransition(this.trajectory.get(trajectory.size() - 1), | ||
157 | // trajectory.get(trajectory.size() - 1), "restore"); | ||
158 | // } | ||
159 | this.trajectory = trajectory; | ||
160 | |||
161 | } | ||
162 | |||
163 | @Override | ||
164 | public void setRandom(Random random) { | ||
165 | this.random = random; | ||
166 | } | ||
167 | |||
168 | @Override | ||
169 | public void setRandom(long seed) { | ||
170 | this.random = new Random(seed); | ||
171 | } | ||
172 | |||
173 | @Override | ||
174 | public Fitness calculateFitness() { | ||
175 | Fitness result = new Fitness(); | ||
176 | boolean satisfiesHardObjectives = true; | ||
177 | for (Objective objective : objectives) { | ||
178 | var fitness = objective.getFitness(this); | ||
179 | result.put(objective.getName(), fitness); | ||
180 | if (objective.isHardObjective() && !objective.satisfiesHardObjective(fitness)) { | ||
181 | satisfiesHardObjectives = false; | ||
182 | } | ||
183 | } | ||
184 | result.setSatisfiesHardObjectives(satisfiesHardObjectives); | ||
185 | |||
186 | lastFitness = result; | ||
187 | |||
188 | return result; | ||
189 | } | ||
190 | |||
191 | @Override | ||
192 | public void newSolution() { | ||
193 | var state = model.getState(); | ||
194 | solutions.add(state); | ||
195 | if (isVisualizationEnabled) { | ||
196 | modelVisualizerAdapter.addSolution(state); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | @Override | ||
201 | public int getDepth() { | ||
202 | return trajectory.size() - 1; | ||
203 | } | ||
204 | |||
205 | public LinkedHashSet<Activation> getUntraversedActivations() { | ||
206 | // return statesAndUntraversedActivations.get(model.getState()); | ||
207 | LinkedHashSet<Activation> untraversedActivations = new LinkedHashSet<>(); | ||
208 | for (Activation activation : getAllActivations()) { | ||
209 | if (!statesAndTraversedActivations.get(model.getState()).contains(activation)) { | ||
210 | untraversedActivations.add(activation); | ||
211 | } | ||
212 | } | ||
213 | |||
214 | return untraversedActivations; | ||
215 | } | ||
216 | |||
217 | @Override | ||
218 | public boolean fireActivation(Activation activation) { | ||
219 | if (activation == null) { | ||
220 | return false; | ||
221 | } | ||
222 | var previousState = model.getState(); | ||
223 | if (!statesAndUntraversedActivations.get(previousState).contains(activation)) { | ||
224 | // TODO: throw exception? | ||
225 | return false; | ||
226 | } | ||
227 | if (!activation.fire()) { | ||
228 | return false; | ||
229 | } | ||
230 | statesAndUntraversedActivations.get(previousState).remove(activation); | ||
231 | statesAndTraversedActivations.get(previousState).add(activation); | ||
232 | var newState = model.commit(); | ||
233 | trajectory.add(newState); | ||
234 | isNewState = !statesAndUntraversedActivations.containsKey(newState); | ||
235 | statesAndUntraversedActivations.put(newState, getAllActivations()); | ||
236 | statesAndTraversedActivations.put(newState, new LinkedHashSet<>()); | ||
237 | if (isVisualizationEnabled) { | ||
238 | if (isNewState) { | ||
239 | modelVisualizerAdapter.addState(newState); | ||
240 | } | ||
241 | modelVisualizerAdapter.addTransition(trajectory.get(trajectory.size() - 2), | ||
242 | trajectory.get(trajectory.size() - 1), activation.transformationRule().getName(), | ||
243 | activation.activation()); | ||
244 | } | ||
245 | return true; | ||
246 | } | ||
247 | |||
248 | @Override | ||
249 | public void fireRandomActivation() { | ||
250 | var activations = getUntraversedActivations(); | ||
251 | if (activations.isEmpty()) { | ||
252 | // TODO: throw exception | ||
253 | return; | ||
254 | } | ||
255 | int index = random.nextInt(activations.size()); | ||
256 | var iterator = activations.iterator(); | ||
257 | while (index-- > 0) { | ||
258 | iterator.next(); | ||
259 | } | ||
260 | var activationId = iterator.next(); | ||
261 | fireActivation(activationId); | ||
262 | } | ||
263 | |||
264 | @Override | ||
265 | public boolean isCurrentInTrajectory() { | ||
266 | return trajectory.contains(model.getState()); | ||
267 | } | ||
268 | |||
269 | public LinkedHashSet<Activation> getAllActivations() { | ||
270 | LinkedHashSet<Activation> result = new LinkedHashSet<>(); | ||
271 | for (var rule : transformationRules) { | ||
272 | result.addAll(rule.getAllActivations()); | ||
273 | } | ||
274 | return result; | ||
275 | } | ||
276 | |||
277 | public boolean isCurrentStateAlreadyTraversed() { | ||
278 | // TODO: check isomorphism? | ||
279 | return !isNewState; | ||
280 | } | ||
281 | |||
282 | public Fitness getLastFitness() { | ||
283 | return lastFitness; | ||
284 | } | ||
285 | |||
286 | public ObjectiveComparatorHelper getObjectiveComparatorHelper() { | ||
287 | if (objectiveComparatorHelper == null) { | ||
288 | objectiveComparatorHelper = new ObjectiveComparatorHelper(objectives); | ||
289 | } | ||
290 | return objectiveComparatorHelper; | ||
291 | } | ||
292 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationBuilderImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationBuilderImpl.java new file mode 100644 index 00000000..8f7056f2 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationBuilderImpl.java | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | ||
9 | import tools.refinery.store.model.ModelStore; | ||
10 | import tools.refinery.store.model.ModelStoreBuilder; | ||
11 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
12 | import tools.refinery.store.dse.DesignSpaceExplorationBuilder; | ||
13 | import tools.refinery.store.dse.Strategy; | ||
14 | import tools.refinery.store.dse.objectives.Objective; | ||
15 | |||
16 | import java.util.LinkedHashSet; | ||
17 | import java.util.LinkedList; | ||
18 | import java.util.List; | ||
19 | |||
20 | public class DesignSpaceExplorationBuilderImpl | ||
21 | extends AbstractModelAdapterBuilder<DesignSpaceExplorationStoreAdapterImpl> | ||
22 | implements DesignSpaceExplorationBuilder { | ||
23 | private final LinkedHashSet<TransformationRule> transformationSpecifications = new LinkedHashSet<>(); | ||
24 | private final LinkedHashSet<RelationalQuery> globalConstraints = new LinkedHashSet<>(); | ||
25 | private final List<Objective> objectives = new LinkedList<>(); | ||
26 | private Strategy strategy; | ||
27 | |||
28 | @Override | ||
29 | protected DesignSpaceExplorationStoreAdapterImpl doBuild(ModelStore store) { | ||
30 | return new DesignSpaceExplorationStoreAdapterImpl(store, transformationSpecifications, globalConstraints, | ||
31 | objectives, strategy); | ||
32 | } | ||
33 | |||
34 | @Override | ||
35 | public DesignSpaceExplorationBuilder transformation(TransformationRule transformationRule) { | ||
36 | checkNotConfigured(); | ||
37 | transformationSpecifications.add(transformationRule); | ||
38 | return this; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public DesignSpaceExplorationBuilder globalConstraint(RelationalQuery globalConstraint) { | ||
43 | checkNotConfigured(); | ||
44 | globalConstraints.add(globalConstraint); | ||
45 | return this; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public DesignSpaceExplorationBuilder objective(Objective objective) { | ||
50 | checkNotConfigured(); | ||
51 | objectives.add(objective); | ||
52 | return this; | ||
53 | } | ||
54 | |||
55 | @Override | ||
56 | public DesignSpaceExplorationBuilder strategy(Strategy strategy) { | ||
57 | checkNotConfigured(); | ||
58 | this.strategy = strategy; | ||
59 | return this; | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | protected void doConfigure(ModelStoreBuilder storeBuilder) { | ||
64 | storeBuilder.symbols(DesignSpaceExplorationAdapterImpl.NODE_COUNT_SYMBOL); | ||
65 | super.doConfigure(storeBuilder); | ||
66 | } | ||
67 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationStoreAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationStoreAdapterImpl.java new file mode 100644 index 00000000..09925ae7 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationStoreAdapterImpl.java | |||
@@ -0,0 +1,62 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapter; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.model.ModelStore; | ||
11 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
12 | import tools.refinery.store.dse.DesignSpaceExplorationStoreAdapter; | ||
13 | import tools.refinery.store.dse.Strategy; | ||
14 | import tools.refinery.store.dse.objectives.Objective; | ||
15 | |||
16 | import java.util.LinkedHashSet; | ||
17 | import java.util.List; | ||
18 | |||
19 | public class DesignSpaceExplorationStoreAdapterImpl implements DesignSpaceExplorationStoreAdapter { | ||
20 | private final ModelStore store; | ||
21 | private final LinkedHashSet<TransformationRule> transformationSpecifications; | ||
22 | private final LinkedHashSet<RelationalQuery> globalConstraints; | ||
23 | private final List<Objective> objectives; | ||
24 | private final Strategy strategy; | ||
25 | |||
26 | public DesignSpaceExplorationStoreAdapterImpl(ModelStore store, | ||
27 | LinkedHashSet<TransformationRule> transformationSpecifications, | ||
28 | LinkedHashSet<RelationalQuery> globalConstraints, | ||
29 | List<Objective> objectives, Strategy strategy) { | ||
30 | this.store = store; | ||
31 | this.transformationSpecifications = transformationSpecifications; | ||
32 | this.globalConstraints = globalConstraints; | ||
33 | this.objectives = objectives; | ||
34 | this.strategy = strategy; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public ModelStore getStore() { | ||
39 | return store; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public ModelAdapter createModelAdapter(Model model) { | ||
44 | return new DesignSpaceExplorationAdapterImpl(model, this); | ||
45 | } | ||
46 | |||
47 | public LinkedHashSet<TransformationRule> getTransformationSpecifications() { | ||
48 | return transformationSpecifications; | ||
49 | } | ||
50 | |||
51 | public LinkedHashSet<RelationalQuery> getGlobalConstraints() { | ||
52 | return globalConstraints; | ||
53 | } | ||
54 | |||
55 | public List<Objective> getObjectives() { | ||
56 | return objectives; | ||
57 | } | ||
58 | |||
59 | public Strategy getStrategy() { | ||
60 | return strategy; | ||
61 | } | ||
62 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java new file mode 100644 index 00000000..015d4815 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.internal; | ||
7 | |||
8 | import org.eclipse.collections.api.block.procedure.Procedure; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
12 | import tools.refinery.store.dse.ActionFactory; | ||
13 | import tools.refinery.store.query.resultset.OrderedResultSet; | ||
14 | import tools.refinery.store.query.resultset.ResultSet; | ||
15 | import tools.refinery.store.tuple.Tuple; | ||
16 | |||
17 | import java.util.LinkedHashSet; | ||
18 | import java.util.Random; | ||
19 | |||
20 | public class TransformationRule { | ||
21 | |||
22 | private final String name; | ||
23 | private final RelationalQuery precondition; | ||
24 | private final ActionFactory actionFactory; | ||
25 | private Procedure<Tuple> action; | ||
26 | private OrderedResultSet<Boolean> activations; | ||
27 | private Random random; | ||
28 | private ModelQueryAdapter queryEngine; | ||
29 | |||
30 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) { | ||
31 | this(name, precondition, actionFactory, new Random()); | ||
32 | } | ||
33 | |||
34 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) { | ||
35 | this(name, precondition, actionFactory, new Random(seed)); | ||
36 | } | ||
37 | |||
38 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, Random random) { | ||
39 | this.name = name; | ||
40 | this.precondition = precondition; | ||
41 | this.actionFactory = actionFactory; | ||
42 | this.random = random; | ||
43 | } | ||
44 | public boolean prepare(Model model, ModelQueryAdapter queryEngine) { | ||
45 | action = actionFactory.prepare(model); | ||
46 | this.queryEngine = queryEngine; | ||
47 | activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition)); | ||
48 | return true; | ||
49 | } | ||
50 | |||
51 | public boolean fireActivation(Tuple activation) { | ||
52 | action.accept(activation); | ||
53 | queryEngine.flushChanges(); | ||
54 | return true; | ||
55 | } | ||
56 | |||
57 | public boolean fireRandomActivation() { | ||
58 | return getRandomActivation().fire(); | ||
59 | } | ||
60 | |||
61 | public String getName() { | ||
62 | return name; | ||
63 | } | ||
64 | |||
65 | public RelationalQuery getPrecondition() { | ||
66 | return precondition; | ||
67 | } | ||
68 | |||
69 | public ResultSet<Boolean> getAllActivationsAsSets() { | ||
70 | return activations; | ||
71 | } | ||
72 | |||
73 | public LinkedHashSet<Activation> getAllActivations() { | ||
74 | var result = new LinkedHashSet<Activation>(); | ||
75 | var cursor = activations.getAll(); | ||
76 | while (cursor.move()) { | ||
77 | result.add(new Activation(this, cursor.getKey())); | ||
78 | } | ||
79 | return result; | ||
80 | } | ||
81 | |||
82 | public Activation getRandomActivation() { | ||
83 | return new Activation(this, activations.getKey(random.nextInt(activations.size()))); | ||
84 | } | ||
85 | |||
86 | public Activation getActivation(int index) { | ||
87 | return new Activation(this, activations.getKey(index)); | ||
88 | } | ||
89 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedDummyHardObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedDummyHardObjective.java new file mode 100644 index 00000000..afed75fd --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedDummyHardObjective.java | |||
@@ -0,0 +1,52 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
13 | |||
14 | /** | ||
15 | * This hard objective is fulfilled in any circumstances. Use it if all states should be regarded as a valid solution. | ||
16 | * | ||
17 | * @author Andras Szabolcs Nagy | ||
18 | * | ||
19 | */ | ||
20 | public class AlwaysSatisfiedDummyHardObjective extends BaseObjective { | ||
21 | |||
22 | private static final String DEFAULT_NAME = "AlwaysSatisfiedDummyHardObjective"; | ||
23 | |||
24 | public AlwaysSatisfiedDummyHardObjective() { | ||
25 | super(DEFAULT_NAME); | ||
26 | } | ||
27 | |||
28 | public AlwaysSatisfiedDummyHardObjective(String name) { | ||
29 | super(name); | ||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public Double getFitness(DesignSpaceExplorationAdapter context) { | ||
34 | return 0d; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public boolean isHardObjective() { | ||
39 | return true; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public boolean satisfiesHardObjective(Double fitness) { | ||
44 | return true; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public Objective createNew() { | ||
49 | return this; | ||
50 | } | ||
51 | |||
52 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java new file mode 100644 index 00000000..7df33efe --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/BaseObjective.java | |||
@@ -0,0 +1,132 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
13 | |||
14 | import java.util.Comparator; | ||
15 | import java.util.Objects; | ||
16 | |||
17 | /** | ||
18 | * This abstract class implements the basic functionality of an objective ({@link Objective} namely its name, | ||
19 | * comparator, level and fitness hard constraint. | ||
20 | * | ||
21 | * @author Andras Szabolcs Nagy | ||
22 | * | ||
23 | */ | ||
24 | public abstract class BaseObjective implements Objective { | ||
25 | |||
26 | protected final String name; | ||
27 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; | ||
28 | |||
29 | protected double fitnessConstraint; | ||
30 | protected boolean isThereFitnessConstraint = false; | ||
31 | protected Comparator<Double> fitnessConstraintComparator; | ||
32 | |||
33 | public BaseObjective(String name) { | ||
34 | Objects.requireNonNull(name, "Name of the objective cannot be null."); | ||
35 | this.name = name; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public String getName() { | ||
40 | return name; | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public void setComparator(Comparator<Double> comparator) { | ||
45 | this.comparator = comparator; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public Comparator<Double> getComparator() { | ||
50 | return comparator; | ||
51 | } | ||
52 | |||
53 | public BaseObjective withComparator(Comparator<Double> comparator) { | ||
54 | setComparator(comparator); | ||
55 | return this; | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
60 | * current state as a solution. | ||
61 | * | ||
62 | * @param fitnessConstraint | ||
63 | * Solutions should be better than this value. | ||
64 | * @param fitnessConstraintComparator | ||
65 | * {@link Comparator} to determine if the current state is better than the given value. | ||
66 | * @return The actual instance to enable builder pattern like usage. | ||
67 | */ | ||
68 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint, | ||
69 | Comparator<Double> fitnessConstraintComparator) { | ||
70 | this.fitnessConstraint = fitnessConstraint; | ||
71 | this.fitnessConstraintComparator = fitnessConstraintComparator; | ||
72 | this.isThereFitnessConstraint = true; | ||
73 | return this; | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
78 | * current state as a solution. The provided comparator will be used. | ||
79 | * | ||
80 | * @param fitnessConstraint | ||
81 | * Solutions should be better than this value. | ||
82 | * @return The actual instance to enable builder pattern like usage. | ||
83 | */ | ||
84 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint) { | ||
85 | return withHardConstraintOnFitness(fitnessConstraint, null); | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public void init(DesignSpaceExplorationAdapter context) { | ||
90 | if (fitnessConstraintComparator == null) { | ||
91 | fitnessConstraintComparator = comparator; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public boolean isHardObjective() { | ||
97 | return isThereFitnessConstraint; | ||
98 | } | ||
99 | |||
100 | @Override | ||
101 | public boolean satisfiesHardObjective(Double fitness) { | ||
102 | if (isThereFitnessConstraint) { | ||
103 | int compare = fitnessConstraintComparator.compare(fitness, fitnessConstraint); | ||
104 | if (compare < 0) { | ||
105 | return false; | ||
106 | } | ||
107 | } | ||
108 | return true; | ||
109 | } | ||
110 | |||
111 | @Override | ||
112 | public int hashCode() { | ||
113 | return name.hashCode(); | ||
114 | } | ||
115 | |||
116 | @Override | ||
117 | public boolean equals(Object obj) { | ||
118 | if (this == obj) { | ||
119 | return true; | ||
120 | } | ||
121 | if (obj instanceof BaseObjective baseObjective) { | ||
122 | return name.equals(baseObjective.getName()); | ||
123 | } | ||
124 | return false; | ||
125 | } | ||
126 | |||
127 | @Override | ||
128 | public String toString() { | ||
129 | return name; | ||
130 | } | ||
131 | |||
132 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Comparators.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Comparators.java new file mode 100644 index 00000000..476504b0 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Comparators.java | |||
@@ -0,0 +1,26 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import java.util.Comparator; | ||
13 | |||
14 | public class Comparators { | ||
15 | |||
16 | private Comparators() { /*Utility class constructor*/ } | ||
17 | |||
18 | public static final Comparator<Double> HIGHER_IS_BETTER = (o1, o2) -> o1.compareTo(o2); | ||
19 | |||
20 | public static final Comparator<Double> LOWER_IS_BETTER = (o1, o2) -> o2.compareTo(o1); | ||
21 | |||
22 | private static final Double ZERO = (double) 0; | ||
23 | |||
24 | public static final Comparator<Double> DIFFERENCE_TO_ZERO_IS_BETTER = (o1, o2) -> ZERO.compareTo(Math.abs(o1)-Math.abs(o2)); | ||
25 | |||
26 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Fitness.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Fitness.java new file mode 100644 index 00000000..92709d3e --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Fitness.java | |||
@@ -0,0 +1,30 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import java.util.HashMap; | ||
13 | |||
14 | public class Fitness extends HashMap<String, Double> { | ||
15 | |||
16 | private boolean satisfiesHardObjectives; | ||
17 | |||
18 | public boolean isSatisfiesHardObjectives() { | ||
19 | return satisfiesHardObjectives; | ||
20 | } | ||
21 | |||
22 | public void setSatisfiesHardObjectives(boolean satisfiesHardObjectives) { | ||
23 | this.satisfiesHardObjectives = satisfiesHardObjectives; | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | public String toString() { | ||
28 | return super.toString() + " hardObjectives=" + satisfiesHardObjectives; | ||
29 | } | ||
30 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Objective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Objective.java new file mode 100644 index 00000000..c7313622 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/Objective.java | |||
@@ -0,0 +1,101 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
13 | |||
14 | import java.util.Comparator; | ||
15 | |||
16 | /** | ||
17 | * | ||
18 | * Implementation of this interface represents a single objective of the DSE problem, which can assess a solution | ||
19 | * (trajectory) in a single number. It has a name and a comparator which orders two solution based on the calculated | ||
20 | * value. | ||
21 | * <p> | ||
22 | * Objectives can be either hard or soft objectives. Hard objectives can be satisfied or unsatisfied. If all of the hard | ||
23 | * objectives are satisfied on a single solution, then it is considered to be a valid (or goal) solution. | ||
24 | * <p> | ||
25 | * Certain objectives can have inner state for calculating the fitness value. In this case a new instance is necessary | ||
26 | * for every new thread, and the {@code createNew} method should not return the same instance more than once. | ||
27 | * | ||
28 | * @author Andras Szabolcs Nagy | ||
29 | * | ||
30 | */ | ||
31 | public interface Objective { | ||
32 | |||
33 | /** | ||
34 | * Returns the name of the objective. | ||
35 | * | ||
36 | * @return The name of the objective. | ||
37 | */ | ||
38 | String getName(); | ||
39 | |||
40 | /** | ||
41 | * Sets the {@link Comparator} which is used to compare fitness (doubles). It determines whether the objective is to | ||
42 | * minimize or maximize (or minimize or maximize a delta from a given number). | ||
43 | * | ||
44 | * @param comparator The comparator. | ||
45 | */ | ||
46 | void setComparator(Comparator<Double> comparator); | ||
47 | |||
48 | /** | ||
49 | * Returns a {@link Comparator} which is used to compare fitness (doubles). It determines whether the objective is | ||
50 | * to minimize or maximize (or minimize or maximize a delta from a given number). | ||
51 | * | ||
52 | * @return The comparator. | ||
53 | */ | ||
54 | Comparator<Double> getComparator(); | ||
55 | |||
56 | /** | ||
57 | * Calculates the value of the objective on a given solution (trajectory). | ||
58 | * | ||
59 | * @param context | ||
60 | * The {@link DesignSpaceExplorationAdapter} | ||
61 | * @return The objective value in double. | ||
62 | */ | ||
63 | Double getFitness(DesignSpaceExplorationAdapter context); | ||
64 | |||
65 | /** | ||
66 | * Initializes the objective. It is called exactly once for every thread starts. | ||
67 | * | ||
68 | * @param context | ||
69 | * The {@link DesignSpaceExplorationAdapter}. | ||
70 | */ | ||
71 | void init(DesignSpaceExplorationAdapter context); | ||
72 | |||
73 | /** | ||
74 | * Returns an instance of the {@link Objective}. If it returns the same instance, all the methods has to be thread | ||
75 | * save as they are called concurrently. | ||
76 | * | ||
77 | * @return An instance of the objective. | ||
78 | */ | ||
79 | Objective createNew(); | ||
80 | |||
81 | /** | ||
82 | * Returns true if the objective is a hard objective. In such a case the method | ||
83 | * {@link Objective#satisfiesHardObjective(Double)} is called. | ||
84 | * | ||
85 | * @return True if the objective is a hard objective. | ||
86 | * @see Objective#satisfiesHardObjective(Double) | ||
87 | * @see Objective | ||
88 | */ | ||
89 | boolean isHardObjective(); | ||
90 | |||
91 | /** | ||
92 | * Determines if the given fitness value satisfies the hard objective. | ||
93 | * | ||
94 | * @param fitness | ||
95 | * The fitness value of a solution. | ||
96 | * @return True if it satisfies the hard objective or it is a soft constraint. | ||
97 | * @see Objective | ||
98 | */ | ||
99 | boolean satisfiesHardObjective(Double fitness); | ||
100 | |||
101 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java new file mode 100644 index 00000000..1d676562 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java | |||
@@ -0,0 +1,59 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.objectives; | ||
11 | |||
12 | import java.util.List; | ||
13 | |||
14 | /** | ||
15 | * This class is responsible to compare and sort fitness values. | ||
16 | * | ||
17 | * @author András Szabolcs Nagy | ||
18 | */ | ||
19 | public class ObjectiveComparatorHelper { | ||
20 | |||
21 | private final List<Objective> objectives; | ||
22 | |||
23 | public ObjectiveComparatorHelper(List<Objective> objectives) { | ||
24 | this.objectives = objectives; | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * Compares two fitnesses based on dominance. Returns -1 if the second parameter {@code o2} is a better | ||
29 | * solution ({@code o2} dominates {@code o1}), 1 if the first parameter {@code o1} is better ({@code o1} dominates | ||
30 | * {@code o2}) and returns 0 if they are non-dominating each other. | ||
31 | */ | ||
32 | public int compare(Fitness o1, Fitness o2) { | ||
33 | |||
34 | boolean o1HasBetterFitness = false; | ||
35 | boolean o2HasBetterFitness = false; | ||
36 | |||
37 | for (Objective objective : objectives) { | ||
38 | String objectiveName = objective.getName(); | ||
39 | int sgn = objective.getComparator().compare(o1.get(objectiveName), o2.get(objectiveName)); | ||
40 | |||
41 | if (sgn < 0) { | ||
42 | o2HasBetterFitness = true; | ||
43 | } | ||
44 | if (sgn > 0) { | ||
45 | o1HasBetterFitness = true; | ||
46 | } | ||
47 | if (o1HasBetterFitness && o2HasBetterFitness) { | ||
48 | break; | ||
49 | } | ||
50 | } | ||
51 | if (o2HasBetterFitness) { | ||
52 | } else if (o1HasBetterFitness) { | ||
53 | return 1; | ||
54 | } | ||
55 | |||
56 | return 0; | ||
57 | |||
58 | } | ||
59 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStrategy.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStrategy.java new file mode 100644 index 00000000..8648864c --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStrategy.java | |||
@@ -0,0 +1,199 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.strategy; | ||
11 | |||
12 | import tools.refinery.store.map.Version; | ||
13 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
14 | import tools.refinery.store.dse.Strategy; | ||
15 | import tools.refinery.store.dse.internal.Activation; | ||
16 | import tools.refinery.store.dse.objectives.Fitness; | ||
17 | import tools.refinery.store.dse.objectives.ObjectiveComparatorHelper; | ||
18 | |||
19 | import java.util.Collection; | ||
20 | import java.util.Iterator; | ||
21 | import java.util.List; | ||
22 | import java.util.PriorityQueue; | ||
23 | |||
24 | public class BestFirstStrategy implements Strategy { | ||
25 | |||
26 | private DesignSpaceExplorationAdapter dseAdapter; | ||
27 | |||
28 | private int maxDepth; | ||
29 | private boolean backTrackIfSolution = true; | ||
30 | private boolean onlyBetterFirst = false; | ||
31 | |||
32 | private PriorityQueue<TrajectoryWithFitness> trajectoriesToExplore; | ||
33 | |||
34 | private static class TrajectoryWithFitness { | ||
35 | |||
36 | public List<Version> trajectory; | ||
37 | public Fitness fitness; | ||
38 | |||
39 | public TrajectoryWithFitness(List<Version> trajectory, Fitness fitness) { | ||
40 | super(); | ||
41 | this.trajectory = trajectory; | ||
42 | this.fitness = fitness; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public String toString() { | ||
47 | return trajectory.toString() + fitness.toString(); | ||
48 | } | ||
49 | |||
50 | } | ||
51 | |||
52 | public BestFirstStrategy() { | ||
53 | this(-1); | ||
54 | } | ||
55 | |||
56 | public BestFirstStrategy(int maxDepth) { | ||
57 | if (maxDepth < 0) { | ||
58 | this.maxDepth = Integer.MAX_VALUE; | ||
59 | } else { | ||
60 | this.maxDepth = maxDepth; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public BestFirstStrategy continueIfHardObjectivesFulfilled() { | ||
65 | backTrackIfSolution = false; | ||
66 | return this; | ||
67 | } | ||
68 | |||
69 | public BestFirstStrategy goOnOnlyIfFitnessIsBetter() { | ||
70 | onlyBetterFirst = true; | ||
71 | return this; | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public void initStrategy(DesignSpaceExplorationAdapter designSpaceExplorationAdapter) { | ||
76 | this.dseAdapter = designSpaceExplorationAdapter; | ||
77 | final ObjectiveComparatorHelper objectiveComparatorHelper = dseAdapter.getObjectiveComparatorHelper(); | ||
78 | |||
79 | trajectoriesToExplore = new PriorityQueue<TrajectoryWithFitness>(11, | ||
80 | (o1, o2) -> objectiveComparatorHelper.compare(o2.fitness, o1.fitness)); | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public void explore() { | ||
85 | final ObjectiveComparatorHelper objectiveComparatorHelper = dseAdapter.getObjectiveComparatorHelper(); | ||
86 | |||
87 | boolean globalConstraintsAreSatisfied = dseAdapter.checkGlobalConstraints(); | ||
88 | if (!globalConstraintsAreSatisfied) { | ||
89 | // "Global constraint is not satisfied in the first state. Terminate."); | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | final Fitness firstFitness = dseAdapter.calculateFitness(); | ||
94 | if (firstFitness.isSatisfiesHardObjectives()) { | ||
95 | dseAdapter.newSolution(); | ||
96 | // "First state is a solution. Terminate."); | ||
97 | if (backTrackIfSolution) { | ||
98 | return; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | if (maxDepth == 0) { | ||
103 | return; | ||
104 | } | ||
105 | |||
106 | final List<Version> firstTrajectory = dseAdapter.getTrajectory(); | ||
107 | TrajectoryWithFitness currentTrajectoryWithFitness = new TrajectoryWithFitness(firstTrajectory, firstFitness); | ||
108 | trajectoriesToExplore.add(currentTrajectoryWithFitness); | ||
109 | |||
110 | mainLoop: while (true) { | ||
111 | |||
112 | if (currentTrajectoryWithFitness == null) { | ||
113 | if (trajectoriesToExplore.isEmpty()) { | ||
114 | // "State space is fully traversed."); | ||
115 | return; | ||
116 | } else { | ||
117 | currentTrajectoryWithFitness = trajectoriesToExplore.element(); | ||
118 | // if (logger.isDebugEnabled()) { | ||
119 | // "New trajectory is chosen: " + currentTrajectoryWithFitness); | ||
120 | // } | ||
121 | dseAdapter.restoreTrajectory(currentTrajectoryWithFitness.trajectory); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | Collection<Activation> activations = dseAdapter.getUntraversedActivations(); | ||
126 | Iterator<Activation> iterator = activations.iterator(); | ||
127 | |||
128 | |||
129 | |||
130 | while (iterator.hasNext()) { | ||
131 | final Activation nextActivation = iterator.next(); | ||
132 | if (!iterator.hasNext()) { | ||
133 | // "Last untraversed activation of the state."); | ||
134 | trajectoriesToExplore.remove(currentTrajectoryWithFitness); | ||
135 | } | ||
136 | |||
137 | // if (logger.isDebugEnabled()) { | ||
138 | // "Executing new activation: " + nextActivation); | ||
139 | // } | ||
140 | dseAdapter.fireActivation(nextActivation); | ||
141 | if (dseAdapter.isCurrentStateAlreadyTraversed()) { | ||
142 | // "The new state is already visited."); | ||
143 | dseAdapter.backtrack(); | ||
144 | } else if (!dseAdapter.checkGlobalConstraints()) { | ||
145 | // "Global constraint is not satisfied."); | ||
146 | dseAdapter.backtrack(); | ||
147 | } else { | ||
148 | final Fitness nextFitness = dseAdapter.calculateFitness(); | ||
149 | if (nextFitness.isSatisfiesHardObjectives()) { | ||
150 | dseAdapter.newSolution(); | ||
151 | // "Found a solution."); | ||
152 | if (backTrackIfSolution) { | ||
153 | dseAdapter.backtrack(); | ||
154 | continue; | ||
155 | } | ||
156 | } | ||
157 | if (dseAdapter.getDepth() >= maxDepth) { | ||
158 | // "Reached max depth."); | ||
159 | dseAdapter.backtrack(); | ||
160 | continue; | ||
161 | } | ||
162 | |||
163 | TrajectoryWithFitness nextTrajectoryWithFitness = new TrajectoryWithFitness( | ||
164 | dseAdapter.getTrajectory(), nextFitness); | ||
165 | trajectoriesToExplore.add(nextTrajectoryWithFitness); | ||
166 | |||
167 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFitness.fitness, | ||
168 | nextTrajectoryWithFitness.fitness); | ||
169 | if (compare < 0) { | ||
170 | // "Better fitness, moving on: " + nextFitness); | ||
171 | currentTrajectoryWithFitness = nextTrajectoryWithFitness; | ||
172 | continue mainLoop; | ||
173 | } else if (compare == 0) { | ||
174 | if (onlyBetterFirst) { | ||
175 | // "Equally good fitness, backtrack: " + nextFitness); | ||
176 | dseAdapter.backtrack(); | ||
177 | continue; | ||
178 | } else { | ||
179 | // "Equally good fitness, moving on: " + nextFitness); | ||
180 | currentTrajectoryWithFitness = nextTrajectoryWithFitness; | ||
181 | continue mainLoop; | ||
182 | } | ||
183 | } else { | ||
184 | // "Worse fitness."); | ||
185 | currentTrajectoryWithFitness = null; | ||
186 | continue mainLoop; | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | // "State is fully traversed."); | ||
192 | trajectoriesToExplore.remove(currentTrajectoryWithFitness); | ||
193 | currentTrajectoryWithFitness = null; | ||
194 | |||
195 | } | ||
196 | // "Interrupted."); | ||
197 | |||
198 | } | ||
199 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java new file mode 100644 index 00000000..1405789b --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java | |||
@@ -0,0 +1,117 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, Zoltan Ujhelyi and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.store.dse.strategy; | ||
11 | |||
12 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
13 | import tools.refinery.store.dse.Strategy; | ||
14 | import tools.refinery.store.dse.internal.Activation; | ||
15 | import tools.refinery.store.dse.objectives.Fitness; | ||
16 | |||
17 | import java.util.Collection; | ||
18 | |||
19 | public class DepthFirstStrategy implements Strategy { | ||
20 | |||
21 | private DesignSpaceExplorationAdapter dseAdapter; | ||
22 | |||
23 | private int maxDepth; | ||
24 | private boolean backTrackIfSolution = true; | ||
25 | |||
26 | public DepthFirstStrategy() { | ||
27 | this(-1); | ||
28 | } | ||
29 | |||
30 | public DepthFirstStrategy(int maxDepth) { | ||
31 | if (maxDepth < 0) { | ||
32 | this.maxDepth = Integer.MAX_VALUE; | ||
33 | } else { | ||
34 | this.maxDepth = maxDepth; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | public DepthFirstStrategy continueIfHardObjectivesFulfilled() { | ||
39 | backTrackIfSolution = false; | ||
40 | return this; | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public void initStrategy(DesignSpaceExplorationAdapter designSpaceExplorationAdapter) { | ||
45 | this.dseAdapter = designSpaceExplorationAdapter; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public void explore() { | ||
50 | mainloop: while (true) { | ||
51 | var globalConstraintsAreSatisfied = dseAdapter.checkGlobalConstraints(); | ||
52 | if (!globalConstraintsAreSatisfied) { | ||
53 | var isSuccessfulUndo = dseAdapter.backtrack(); | ||
54 | if (!isSuccessfulUndo) { | ||
55 | // "Global constraint is not satisfied and cannot backtrack." | ||
56 | break; | ||
57 | } | ||
58 | else { | ||
59 | // "Global constraint is not satisfied, backtrack." | ||
60 | continue; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | Fitness fitness = dseAdapter.calculateFitness(); | ||
65 | if (fitness.isSatisfiesHardObjectives()) { | ||
66 | dseAdapter.newSolution(); | ||
67 | if (backTrackIfSolution) { | ||
68 | var isSuccessfulUndo = dseAdapter.backtrack(); | ||
69 | if (!isSuccessfulUndo) { | ||
70 | // "Found a solution but cannot backtrack." | ||
71 | break; | ||
72 | } else { | ||
73 | // "Found a solution, backtrack." | ||
74 | continue; | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | var depth = dseAdapter.getDepth(); | ||
80 | if (dseAdapter.getDepth() >= maxDepth) { | ||
81 | var isSuccessfulUndo = dseAdapter.backtrack(); | ||
82 | if (!isSuccessfulUndo) { | ||
83 | // "Reached max depth but cannot backtrack." | ||
84 | break; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | Collection<Activation> activations; | ||
89 | do { | ||
90 | activations = dseAdapter.getUntraversedActivations(); | ||
91 | if (activations.isEmpty()) { | ||
92 | if (!dseAdapter.backtrack()) { | ||
93 | // "No more transitions from current state and cannot backtrack." | ||
94 | break mainloop; | ||
95 | } | ||
96 | else { | ||
97 | // "No more transitions from current state, backtrack." | ||
98 | continue; | ||
99 | } | ||
100 | } | ||
101 | } while (activations.isEmpty()); | ||
102 | |||
103 | dseAdapter.fireRandomActivation(); | ||
104 | // if (dseAdapter.isCurrentInTrajectory()) { | ||
105 | // if (!dseAdapter.backtrack()) { | ||
106 | //// TODO: throw exception | ||
107 | //// "The new state is present in the trajectory but cannot backtrack. Should never happen!" | ||
108 | // break; | ||
109 | // } | ||
110 | // else { | ||
111 | //// "The new state is already visited in the trajectory, backtrack." | ||
112 | // continue; | ||
113 | // } | ||
114 | // } | ||
115 | } | ||
116 | } | ||
117 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java new file mode 100644 index 00000000..4bdb05ec --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java | |||
@@ -0,0 +1,279 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
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.dnf.RelationalQuery; | ||
13 | import tools.refinery.store.dse.internal.TransformationRule; | ||
14 | import tools.refinery.store.dse.strategy.BestFirstStrategy; | ||
15 | import tools.refinery.store.dse.strategy.DepthFirstStrategy; | ||
16 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
17 | import tools.refinery.store.query.view.AnySymbolView; | ||
18 | import tools.refinery.store.query.view.KeyOnlyView; | ||
19 | import tools.refinery.store.representation.Symbol; | ||
20 | import tools.refinery.store.tuple.Tuple; | ||
21 | import tools.refinery.visualization.ModelVisualizerAdapter; | ||
22 | import tools.refinery.visualization.internal.FileFormat; | ||
23 | |||
24 | import java.util.List; | ||
25 | |||
26 | import static tools.refinery.store.query.literal.Literals.not; | ||
27 | |||
28 | public class CRAExamplesTest { | ||
29 | private static final Symbol<String> name = Symbol.of("Name", 1, String.class); | ||
30 | |||
31 | // private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
32 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
33 | // private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
34 | private static final Symbol<Boolean> attribute = Symbol.of("Attribute", 1); | ||
35 | private static final Symbol<Boolean> method = Symbol.of("Method", 1); | ||
36 | |||
37 | // private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
38 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
39 | private static final Symbol<Boolean> dataDependency = Symbol.of("DataDependency", 2); | ||
40 | private static final Symbol<Boolean> functionalDependency = Symbol.of("FunctionalDependency", 2); | ||
41 | |||
42 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
43 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
44 | |||
45 | // private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
46 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
47 | // private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
48 | private static final AnySymbolView attributeView = new KeyOnlyView<>(attribute); | ||
49 | private static final AnySymbolView methodView = new KeyOnlyView<>(method); | ||
50 | // private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
51 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
52 | private static final AnySymbolView dataDependencyView = new KeyOnlyView<>(dataDependency); | ||
53 | private static final AnySymbolView functionalDependencyView = new KeyOnlyView<>(functionalDependency); | ||
54 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
55 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
56 | |||
57 | /*Example Transformation rules*/ | ||
58 | private static final RelationalQuery feature = Query.of("Feature", | ||
59 | (builder, f) -> builder.clause( | ||
60 | attributeView.call(f)) | ||
61 | .clause( | ||
62 | methodView.call(f)) | ||
63 | ); | ||
64 | |||
65 | private static final RelationalQuery assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper", | ||
66 | (builder, c, f) -> builder.clause( | ||
67 | classElementView.call(c), | ||
68 | // classesView.call(model, c), | ||
69 | encapsulatesView.call(c, f) | ||
70 | )); | ||
71 | |||
72 | private static final RelationalQuery assignFeaturePrecondition = Query.of("AssignFeaturePrecondition", | ||
73 | (builder, f, c1) -> builder.clause((c2) -> List.of( | ||
74 | // classModelView.call(model), | ||
75 | feature.call(f), | ||
76 | classElementView.call(c1), | ||
77 | // featuresView.call(model, f), | ||
78 | not(assignFeaturePreconditionHelper.call(c2, f)), | ||
79 | not(encapsulatesView.call(c1, f)) | ||
80 | ))); | ||
81 | |||
82 | private static final RelationalQuery deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
83 | (builder, c) -> builder.clause((f) -> List.of( | ||
84 | // classModelView.call(model), | ||
85 | classElementView.call(c), | ||
86 | // featuresView.call(model, f), | ||
87 | not(encapsulatesView.call(c, f)) | ||
88 | ))); | ||
89 | |||
90 | private static final RelationalQuery createClassPreconditionHelper = Query.of("CreateClassPreconditionHelper", | ||
91 | (builder, f, c) -> builder.clause( | ||
92 | classElementView.call(c), | ||
93 | // classesView.call(model, c), | ||
94 | encapsulatesView.call(c, f) | ||
95 | )); | ||
96 | |||
97 | private static final RelationalQuery createClassPrecondition = Query.of("CreateClassPrecondition", | ||
98 | (builder, f) -> builder.clause((c) -> List.of( | ||
99 | // classModelView.call(model), | ||
100 | feature.call(f), | ||
101 | not(createClassPreconditionHelper.call(f, c)) | ||
102 | ))); | ||
103 | |||
104 | private static final RelationalQuery moveFeaturePrecondition = Query.of("MoveFeature", | ||
105 | (builder, c1, c2, f) -> builder.clause( | ||
106 | // classModelView.call(model), | ||
107 | classElementView.call(c1), | ||
108 | classElementView.call(c2), | ||
109 | c1.notEquivalent(c2), | ||
110 | feature.call(f), | ||
111 | // classesView.call(model, c1), | ||
112 | // classesView.call(model, c2), | ||
113 | // featuresView.call(model, f), | ||
114 | encapsulatesView.call(c1, f) | ||
115 | )); | ||
116 | |||
117 | private static final TransformationRule assignFeatureRule = new TransformationRule("AssignFeature", | ||
118 | assignFeaturePrecondition, | ||
119 | (model) -> { | ||
120 | // var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); | ||
121 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
122 | return ((Tuple activation) -> { | ||
123 | var feature = activation.get(0); | ||
124 | var classElement = activation.get(1); | ||
125 | |||
126 | // isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); | ||
127 | encapsulatesInterpretation.put(Tuple.of(classElement, feature), true); | ||
128 | }); | ||
129 | }); | ||
130 | |||
131 | private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
132 | deleteEmptyClassPrecondition, | ||
133 | (model) -> { | ||
134 | // var classesInterpretation = model.getInterpretation(classes); | ||
135 | var classElementInterpretation = model.getInterpretation(classElement); | ||
136 | return ((Tuple activation) -> { | ||
137 | // TODO: can we move dseAdapter outside? | ||
138 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
139 | // var modelElement = activation.get(0); | ||
140 | var classElement = activation.get(0); | ||
141 | |||
142 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
143 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
144 | dseAdapter.deleteObject(Tuple.of(classElement)); | ||
145 | }); | ||
146 | }); | ||
147 | |||
148 | private static final TransformationRule createClassRule = new TransformationRule("CreateClass", | ||
149 | createClassPrecondition, | ||
150 | (model) -> { | ||
151 | var classElementInterpretation = model.getInterpretation(classElement); | ||
152 | // var classesInterpretation = model.getInterpretation(classes); | ||
153 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
154 | return ((Tuple activation) -> { | ||
155 | // TODO: can we move dseAdapter outside? | ||
156 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
157 | // var modelElement = activation.get(0); | ||
158 | var feature = activation.get(0); | ||
159 | |||
160 | var newClassElement = dseAdapter.createObject(); | ||
161 | var newClassElementId = newClassElement.get(0); | ||
162 | classElementInterpretation.put(newClassElement, true); | ||
163 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
164 | encapsulatesInterpretation.put(Tuple.of(newClassElementId, feature), true); | ||
165 | }); | ||
166 | }); | ||
167 | |||
168 | private static final TransformationRule moveFeatureRule = new TransformationRule("MoveFeature", | ||
169 | moveFeaturePrecondition, | ||
170 | (model) -> { | ||
171 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
172 | return ((Tuple activation) -> { | ||
173 | var classElement1 = activation.get(0); | ||
174 | var classElement2 = activation.get(1); | ||
175 | var feature = activation.get(2); | ||
176 | |||
177 | encapsulatesInterpretation.put(Tuple.of(classElement1, feature), false); | ||
178 | encapsulatesInterpretation.put(Tuple.of(classElement2, feature), true); | ||
179 | }); | ||
180 | }); | ||
181 | |||
182 | @Test | ||
183 | void craTest() { | ||
184 | var store = ModelStore.builder() | ||
185 | .symbols(classElement, encapsulates, classes, features, attribute, method, dataDependency, | ||
186 | functionalDependency, name) | ||
187 | .with(ViatraModelQueryAdapter.builder() | ||
188 | .queries(feature, assignFeaturePreconditionHelper, assignFeaturePrecondition, | ||
189 | deleteEmptyClassPrecondition, createClassPreconditionHelper, createClassPrecondition, | ||
190 | moveFeaturePrecondition)) | ||
191 | .with(ModelVisualizerAdapter.builder()) | ||
192 | .with(DesignSpaceExplorationAdapter.builder() | ||
193 | .transformations(assignFeatureRule, deleteEmptyClassRule, createClassRule, moveFeatureRule) | ||
194 | // .strategy(new DepthFirstStrategy(3).continueIfHardObjectivesFulfilled() | ||
195 | .strategy(new BestFirstStrategy(6).continueIfHardObjectivesFulfilled() | ||
196 | // .goOnOnlyIfFitnessIsBetter() | ||
197 | )) | ||
198 | .build(); | ||
199 | |||
200 | var model = store.createEmptyModel(); | ||
201 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
202 | // dseAdapter.setRandom(1); | ||
203 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
204 | |||
205 | // var modelInterpretation = model.getInterpretation(classModel); | ||
206 | var nameInterpretation = model.getInterpretation(name); | ||
207 | var methodInterpretation = model.getInterpretation(method); | ||
208 | var attributeInterpretation = model.getInterpretation(attribute); | ||
209 | var dataDependencyInterpretation = model.getInterpretation(dataDependency); | ||
210 | var functionalDependencyInterpretation = model.getInterpretation(functionalDependency); | ||
211 | |||
212 | // var modelElement = dseAdapter.createObject(); | ||
213 | var method1 = dseAdapter.createObject(); | ||
214 | var method1Id = method1.get(0); | ||
215 | var method2 = dseAdapter.createObject(); | ||
216 | var method2Id = method2.get(0); | ||
217 | var method3 = dseAdapter.createObject(); | ||
218 | var method3Id = method3.get(0); | ||
219 | var method4 = dseAdapter.createObject(); | ||
220 | var method4Id = method4.get(0); | ||
221 | var attribute1 = dseAdapter.createObject(); | ||
222 | var attribute1Id = attribute1.get(0); | ||
223 | var attribute2 = dseAdapter.createObject(); | ||
224 | var attribute2Id = attribute2.get(0); | ||
225 | var attribute3 = dseAdapter.createObject(); | ||
226 | var attribute3Id = attribute3.get(0); | ||
227 | var attribute4 = dseAdapter.createObject(); | ||
228 | var attribute4Id = attribute4.get(0); | ||
229 | var attribute5 = dseAdapter.createObject(); | ||
230 | var attribute5Id = attribute5.get(0); | ||
231 | |||
232 | nameInterpretation.put(method1, "M1"); | ||
233 | nameInterpretation.put(method2, "M2"); | ||
234 | nameInterpretation.put(method3, "M3"); | ||
235 | nameInterpretation.put(method4, "M4"); | ||
236 | nameInterpretation.put(attribute1, "A1"); | ||
237 | nameInterpretation.put(attribute2, "A2"); | ||
238 | nameInterpretation.put(attribute3, "A3"); | ||
239 | nameInterpretation.put(attribute4, "A4"); | ||
240 | nameInterpretation.put(attribute5, "A5"); | ||
241 | |||
242 | |||
243 | |||
244 | // modelInterpretation.put(modelElement, true); | ||
245 | methodInterpretation.put(method1, true); | ||
246 | methodInterpretation.put(method2, true); | ||
247 | methodInterpretation.put(method3, true); | ||
248 | methodInterpretation.put(method4, true); | ||
249 | attributeInterpretation.put(attribute1, true); | ||
250 | attributeInterpretation.put(attribute2, true); | ||
251 | attributeInterpretation.put(attribute3, true); | ||
252 | attributeInterpretation.put(attribute4, true); | ||
253 | attributeInterpretation.put(attribute5, true); | ||
254 | |||
255 | dataDependencyInterpretation.put(Tuple.of(method1Id, attribute1Id), true); | ||
256 | dataDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true); | ||
257 | dataDependencyInterpretation.put(Tuple.of(method2Id, attribute2Id), true); | ||
258 | dataDependencyInterpretation.put(Tuple.of(method3Id, attribute3Id), true); | ||
259 | dataDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true); | ||
260 | dataDependencyInterpretation.put(Tuple.of(method4Id, attribute3Id), true); | ||
261 | dataDependencyInterpretation.put(Tuple.of(method4Id, attribute5Id), true); | ||
262 | |||
263 | functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true); | ||
264 | functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute4Id), true); | ||
265 | functionalDependencyInterpretation.put(Tuple.of(method2Id, attribute1Id), true); | ||
266 | functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute1Id), true); | ||
267 | functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true); | ||
268 | functionalDependencyInterpretation.put(Tuple.of(method4Id, attribute2Id), true); | ||
269 | |||
270 | queryEngine.flushChanges(); | ||
271 | |||
272 | var states = dseAdapter.explore(); | ||
273 | System.out.println("states size: " + states.size()); | ||
274 | System.out.println("states: " + states); | ||
275 | var visualizer = model.getAdapter(ModelVisualizerAdapter.class); | ||
276 | visualizer.renderDesignSpace("test_output", FileFormat.SVG); | ||
277 | } | ||
278 | |||
279 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DebugTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DebugTest.java new file mode 100644 index 00000000..101a5362 --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DebugTest.java | |||
@@ -0,0 +1,122 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
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.dse.internal.TransformationRule; | ||
13 | import tools.refinery.store.dse.strategy.BestFirstStrategy; | ||
14 | import tools.refinery.store.dse.strategy.DepthFirstStrategy; | ||
15 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
16 | import tools.refinery.store.query.view.AnySymbolView; | ||
17 | import tools.refinery.store.query.view.KeyOnlyView; | ||
18 | import tools.refinery.store.representation.Symbol; | ||
19 | import tools.refinery.store.tuple.Tuple; | ||
20 | import tools.refinery.visualization.ModelVisualizerAdapter; | ||
21 | import tools.refinery.visualization.internal.FileFormat; | ||
22 | |||
23 | public class DebugTest { | ||
24 | private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
25 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
26 | private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
27 | |||
28 | private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
29 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
30 | |||
31 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
32 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
33 | |||
34 | private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
35 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
36 | private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
37 | private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
38 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
39 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
40 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
41 | |||
42 | |||
43 | @Test | ||
44 | void BFSTest() { | ||
45 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
46 | (builder, model) -> builder.clause( | ||
47 | classModelView.call(model) | ||
48 | )); | ||
49 | |||
50 | var createClassRule = new TransformationRule("CreateClass", | ||
51 | createClassPrecondition, | ||
52 | (model) -> { | ||
53 | var classesInterpretation = model.getInterpretation(classes); | ||
54 | var classElementInterpretation = model.getInterpretation(classElement); | ||
55 | return ((Tuple activation) -> { | ||
56 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
57 | var modelElement = activation.get(0); | ||
58 | |||
59 | var newClassElement = dseAdapter.createObject(); | ||
60 | var newClassElementId = newClassElement.get(0); | ||
61 | |||
62 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
63 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
64 | }); | ||
65 | }); | ||
66 | |||
67 | var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
68 | (builder, model) -> builder.clause( | ||
69 | classModelView.call(model) | ||
70 | )); | ||
71 | |||
72 | var createFeatureRule = new TransformationRule("CreateFeature", | ||
73 | createFeaturePrecondition, | ||
74 | (model) -> { | ||
75 | var featuresInterpretation = model.getInterpretation(features); | ||
76 | var featureInterpretation = model.getInterpretation(feature); | ||
77 | return ((Tuple activation) -> { | ||
78 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
79 | var modelElement = activation.get(0); | ||
80 | |||
81 | var newClassElement = dseAdapter.createObject(); | ||
82 | var newClassElementId = newClassElement.get(0); | ||
83 | |||
84 | featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
85 | featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
86 | }); | ||
87 | }); | ||
88 | |||
89 | var store = ModelStore.builder() | ||
90 | .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
91 | .with(ViatraModelQueryAdapter.builder() | ||
92 | .queries(createClassPrecondition, createFeaturePrecondition)) | ||
93 | .with(ModelVisualizerAdapter.builder()) | ||
94 | .with(DesignSpaceExplorationAdapter.builder() | ||
95 | .transformations(createClassRule, createFeatureRule) | ||
96 | .strategy(new DepthFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
97 | // .strategy(new BestFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
98 | // .goOnOnlyIfFitnessIsBetter() | ||
99 | )) | ||
100 | .build(); | ||
101 | |||
102 | var model = store.createEmptyModel(); | ||
103 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
104 | // dseAdapter.setRandom(1); | ||
105 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
106 | |||
107 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
108 | var classElementInterpretation = model.getInterpretation(classElement); | ||
109 | var modelElement = dseAdapter.createObject(); | ||
110 | modelElementInterpretation.put(modelElement, true); | ||
111 | classElementInterpretation.put(modelElement, true); | ||
112 | queryEngine.flushChanges(); | ||
113 | |||
114 | |||
115 | var states = dseAdapter.explore(); | ||
116 | var visualizer = model.getAdapter(ModelVisualizerAdapter.class); | ||
117 | visualizer.renderDesignSpace("test_output", FileFormat.SVG); | ||
118 | System.out.println("states size: " + states.size()); | ||
119 | System.out.println("states: " + states); | ||
120 | |||
121 | } | ||
122 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DesignSpaceExplorationTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DesignSpaceExplorationTest.java new file mode 100644 index 00000000..f4644407 --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DesignSpaceExplorationTest.java | |||
@@ -0,0 +1,476 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
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.dse.internal.TransformationRule; | ||
13 | import tools.refinery.store.dse.strategy.BestFirstStrategy; | ||
14 | import tools.refinery.store.dse.strategy.DepthFirstStrategy; | ||
15 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
16 | import tools.refinery.store.query.view.AnySymbolView; | ||
17 | import tools.refinery.store.query.view.KeyOnlyView; | ||
18 | import tools.refinery.store.representation.Symbol; | ||
19 | import tools.refinery.store.tuple.Tuple; | ||
20 | import tools.refinery.visualization.ModelVisualizerAdapter; | ||
21 | |||
22 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
23 | |||
24 | public class DesignSpaceExplorationTest { | ||
25 | // private static final Symbol<Boolean> namedElement = Symbol.of("NamedElement", 1); | ||
26 | // private static final Symbol<Boolean> attribute = Symbol.of("Attribute", 1); | ||
27 | // private static final Symbol<Boolean> method = Symbol.of("Method", 1); | ||
28 | // private static final Symbol<Boolean> dataDependency = Symbol.of("DataDependency", 2); | ||
29 | // private static final Symbol<Boolean> functionalDependency = Symbol.of("FunctionalDependency", 2); | ||
30 | |||
31 | private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
32 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
33 | private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
34 | |||
35 | private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
36 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
37 | |||
38 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
39 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
40 | |||
41 | private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
42 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
43 | private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
44 | private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
45 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
46 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
47 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
48 | |||
49 | @Test | ||
50 | void createObjectTest() { | ||
51 | var store = ModelStore.builder() | ||
52 | .with(ViatraModelQueryAdapter.builder()) | ||
53 | .with(DesignSpaceExplorationAdapter.builder()) | ||
54 | .build(); | ||
55 | |||
56 | var model = store.createEmptyModel(); | ||
57 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
58 | |||
59 | assertEquals(0, dseAdapter.getModelSize()); | ||
60 | |||
61 | var newModel = dseAdapter.createObject(); | ||
62 | var newModelId = newModel.get(0); | ||
63 | var newClass1 = dseAdapter.createObject(); | ||
64 | var newClass1Id = newClass1.get(0); | ||
65 | var newClass2 = dseAdapter.createObject(); | ||
66 | var newClass2Id = newClass2.get(0); | ||
67 | var newField = dseAdapter.createObject(); | ||
68 | var newFieldId = newField.get(0); | ||
69 | |||
70 | assertEquals(0, newModelId); | ||
71 | assertEquals(1, newClass1Id); | ||
72 | assertEquals(2, newClass2Id); | ||
73 | assertEquals(3, newFieldId); | ||
74 | assertEquals(4, dseAdapter.getModelSize()); | ||
75 | } | ||
76 | |||
77 | @Test | ||
78 | void deleteMiddleObjectTest() { | ||
79 | var store = ModelStore.builder() | ||
80 | .with(ViatraModelQueryAdapter.builder()) | ||
81 | .with(DesignSpaceExplorationAdapter.builder()) | ||
82 | .build(); | ||
83 | |||
84 | var model = store.createEmptyModel(); | ||
85 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
86 | |||
87 | assertEquals(0, dseAdapter.getModelSize()); | ||
88 | |||
89 | var newObject0 = dseAdapter.createObject(); | ||
90 | var newObject0Id = newObject0.get(0); | ||
91 | var newObject1 = dseAdapter.createObject(); | ||
92 | var newObject1Id = newObject1.get(0); | ||
93 | var newObject2 = dseAdapter.createObject(); | ||
94 | var newObject2Id = newObject2.get(0); | ||
95 | var newObject3 = dseAdapter.createObject(); | ||
96 | var newObject3Id = newObject3.get(0); | ||
97 | |||
98 | assertEquals(0, newObject0Id); | ||
99 | assertEquals(1, newObject1Id); | ||
100 | assertEquals(2, newObject2Id); | ||
101 | assertEquals(3, newObject3Id); | ||
102 | assertEquals(4, dseAdapter.getModelSize()); | ||
103 | |||
104 | dseAdapter.deleteObject(newObject1); | ||
105 | assertEquals(4, dseAdapter.getModelSize()); | ||
106 | |||
107 | var newObject4 = dseAdapter.createObject(); | ||
108 | var newObject4Id = newObject4.get(0); | ||
109 | assertEquals(4, newObject4Id); | ||
110 | assertEquals(5, dseAdapter.getModelSize()); | ||
111 | |||
112 | dseAdapter.deleteObject(newObject4); | ||
113 | assertEquals(5, dseAdapter.getModelSize()); | ||
114 | } | ||
115 | |||
116 | @Test | ||
117 | void DFSTrivialTest() { | ||
118 | var store = ModelStore.builder() | ||
119 | .symbols(classModel) | ||
120 | .with(ViatraModelQueryAdapter.builder()) | ||
121 | .with(ModelVisualizerAdapter.builder()) | ||
122 | .with(DesignSpaceExplorationAdapter.builder() | ||
123 | .strategy(new DepthFirstStrategy(0))) | ||
124 | .build(); | ||
125 | |||
126 | var model = store.createEmptyModel(); | ||
127 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
128 | |||
129 | var states = dseAdapter.explore(); | ||
130 | assertEquals(1, states.size()); | ||
131 | } | ||
132 | |||
133 | @Test | ||
134 | void DFSOneRuleTest() { | ||
135 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
136 | (builder, model) -> builder.clause( | ||
137 | classModelView.call(model) | ||
138 | )); | ||
139 | |||
140 | var createClassRule = new TransformationRule("CreateClass", | ||
141 | createClassPrecondition, | ||
142 | (model) -> { | ||
143 | var classesInterpretation = model.getInterpretation(classes); | ||
144 | var classElementInterpretation = model.getInterpretation(classElement); | ||
145 | return ((Tuple activation) -> { | ||
146 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
147 | var modelElement = activation.get(0); | ||
148 | |||
149 | var newClassElement = dseAdapter.createObject(); | ||
150 | var newClassElementId = newClassElement.get(0); | ||
151 | |||
152 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
153 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
154 | }); | ||
155 | }); | ||
156 | |||
157 | var store = ModelStore.builder() | ||
158 | .symbols(classModel, classElement, classes) | ||
159 | .with(ViatraModelQueryAdapter.builder() | ||
160 | .queries(createClassPrecondition)) | ||
161 | .with(ModelVisualizerAdapter.builder()) | ||
162 | .with(DesignSpaceExplorationAdapter.builder() | ||
163 | .transformations(createClassRule) | ||
164 | .strategy(new DepthFirstStrategy(4) | ||
165 | )) | ||
166 | .build(); | ||
167 | |||
168 | var model = store.createEmptyModel(); | ||
169 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
170 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
171 | |||
172 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
173 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
174 | queryEngine.flushChanges(); | ||
175 | |||
176 | var states = dseAdapter.explore(); | ||
177 | assertEquals(1, states.size()); | ||
178 | } | ||
179 | |||
180 | @Test | ||
181 | void DFSContinueTest() { | ||
182 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
183 | (builder, model) -> builder.clause( | ||
184 | classModelView.call(model) | ||
185 | )); | ||
186 | |||
187 | var createClassRule = new TransformationRule("CreateClass", | ||
188 | createClassPrecondition, | ||
189 | (model) -> { | ||
190 | var classesInterpretation = model.getInterpretation(classes); | ||
191 | var classElementInterpretation = model.getInterpretation(classElement); | ||
192 | return ((Tuple activation) -> { | ||
193 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
194 | var modelElement = activation.get(0); | ||
195 | |||
196 | var newClassElement = dseAdapter.createObject(); | ||
197 | var newClassElementId = newClassElement.get(0); | ||
198 | |||
199 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
200 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
201 | }); | ||
202 | }); | ||
203 | |||
204 | var store = ModelStore.builder() | ||
205 | .symbols(classModel, classElement, classes) | ||
206 | .with(ViatraModelQueryAdapter.builder() | ||
207 | .queries(createClassPrecondition)) | ||
208 | .with(ModelVisualizerAdapter.builder()) | ||
209 | .with(DesignSpaceExplorationAdapter.builder() | ||
210 | .transformations(createClassRule) | ||
211 | .strategy(new DepthFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
212 | )) | ||
213 | .build(); | ||
214 | |||
215 | var model = store.createEmptyModel(); | ||
216 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
217 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
218 | |||
219 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
220 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
221 | queryEngine.flushChanges(); | ||
222 | |||
223 | var states = dseAdapter.explore(); | ||
224 | assertEquals(5, states.size()); | ||
225 | } | ||
226 | |||
227 | @Test | ||
228 | void DFSCompletenessTest() { | ||
229 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
230 | (builder, model) -> builder.clause( | ||
231 | classModelView.call(model) | ||
232 | )); | ||
233 | |||
234 | var createClassRule = new TransformationRule("CreateClass", | ||
235 | createClassPrecondition, | ||
236 | (model) -> { | ||
237 | var classesInterpretation = model.getInterpretation(classes); | ||
238 | var classElementInterpretation = model.getInterpretation(classElement); | ||
239 | return ((Tuple activation) -> { | ||
240 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
241 | var modelElement = activation.get(0); | ||
242 | |||
243 | var newClassElement = dseAdapter.createObject(); | ||
244 | var newClassElementId = newClassElement.get(0); | ||
245 | |||
246 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
247 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
248 | }); | ||
249 | }); | ||
250 | |||
251 | var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
252 | (builder, model) -> builder.clause( | ||
253 | classModelView.call(model) | ||
254 | )); | ||
255 | |||
256 | var createFeatureRule = new TransformationRule("CreateFeature", | ||
257 | createFeaturePrecondition, | ||
258 | (model) -> { | ||
259 | var featuresInterpretation = model.getInterpretation(features); | ||
260 | var featureInterpretation = model.getInterpretation(feature); | ||
261 | return ((Tuple activation) -> { | ||
262 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
263 | var modelElement = activation.get(0); | ||
264 | |||
265 | var newClassElement = dseAdapter.createObject(); | ||
266 | var newClassElementId = newClassElement.get(0); | ||
267 | |||
268 | featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
269 | featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
270 | }); | ||
271 | }); | ||
272 | |||
273 | var store = ModelStore.builder() | ||
274 | .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
275 | .with(ViatraModelQueryAdapter.builder() | ||
276 | .queries(createClassPrecondition, createFeaturePrecondition)) | ||
277 | .with(ModelVisualizerAdapter.builder()) | ||
278 | .with(DesignSpaceExplorationAdapter.builder() | ||
279 | .transformations(createClassRule, createFeatureRule) | ||
280 | .strategy(new DepthFirstStrategy(10).continueIfHardObjectivesFulfilled() | ||
281 | )) | ||
282 | .build(); | ||
283 | |||
284 | var model = store.createEmptyModel(); | ||
285 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
286 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
287 | |||
288 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
289 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
290 | queryEngine.flushChanges(); | ||
291 | |||
292 | var states = dseAdapter.explore(); | ||
293 | assertEquals(2047, states.size()); | ||
294 | } | ||
295 | |||
296 | @Test | ||
297 | void BFSTrivialTest() { | ||
298 | var store = ModelStore.builder() | ||
299 | .symbols(classModel) | ||
300 | .with(ViatraModelQueryAdapter.builder()) | ||
301 | .with(ModelVisualizerAdapter.builder()) | ||
302 | .with(DesignSpaceExplorationAdapter.builder() | ||
303 | .strategy(new BestFirstStrategy(0))) | ||
304 | .build(); | ||
305 | |||
306 | var model = store.createEmptyModel(); | ||
307 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
308 | |||
309 | var states = dseAdapter.explore(); | ||
310 | assertEquals(1, states.size()); | ||
311 | } | ||
312 | |||
313 | @Test | ||
314 | void BFSOneRuleTest() { | ||
315 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
316 | (builder, model) -> builder.clause( | ||
317 | classModelView.call(model) | ||
318 | )); | ||
319 | |||
320 | var createClassRule = new TransformationRule("CreateClass", | ||
321 | createClassPrecondition, | ||
322 | (model) -> { | ||
323 | var classesInterpretation = model.getInterpretation(classes); | ||
324 | var classElementInterpretation = model.getInterpretation(classElement); | ||
325 | return ((Tuple activation) -> { | ||
326 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
327 | var modelElement = activation.get(0); | ||
328 | |||
329 | var newClassElement = dseAdapter.createObject(); | ||
330 | var newClassElementId = newClassElement.get(0); | ||
331 | |||
332 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
333 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
334 | }); | ||
335 | }); | ||
336 | |||
337 | var store = ModelStore.builder() | ||
338 | .symbols(classModel, classElement, classes) | ||
339 | .with(ViatraModelQueryAdapter.builder() | ||
340 | .queries(createClassPrecondition)) | ||
341 | .with(ModelVisualizerAdapter.builder()) | ||
342 | .with(DesignSpaceExplorationAdapter.builder() | ||
343 | .transformations(createClassRule) | ||
344 | .strategy(new BestFirstStrategy(4) | ||
345 | )) | ||
346 | .build(); | ||
347 | |||
348 | var model = store.createEmptyModel(); | ||
349 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
350 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
351 | |||
352 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
353 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
354 | queryEngine.flushChanges(); | ||
355 | |||
356 | var states = dseAdapter.explore(); | ||
357 | assertEquals(1, states.size()); | ||
358 | } | ||
359 | |||
360 | @Test | ||
361 | void BFSContinueTest() { | ||
362 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
363 | (builder, model) -> builder.clause( | ||
364 | classModelView.call(model) | ||
365 | )); | ||
366 | |||
367 | var createClassRule = new TransformationRule("CreateClass", | ||
368 | createClassPrecondition, | ||
369 | (model) -> { | ||
370 | var classesInterpretation = model.getInterpretation(classes); | ||
371 | var classElementInterpretation = model.getInterpretation(classElement); | ||
372 | return ((Tuple activation) -> { | ||
373 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
374 | var modelElement = activation.get(0); | ||
375 | |||
376 | var newClassElement = dseAdapter.createObject(); | ||
377 | var newClassElementId = newClassElement.get(0); | ||
378 | |||
379 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
380 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
381 | }); | ||
382 | }); | ||
383 | |||
384 | var store = ModelStore.builder() | ||
385 | .symbols(classModel, classElement, classes) | ||
386 | .with(ViatraModelQueryAdapter.builder() | ||
387 | .queries(createClassPrecondition)) | ||
388 | .with(ModelVisualizerAdapter.builder()) | ||
389 | .with(DesignSpaceExplorationAdapter.builder() | ||
390 | .transformations(createClassRule) | ||
391 | .strategy(new BestFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
392 | )) | ||
393 | .build(); | ||
394 | |||
395 | var model = store.createEmptyModel(); | ||
396 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
397 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
398 | |||
399 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
400 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
401 | queryEngine.flushChanges(); | ||
402 | |||
403 | var states = dseAdapter.explore(); | ||
404 | assertEquals(5, states.size()); | ||
405 | } | ||
406 | |||
407 | @Test | ||
408 | void BFSCompletenessTest() { | ||
409 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
410 | (builder, model) -> builder.clause( | ||
411 | classModelView.call(model) | ||
412 | )); | ||
413 | |||
414 | var createClassRule = new TransformationRule("CreateClass", | ||
415 | createClassPrecondition, | ||
416 | (model) -> { | ||
417 | var classesInterpretation = model.getInterpretation(classes); | ||
418 | var classElementInterpretation = model.getInterpretation(classElement); | ||
419 | return ((Tuple activation) -> { | ||
420 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
421 | var modelElement = activation.get(0); | ||
422 | |||
423 | var newClassElement = dseAdapter.createObject(); | ||
424 | var newClassElementId = newClassElement.get(0); | ||
425 | |||
426 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
427 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
428 | }); | ||
429 | }); | ||
430 | |||
431 | var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
432 | (builder, model) -> builder.clause( | ||
433 | classModelView.call(model) | ||
434 | )); | ||
435 | |||
436 | var createFeatureRule = new TransformationRule("CreateFeature", | ||
437 | createFeaturePrecondition, | ||
438 | (model) -> { | ||
439 | var featuresInterpretation = model.getInterpretation(features); | ||
440 | var featureInterpretation = model.getInterpretation(feature); | ||
441 | return ((Tuple activation) -> { | ||
442 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
443 | var modelElement = activation.get(0); | ||
444 | |||
445 | var newClassElement = dseAdapter.createObject(); | ||
446 | var newClassElementId = newClassElement.get(0); | ||
447 | |||
448 | featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
449 | featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
450 | }); | ||
451 | }); | ||
452 | |||
453 | var store = ModelStore.builder() | ||
454 | .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
455 | .with(ViatraModelQueryAdapter.builder() | ||
456 | .queries(createClassPrecondition, createFeaturePrecondition)) | ||
457 | .with(ModelVisualizerAdapter.builder()) | ||
458 | .with(DesignSpaceExplorationAdapter.builder() | ||
459 | .transformations(createClassRule, createFeatureRule) | ||
460 | .strategy(new BestFirstStrategy(10).continueIfHardObjectivesFulfilled() | ||
461 | )) | ||
462 | .build(); | ||
463 | |||
464 | var model = store.createEmptyModel(); | ||
465 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
466 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
467 | |||
468 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
469 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
470 | queryEngine.flushChanges(); | ||
471 | |||
472 | var states = dseAdapter.explore(); | ||
473 | assertEquals(2047, states.size()); | ||
474 | } | ||
475 | |||
476 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/TransformationRuleTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/TransformationRuleTest.java new file mode 100644 index 00000000..312bcebd --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/TransformationRuleTest.java | |||
@@ -0,0 +1,408 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
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.dse.internal.TransformationRule; | ||
13 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
14 | import tools.refinery.store.query.view.AnySymbolView; | ||
15 | import tools.refinery.store.query.view.KeyOnlyView; | ||
16 | import tools.refinery.store.representation.Symbol; | ||
17 | import tools.refinery.store.tuple.Tuple; | ||
18 | |||
19 | import java.util.List; | ||
20 | import java.util.Map; | ||
21 | |||
22 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
23 | import static tools.refinery.store.query.literal.Literals.not; | ||
24 | import static tools.refinery.store.dse.tests.QueryAssertions.assertResults; | ||
25 | |||
26 | public class TransformationRuleTest { | ||
27 | |||
28 | private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
29 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
30 | private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
31 | |||
32 | private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
33 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
34 | |||
35 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
36 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
37 | |||
38 | private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
39 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
40 | private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
41 | private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
42 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
43 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
44 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
45 | |||
46 | @Test | ||
47 | void activationsTest() { | ||
48 | var assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper", | ||
49 | (builder, model, c, f) -> builder.clause( | ||
50 | classElementView.call(c), | ||
51 | classesView.call(model, c), | ||
52 | encapsulatesView.call(c, f) | ||
53 | )); | ||
54 | |||
55 | var assignFeaturePrecondition = Query.of("AssignFeaturePrecondition", (builder, c2, f) | ||
56 | -> builder.clause((model, c1) -> List.of( | ||
57 | classModelView.call(model), | ||
58 | featureView.call(f), | ||
59 | classElementView.call(c2), | ||
60 | featuresView.call(model, f), | ||
61 | classesView.call(model, c1), | ||
62 | not(assignFeaturePreconditionHelper.call(model, c2, f)), | ||
63 | not(encapsulatesView.call(c2, f)) | ||
64 | ))); | ||
65 | |||
66 | var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
67 | (builder, model, c) -> builder.clause((f) -> List.of( | ||
68 | classModelView.call(model), | ||
69 | classElementView.call(c), | ||
70 | featuresView.call(model, f), | ||
71 | not(encapsulatesView.call(c, f)) | ||
72 | ))); | ||
73 | |||
74 | TransformationRule assignFeatureRule = new TransformationRule("AssignFeature", | ||
75 | assignFeaturePrecondition, | ||
76 | (model) -> { | ||
77 | var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); | ||
78 | return ((Tuple activation) -> { | ||
79 | var feature = activation.get(0); | ||
80 | var classElement = activation.get(1); | ||
81 | |||
82 | isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); | ||
83 | }); | ||
84 | }); | ||
85 | |||
86 | TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
87 | deleteEmptyClassPrecondition, | ||
88 | (model) -> { | ||
89 | var classesInterpretation = model.getInterpretation(classes); | ||
90 | var classElementInterpretation = model.getInterpretation(classElement); | ||
91 | return ((Tuple activation) -> { | ||
92 | var modelElement = activation.get(0); | ||
93 | var classElement = activation.get(1); | ||
94 | |||
95 | classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
96 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
97 | }); | ||
98 | }); | ||
99 | |||
100 | |||
101 | var store = ModelStore.builder() | ||
102 | .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
103 | .with(ViatraModelQueryAdapter.builder() | ||
104 | .queries(assignFeaturePrecondition, assignFeaturePreconditionHelper, | ||
105 | deleteEmptyClassPrecondition)) | ||
106 | .with(DesignSpaceExplorationAdapter.builder()) | ||
107 | .build(); | ||
108 | |||
109 | var model = store.createEmptyModel(); | ||
110 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
111 | assignFeatureRule.prepare(model, queryEngine); | ||
112 | deleteEmptyClassRule.prepare(model, queryEngine); | ||
113 | |||
114 | var classModelInterpretation = model.getInterpretation(classModel); | ||
115 | var classElementInterpretation = model.getInterpretation(classElement); | ||
116 | var featureInterpretation = model.getInterpretation(feature); | ||
117 | var featuresInterpretation = model.getInterpretation(features); | ||
118 | var classesInterpretation = model.getInterpretation(classes); | ||
119 | |||
120 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
121 | var newModel = dseAdapter.createObject(); | ||
122 | var newModelId = newModel.get(0); | ||
123 | var newClass1 = dseAdapter.createObject(); | ||
124 | var newClass1Id = newClass1.get(0); | ||
125 | var newClass2 = dseAdapter.createObject(); | ||
126 | var newClass2Id = newClass2.get(0); | ||
127 | var newField = dseAdapter.createObject(); | ||
128 | var newFieldId = newField.get(0); | ||
129 | |||
130 | classModelInterpretation.put(newModel, true); | ||
131 | classElementInterpretation.put(newClass1, true); | ||
132 | classElementInterpretation.put(newClass2, true); | ||
133 | featureInterpretation.put(newField, true); | ||
134 | classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
135 | classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
136 | featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
137 | |||
138 | queryEngine.flushChanges(); | ||
139 | |||
140 | var assignFeatureRuleActivations = assignFeatureRule.getAllActivationsAsSets(); | ||
141 | var deleteEmptyClassRuleActivations = deleteEmptyClassRule.getAllActivationsAsSets(); | ||
142 | |||
143 | assertResults(Map.of( | ||
144 | Tuple.of(newClass1Id, newFieldId), true, | ||
145 | Tuple.of(newClass2Id, newFieldId), true | ||
146 | ), assignFeatureRuleActivations); | ||
147 | |||
148 | assertResults(Map.of( | ||
149 | Tuple.of(newModelId, newClass1Id), true, | ||
150 | Tuple.of(newModelId, newClass2Id), true | ||
151 | ), deleteEmptyClassRuleActivations); | ||
152 | } | ||
153 | |||
154 | @Test | ||
155 | void randomActivationTest() { | ||
156 | var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
157 | (builder, model, c) -> builder.clause((f) -> List.of( | ||
158 | classModelView.call(model), | ||
159 | classElementView.call(c), | ||
160 | featuresView.call(model, f), | ||
161 | not(encapsulatesView.call(c, f)) | ||
162 | ))); | ||
163 | |||
164 | TransformationRule deleteEmptyClassRule0 = new TransformationRule("DeleteEmptyClass0", | ||
165 | deleteEmptyClassPrecondition, | ||
166 | (model) -> { | ||
167 | var classesInterpretation = model.getInterpretation(classes); | ||
168 | var classElementInterpretation = model.getInterpretation(classElement); | ||
169 | return ((Tuple activation) -> { | ||
170 | var modelElement = activation.get(0); | ||
171 | var classElement = activation.get(1); | ||
172 | |||
173 | classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
174 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
175 | }); | ||
176 | }, | ||
177 | 0L); | ||
178 | |||
179 | TransformationRule deleteEmptyClassRule1 = new TransformationRule("DeleteEmptyClass1", | ||
180 | deleteEmptyClassPrecondition, | ||
181 | (model) -> { | ||
182 | var classesInterpretation = model.getInterpretation(classes); | ||
183 | var classElementInterpretation = model.getInterpretation(classElement); | ||
184 | return ((Tuple activation) -> { | ||
185 | var modelElement = activation.get(0); | ||
186 | var classElement = activation.get(1); | ||
187 | |||
188 | classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
189 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
190 | }); | ||
191 | }, | ||
192 | 78634L); | ||
193 | |||
194 | var store = ModelStore.builder() | ||
195 | .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
196 | .with(ViatraModelQueryAdapter.builder() | ||
197 | .queries(deleteEmptyClassPrecondition)) | ||
198 | .with(DesignSpaceExplorationAdapter.builder()) | ||
199 | .build(); | ||
200 | |||
201 | var model = store.createEmptyModel(); | ||
202 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
203 | deleteEmptyClassRule0.prepare(model, queryEngine); | ||
204 | deleteEmptyClassRule1.prepare(model, queryEngine); | ||
205 | |||
206 | var classModelInterpretation = model.getInterpretation(classModel); | ||
207 | var classElementInterpretation = model.getInterpretation(classElement); | ||
208 | var featureInterpretation = model.getInterpretation(feature); | ||
209 | var featuresInterpretation = model.getInterpretation(features); | ||
210 | var classesInterpretation = model.getInterpretation(classes); | ||
211 | |||
212 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
213 | var newModel = dseAdapter.createObject(); | ||
214 | var newModelId = newModel.get(0); | ||
215 | var newClass1 = dseAdapter.createObject(); | ||
216 | var newClass1Id = newClass1.get(0); | ||
217 | var newClass2 = dseAdapter.createObject(); | ||
218 | var newClass2Id = newClass2.get(0); | ||
219 | var newField = dseAdapter.createObject(); | ||
220 | var newFieldId = newField.get(0); | ||
221 | |||
222 | classModelInterpretation.put(newModel, true); | ||
223 | classElementInterpretation.put(newClass1, true); | ||
224 | classElementInterpretation.put(newClass2, true); | ||
225 | featureInterpretation.put(newField, true); | ||
226 | classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
227 | classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
228 | featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
229 | |||
230 | queryEngine.flushChanges(); | ||
231 | |||
232 | |||
233 | var activation0 = deleteEmptyClassRule0.getRandomActivation().activation(); | ||
234 | var activation1 = deleteEmptyClassRule1.getRandomActivation().activation(); | ||
235 | |||
236 | assertResults(Map.of( | ||
237 | Tuple.of(newModelId, newClass1Id), true, | ||
238 | Tuple.of(newModelId, newClass2Id), true | ||
239 | ), deleteEmptyClassRule0.getAllActivationsAsSets()); | ||
240 | |||
241 | assertResults(Map.of( | ||
242 | Tuple.of(newModelId, newClass1Id), true, | ||
243 | Tuple.of(newModelId, newClass2Id), true | ||
244 | ), deleteEmptyClassRule1.getAllActivationsAsSets()); | ||
245 | |||
246 | assertEquals(Tuple.of(newModelId, newClass2Id), activation0); | ||
247 | assertEquals(Tuple.of(newModelId, newClass1Id), activation1); | ||
248 | |||
249 | } | ||
250 | |||
251 | @Test | ||
252 | void fireTest() { | ||
253 | var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
254 | (builder, model, c) -> builder.clause((f) -> List.of( | ||
255 | classModelView.call(model), | ||
256 | classElementView.call(c), | ||
257 | featuresView.call(model, f), | ||
258 | not(encapsulatesView.call(c, f)) | ||
259 | ))); | ||
260 | |||
261 | TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
262 | deleteEmptyClassPrecondition, | ||
263 | (model) -> { | ||
264 | var classesInterpretation = model.getInterpretation(classes); | ||
265 | var classElementInterpretation = model.getInterpretation(classElement); | ||
266 | return ((Tuple activation) -> { | ||
267 | var modelElement = activation.get(0); | ||
268 | var classElement = activation.get(1); | ||
269 | |||
270 | classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
271 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
272 | }); | ||
273 | }); | ||
274 | |||
275 | var store = ModelStore.builder() | ||
276 | .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
277 | .with(ViatraModelQueryAdapter.builder() | ||
278 | .queries(deleteEmptyClassPrecondition)) | ||
279 | .with(DesignSpaceExplorationAdapter.builder()) | ||
280 | .build(); | ||
281 | |||
282 | var model = store.createEmptyModel(); | ||
283 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
284 | deleteEmptyClassRule.prepare(model, queryEngine); | ||
285 | |||
286 | var classModelInterpretation = model.getInterpretation(classModel); | ||
287 | var classElementInterpretation = model.getInterpretation(classElement); | ||
288 | var featureInterpretation = model.getInterpretation(feature); | ||
289 | var featuresInterpretation = model.getInterpretation(features); | ||
290 | var classesInterpretation = model.getInterpretation(classes); | ||
291 | |||
292 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
293 | var newModel = dseAdapter.createObject(); | ||
294 | var newModelId = newModel.get(0); | ||
295 | var newClass1 = dseAdapter.createObject(); | ||
296 | var newClass1Id = newClass1.get(0); | ||
297 | var newClass2 = dseAdapter.createObject(); | ||
298 | var newClass2Id = newClass2.get(0); | ||
299 | var newField = dseAdapter.createObject(); | ||
300 | var newFieldId = newField.get(0); | ||
301 | |||
302 | classModelInterpretation.put(newModel, true); | ||
303 | classElementInterpretation.put(newClass1, true); | ||
304 | classElementInterpretation.put(newClass2, true); | ||
305 | featureInterpretation.put(newField, true); | ||
306 | classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
307 | classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
308 | featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
309 | |||
310 | queryEngine.flushChanges(); | ||
311 | |||
312 | assertResults(Map.of( | ||
313 | Tuple.of(newModelId, newClass1Id), true, | ||
314 | Tuple.of(newModelId, newClass2Id), true | ||
315 | ), deleteEmptyClassRule.getAllActivationsAsSets()); | ||
316 | |||
317 | |||
318 | deleteEmptyClassRule.fireActivation(Tuple.of(0, 1)); | ||
319 | |||
320 | assertResults(Map.of( | ||
321 | Tuple.of(newModelId, newClass1Id), false, | ||
322 | Tuple.of(newModelId, newClass2Id), true | ||
323 | ), deleteEmptyClassRule.getAllActivationsAsSets()); | ||
324 | } | ||
325 | |||
326 | @Test | ||
327 | void randomFireTest() { | ||
328 | var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
329 | (builder, model, c) -> builder.clause((f) -> List.of( | ||
330 | classModelView.call(model), | ||
331 | classElementView.call(c), | ||
332 | featuresView.call(model, f), | ||
333 | not(encapsulatesView.call(c, f)) | ||
334 | ))); | ||
335 | |||
336 | TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass0", | ||
337 | deleteEmptyClassPrecondition, | ||
338 | (model) -> { | ||
339 | var classesInterpretation = model.getInterpretation(classes); | ||
340 | var classElementInterpretation = model.getInterpretation(classElement); | ||
341 | return ((Tuple activation) -> { | ||
342 | var modelElement = activation.get(0); | ||
343 | var classElement = activation.get(1); | ||
344 | |||
345 | classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
346 | classElementInterpretation.put(Tuple.of(classElement), false); | ||
347 | }); | ||
348 | }, | ||
349 | 0L); | ||
350 | |||
351 | var store = ModelStore.builder() | ||
352 | .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
353 | .with(ViatraModelQueryAdapter.builder() | ||
354 | .queries(deleteEmptyClassPrecondition)) | ||
355 | .with(DesignSpaceExplorationAdapter.builder()) | ||
356 | .build(); | ||
357 | |||
358 | var model = store.createEmptyModel(); | ||
359 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
360 | deleteEmptyClassRule.prepare(model, queryEngine); | ||
361 | |||
362 | var classModelInterpretation = model.getInterpretation(classModel); | ||
363 | var classElementInterpretation = model.getInterpretation(classElement); | ||
364 | var featureInterpretation = model.getInterpretation(feature); | ||
365 | var featuresInterpretation = model.getInterpretation(features); | ||
366 | var classesInterpretation = model.getInterpretation(classes); | ||
367 | |||
368 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
369 | var newModel = dseAdapter.createObject(); | ||
370 | var newModelId = newModel.get(0); | ||
371 | var newClass1 = dseAdapter.createObject(); | ||
372 | var newClass1Id = newClass1.get(0); | ||
373 | var newClass2 = dseAdapter.createObject(); | ||
374 | var newClass2Id = newClass2.get(0); | ||
375 | var newField = dseAdapter.createObject(); | ||
376 | var newFieldId = newField.get(0); | ||
377 | |||
378 | classModelInterpretation.put(newModel, true); | ||
379 | classElementInterpretation.put(newClass1, true); | ||
380 | classElementInterpretation.put(newClass2, true); | ||
381 | featureInterpretation.put(newField, true); | ||
382 | classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
383 | classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
384 | featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
385 | |||
386 | queryEngine.flushChanges(); | ||
387 | |||
388 | assertResults(Map.of( | ||
389 | Tuple.of(newModelId, newClass1Id), true, | ||
390 | Tuple.of(newModelId, newClass2Id), true | ||
391 | ), deleteEmptyClassRule.getAllActivationsAsSets()); | ||
392 | |||
393 | deleteEmptyClassRule.fireRandomActivation(); | ||
394 | |||
395 | assertResults(Map.of( | ||
396 | Tuple.of(newModelId, newClass1Id), true, | ||
397 | Tuple.of(newModelId, newClass2Id), false | ||
398 | ), deleteEmptyClassRule.getAllActivationsAsSets()); | ||
399 | |||
400 | deleteEmptyClassRule.fireRandomActivation(); | ||
401 | |||
402 | assertResults(Map.of( | ||
403 | Tuple.of(newModelId, newClass1Id), false, | ||
404 | Tuple.of(newModelId, newClass2Id), false | ||
405 | ), deleteEmptyClassRule.getAllActivationsAsSets()); | ||
406 | |||
407 | } | ||
408 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/tests/QueryAssertions.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/tests/QueryAssertions.java new file mode 100644 index 00000000..be514eaf --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/tests/QueryAssertions.java | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.tests; | ||
7 | |||
8 | import org.junit.jupiter.api.function.Executable; | ||
9 | import tools.refinery.store.query.resultset.ResultSet; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | |||
12 | import java.util.*; | ||
13 | |||
14 | import static org.hamcrest.MatcherAssert.assertThat; | ||
15 | import static org.hamcrest.Matchers.is; | ||
16 | import static org.hamcrest.Matchers.nullValue; | ||
17 | import static org.junit.jupiter.api.Assertions.assertAll; | ||
18 | |||
19 | public final class QueryAssertions { | ||
20 | private QueryAssertions() { | ||
21 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | ||
22 | } | ||
23 | |||
24 | public static <T> void assertNullableResults(Map<Tuple, Optional<T>> expected, ResultSet<T> resultSet) { | ||
25 | var nullableValuesMap = new LinkedHashMap<Tuple, T>(expected.size()); | ||
26 | for (var entry : expected.entrySet()) { | ||
27 | nullableValuesMap.put(entry.getKey(), entry.getValue().orElse(null)); | ||
28 | } | ||
29 | assertResults(nullableValuesMap, resultSet); | ||
30 | } | ||
31 | |||
32 | public static <T> void assertResults(Map<Tuple, T> expected, ResultSet<T> resultSet) { | ||
33 | var defaultValue = resultSet.getQuery().defaultValue(); | ||
34 | var filteredExpected = new LinkedHashMap<Tuple, T>(); | ||
35 | var executables = new ArrayList<Executable>(); | ||
36 | for (var entry : expected.entrySet()) { | ||
37 | var key = entry.getKey(); | ||
38 | var value = entry.getValue(); | ||
39 | if (!Objects.equals(value, defaultValue)) { | ||
40 | filteredExpected.put(key, value); | ||
41 | } | ||
42 | executables.add(() -> assertThat("value for key " + key,resultSet.get(key), is(value))); | ||
43 | } | ||
44 | executables.add(() -> assertThat("results size", resultSet.size(), is(filteredExpected.size()))); | ||
45 | |||
46 | var actual = new LinkedHashMap<Tuple, T>(); | ||
47 | var cursor = resultSet.getAll(); | ||
48 | while (cursor.move()) { | ||
49 | var key = cursor.getKey(); | ||
50 | var previous = actual.put(key, cursor.getValue()); | ||
51 | assertThat("duplicate value for key " + key, previous, nullValue()); | ||
52 | } | ||
53 | executables.add(() -> assertThat("results cursor", actual, is(filteredExpected))); | ||
54 | |||
55 | assertAll(executables); | ||
56 | } | ||
57 | } | ||