diff options
Diffstat (limited to 'subprojects/store-dse/src/test/java/tools')
6 files changed, 1532 insertions, 0 deletions
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..36517709 --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java | |||
@@ -0,0 +1,277 @@ | |||
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.dse.transition.TransformationRule; | ||
9 | import tools.refinery.store.query.dnf.Query; | ||
10 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
11 | import tools.refinery.store.query.view.AnySymbolView; | ||
12 | import tools.refinery.store.query.view.KeyOnlyView; | ||
13 | import tools.refinery.store.representation.Symbol; | ||
14 | import tools.refinery.store.tuple.Tuple; | ||
15 | |||
16 | import java.util.List; | ||
17 | |||
18 | import static tools.refinery.store.query.literal.Literals.not; | ||
19 | |||
20 | class CRAExamplesTest { | ||
21 | |||
22 | private static final Symbol<String> name = Symbol.of("Name", 1, String.class); | ||
23 | |||
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 | private static final Symbol<Boolean> attribute = Symbol.of("Attribute", 1); | ||
28 | private static final Symbol<Boolean> method = Symbol.of("Method", 1); | ||
29 | |||
30 | // private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
31 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
32 | private static final Symbol<Boolean> dataDependency = Symbol.of("DataDependency", 2); | ||
33 | private static final Symbol<Boolean> functionalDependency = Symbol.of("FunctionalDependency", 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 attributeView = new KeyOnlyView<>(attribute); | ||
42 | private static final AnySymbolView methodView = new KeyOnlyView<>(method); | ||
43 | // private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
44 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
45 | private static final AnySymbolView dataDependencyView = new KeyOnlyView<>(dataDependency); | ||
46 | private static final AnySymbolView functionalDependencyView = new KeyOnlyView<>(functionalDependency); | ||
47 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
48 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
49 | |||
50 | /*Example Transformation rules*/ | ||
51 | private static final RelationalQuery feature = Query.of("Feature", | ||
52 | (builder, f) -> builder.clause( | ||
53 | attributeView.call(f)) | ||
54 | .clause( | ||
55 | methodView.call(f)) | ||
56 | ); | ||
57 | |||
58 | private static final RelationalQuery assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper", | ||
59 | (builder, c, f) -> builder.clause( | ||
60 | classElementView.call(c), | ||
61 | // classesView.call(model, c), | ||
62 | encapsulatesView.call(c, f) | ||
63 | )); | ||
64 | |||
65 | private static final RelationalQuery assignFeaturePrecondition = Query.of("AssignFeaturePrecondition", | ||
66 | (builder, f, c1) -> builder.clause((c2) -> List.of( | ||
67 | // classModelView.call(model), | ||
68 | feature.call(f), | ||
69 | classElementView.call(c1), | ||
70 | // featuresView.call(model, f), | ||
71 | not(assignFeaturePreconditionHelper.call(c2, f)), | ||
72 | not(encapsulatesView.call(c1, f)) | ||
73 | ))); | ||
74 | |||
75 | private static final RelationalQuery deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
76 | (builder, c) -> builder.clause((f) -> List.of( | ||
77 | // classModelView.call(model), | ||
78 | classElementView.call(c), | ||
79 | // featuresView.call(model, f), | ||
80 | not(encapsulatesView.call(c, f)) | ||
81 | ))); | ||
82 | |||
83 | private static final RelationalQuery createClassPreconditionHelper = Query.of("CreateClassPreconditionHelper", | ||
84 | (builder, f, c) -> builder.clause( | ||
85 | classElementView.call(c), | ||
86 | // classesView.call(model, c), | ||
87 | encapsulatesView.call(c, f) | ||
88 | )); | ||
89 | |||
90 | private static final RelationalQuery createClassPrecondition = Query.of("CreateClassPrecondition", | ||
91 | (builder, f) -> builder.clause((c) -> List.of( | ||
92 | // classModelView.call(model), | ||
93 | feature.call(f), | ||
94 | not(createClassPreconditionHelper.call(f, c)) | ||
95 | ))); | ||
96 | |||
97 | private static final RelationalQuery moveFeaturePrecondition = Query.of("MoveFeature", | ||
98 | (builder, c1, c2, f) -> builder.clause( | ||
99 | // classModelView.call(model), | ||
100 | classElementView.call(c1), | ||
101 | classElementView.call(c2), | ||
102 | c1.notEquivalent(c2), | ||
103 | feature.call(f), | ||
104 | // classesView.call(model, c1), | ||
105 | // classesView.call(model, c2), | ||
106 | // featuresView.call(model, f), | ||
107 | encapsulatesView.call(c1, f) | ||
108 | )); | ||
109 | |||
110 | private static final TransformationRule assignFeatureRule = new TransformationRule("AssignFeature", | ||
111 | assignFeaturePrecondition, | ||
112 | (model) -> { | ||
113 | // var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); | ||
114 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
115 | return ((Tuple activation) -> { | ||
116 | var feature = activation.get(0); | ||
117 | var classElement = activation.get(1); | ||
118 | |||
119 | // isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); | ||
120 | encapsulatesInterpretation.put(Tuple.of(classElement, feature), true); | ||
121 | }); | ||
122 | }); | ||
123 | |||
124 | // private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
125 | // deleteEmptyClassPrecondition, | ||
126 | // (model) -> { | ||
127 | //// var classesInterpretation = model.getInterpretation(classes); | ||
128 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
129 | // return ((Tuple activation) -> { | ||
130 | // // TODO: can we move dseAdapter outside? | ||
131 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
132 | //// var modelElement = activation.get(0); | ||
133 | // var classElement = activation.get(0); | ||
134 | // | ||
135 | //// classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
136 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
137 | // dseAdapter.deleteObject(Tuple.of(classElement)); | ||
138 | // }); | ||
139 | // }); | ||
140 | |||
141 | // private static final TransformationRule createClassRule = new TransformationRule("CreateClass", | ||
142 | // createClassPrecondition, | ||
143 | // (model) -> { | ||
144 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
145 | //// var classesInterpretation = model.getInterpretation(classes); | ||
146 | // var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
147 | // return ((Tuple activation) -> { | ||
148 | // // TODO: can we move dseAdapter outside? | ||
149 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
150 | //// var modelElement = activation.get(0); | ||
151 | // var feature = activation.get(0); | ||
152 | // | ||
153 | // var newClassElement = dseAdapter.createObject(); | ||
154 | // var newClassElementId = newClassElement.get(0); | ||
155 | // classElementInterpretation.put(newClassElement, true); | ||
156 | //// classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
157 | // encapsulatesInterpretation.put(Tuple.of(newClassElementId, feature), true); | ||
158 | // }); | ||
159 | // }); | ||
160 | |||
161 | private static final TransformationRule moveFeatureRule = new TransformationRule("MoveFeature", | ||
162 | moveFeaturePrecondition, | ||
163 | (model) -> { | ||
164 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | ||
165 | return ((Tuple activation) -> { | ||
166 | var classElement1 = activation.get(0); | ||
167 | var classElement2 = activation.get(1); | ||
168 | var feature = activation.get(2); | ||
169 | |||
170 | encapsulatesInterpretation.put(Tuple.of(classElement1, feature), false); | ||
171 | encapsulatesInterpretation.put(Tuple.of(classElement2, feature), true); | ||
172 | }); | ||
173 | }); | ||
174 | |||
175 | // @Test | ||
176 | // @Disabled("This test is only for debugging purposes") | ||
177 | // void craTest() { | ||
178 | // var store = ModelStore.builder() | ||
179 | // .symbols(classElement, encapsulates, classes, features, attribute, method, dataDependency, | ||
180 | // functionalDependency, name) | ||
181 | // .with(ViatraModelQueryAdapter.builder() | ||
182 | // .queries(feature, assignFeaturePreconditionHelper, assignFeaturePrecondition, | ||
183 | // deleteEmptyClassPrecondition, createClassPreconditionHelper, createClassPrecondition, | ||
184 | // moveFeaturePrecondition)) | ||
185 | // .with(ModelVisualizerAdapter.builder() | ||
186 | // .withOutputpath("test_output") | ||
187 | // .withFormat(FileFormat.DOT) | ||
188 | // .withFormat(FileFormat.SVG) | ||
189 | // .saveStates() | ||
190 | // .saveDesignSpace() | ||
191 | // ) | ||
192 | // .with(DesignSpaceExplorationAdapter.builder() | ||
193 | // .transformations(assignFeatureRule, deleteEmptyClassRule, createClassRule, moveFeatureRule) | ||
194 | // .objectives(new AlwaysSatisfiedRandomHardObjective()) | ||
195 | //// .strategy(new DepthFirstStrategy().withDepthLimit(3).continueIfHardObjectivesFulfilled() | ||
196 | // .strategy(new BestFirstStrategy().withDepthLimit(6).continueIfHardObjectivesFulfilled() | ||
197 | //// .goOnOnlyIfFitnessIsBetter() | ||
198 | // )) | ||
199 | // .build(); | ||
200 | // | ||
201 | // var model = store.createEmptyModel(); | ||
202 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
203 | //// dseAdapter.setRandom(1); | ||
204 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
205 | // | ||
206 | //// var modelInterpretation = model.getInterpretation(classModel); | ||
207 | // var nameInterpretation = model.getInterpretation(name); | ||
208 | // var methodInterpretation = model.getInterpretation(method); | ||
209 | // var attributeInterpretation = model.getInterpretation(attribute); | ||
210 | // var dataDependencyInterpretation = model.getInterpretation(dataDependency); | ||
211 | // var functionalDependencyInterpretation = model.getInterpretation(functionalDependency); | ||
212 | // | ||
213 | //// var modelElement = dseAdapter.createObject(); | ||
214 | // var method1 = dseAdapter.createObject(); | ||
215 | // var method1Id = method1.get(0); | ||
216 | // var method2 = dseAdapter.createObject(); | ||
217 | // var method2Id = method2.get(0); | ||
218 | // var method3 = dseAdapter.createObject(); | ||
219 | // var method3Id = method3.get(0); | ||
220 | // var method4 = dseAdapter.createObject(); | ||
221 | // var method4Id = method4.get(0); | ||
222 | // var attribute1 = dseAdapter.createObject(); | ||
223 | // var attribute1Id = attribute1.get(0); | ||
224 | // var attribute2 = dseAdapter.createObject(); | ||
225 | // var attribute2Id = attribute2.get(0); | ||
226 | // var attribute3 = dseAdapter.createObject(); | ||
227 | // var attribute3Id = attribute3.get(0); | ||
228 | // var attribute4 = dseAdapter.createObject(); | ||
229 | // var attribute4Id = attribute4.get(0); | ||
230 | // var attribute5 = dseAdapter.createObject(); | ||
231 | // var attribute5Id = attribute5.get(0); | ||
232 | // | ||
233 | // nameInterpretation.put(method1, "M1"); | ||
234 | // nameInterpretation.put(method2, "M2"); | ||
235 | // nameInterpretation.put(method3, "M3"); | ||
236 | // nameInterpretation.put(method4, "M4"); | ||
237 | // nameInterpretation.put(attribute1, "A1"); | ||
238 | // nameInterpretation.put(attribute2, "A2"); | ||
239 | // nameInterpretation.put(attribute3, "A3"); | ||
240 | // nameInterpretation.put(attribute4, "A4"); | ||
241 | // nameInterpretation.put(attribute5, "A5"); | ||
242 | // | ||
243 | // | ||
244 | // | ||
245 | //// modelInterpretation.put(modelElement, true); | ||
246 | // methodInterpretation.put(method1, true); | ||
247 | // methodInterpretation.put(method2, true); | ||
248 | // methodInterpretation.put(method3, true); | ||
249 | // methodInterpretation.put(method4, true); | ||
250 | // attributeInterpretation.put(attribute1, true); | ||
251 | // attributeInterpretation.put(attribute2, true); | ||
252 | // attributeInterpretation.put(attribute3, true); | ||
253 | // attributeInterpretation.put(attribute4, true); | ||
254 | // attributeInterpretation.put(attribute5, true); | ||
255 | // | ||
256 | // dataDependencyInterpretation.put(Tuple.of(method1Id, attribute1Id), true); | ||
257 | // dataDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true); | ||
258 | // dataDependencyInterpretation.put(Tuple.of(method2Id, attribute2Id), true); | ||
259 | // dataDependencyInterpretation.put(Tuple.of(method3Id, attribute3Id), true); | ||
260 | // dataDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true); | ||
261 | // dataDependencyInterpretation.put(Tuple.of(method4Id, attribute3Id), true); | ||
262 | // dataDependencyInterpretation.put(Tuple.of(method4Id, attribute5Id), true); | ||
263 | // | ||
264 | // functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true); | ||
265 | // functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute4Id), true); | ||
266 | // functionalDependencyInterpretation.put(Tuple.of(method2Id, attribute1Id), true); | ||
267 | // functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute1Id), true); | ||
268 | // functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true); | ||
269 | // functionalDependencyInterpretation.put(Tuple.of(method4Id, attribute2Id), true); | ||
270 | // | ||
271 | // queryEngine.flushChanges(); | ||
272 | // | ||
273 | // var states = dseAdapter.explore(); | ||
274 | // System.out.println("states size: " + states.size()); | ||
275 | // } | ||
276 | //*/ | ||
277 | } | ||
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..1d757a5f --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DebugTest.java | |||
@@ -0,0 +1,116 @@ | |||
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.query.view.AnySymbolView; | ||
9 | import tools.refinery.store.query.view.KeyOnlyView; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | |||
12 | class DebugTest { | ||
13 | private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
14 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
15 | private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
16 | |||
17 | private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
18 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
19 | |||
20 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
21 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
22 | |||
23 | private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
24 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
25 | private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
26 | private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
27 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
28 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
29 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
30 | |||
31 | |||
32 | // @Test | ||
33 | // @Disabled("This test is only for debugging purposes") | ||
34 | // void BFSTest() { | ||
35 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
36 | // (builder, model) -> builder.clause( | ||
37 | // classModelView.call(model) | ||
38 | // )); | ||
39 | // | ||
40 | // var createClassRule = new TransformationRule("CreateClass", | ||
41 | // createClassPrecondition, | ||
42 | // (model) -> { | ||
43 | // var classesInterpretation = model.getInterpretation(classes); | ||
44 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
45 | // return ((Tuple activation) -> { | ||
46 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
47 | // var modelElement = activation.get(0); | ||
48 | // | ||
49 | // var newClassElement = dseAdapter.createObject(); | ||
50 | // var newClassElementId = newClassElement.get(0); | ||
51 | // | ||
52 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
53 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
54 | // }); | ||
55 | // }); | ||
56 | // | ||
57 | // var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
58 | // (builder, model) -> builder.clause( | ||
59 | // classModelView.call(model) | ||
60 | // )); | ||
61 | // | ||
62 | // var createFeatureRule = new TransformationRule("CreateFeature", | ||
63 | // createFeaturePrecondition, | ||
64 | // (model) -> { | ||
65 | // var featuresInterpretation = model.getInterpretation(features); | ||
66 | // var featureInterpretation = model.getInterpretation(feature); | ||
67 | // return ((Tuple activation) -> { | ||
68 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
69 | // var modelElement = activation.get(0); | ||
70 | // | ||
71 | // var newClassElement = dseAdapter.createObject(); | ||
72 | // var newClassElementId = newClassElement.get(0); | ||
73 | // | ||
74 | // featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
75 | // featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
76 | // }); | ||
77 | // }); | ||
78 | // | ||
79 | // var store = ModelStore.builder() | ||
80 | // .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
81 | // .with(ViatraModelQueryAdapter.builder() | ||
82 | // .queries(createClassPrecondition, createFeaturePrecondition)) | ||
83 | // .with(ModelVisualizerAdapter.builder() | ||
84 | // .withOutputpath("test_output") | ||
85 | // .withFormat(FileFormat.DOT) | ||
86 | // .withFormat(FileFormat.SVG) | ||
87 | // .saveStates() | ||
88 | // .saveDesignSpace() | ||
89 | // ) | ||
90 | // .with(DesignSpaceExplorationAdapter.builder() | ||
91 | // .transformations(createClassRule, createFeatureRule) | ||
92 | // .objectives(new AlwaysSatisfiedRandomHardObjective()) | ||
93 | // .strategy(new DepthFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled() | ||
94 | //// .strategy(new BestFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled() | ||
95 | //// .goOnOnlyIfFitnessIsBetter() | ||
96 | // )) | ||
97 | // .build(); | ||
98 | // | ||
99 | // var model = store.createEmptyModel(); | ||
100 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
101 | //// dseAdapter.setRandom(1); | ||
102 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
103 | // | ||
104 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
105 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
106 | // var modelElement = dseAdapter.createObject(); | ||
107 | // modelElementInterpretation.put(modelElement, true); | ||
108 | // classElementInterpretation.put(modelElement, true); | ||
109 | // queryEngine.flushChanges(); | ||
110 | // | ||
111 | // | ||
112 | // var states = dseAdapter.explore(); | ||
113 | // System.out.println("states size: " + states.size()); | ||
114 | // | ||
115 | // } | ||
116 | } | ||
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..f5f13433 --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DesignSpaceExplorationTest.java | |||
@@ -0,0 +1,596 @@ | |||
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.query.view.AnySymbolView; | ||
9 | import tools.refinery.store.query.view.KeyOnlyView; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | |||
12 | class DesignSpaceExplorationTest { | ||
13 | // private static final Symbol<Boolean> namedElement = Symbol.of("NamedElement", 1); | ||
14 | // private static final Symbol<Boolean> attribute = Symbol.of("Attribute", 1); | ||
15 | // private static final Symbol<Boolean> method = Symbol.of("Method", 1); | ||
16 | // private static final Symbol<Boolean> dataDependency = Symbol.of("DataDependency", 2); | ||
17 | // private static final Symbol<Boolean> functionalDependency = Symbol.of("FunctionalDependency", 2); | ||
18 | |||
19 | private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
20 | private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
21 | private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
22 | |||
23 | private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
24 | private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
25 | |||
26 | private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
27 | private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
28 | |||
29 | private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
30 | private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
31 | private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
32 | private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
33 | private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
34 | private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
35 | private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
36 | |||
37 | // @Test | ||
38 | // void createObjectTest() { | ||
39 | // var store = ModelStore.builder() | ||
40 | // .with(ViatraModelQueryAdapter.builder()) | ||
41 | // .with(DesignSpaceExplorationAdapter.builder() | ||
42 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
43 | // .build(); | ||
44 | // | ||
45 | // var model = store.createEmptyModel(); | ||
46 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
47 | // | ||
48 | // assertEquals(0, dseAdapter.getModelSize()); | ||
49 | // | ||
50 | // var newModel = dseAdapter.createObject(); | ||
51 | // var newModelId = newModel.get(0); | ||
52 | // var newClass1 = dseAdapter.createObject(); | ||
53 | // var newClass1Id = newClass1.get(0); | ||
54 | // var newClass2 = dseAdapter.createObject(); | ||
55 | // var newClass2Id = newClass2.get(0); | ||
56 | // var newField = dseAdapter.createObject(); | ||
57 | // var newFieldId = newField.get(0); | ||
58 | // | ||
59 | // assertEquals(0, newModelId); | ||
60 | // assertEquals(1, newClass1Id); | ||
61 | // assertEquals(2, newClass2Id); | ||
62 | // assertEquals(3, newFieldId); | ||
63 | // assertEquals(4, dseAdapter.getModelSize()); | ||
64 | // } | ||
65 | |||
66 | // @Test | ||
67 | // void deleteMiddleObjectTest() { | ||
68 | // var store = ModelStore.builder() | ||
69 | // .with(ViatraModelQueryAdapter.builder()) | ||
70 | // .with(DesignSpaceExplorationAdapter.builder() | ||
71 | // .strategy(new DepthFirstStrategy())) | ||
72 | // .build(); | ||
73 | // | ||
74 | // var model = store.createEmptyModel(); | ||
75 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
76 | // | ||
77 | // assertEquals(0, dseAdapter.getModelSize()); | ||
78 | // | ||
79 | // var newObject0 = dseAdapter.createObject(); | ||
80 | // var newObject0Id = newObject0.get(0); | ||
81 | // var newObject1 = dseAdapter.createObject(); | ||
82 | // var newObject1Id = newObject1.get(0); | ||
83 | // var newObject2 = dseAdapter.createObject(); | ||
84 | // var newObject2Id = newObject2.get(0); | ||
85 | // var newObject3 = dseAdapter.createObject(); | ||
86 | // var newObject3Id = newObject3.get(0); | ||
87 | // | ||
88 | // assertEquals(0, newObject0Id); | ||
89 | // assertEquals(1, newObject1Id); | ||
90 | // assertEquals(2, newObject2Id); | ||
91 | // assertEquals(3, newObject3Id); | ||
92 | // assertEquals(4, dseAdapter.getModelSize()); | ||
93 | // | ||
94 | // dseAdapter.deleteObject(newObject1); | ||
95 | // assertEquals(4, dseAdapter.getModelSize()); | ||
96 | // | ||
97 | // var newObject4 = dseAdapter.createObject(); | ||
98 | // var newObject4Id = newObject4.get(0); | ||
99 | // assertEquals(4, newObject4Id); | ||
100 | // assertEquals(5, dseAdapter.getModelSize()); | ||
101 | // | ||
102 | // dseAdapter.deleteObject(newObject4); | ||
103 | // assertEquals(5, dseAdapter.getModelSize()); | ||
104 | // } | ||
105 | // | ||
106 | // @Test | ||
107 | // void DFSTrivialTest() { | ||
108 | // var store = ModelStore.builder() | ||
109 | // .symbols(classModel) | ||
110 | // .with(ViatraModelQueryAdapter.builder()) | ||
111 | // .with(DesignSpaceExplorationAdapter.builder() | ||
112 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
113 | // .build(); | ||
114 | // | ||
115 | // var model = store.createEmptyModel(); | ||
116 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
117 | // | ||
118 | // var states = dseAdapter.explore(); | ||
119 | // assertEquals(1, states.size()); | ||
120 | // } | ||
121 | // | ||
122 | // @Test | ||
123 | // void DFSOneRuleTest() { | ||
124 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
125 | // (builder, model) -> builder.clause( | ||
126 | // classModelView.call(model) | ||
127 | // )); | ||
128 | // | ||
129 | // var createClassRule = new TransformationRule("CreateClass", | ||
130 | // createClassPrecondition, | ||
131 | // (model) -> { | ||
132 | // var classesInterpretation = model.getInterpretation(classes); | ||
133 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
134 | // return ((Tuple activation) -> { | ||
135 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
136 | // var modelElement = activation.get(0); | ||
137 | // | ||
138 | // var newClassElement = dseAdapter.createObject(); | ||
139 | // var newClassElementId = newClassElement.get(0); | ||
140 | // | ||
141 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
142 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
143 | // }); | ||
144 | // }); | ||
145 | // | ||
146 | // var store = ModelStore.builder() | ||
147 | // .symbols(classModel, classElement, classes) | ||
148 | // .with(ViatraModelQueryAdapter.builder() | ||
149 | // .queries(createClassPrecondition)) | ||
150 | // .with(DesignSpaceExplorationAdapter.builder() | ||
151 | // .transformations(createClassRule) | ||
152 | // .strategy(new DepthFirstStrategy().withDepthLimit(0) | ||
153 | // )) | ||
154 | // .build(); | ||
155 | // | ||
156 | // var model = store.createEmptyModel(); | ||
157 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
158 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
159 | // | ||
160 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
161 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
162 | // queryEngine.flushChanges(); | ||
163 | // | ||
164 | // var states = dseAdapter.explore(); | ||
165 | // assertEquals(1, states.size()); | ||
166 | // } | ||
167 | // | ||
168 | // @Test | ||
169 | // void DFSContinueTest() { | ||
170 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
171 | // (builder, model) -> builder.clause( | ||
172 | // classModelView.call(model) | ||
173 | // )); | ||
174 | // | ||
175 | // var createClassRule = new TransformationRule("CreateClass", | ||
176 | // createClassPrecondition, | ||
177 | // (model) -> { | ||
178 | // var classesInterpretation = model.getInterpretation(classes); | ||
179 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
180 | // return ((Tuple activation) -> { | ||
181 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
182 | // var modelElement = activation.get(0); | ||
183 | // | ||
184 | // var newClassElement = dseAdapter.createObject(); | ||
185 | // var newClassElementId = newClassElement.get(0); | ||
186 | // | ||
187 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
188 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
189 | // }); | ||
190 | // }); | ||
191 | // | ||
192 | // var store = ModelStore.builder() | ||
193 | // .symbols(classModel, classElement, classes) | ||
194 | // .with(ViatraModelQueryAdapter.builder() | ||
195 | // .queries(createClassPrecondition)) | ||
196 | // .with(DesignSpaceExplorationAdapter.builder() | ||
197 | // .transformations(createClassRule) | ||
198 | // .strategy(new DepthFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled() | ||
199 | // )) | ||
200 | // .build(); | ||
201 | // | ||
202 | // var model = store.createEmptyModel(); | ||
203 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
204 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
205 | // | ||
206 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
207 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
208 | // queryEngine.flushChanges(); | ||
209 | // | ||
210 | // var states = dseAdapter.explore(); | ||
211 | // assertEquals(5, states.size()); | ||
212 | // } | ||
213 | // | ||
214 | // @Test | ||
215 | // void DFSCompletenessTest() { | ||
216 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
217 | // (builder, model) -> builder.clause( | ||
218 | // classModelView.call(model) | ||
219 | // )); | ||
220 | // | ||
221 | // var createClassRule = new TransformationRule("CreateClass", | ||
222 | // createClassPrecondition, | ||
223 | // (model) -> { | ||
224 | // var classesInterpretation = model.getInterpretation(classes); | ||
225 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
226 | // return ((Tuple activation) -> { | ||
227 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
228 | // var modelElement = activation.get(0); | ||
229 | // | ||
230 | // var newClassElement = dseAdapter.createObject(); | ||
231 | // var newClassElementId = newClassElement.get(0); | ||
232 | // | ||
233 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
234 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
235 | // }); | ||
236 | // }); | ||
237 | // | ||
238 | // var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
239 | // (builder, model) -> builder.clause( | ||
240 | // classModelView.call(model) | ||
241 | // )); | ||
242 | // | ||
243 | // var createFeatureRule = new TransformationRule("CreateFeature", | ||
244 | // createFeaturePrecondition, | ||
245 | // (model) -> { | ||
246 | // var featuresInterpretation = model.getInterpretation(features); | ||
247 | // var featureInterpretation = model.getInterpretation(feature); | ||
248 | // return ((Tuple activation) -> { | ||
249 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
250 | // var modelElement = activation.get(0); | ||
251 | // | ||
252 | // var newClassElement = dseAdapter.createObject(); | ||
253 | // var newClassElementId = newClassElement.get(0); | ||
254 | // | ||
255 | // featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
256 | // featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
257 | // }); | ||
258 | // }); | ||
259 | // | ||
260 | // var store = ModelStore.builder() | ||
261 | // .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
262 | // .with(ViatraModelQueryAdapter.builder() | ||
263 | // .queries(createClassPrecondition, createFeaturePrecondition)) | ||
264 | // .with(DesignSpaceExplorationAdapter.builder() | ||
265 | // .transformations(createClassRule, createFeatureRule) | ||
266 | // .strategy(new DepthFirstStrategy().withDepthLimit(10).continueIfHardObjectivesFulfilled() | ||
267 | // )) | ||
268 | // .build(); | ||
269 | // | ||
270 | // var model = store.createEmptyModel(); | ||
271 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
272 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
273 | // | ||
274 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
275 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
276 | // queryEngine.flushChanges(); | ||
277 | // | ||
278 | // var states = dseAdapter.explore(); | ||
279 | // assertEquals(2047, states.size()); | ||
280 | // } | ||
281 | // | ||
282 | // @Test | ||
283 | // void DFSSolutionLimitTest() { | ||
284 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
285 | // (builder, model) -> builder.clause( | ||
286 | // classModelView.call(model) | ||
287 | // )); | ||
288 | // | ||
289 | // var createClassRule = new TransformationRule("CreateClass", | ||
290 | // createClassPrecondition, | ||
291 | // (model) -> { | ||
292 | // var classesInterpretation = model.getInterpretation(classes); | ||
293 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
294 | // return ((Tuple activation) -> { | ||
295 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
296 | // var modelElement = activation.get(0); | ||
297 | // | ||
298 | // var newClassElement = dseAdapter.createObject(); | ||
299 | // var newClassElementId = newClassElement.get(0); | ||
300 | // | ||
301 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
302 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
303 | // }); | ||
304 | // }); | ||
305 | // | ||
306 | // var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
307 | // (builder, model) -> builder.clause( | ||
308 | // classModelView.call(model) | ||
309 | // )); | ||
310 | // | ||
311 | // var createFeatureRule = new TransformationRule("CreateFeature", | ||
312 | // createFeaturePrecondition, | ||
313 | // (model) -> { | ||
314 | // var featuresInterpretation = model.getInterpretation(features); | ||
315 | // var featureInterpretation = model.getInterpretation(feature); | ||
316 | // return ((Tuple activation) -> { | ||
317 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
318 | // var modelElement = activation.get(0); | ||
319 | // | ||
320 | // var newClassElement = dseAdapter.createObject(); | ||
321 | // var newClassElementId = newClassElement.get(0); | ||
322 | // | ||
323 | // featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
324 | // featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
325 | // }); | ||
326 | // }); | ||
327 | // | ||
328 | // var store = ModelStore.builder() | ||
329 | // .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
330 | // .with(ViatraModelQueryAdapter.builder() | ||
331 | // .queries(createClassPrecondition, createFeaturePrecondition)) | ||
332 | // .with(DesignSpaceExplorationAdapter.builder() | ||
333 | // .transformations(createClassRule, createFeatureRule) | ||
334 | // .strategy(new DepthFirstStrategy().withSolutionLimit(222) | ||
335 | // .continueIfHardObjectivesFulfilled() | ||
336 | // )) | ||
337 | // .build(); | ||
338 | // | ||
339 | // var model = store.createEmptyModel(); | ||
340 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
341 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
342 | // | ||
343 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
344 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
345 | // queryEngine.flushChanges(); | ||
346 | // | ||
347 | // var states = dseAdapter.explore(); | ||
348 | // assertEquals(222, states.size()); | ||
349 | // } | ||
350 | // | ||
351 | // @Test | ||
352 | // void BeFSTrivialTest() { | ||
353 | // var store = ModelStore.builder() | ||
354 | // .symbols(classModel) | ||
355 | // .with(ViatraModelQueryAdapter.builder()) | ||
356 | // .with(DesignSpaceExplorationAdapter.builder() | ||
357 | // .strategy(new BestFirstStrategy().withDepthLimit(0))) | ||
358 | // .build(); | ||
359 | // | ||
360 | // var model = store.createEmptyModel(); | ||
361 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
362 | // | ||
363 | // var states = dseAdapter.explore(); | ||
364 | // assertEquals(1, states.size()); | ||
365 | // } | ||
366 | // | ||
367 | // @Test | ||
368 | // void BeFSOneRuleTest() { | ||
369 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
370 | // (builder, model) -> builder.clause( | ||
371 | // classModelView.call(model) | ||
372 | // )); | ||
373 | // | ||
374 | // var createClassRule = new TransformationRule("CreateClass", | ||
375 | // createClassPrecondition, | ||
376 | // (model) -> { | ||
377 | // var classesInterpretation = model.getInterpretation(classes); | ||
378 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
379 | // return ((Tuple activation) -> { | ||
380 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
381 | // var modelElement = activation.get(0); | ||
382 | // | ||
383 | // var newClassElement = dseAdapter.createObject(); | ||
384 | // var newClassElementId = newClassElement.get(0); | ||
385 | // | ||
386 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
387 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
388 | // }); | ||
389 | // }); | ||
390 | // | ||
391 | // var store = ModelStore.builder() | ||
392 | // .symbols(classModel, classElement, classes) | ||
393 | // .with(ViatraModelQueryAdapter.builder() | ||
394 | // .queries(createClassPrecondition)) | ||
395 | // .with(DesignSpaceExplorationAdapter.builder() | ||
396 | // .transformations(createClassRule) | ||
397 | // .strategy(new BestFirstStrategy().withDepthLimit(4) | ||
398 | // )) | ||
399 | // .build(); | ||
400 | // | ||
401 | // var model = store.createEmptyModel(); | ||
402 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
403 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
404 | // | ||
405 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
406 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
407 | // queryEngine.flushChanges(); | ||
408 | // | ||
409 | // var states = dseAdapter.explore(); | ||
410 | // assertEquals(1, states.size()); | ||
411 | // } | ||
412 | // | ||
413 | // @Test | ||
414 | // void BeFSContinueTest() { | ||
415 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
416 | // (builder, model) -> builder.clause( | ||
417 | // classModelView.call(model) | ||
418 | // )); | ||
419 | // | ||
420 | // var createClassRule = new TransformationRule("CreateClass", | ||
421 | // createClassPrecondition, | ||
422 | // (model) -> { | ||
423 | // var classesInterpretation = model.getInterpretation(classes); | ||
424 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
425 | // return ((Tuple activation) -> { | ||
426 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
427 | // var modelElement = activation.get(0); | ||
428 | // | ||
429 | // var newClassElement = dseAdapter.createObject(); | ||
430 | // var newClassElementId = newClassElement.get(0); | ||
431 | // | ||
432 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
433 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
434 | // }); | ||
435 | // }); | ||
436 | // | ||
437 | // var store = ModelStore.builder() | ||
438 | // .symbols(classModel, classElement, classes) | ||
439 | // .with(ViatraModelQueryAdapter.builder() | ||
440 | // .queries(createClassPrecondition)) | ||
441 | // .with(DesignSpaceExplorationAdapter.builder() | ||
442 | // .transformations(createClassRule) | ||
443 | // .strategy(new BestFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled() | ||
444 | // )) | ||
445 | // .build(); | ||
446 | // | ||
447 | // var model = store.createEmptyModel(); | ||
448 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
449 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
450 | // | ||
451 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
452 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
453 | // queryEngine.flushChanges(); | ||
454 | // | ||
455 | // var states = dseAdapter.explore(); | ||
456 | // assertEquals(5, states.size()); | ||
457 | // } | ||
458 | // | ||
459 | // @Test | ||
460 | // void BeFSCompletenessTest() { | ||
461 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
462 | // (builder, model) -> builder.clause( | ||
463 | // classModelView.call(model) | ||
464 | // )); | ||
465 | // | ||
466 | // var createClassRule = new TransformationRule("CreateClass", | ||
467 | // createClassPrecondition, | ||
468 | // (model) -> { | ||
469 | // var classesInterpretation = model.getInterpretation(classes); | ||
470 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
471 | // return ((Tuple activation) -> { | ||
472 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
473 | // var modelElement = activation.get(0); | ||
474 | // | ||
475 | // var newClassElement = dseAdapter.createObject(); | ||
476 | // var newClassElementId = newClassElement.get(0); | ||
477 | // | ||
478 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
479 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
480 | // }); | ||
481 | // }); | ||
482 | // | ||
483 | // var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
484 | // (builder, model) -> builder.clause( | ||
485 | // classModelView.call(model) | ||
486 | // )); | ||
487 | // | ||
488 | // var createFeatureRule = new TransformationRule("CreateFeature", | ||
489 | // createFeaturePrecondition, | ||
490 | // (model) -> { | ||
491 | // var featuresInterpretation = model.getInterpretation(features); | ||
492 | // var featureInterpretation = model.getInterpretation(feature); | ||
493 | // return ((Tuple activation) -> { | ||
494 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
495 | // var modelElement = activation.get(0); | ||
496 | // | ||
497 | // var newClassElement = dseAdapter.createObject(); | ||
498 | // var newClassElementId = newClassElement.get(0); | ||
499 | // | ||
500 | // featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
501 | // featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
502 | // }); | ||
503 | // }); | ||
504 | // | ||
505 | // var store = ModelStore.builder() | ||
506 | // .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
507 | // .with(ViatraModelQueryAdapter.builder() | ||
508 | // .queries(createClassPrecondition, createFeaturePrecondition)) | ||
509 | // .with(DesignSpaceExplorationAdapter.builder() | ||
510 | // .transformations(createClassRule, createFeatureRule) | ||
511 | // .strategy(new BestFirstStrategy().withDepthLimit(10).continueIfHardObjectivesFulfilled() | ||
512 | // )) | ||
513 | // .build(); | ||
514 | // | ||
515 | // var model = store.createEmptyModel(); | ||
516 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
517 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
518 | // | ||
519 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
520 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
521 | // queryEngine.flushChanges(); | ||
522 | // | ||
523 | // var states = dseAdapter.explore(); | ||
524 | // assertEquals(2047, states.size()); | ||
525 | // } | ||
526 | // | ||
527 | // @Test | ||
528 | // void BeFSSolutionLimitTest() { | ||
529 | // var createClassPrecondition = Query.of("CreateClassPrecondition", | ||
530 | // (builder, model) -> builder.clause( | ||
531 | // classModelView.call(model) | ||
532 | // )); | ||
533 | // | ||
534 | // var createClassRule = new TransformationRule("CreateClass", | ||
535 | // createClassPrecondition, | ||
536 | // (model) -> { | ||
537 | // var classesInterpretation = model.getInterpretation(classes); | ||
538 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
539 | // return ((Tuple activation) -> { | ||
540 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
541 | // var modelElement = activation.get(0); | ||
542 | // | ||
543 | // var newClassElement = dseAdapter.createObject(); | ||
544 | // var newClassElementId = newClassElement.get(0); | ||
545 | // | ||
546 | // classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
547 | // classElementInterpretation.put(Tuple.of(newClassElementId), true); | ||
548 | // }); | ||
549 | // }); | ||
550 | // | ||
551 | // var createFeaturePrecondition = Query.of("CreateFeaturePrecondition", | ||
552 | // (builder, model) -> builder.clause( | ||
553 | // classModelView.call(model) | ||
554 | // )); | ||
555 | // | ||
556 | // var createFeatureRule = new TransformationRule("CreateFeature", | ||
557 | // createFeaturePrecondition, | ||
558 | // (model) -> { | ||
559 | // var featuresInterpretation = model.getInterpretation(features); | ||
560 | // var featureInterpretation = model.getInterpretation(feature); | ||
561 | // return ((Tuple activation) -> { | ||
562 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
563 | // var modelElement = activation.get(0); | ||
564 | // | ||
565 | // var newClassElement = dseAdapter.createObject(); | ||
566 | // var newClassElementId = newClassElement.get(0); | ||
567 | // | ||
568 | // featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true); | ||
569 | // featureInterpretation.put(Tuple.of(newClassElementId), true); | ||
570 | // }); | ||
571 | // }); | ||
572 | // | ||
573 | // var store = ModelStore.builder() | ||
574 | // .symbols(classModel, classElement, classes, feature, features, isEncapsulatedBy, encapsulates) | ||
575 | // .with(ViatraModelQueryAdapter.builder() | ||
576 | // .queries(createClassPrecondition, createFeaturePrecondition)) | ||
577 | // .with(DesignSpaceExplorationAdapter.builder() | ||
578 | // .transformations(createClassRule, createFeatureRule) | ||
579 | // .strategy(new BestFirstStrategy().withSolutionLimit(222) | ||
580 | // .continueIfHardObjectivesFulfilled() | ||
581 | // )) | ||
582 | // .build(); | ||
583 | // | ||
584 | // var model = store.createEmptyModel(); | ||
585 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
586 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
587 | // | ||
588 | // var modelElementInterpretation = model.getInterpretation(classModel); | ||
589 | // modelElementInterpretation.put(dseAdapter.createObject(), true); | ||
590 | // queryEngine.flushChanges(); | ||
591 | // | ||
592 | // var states = dseAdapter.explore(); | ||
593 | // assertEquals(222, states.size()); | ||
594 | // } | ||
595 | |||
596 | } | ||
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..43b04e0d --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/TransformationRuleTest.java | |||
@@ -0,0 +1,414 @@ | |||
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 | // | ||
10 | //import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter; | ||
11 | //import tools.refinery.store.model.ModelStore; | ||
12 | //import tools.refinery.store.query.ModelQueryAdapter; | ||
13 | //import tools.refinery.store.query.dnf.Query; | ||
14 | //import tools.refinery.store.dse.transition.TransformationRule; | ||
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 | // | ||
21 | //import java.util.List; | ||
22 | //import java.util.Map; | ||
23 | // | ||
24 | //import static org.junit.jupiter.api.Assertions.assertEquals; | ||
25 | //import static tools.refinery.store.query.literal.Literals.not; | ||
26 | //import static tools.refinery.store.dse.tests.QueryAssertions.assertResults; | ||
27 | // | ||
28 | class TransformationRuleTest { | ||
29 | // | ||
30 | // private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1); | ||
31 | // private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1); | ||
32 | // private static final Symbol<Boolean> feature = Symbol.of("Feature", 1); | ||
33 | // | ||
34 | // private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2); | ||
35 | // private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2); | ||
36 | // | ||
37 | // private static final Symbol<Boolean> features = Symbol.of("Features", 2); | ||
38 | // private static final Symbol<Boolean> classes = Symbol.of("Classes", 2); | ||
39 | // | ||
40 | // private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel); | ||
41 | // private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement); | ||
42 | // private static final AnySymbolView featureView = new KeyOnlyView<>(feature); | ||
43 | // private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy); | ||
44 | // private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates); | ||
45 | // private static final AnySymbolView featuresView = new KeyOnlyView<>(features); | ||
46 | // private static final AnySymbolView classesView = new KeyOnlyView<>(classes); | ||
47 | // | ||
48 | // @Test | ||
49 | // void activationsTest() { | ||
50 | // var assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper", | ||
51 | // (builder, model, c, f) -> builder.clause( | ||
52 | // classElementView.call(c), | ||
53 | // classesView.call(model, c), | ||
54 | // encapsulatesView.call(c, f) | ||
55 | // )); | ||
56 | // | ||
57 | // var assignFeaturePrecondition = Query.of("AssignFeaturePrecondition", (builder, c2, f) | ||
58 | // -> builder.clause((model, c1) -> List.of( | ||
59 | // classModelView.call(model), | ||
60 | // featureView.call(f), | ||
61 | // classElementView.call(c2), | ||
62 | // featuresView.call(model, f), | ||
63 | // classesView.call(model, c1), | ||
64 | // not(assignFeaturePreconditionHelper.call(model, c2, f)), | ||
65 | // not(encapsulatesView.call(c2, f)) | ||
66 | // ))); | ||
67 | // | ||
68 | // var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
69 | // (builder, model, c) -> builder.clause((f) -> List.of( | ||
70 | // classModelView.call(model), | ||
71 | // classElementView.call(c), | ||
72 | // featuresView.call(model, f), | ||
73 | // not(encapsulatesView.call(c, f)) | ||
74 | // ))); | ||
75 | // | ||
76 | // TransformationRule assignFeatureRule = new TransformationRule("AssignFeature", | ||
77 | // assignFeaturePrecondition, | ||
78 | // (model) -> { | ||
79 | // var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); | ||
80 | // return ((Tuple activation) -> { | ||
81 | // var feature = activation.get(0); | ||
82 | // var classElement = activation.get(1); | ||
83 | // | ||
84 | // isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); | ||
85 | // }); | ||
86 | // }); | ||
87 | // | ||
88 | // TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
89 | // deleteEmptyClassPrecondition, | ||
90 | // (model) -> { | ||
91 | // var classesInterpretation = model.getInterpretation(classes); | ||
92 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
93 | // return ((Tuple activation) -> { | ||
94 | // var modelElement = activation.get(0); | ||
95 | // var classElement = activation.get(1); | ||
96 | // | ||
97 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
98 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
99 | // }); | ||
100 | // }); | ||
101 | // | ||
102 | // | ||
103 | // var store = ModelStore.builder() | ||
104 | // .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
105 | // .with(ViatraModelQueryAdapter.builder() | ||
106 | // .queries(assignFeaturePrecondition, assignFeaturePreconditionHelper, | ||
107 | // deleteEmptyClassPrecondition)) | ||
108 | // .with(DesignSpaceExplorationAdapter.builder() | ||
109 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
110 | // .build(); | ||
111 | // | ||
112 | // var model = store.createEmptyModel(); | ||
113 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
114 | // assignFeatureRule.prepare(model, queryEngine); | ||
115 | // deleteEmptyClassRule.prepare(model, queryEngine); | ||
116 | // | ||
117 | // var classModelInterpretation = model.getInterpretation(classModel); | ||
118 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
119 | // var featureInterpretation = model.getInterpretation(feature); | ||
120 | // var featuresInterpretation = model.getInterpretation(features); | ||
121 | // var classesInterpretation = model.getInterpretation(classes); | ||
122 | // | ||
123 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
124 | // var newModel = dseAdapter.createObject(); | ||
125 | // var newModelId = newModel.get(0); | ||
126 | // var newClass1 = dseAdapter.createObject(); | ||
127 | // var newClass1Id = newClass1.get(0); | ||
128 | // var newClass2 = dseAdapter.createObject(); | ||
129 | // var newClass2Id = newClass2.get(0); | ||
130 | // var newField = dseAdapter.createObject(); | ||
131 | // var newFieldId = newField.get(0); | ||
132 | // | ||
133 | // classModelInterpretation.put(newModel, true); | ||
134 | // classElementInterpretation.put(newClass1, true); | ||
135 | // classElementInterpretation.put(newClass2, true); | ||
136 | // featureInterpretation.put(newField, true); | ||
137 | // classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
138 | // classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
139 | // featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
140 | // | ||
141 | // queryEngine.flushChanges(); | ||
142 | // | ||
143 | // var assignFeatureRuleActivations = assignFeatureRule.getAllActivationsAsResultSet(); | ||
144 | // var deleteEmptyClassRuleActivations = deleteEmptyClassRule.getAllActivationsAsResultSet(); | ||
145 | // | ||
146 | // assertResults(Map.of( | ||
147 | // Tuple.of(newClass1Id, newFieldId), true, | ||
148 | // Tuple.of(newClass2Id, newFieldId), true | ||
149 | // ), assignFeatureRuleActivations); | ||
150 | // | ||
151 | // assertResults(Map.of( | ||
152 | // Tuple.of(newModelId, newClass1Id), true, | ||
153 | // Tuple.of(newModelId, newClass2Id), true | ||
154 | // ), deleteEmptyClassRuleActivations); | ||
155 | // } | ||
156 | // | ||
157 | // @Test | ||
158 | // void randomActivationTest() { | ||
159 | // var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
160 | // (builder, model, c) -> builder.clause((f) -> List.of( | ||
161 | // classModelView.call(model), | ||
162 | // classElementView.call(c), | ||
163 | // featuresView.call(model, f), | ||
164 | // not(encapsulatesView.call(c, f)) | ||
165 | // ))); | ||
166 | // | ||
167 | // TransformationRule deleteEmptyClassRule0 = new TransformationRule("DeleteEmptyClass0", | ||
168 | // deleteEmptyClassPrecondition, | ||
169 | // (model) -> { | ||
170 | // var classesInterpretation = model.getInterpretation(classes); | ||
171 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
172 | // return ((Tuple activation) -> { | ||
173 | // var modelElement = activation.get(0); | ||
174 | // var classElement = activation.get(1); | ||
175 | // | ||
176 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
177 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
178 | // }); | ||
179 | // }, | ||
180 | // 0L); | ||
181 | // | ||
182 | // TransformationRule deleteEmptyClassRule1 = new TransformationRule("DeleteEmptyClass1", | ||
183 | // deleteEmptyClassPrecondition, | ||
184 | // (model) -> { | ||
185 | // var classesInterpretation = model.getInterpretation(classes); | ||
186 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
187 | // return ((Tuple activation) -> { | ||
188 | // var modelElement = activation.get(0); | ||
189 | // var classElement = activation.get(1); | ||
190 | // | ||
191 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
192 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
193 | // }); | ||
194 | // }, | ||
195 | // 78634L); | ||
196 | // | ||
197 | // var store = ModelStore.builder() | ||
198 | // .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
199 | // .with(ViatraModelQueryAdapter.builder() | ||
200 | // .queries(deleteEmptyClassPrecondition)) | ||
201 | // .with(DesignSpaceExplorationAdapter.builder() | ||
202 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
203 | // .build(); | ||
204 | // | ||
205 | // var model = store.createEmptyModel(); | ||
206 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
207 | // deleteEmptyClassRule0.prepare(model, queryEngine); | ||
208 | // deleteEmptyClassRule1.prepare(model, queryEngine); | ||
209 | // | ||
210 | // var classModelInterpretation = model.getInterpretation(classModel); | ||
211 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
212 | // var featureInterpretation = model.getInterpretation(feature); | ||
213 | // var featuresInterpretation = model.getInterpretation(features); | ||
214 | // var classesInterpretation = model.getInterpretation(classes); | ||
215 | // | ||
216 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
217 | // var newModel = dseAdapter.createObject(); | ||
218 | // var newModelId = newModel.get(0); | ||
219 | // var newClass1 = dseAdapter.createObject(); | ||
220 | // var newClass1Id = newClass1.get(0); | ||
221 | // var newClass2 = dseAdapter.createObject(); | ||
222 | // var newClass2Id = newClass2.get(0); | ||
223 | // var newField = dseAdapter.createObject(); | ||
224 | // var newFieldId = newField.get(0); | ||
225 | // | ||
226 | // classModelInterpretation.put(newModel, true); | ||
227 | // classElementInterpretation.put(newClass1, true); | ||
228 | // classElementInterpretation.put(newClass2, true); | ||
229 | // featureInterpretation.put(newField, true); | ||
230 | // classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
231 | // classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
232 | // featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
233 | // | ||
234 | // queryEngine.flushChanges(); | ||
235 | // | ||
236 | // | ||
237 | // var activation0 = deleteEmptyClassRule0.getRandomActivation().activation(); | ||
238 | // var activation1 = deleteEmptyClassRule1.getRandomActivation().activation(); | ||
239 | // | ||
240 | // assertResults(Map.of( | ||
241 | // Tuple.of(newModelId, newClass1Id), true, | ||
242 | // Tuple.of(newModelId, newClass2Id), true | ||
243 | // ), deleteEmptyClassRule0.getAllActivationsAsResultSet()); | ||
244 | // | ||
245 | // assertResults(Map.of( | ||
246 | // Tuple.of(newModelId, newClass1Id), true, | ||
247 | // Tuple.of(newModelId, newClass2Id), true | ||
248 | // ), deleteEmptyClassRule1.getAllActivationsAsResultSet()); | ||
249 | // | ||
250 | // assertEquals(Tuple.of(newModelId, newClass2Id), activation0); | ||
251 | // assertEquals(Tuple.of(newModelId, newClass1Id), activation1); | ||
252 | // | ||
253 | // } | ||
254 | // | ||
255 | // @Test | ||
256 | // void fireTest() { | ||
257 | // var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
258 | // (builder, model, c) -> builder.clause((f) -> List.of( | ||
259 | // classModelView.call(model), | ||
260 | // classElementView.call(c), | ||
261 | // featuresView.call(model, f), | ||
262 | // not(encapsulatesView.call(c, f)) | ||
263 | // ))); | ||
264 | // | ||
265 | // TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | ||
266 | // deleteEmptyClassPrecondition, | ||
267 | // (model) -> { | ||
268 | // var classesInterpretation = model.getInterpretation(classes); | ||
269 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
270 | // return ((Tuple activation) -> { | ||
271 | // var modelElement = activation.get(0); | ||
272 | // var classElement = activation.get(1); | ||
273 | // | ||
274 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
275 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
276 | // }); | ||
277 | // }); | ||
278 | // | ||
279 | // var store = ModelStore.builder() | ||
280 | // .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
281 | // .with(ViatraModelQueryAdapter.builder() | ||
282 | // .queries(deleteEmptyClassPrecondition)) | ||
283 | // .with(DesignSpaceExplorationAdapter.builder() | ||
284 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
285 | // .build(); | ||
286 | // | ||
287 | // var model = store.createEmptyModel(); | ||
288 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
289 | // deleteEmptyClassRule.prepare(model, queryEngine); | ||
290 | // | ||
291 | // var classModelInterpretation = model.getInterpretation(classModel); | ||
292 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
293 | // var featureInterpretation = model.getInterpretation(feature); | ||
294 | // var featuresInterpretation = model.getInterpretation(features); | ||
295 | // var classesInterpretation = model.getInterpretation(classes); | ||
296 | // | ||
297 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
298 | // var newModel = dseAdapter.createObject(); | ||
299 | // var newModelId = newModel.get(0); | ||
300 | // var newClass1 = dseAdapter.createObject(); | ||
301 | // var newClass1Id = newClass1.get(0); | ||
302 | // var newClass2 = dseAdapter.createObject(); | ||
303 | // var newClass2Id = newClass2.get(0); | ||
304 | // var newField = dseAdapter.createObject(); | ||
305 | // var newFieldId = newField.get(0); | ||
306 | // | ||
307 | // classModelInterpretation.put(newModel, true); | ||
308 | // classElementInterpretation.put(newClass1, true); | ||
309 | // classElementInterpretation.put(newClass2, true); | ||
310 | // featureInterpretation.put(newField, true); | ||
311 | // classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
312 | // classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
313 | // featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
314 | // | ||
315 | // queryEngine.flushChanges(); | ||
316 | // | ||
317 | // assertResults(Map.of( | ||
318 | // Tuple.of(newModelId, newClass1Id), true, | ||
319 | // Tuple.of(newModelId, newClass2Id), true | ||
320 | // ), deleteEmptyClassRule.getAllActivationsAsResultSet()); | ||
321 | // | ||
322 | // | ||
323 | // deleteEmptyClassRule.fireActivation(Tuple.of(0, 1)); | ||
324 | // | ||
325 | // assertResults(Map.of( | ||
326 | // Tuple.of(newModelId, newClass1Id), false, | ||
327 | // Tuple.of(newModelId, newClass2Id), true | ||
328 | // ), deleteEmptyClassRule.getAllActivationsAsResultSet()); | ||
329 | // } | ||
330 | // | ||
331 | // @Test | ||
332 | // void randomFireTest() { | ||
333 | // var deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition", | ||
334 | // (builder, model, c) -> builder.clause((f) -> List.of( | ||
335 | // classModelView.call(model), | ||
336 | // classElementView.call(c), | ||
337 | // featuresView.call(model, f), | ||
338 | // not(encapsulatesView.call(c, f)) | ||
339 | // ))); | ||
340 | // | ||
341 | // TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass0", | ||
342 | // deleteEmptyClassPrecondition, | ||
343 | // (model) -> { | ||
344 | // var classesInterpretation = model.getInterpretation(classes); | ||
345 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
346 | // return ((Tuple activation) -> { | ||
347 | // var modelElement = activation.get(0); | ||
348 | // var classElement = activation.get(1); | ||
349 | // | ||
350 | // classesInterpretation.put(Tuple.of(modelElement, classElement), false); | ||
351 | // classElementInterpretation.put(Tuple.of(classElement), false); | ||
352 | // }); | ||
353 | // }, | ||
354 | // 0L); | ||
355 | // | ||
356 | // var store = ModelStore.builder() | ||
357 | // .symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features) | ||
358 | // .with(ViatraModelQueryAdapter.builder() | ||
359 | // .queries(deleteEmptyClassPrecondition)) | ||
360 | // .with(DesignSpaceExplorationAdapter.builder() | ||
361 | // .strategy(new DepthFirstStrategy().withDepthLimit(0))) | ||
362 | // .build(); | ||
363 | // | ||
364 | // var model = store.createEmptyModel(); | ||
365 | // var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
366 | // deleteEmptyClassRule.prepare(model, queryEngine); | ||
367 | // | ||
368 | // var classModelInterpretation = model.getInterpretation(classModel); | ||
369 | // var classElementInterpretation = model.getInterpretation(classElement); | ||
370 | // var featureInterpretation = model.getInterpretation(feature); | ||
371 | // var featuresInterpretation = model.getInterpretation(features); | ||
372 | // var classesInterpretation = model.getInterpretation(classes); | ||
373 | // | ||
374 | // var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
375 | // var newModel = dseAdapter.createObject(); | ||
376 | // var newModelId = newModel.get(0); | ||
377 | // var newClass1 = dseAdapter.createObject(); | ||
378 | // var newClass1Id = newClass1.get(0); | ||
379 | // var newClass2 = dseAdapter.createObject(); | ||
380 | // var newClass2Id = newClass2.get(0); | ||
381 | // var newField = dseAdapter.createObject(); | ||
382 | // var newFieldId = newField.get(0); | ||
383 | // | ||
384 | // classModelInterpretation.put(newModel, true); | ||
385 | // classElementInterpretation.put(newClass1, true); | ||
386 | // classElementInterpretation.put(newClass2, true); | ||
387 | // featureInterpretation.put(newField, true); | ||
388 | // classesInterpretation.put(Tuple.of(newModelId, newClass1Id), true); | ||
389 | // classesInterpretation.put(Tuple.of(newModelId, newClass2Id), true); | ||
390 | // featuresInterpretation.put(Tuple.of(newModelId, newFieldId), true); | ||
391 | // | ||
392 | // queryEngine.flushChanges(); | ||
393 | // | ||
394 | // assertResults(Map.of( | ||
395 | // Tuple.of(newModelId, newClass1Id), true, | ||
396 | // Tuple.of(newModelId, newClass2Id), true | ||
397 | // ), deleteEmptyClassRule.getAllActivationsAsResultSet()); | ||
398 | // | ||
399 | // deleteEmptyClassRule.fireRandomActivation(); | ||
400 | // | ||
401 | // assertResults(Map.of( | ||
402 | // Tuple.of(newModelId, newClass1Id), true, | ||
403 | // Tuple.of(newModelId, newClass2Id), false | ||
404 | // ), deleteEmptyClassRule.getAllActivationsAsResultSet()); | ||
405 | // | ||
406 | // deleteEmptyClassRule.fireRandomActivation(); | ||
407 | // | ||
408 | // assertResults(Map.of( | ||
409 | // Tuple.of(newModelId, newClass1Id), false, | ||
410 | // Tuple.of(newModelId, newClass2Id), false | ||
411 | // ), deleteEmptyClassRule.getAllActivationsAsResultSet()); | ||
412 | // | ||
413 | // } | ||
414 | } | ||
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..f0a20720 --- /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.getCanonicalQuery().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 | } | ||
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/statespace/internal/ActivationUnitTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/statespace/internal/ActivationUnitTest.java new file mode 100644 index 00000000..e7960a06 --- /dev/null +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/transition/statespace/internal/ActivationUnitTest.java | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import org.junit.jupiter.api.Assertions; | ||
9 | import org.junit.jupiter.params.ParameterizedTest; | ||
10 | import org.junit.jupiter.params.provider.MethodSource; | ||
11 | |||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | class ActivationUnitTest { | ||
15 | private final static int SMALL_SIZE = 5; | ||
16 | |||
17 | private static Stream<ActivationStoreEntry> entries() { | ||
18 | return Stream.of( | ||
19 | new ActivationStoreBitVectorEntry(SMALL_SIZE), | ||
20 | new ActivationStoreListEntry(SMALL_SIZE)); | ||
21 | } | ||
22 | |||
23 | void addTest(ActivationStoreEntry entry, int elementsAdded) { | ||
24 | Assertions.assertEquals(elementsAdded,entry.getNumberOfVisitedActivations()); | ||
25 | Assertions.assertEquals(SMALL_SIZE-elementsAdded,entry.getNumberOfUnvisitedActivations()); | ||
26 | } | ||
27 | |||
28 | @ParameterizedTest | ||
29 | @MethodSource("entries") | ||
30 | void testDifferent(ActivationStoreEntry entry) { | ||
31 | int elementsAdded = 0; | ||
32 | addTest(entry,elementsAdded); | ||
33 | Assertions.assertEquals(2, entry.getAndAddActivationAfter(2)); | ||
34 | addTest(entry,++elementsAdded); | ||
35 | Assertions.assertEquals(3,entry.getAndAddActivationAfter(3)); | ||
36 | addTest(entry,++elementsAdded); | ||
37 | Assertions.assertEquals(1,entry.getAndAddActivationAfter(1)); | ||
38 | addTest(entry,++elementsAdded); | ||
39 | Assertions.assertEquals(4,entry.getAndAddActivationAfter(4)); | ||
40 | addTest(entry,++elementsAdded); | ||
41 | Assertions.assertEquals(0,entry.getAndAddActivationAfter(0)); | ||
42 | addTest(entry,++elementsAdded); | ||
43 | } | ||
44 | |||
45 | @ParameterizedTest | ||
46 | @MethodSource("entries") | ||
47 | void testSame(ActivationStoreEntry entry) { | ||
48 | int elementsAdded = 0; | ||
49 | addTest(entry,elementsAdded); | ||
50 | entry.getAndAddActivationAfter(2); | ||
51 | addTest(entry,++elementsAdded); | ||
52 | entry.getAndAddActivationAfter(2); | ||
53 | addTest(entry,++elementsAdded); | ||
54 | entry.getAndAddActivationAfter(2); | ||
55 | addTest(entry,++elementsAdded); | ||
56 | entry.getAndAddActivationAfter(2); | ||
57 | addTest(entry,++elementsAdded); | ||
58 | entry.getAndAddActivationAfter(2); | ||
59 | addTest(entry,++elementsAdded); | ||
60 | } | ||
61 | |||
62 | @ParameterizedTest | ||
63 | @MethodSource("entries") | ||
64 | void testFilling(ActivationStoreEntry entry) { | ||
65 | int elementsAdded = 0; | ||
66 | while(elementsAdded < SMALL_SIZE) { | ||
67 | entry.getAndAddActivationAfter(2); | ||
68 | elementsAdded++; | ||
69 | } | ||
70 | Assertions.assertThrows(IllegalArgumentException.class,()-> entry.getAndAddActivationAfter(2)); | ||
71 | } | ||
72 | } | ||