diff options
-rw-r--r-- | subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DesignSpaceExplorationTest.java | 382 |
1 files changed, 376 insertions, 6 deletions
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DesignSpaceExplorationTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DesignSpaceExplorationTest.java index 06e2e076..59775b43 100644 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DesignSpaceExplorationTest.java +++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DesignSpaceExplorationTest.java | |||
@@ -1,6 +1,5 @@ | |||
1 | package tools.refinery.store.query.dse; | 1 | package tools.refinery.store.query.dse; |
2 | 2 | ||
3 | import guru.nidi.graphviz.engine.Format; | ||
4 | import org.junit.jupiter.api.Test; | 3 | import org.junit.jupiter.api.Test; |
5 | import tools.refinery.store.model.ModelStore; | 4 | import tools.refinery.store.model.ModelStore; |
6 | import tools.refinery.store.query.ModelQueryAdapter; | 5 | import tools.refinery.store.query.ModelQueryAdapter; |
@@ -15,12 +14,7 @@ import tools.refinery.store.representation.Symbol; | |||
15 | import tools.refinery.store.tuple.Tuple; | 14 | import tools.refinery.store.tuple.Tuple; |
16 | import tools.refinery.visualization.ModelVisualizerAdapter; | 15 | import tools.refinery.visualization.ModelVisualizerAdapter; |
17 | 16 | ||
18 | import java.util.List; | ||
19 | import java.util.Map; | ||
20 | |||
21 | import static org.junit.jupiter.api.Assertions.assertEquals; | 17 | import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | import static tools.refinery.store.query.literal.Literals.not; | ||
23 | import static tools.refinery.store.query.viatra.tests.QueryAssertions.assertResults; | ||
24 | 18 | ||
25 | public class DesignSpaceExplorationTest { | 19 | public class DesignSpaceExplorationTest { |
26 | // private static final Symbol<Boolean> namedElement = Symbol.of("NamedElement", 1); | 20 | // private static final Symbol<Boolean> namedElement = Symbol.of("NamedElement", 1); |
@@ -114,4 +108,380 @@ public class DesignSpaceExplorationTest { | |||
114 | assertEquals(5, dseAdapter.getModelSize()); | 108 | assertEquals(5, dseAdapter.getModelSize()); |
115 | } | 109 | } |
116 | 110 | ||
111 | @Test | ||
112 | void DFSTrivialTest() { | ||
113 | var store = ModelStore.builder() | ||
114 | .symbols(classModel) | ||
115 | .with(ViatraModelQueryAdapter.builder()) | ||
116 | .with(ModelVisualizerAdapter.builder()) | ||
117 | .with(DesignSpaceExplorationAdapter.builder() | ||
118 | .strategy(new DepthFirstStrategy(0))) | ||
119 | .build(); | ||
120 | |||
121 | var model = store.createEmptyModel(); | ||
122 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
123 | |||
124 | var states = dseAdapter.explore(); | ||
125 | assertEquals(1, states.size()); | ||
126 | assertEquals(0, states.iterator().next()); | ||
127 | } | ||
128 | |||
129 | @Test | ||
130 | void DFSOneRuleTest() { | ||
131 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
132 | (builder, model) -> builder.clause( | ||
133 | classModelView.call(model) | ||
134 | )); | ||
135 | |||
136 | var createClassRule = new TransformationRule("CreateClass", | ||
137 | createClassPrecondition, | ||
138 | (model) -> { | ||
139 | var classesInterpretation = model.getInterpretation(classes); | ||
140 | var classElementInterpretation = model.getInterpretation(classElement); | ||
141 | return ((Tuple activation) -> { | ||
142 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
143 | var modelElement = activation.get(0); | ||
144 | |||
145 | var newClassElement = dseAdapter.createObject(); | ||
146 | var newClassElementId = newClassElement.get(0); | ||
147 | |||
148 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
149 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
150 | }); | ||
151 | }); | ||
152 | |||
153 | var store = ModelStore.builder() | ||
154 | .symbols(classModel, classElement, classes) | ||
155 | .with(ViatraModelQueryAdapter.builder() | ||
156 | .queries(createClassPrecondition)) | ||
157 | .with(ModelVisualizerAdapter.builder()) | ||
158 | .with(DesignSpaceExplorationAdapter.builder() | ||
159 | .transformations(createClassRule) | ||
160 | .strategy(new DepthFirstStrategy(4) | ||
161 | )) | ||
162 | .build(); | ||
163 | |||
164 | var model = store.createEmptyModel(); | ||
165 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
166 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
167 | |||
168 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
169 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
170 | queryEngine.flushChanges(); | ||
171 | |||
172 | var states = dseAdapter.explore(); | ||
173 | assertEquals(1, states.size()); | ||
174 | assertEquals(0, states.iterator().next()); | ||
175 | } | ||
176 | |||
177 | @Test | ||
178 | void DFSContinueTest() { | ||
179 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
180 | (builder, model) -> builder.clause( | ||
181 | classModelView.call(model) | ||
182 | )); | ||
183 | |||
184 | var createClassRule = new TransformationRule("CreateClass", | ||
185 | createClassPrecondition, | ||
186 | (model) -> { | ||
187 | var classesInterpretation = model.getInterpretation(classes); | ||
188 | var classElementInterpretation = model.getInterpretation(classElement); | ||
189 | return ((Tuple activation) -> { | ||
190 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
191 | var modelElement = activation.get(0); | ||
192 | |||
193 | var newClassElement = dseAdapter.createObject(); | ||
194 | var newClassElementId = newClassElement.get(0); | ||
195 | |||
196 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
197 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
198 | }); | ||
199 | }); | ||
200 | |||
201 | var store = ModelStore.builder() | ||
202 | .symbols(classModel, classElement, classes) | ||
203 | .with(ViatraModelQueryAdapter.builder() | ||
204 | .queries(createClassPrecondition)) | ||
205 | .with(ModelVisualizerAdapter.builder()) | ||
206 | .with(DesignSpaceExplorationAdapter.builder() | ||
207 | .transformations(createClassRule) | ||
208 | .strategy(new DepthFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
209 | )) | ||
210 | .build(); | ||
211 | |||
212 | var model = store.createEmptyModel(); | ||
213 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
214 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
215 | |||
216 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
217 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
218 | queryEngine.flushChanges(); | ||
219 | |||
220 | var states = dseAdapter.explore(); | ||
221 | var iterator = states.iterator(); | ||
222 | assertEquals(5, states.size()); | ||
223 | assertEquals(0, iterator.next()); | ||
224 | assertEquals(1, iterator.next()); | ||
225 | assertEquals(2, iterator.next()); | ||
226 | assertEquals(3, iterator.next()); | ||
227 | assertEquals(4, iterator.next()); | ||
228 | } | ||
229 | |||
230 | @Test | ||
231 | void DFSCompletenessTest() { | ||
232 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
233 | (builder, model) -> builder.clause( | ||
234 | classModelView.call(model) | ||
235 | )); | ||
236 | |||
237 | var createClassRule = new TransformationRule("CreateClass", | ||
238 | createClassPrecondition, | ||
239 | (model) -> { | ||
240 | var classesInterpretation = model.getInterpretation(classes); | ||
241 | var classElementInterpretation = model.getInterpretation(classElement); | ||
242 | return ((Tuple activation) -> { | ||
243 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
244 | var modelElement = activation.get(0); | ||
245 | |||
246 | var newClassElement = dseAdapter.createObject(); | ||
247 | var newClassElementId = newClassElement.get(0); | ||
248 | |||
249 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
250 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
251 | }); | ||
252 | }); | ||
253 | |||
254 | var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
255 | (builder, model) -> builder.clause( | ||
256 | classModelView.call(model) | ||
257 | )); | ||
258 | |||
259 | var createFeatureRule = new TransformationRule("CreateFeature", | ||
260 | createFeaturePrecondition, | ||
261 | (model) -> { | ||
262 | var featuresInterpretation = model.getInterpretation(features); | ||
263 | var featureInterpretation = model.getInterpretation(feature); | ||
264 | return ((Tuple activation) -> { | ||
265 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
266 | var modelElement = activation.get(0); | ||
267 | |||
268 | var newClassElement = dseAdapter.createObject(); | ||
269 | var newClassElementId = newClassElement.get(0); | ||
270 | |||
271 | featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
272 | featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
273 | }); | ||
274 | }); | ||
275 | |||
276 | var store = ModelStore.builder() | ||
277 | .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
278 | .with(ViatraModelQueryAdapter.builder() | ||
279 | .queries(createClassPrecondition, createFeaturePrecondition)) | ||
280 | .with(ModelVisualizerAdapter.builder()) | ||
281 | .with(DesignSpaceExplorationAdapter.builder() | ||
282 | .transformations(createClassRule, createFeatureRule) | ||
283 | .strategy(new DepthFirstStrategy(10).continueIfHardObjectivesFulfilled() | ||
284 | )) | ||
285 | .build(); | ||
286 | |||
287 | var model = store.createEmptyModel(); | ||
288 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
289 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
290 | |||
291 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
292 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
293 | queryEngine.flushChanges(); | ||
294 | |||
295 | var states = dseAdapter.explore(); | ||
296 | assertEquals(2047, states.size()); | ||
297 | } | ||
298 | |||
299 | @Test | ||
300 | void BFSTrivialTest() { | ||
301 | var store = ModelStore.builder() | ||
302 | .symbols(classModel) | ||
303 | .with(ViatraModelQueryAdapter.builder()) | ||
304 | .with(ModelVisualizerAdapter.builder()) | ||
305 | .with(DesignSpaceExplorationAdapter.builder() | ||
306 | .strategy(new BestFirstStrategy(0))) | ||
307 | .build(); | ||
308 | |||
309 | var model = store.createEmptyModel(); | ||
310 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
311 | |||
312 | var states = dseAdapter.explore(); | ||
313 | assertEquals(1, states.size()); | ||
314 | assertEquals(0, states.iterator().next()); | ||
315 | } | ||
316 | |||
317 | @Test | ||
318 | void BFSOneRuleTest() { | ||
319 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
320 | (builder, model) -> builder.clause( | ||
321 | classModelView.call(model) | ||
322 | )); | ||
323 | |||
324 | var createClassRule = new TransformationRule("CreateClass", | ||
325 | createClassPrecondition, | ||
326 | (model) -> { | ||
327 | var classesInterpretation = model.getInterpretation(classes); | ||
328 | var classElementInterpretation = model.getInterpretation(classElement); | ||
329 | return ((Tuple activation) -> { | ||
330 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
331 | var modelElement = activation.get(0); | ||
332 | |||
333 | var newClassElement = dseAdapter.createObject(); | ||
334 | var newClassElementId = newClassElement.get(0); | ||
335 | |||
336 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
337 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
338 | }); | ||
339 | }); | ||
340 | |||
341 | var store = ModelStore.builder() | ||
342 | .symbols(classModel, classElement, classes) | ||
343 | .with(ViatraModelQueryAdapter.builder() | ||
344 | .queries(createClassPrecondition)) | ||
345 | .with(ModelVisualizerAdapter.builder()) | ||
346 | .with(DesignSpaceExplorationAdapter.builder() | ||
347 | .transformations(createClassRule) | ||
348 | .strategy(new BestFirstStrategy(4) | ||
349 | )) | ||
350 | .build(); | ||
351 | |||
352 | var model = store.createEmptyModel(); | ||
353 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
354 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
355 | |||
356 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
357 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
358 | queryEngine.flushChanges(); | ||
359 | |||
360 | var states = dseAdapter.explore(); | ||
361 | assertEquals(1, states.size()); | ||
362 | assertEquals(0, states.iterator().next()); | ||
363 | } | ||
364 | |||
365 | @Test | ||
366 | void BFSContinueTest() { | ||
367 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
368 | (builder, model) -> builder.clause( | ||
369 | classModelView.call(model) | ||
370 | )); | ||
371 | |||
372 | var createClassRule = new TransformationRule("CreateClass", | ||
373 | createClassPrecondition, | ||
374 | (model) -> { | ||
375 | var classesInterpretation = model.getInterpretation(classes); | ||
376 | var classElementInterpretation = model.getInterpretation(classElement); | ||
377 | return ((Tuple activation) -> { | ||
378 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
379 | var modelElement = activation.get(0); | ||
380 | |||
381 | var newClassElement = dseAdapter.createObject(); | ||
382 | var newClassElementId = newClassElement.get(0); | ||
383 | |||
384 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
385 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
386 | }); | ||
387 | }); | ||
388 | |||
389 | var store = ModelStore.builder() | ||
390 | .symbols(classModel, classElement, classes) | ||
391 | .with(ViatraModelQueryAdapter.builder() | ||
392 | .queries(createClassPrecondition)) | ||
393 | .with(ModelVisualizerAdapter.builder()) | ||
394 | .with(DesignSpaceExplorationAdapter.builder() | ||
395 | .transformations(createClassRule) | ||
396 | .strategy(new BestFirstStrategy(4).continueIfHardObjectivesFulfilled() | ||
397 | )) | ||
398 | .build(); | ||
399 | |||
400 | var model = store.createEmptyModel(); | ||
401 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
402 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
403 | |||
404 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
405 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
406 | queryEngine.flushChanges(); | ||
407 | |||
408 | var states = dseAdapter.explore(); | ||
409 | var iterator = states.iterator(); | ||
410 | assertEquals(5, states.size()); | ||
411 | assertEquals(0, iterator.next()); | ||
412 | assertEquals(1, iterator.next()); | ||
413 | assertEquals(2, iterator.next()); | ||
414 | assertEquals(3, iterator.next()); | ||
415 | assertEquals(4, iterator.next()); | ||
416 | } | ||
417 | |||
418 | @Test | ||
419 | void BFSCompletenessTest() { | ||
420 | var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
421 | (builder, model) -> builder.clause( | ||
422 | classModelView.call(model) | ||
423 | )); | ||
424 | |||
425 | var createClassRule = new TransformationRule("CreateClass", | ||
426 | createClassPrecondition, | ||
427 | (model) -> { | ||
428 | var classesInterpretation = model.getInterpretation(classes); | ||
429 | var classElementInterpretation = model.getInterpretation(classElement); | ||
430 | return ((Tuple activation) -> { | ||
431 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
432 | var modelElement = activation.get(0); | ||
433 | |||
434 | var newClassElement = dseAdapter.createObject(); | ||
435 | var newClassElementId = newClassElement.get(0); | ||
436 | |||
437 | classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
438 | classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
439 | }); | ||
440 | }); | ||
441 | |||
442 | var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
443 | (builder, model) -> builder.clause( | ||
444 | classModelView.call(model) | ||
445 | )); | ||
446 | |||
447 | var createFeatureRule = new TransformationRule("CreateFeature", | ||
448 | createFeaturePrecondition, | ||
449 | (model) -> { | ||
450 | var featuresInterpretation = model.getInterpretation(features); | ||
451 | var featureInterpretation = model.getInterpretation(feature); | ||
452 | return ((Tuple activation) -> { | ||
453 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
454 | var modelElement = activation.get(0); | ||
455 | |||
456 | var newClassElement = dseAdapter.createObject(); | ||
457 | var newClassElementId = newClassElement.get(0); | ||
458 | |||
459 | featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
460 | featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
461 | }); | ||
462 | }); | ||
463 | |||
464 | var store = ModelStore.builder() | ||
465 | .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
466 | .with(ViatraModelQueryAdapter.builder() | ||
467 | .queries(createClassPrecondition, createFeaturePrecondition)) | ||
468 | .with(ModelVisualizerAdapter.builder()) | ||
469 | .with(DesignSpaceExplorationAdapter.builder() | ||
470 | .transformations(createClassRule, createFeatureRule) | ||
471 | .strategy(new BestFirstStrategy(10).continueIfHardObjectivesFulfilled() | ||
472 | )) | ||
473 | .build(); | ||
474 | |||
475 | var model = store.createEmptyModel(); | ||
476 | var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
477 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
478 | |||
479 | var modelElementInterpretation = model.getInterpretation(classModel); | ||
480 | modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
481 | queryEngine.flushChanges(); | ||
482 | |||
483 | var states = dseAdapter.explore(); | ||
484 | assertEquals(2047, states.size()); | ||
485 | } | ||
486 | |||
117 | } | 487 | } |