aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects
diff options
context:
space:
mode:
authorLibravatar nagilooh <ficsorattila96@gmail.com>2023-07-25 21:55:57 +0200
committerLibravatar nagilooh <ficsorattila96@gmail.com>2023-08-02 12:09:14 +0200
commit4a528d6d5b5502c81a1cd094fcf9e706062b982c (patch)
tree21fadb0fc55fe5a6ee6ee5009e0bb02daa3489c2 /subprojects
parentFix issue with storing trajectory (diff)
downloadrefinery-4a528d6d5b5502c81a1cd094fcf9e706062b982c.tar.gz
refinery-4a528d6d5b5502c81a1cd094fcf9e706062b982c.tar.zst
refinery-4a528d6d5b5502c81a1cd094fcf9e706062b982c.zip
Add new test files
Diffstat (limited to 'subprojects')
-rw-r--r--subprojects/store-query-viatra/build.gradle.kts2
-rw-r--r--subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/CRAExamplesTest.java135
-rw-r--r--subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DebugTest.java114
3 files changed, 251 insertions, 0 deletions
diff --git a/subprojects/store-query-viatra/build.gradle.kts b/subprojects/store-query-viatra/build.gradle.kts
index 85ea7e2e..1f804247 100644
--- a/subprojects/store-query-viatra/build.gradle.kts
+++ b/subprojects/store-query-viatra/build.gradle.kts
@@ -10,7 +10,9 @@ plugins {
10 10
11dependencies { 11dependencies {
12 implementation(libs.ecore) 12 implementation(libs.ecore)
13 implementation ("guru.nidi:graphviz-java:0.18.1")
13 api(libs.viatra) 14 api(libs.viatra)
14 api(project(":refinery-store-query")) 15 api(project(":refinery-store-query"))
15 api(project(":refinery-store-reasoning")) 16 api(project(":refinery-store-reasoning"))
17 api(project(":refinery-visualization"))
16} 18}
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/CRAExamplesTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/CRAExamplesTest.java
new file mode 100644
index 00000000..8fe50a42
--- /dev/null
+++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/CRAExamplesTest.java
@@ -0,0 +1,135 @@
1package tools.refinery.store.query.dse;
2
3import tools.refinery.store.query.dnf.Query;
4import tools.refinery.store.query.dnf.RelationalQuery;
5import tools.refinery.store.query.dse.internal.TransformationRule;
6import tools.refinery.store.query.view.AnySymbolView;
7import tools.refinery.store.query.view.KeyOnlyView;
8import tools.refinery.store.representation.Symbol;
9import tools.refinery.store.tuple.Tuple;
10
11import java.util.List;
12
13import static tools.refinery.store.query.literal.Literals.not;
14
15public class CRAExamplesTest {
16 private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1);
17 private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1);
18 private static final Symbol<Boolean> feature = Symbol.of("Feature", 1);
19
20 private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2);
21 private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2);
22
23 private static final Symbol<Boolean> features = Symbol.of("Features", 2);
24 private static final Symbol<Boolean> classes = Symbol.of("Classes", 2);
25
26 private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel);
27 private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement);
28 private static final AnySymbolView featureView = new KeyOnlyView<>(feature);
29 private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy);
30 private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates);
31 private static final AnySymbolView featuresView = new KeyOnlyView<>(features);
32 private static final AnySymbolView classesView = new KeyOnlyView<>(classes);
33
34 /*Example Transformation rules*/
35 private static final RelationalQuery assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper",
36 (builder, model, c, f) -> builder.clause(
37 classElementView.call(c),
38 classesView.call(model, c),
39 encapsulatesView.call(c, f)
40 ));
41
42 private static final RelationalQuery assignFeaturePrecondition = Query.of("AssignFeaturePrecondition", (builder, c2, f)
43 -> builder.clause((model, c1) -> List.of(
44 classModelView.call(model),
45 featureView.call(f),
46 classElementView.call(c2),
47 featuresView.call(model, f),
48 classesView.call(model, c1),
49 not(assignFeaturePreconditionHelper.call(model, c2, f)),
50 not(encapsulatesView.call(c2, f))
51 )));
52
53 private static final RelationalQuery deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition",
54 (builder, model, c) -> builder.clause((f) -> List.of(
55 classModelView.call(model),
56 classElementView.call(c),
57 featuresView.call(model, f),
58 not(encapsulatesView.call(c, f))
59 )));
60
61 private static final RelationalQuery createClassPreconditionHelper = Query.of("CreateClassPreconditionHelper",
62 (builder, model, f, c) -> builder.clause(
63 classElementView.call(c),
64 classesView.call(model, c),
65 encapsulatesView.call(c, f)
66 ));
67
68 private static final RelationalQuery createClassPrecondition = Query.of("CreateClassPrecondition",
69 (builder, model, f) -> builder.clause((c) -> List.of(
70 classModelView.call(model),
71 featureView.call(f),
72 not(createClassPreconditionHelper.call(model, f, c))
73 )));
74
75 private static final RelationalQuery moveFeature = Query.of("MoveFeature",
76 (builder, c1, c2, f) -> builder.clause((model) -> List.of(
77 classModelView.call(model),
78 classElementView.call(c1),
79 classElementView.call(c2),
80 featureView.call(f),
81 classesView.call(model, c1),
82 classesView.call(model, c2),
83 featuresView.call(model, f),
84 encapsulatesView.call(c1, f)
85 )));
86
87 private static final TransformationRule assignFeatureRule = new TransformationRule("AssignFeature",
88 assignFeaturePrecondition,
89 (model) -> {
90 var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy);
91 return ((Tuple activation) -> {
92 var feature = activation.get(0);
93 var classElement = activation.get(1);
94
95 isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true);
96 });
97 });
98
99 private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass",
100 deleteEmptyClassPrecondition,
101 (model) -> {
102 var classesInterpretation = model.getInterpretation(classes);
103 var classElementInterpretation = model.getInterpretation(classElement);
104 var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
105 return ((Tuple activation) -> {
106 var modelElement = activation.get(0);
107 var classElement = activation.get(1);
108
109 classesInterpretation.put(Tuple.of(modelElement, classElement), false);
110 classElementInterpretation.put(Tuple.of(classElement), false);
111 dseAdapter.deleteObject(Tuple.of(classElement));
112 });
113 });
114
115 private static final TransformationRule createClassRule = new TransformationRule("CreateClass",
116 createClassPrecondition,
117 (model) -> {
118 var adapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
119 var classElementInterpretation = model.getInterpretation(classElement);
120 var classesInterpretation = model.getInterpretation(classes);
121 var encapsulatesInterpretation = model.getInterpretation(encapsulates);
122 var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
123 return ((Tuple activation) -> {
124 var modelElement = activation.get(0);
125 var feature = activation.get(1);
126
127 var newClassElement = dseAdapter.createObject();
128 var newClassElementId = newClassElement.get(0);
129 classElementInterpretation.put(newClassElement, true);
130 classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
131 encapsulatesInterpretation.put(Tuple.of(newClassElementId, feature), true);
132 });
133 });
134
135}
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DebugTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DebugTest.java
new file mode 100644
index 00000000..969afbcb
--- /dev/null
+++ b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/dse/DebugTest.java
@@ -0,0 +1,114 @@
1package tools.refinery.store.query.dse;
2
3import org.junit.jupiter.api.Test;
4import tools.refinery.store.model.ModelStore;
5import tools.refinery.store.query.ModelQueryAdapter;
6import tools.refinery.store.query.dnf.Query;
7import tools.refinery.store.query.dse.internal.TransformationRule;
8import tools.refinery.store.query.dse.strategy.DepthFirstStrategy;
9import tools.refinery.store.query.viatra.ViatraModelQueryAdapter;
10import tools.refinery.store.query.view.AnySymbolView;
11import tools.refinery.store.query.view.KeyOnlyView;
12import tools.refinery.store.representation.Symbol;
13import tools.refinery.store.tuple.Tuple;
14import tools.refinery.visualization.ModelVisualizerAdapter;
15
16public class DebugTest {
17 private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1);
18 private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1);
19 private static final Symbol<Boolean> feature = Symbol.of("Feature", 1);
20
21 private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2);
22 private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2);
23
24 private static final Symbol<Boolean> features = Symbol.of("Features", 2);
25 private static final Symbol<Boolean> classes = Symbol.of("Classes", 2);
26
27 private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel);
28 private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement);
29 private static final AnySymbolView featureView = new KeyOnlyView<>(feature);
30 private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy);
31 private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates);
32 private static final AnySymbolView featuresView = new KeyOnlyView<>(features);
33 private static final AnySymbolView classesView = new KeyOnlyView<>(classes);
34
35
36 @Test
37 void BFSTest() {
38 var createClassPrecondition = Query.of("CreateClassPrecondition",
39 (builder, model) -> builder.clause(
40 classModelView.call(model)
41 ));
42
43 var createClassRule = new TransformationRule("CreateClass",
44 createClassPrecondition,
45 (model) -> {
46 var classesInterpretation = model.getInterpretation(classes);
47 var classElementInterpretation = model.getInterpretation(classElement);
48 return ((Tuple activation) -> {
49 var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
50 var modelElement = activation.get(0);
51
52 var newClassElement = dseAdapter.createObject();
53 var newClassElementId = newClassElement.get(0);
54
55 classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
56 classElementInterpretation.put(Tuple.of(newClassElementId), true);
57 });
58 });
59
60 var createFeaturePrecondition = Query.of("CreateFeaturePrecondition",
61 (builder, model) -> builder.clause(
62 classModelView.call(model)
63 ));
64
65 var createFeatureRule = new TransformationRule("CreateFeature",
66 createFeaturePrecondition,
67 (model) -> {
68 var featuresInterpretation = model.getInterpretation(features);
69 var featureInterpretation = model.getInterpretation(feature);
70 return ((Tuple activation) -> {
71 var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
72 var modelElement = activation.get(0);
73
74 var newClassElement = dseAdapter.createObject();
75 var newClassElementId = newClassElement.get(0);
76
77 featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
78 featureInterpretation.put(Tuple.of(newClassElementId), true);
79 });
80 });
81
82 var store = ModelStore.builder()
83 .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features)
84 .with(ViatraModelQueryAdapter.builder()
85 .queries(createClassPrecondition, createFeaturePrecondition))
86 .with(DesignSpaceExplorationAdapter.builder()
87 .transformations(createClassRule, createFeatureRule)
88 .strategy(new DepthFirstStrategy(4).continueIfHardObjectivesFulfilled()
89// .strategy(new BestFirstStrategy(4).continueIfHardObjectivesFulfilled()
90// .goOnOnlyIfFitnessIsBetter()
91 ))
92 .with(ModelVisualizerAdapter.builder())
93 .build();
94
95 var model = store.createEmptyModel();
96 var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
97 var queryEngine = model.getAdapter(ModelQueryAdapter.class);
98
99 var modelElementInterpretation = model.getInterpretation(classModel);
100 modelElementInterpretation.put(dseAdapter.createObject(), true);
101 queryEngine.flushChanges();
102
103
104 var states = dseAdapter.explore();
105 var visualizer = model.getAdapter(ModelVisualizerAdapter.class);
106 for (var state : states) {
107 var visualization = visualizer.createVisualizationForModelState(state);
108 visualizer.saveVisualization(visualization, "test_output" + state.hashCode() + ".png");
109 }
110 System.out.println("states size: " + states.size());
111 System.out.println("states: " + states);
112
113 }
114}