aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java
blob: 225de32e81c2be41794ef1c1b33628aae9f0d84c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import tools.refinery.store.dse.objectives.AlwaysSatisfiedRandomHardObjective;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.dse.internal.TransformationRule;
import tools.refinery.store.dse.strategy.BestFirstStrategy;
import tools.refinery.store.dse.strategy.DepthFirstStrategy;
import tools.refinery.store.query.viatra.ViatraModelQueryAdapter;
import tools.refinery.store.query.view.AnySymbolView;
import tools.refinery.store.query.view.KeyOnlyView;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.tuple.Tuple;
import tools.refinery.visualization.ModelVisualizerAdapter;
import tools.refinery.visualization.internal.FileFormat;

import java.util.List;

import static tools.refinery.store.query.literal.Literals.not;

class CRAExamplesTest {
	private static final Symbol<String> name = Symbol.of("Name", 1, String.class);

//	private static final Symbol<Boolean> classModel = Symbol.of("ClassModel", 1);
	private static final Symbol<Boolean> classElement = Symbol.of("ClassElement", 1);
//	private static final Symbol<Boolean> feature = Symbol.of("Feature", 1);
	private static final Symbol<Boolean> attribute = Symbol.of("Attribute", 1);
	private static final Symbol<Boolean> method = Symbol.of("Method", 1);

//	private static final Symbol<Boolean> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2);
	private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 2);
	private static final Symbol<Boolean> dataDependency = Symbol.of("DataDependency", 2);
	private static final Symbol<Boolean> functionalDependency = Symbol.of("FunctionalDependency", 2);

	private static final Symbol<Boolean> features = Symbol.of("Features", 2);
	private static final Symbol<Boolean> classes = Symbol.of("Classes", 2);

//	private static final AnySymbolView classModelView = new KeyOnlyView<>(classModel);
	private static final AnySymbolView classElementView = new KeyOnlyView<>(classElement);
//	private static final AnySymbolView featureView = new KeyOnlyView<>(feature);
	private static final AnySymbolView attributeView = new KeyOnlyView<>(attribute);
	private static final AnySymbolView methodView = new KeyOnlyView<>(method);
//	private static final AnySymbolView isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy);
	private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates);
	private static final AnySymbolView dataDependencyView = new KeyOnlyView<>(dataDependency);
	private static final AnySymbolView functionalDependencyView = new KeyOnlyView<>(functionalDependency);
	private static final AnySymbolView featuresView = new KeyOnlyView<>(features);
	private static final AnySymbolView classesView = new KeyOnlyView<>(classes);

	/*Example Transformation rules*/
	private static final RelationalQuery feature = Query.of("Feature",
			(builder, f) -> builder.clause(
				attributeView.call(f))
			.clause(
				methodView.call(f))
			);

	private static final RelationalQuery assignFeaturePreconditionHelper = Query.of("AssignFeaturePreconditionHelper",
			(builder, c, f) -> builder.clause(
					classElementView.call(c),
//					classesView.call(model, c),
					encapsulatesView.call(c, f)
			));

	private static final RelationalQuery assignFeaturePrecondition = Query.of("AssignFeaturePrecondition",
			(builder, f, c1) -> builder.clause((c2) -> List.of(
//					classModelView.call(model),
					feature.call(f),
					classElementView.call(c1),
//					featuresView.call(model, f),
					not(assignFeaturePreconditionHelper.call(c2, f)),
					not(encapsulatesView.call(c1, f))
			)));

	private static final RelationalQuery deleteEmptyClassPrecondition = Query.of("DeleteEmptyClassPrecondition",
			(builder, c) -> builder.clause((f) -> List.of(
//					classModelView.call(model),
					classElementView.call(c),
//					featuresView.call(model, f),
					not(encapsulatesView.call(c, f))
			)));

	private static final RelationalQuery createClassPreconditionHelper = Query.of("CreateClassPreconditionHelper",
			(builder, f, c) -> builder.clause(
					classElementView.call(c),
//					classesView.call(model, c),
					encapsulatesView.call(c, f)
			));

	private static final RelationalQuery createClassPrecondition = Query.of("CreateClassPrecondition",
			(builder, f) -> builder.clause((c) -> List.of(
//					classModelView.call(model),
					feature.call(f),
					not(createClassPreconditionHelper.call(f, c))
			)));

	private static final RelationalQuery moveFeaturePrecondition = Query.of("MoveFeature",
			(builder, c1, c2, f) -> builder.clause(
//					classModelView.call(model),
					classElementView.call(c1),
					classElementView.call(c2),
					c1.notEquivalent(c2),
					feature.call(f),
//					classesView.call(model, c1),
//					classesView.call(model, c2),
//					featuresView.call(model, f),
					encapsulatesView.call(c1, f)
			));

	private static final TransformationRule assignFeatureRule = new TransformationRule("AssignFeature",
			assignFeaturePrecondition,
			(model) -> {
//				var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy);
				var encapsulatesInterpretation = model.getInterpretation(encapsulates);
				return ((Tuple activation) -> {
					var feature = activation.get(0);
					var classElement = activation.get(1);

//					isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true);
					encapsulatesInterpretation.put(Tuple.of(classElement, feature), true);
				});
			});

	private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass",
			deleteEmptyClassPrecondition,
			(model) -> {
//				var classesInterpretation = model.getInterpretation(classes);
				var classElementInterpretation = model.getInterpretation(classElement);
				return ((Tuple activation) -> {
					// TODO: can we move dseAdapter outside?
					var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
//					var modelElement = activation.get(0);
					var classElement = activation.get(0);

//					classesInterpretation.put(Tuple.of(modelElement, classElement), false);
					classElementInterpretation.put(Tuple.of(classElement), false);
					dseAdapter.deleteObject(Tuple.of(classElement));
				});
			});

	private static final TransformationRule createClassRule = new TransformationRule("CreateClass",
			createClassPrecondition,
			(model) -> {
				var classElementInterpretation = model.getInterpretation(classElement);
//				var classesInterpretation = model.getInterpretation(classes);
				var encapsulatesInterpretation = model.getInterpretation(encapsulates);
				return ((Tuple activation) -> {
					// TODO: can we move dseAdapter outside?
					var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
//					var modelElement = activation.get(0);
					var feature = activation.get(0);

					var newClassElement = dseAdapter.createObject();
					var newClassElementId = newClassElement.get(0);
					classElementInterpretation.put(newClassElement, true);
//					classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
					encapsulatesInterpretation.put(Tuple.of(newClassElementId, feature), true);
				});
			});

	private static final TransformationRule moveFeatureRule = new TransformationRule("MoveFeature",
			moveFeaturePrecondition,
			(model) -> {
				var encapsulatesInterpretation = model.getInterpretation(encapsulates);
				return ((Tuple activation) -> {
					var classElement1 = activation.get(0);
					var classElement2 = activation.get(1);
					var feature = activation.get(2);

					encapsulatesInterpretation.put(Tuple.of(classElement1, feature), false);
					encapsulatesInterpretation.put(Tuple.of(classElement2, feature), true);
				});
			});

	@Test
	@Disabled("This test is only for debugging purposes")
	void craTest() {
		var store = ModelStore.builder()
				.symbols(classElement, encapsulates, classes, features, attribute, method, dataDependency,
						functionalDependency, name)
				.with(ViatraModelQueryAdapter.builder()
						.queries(feature, assignFeaturePreconditionHelper, assignFeaturePrecondition,
								deleteEmptyClassPrecondition, createClassPreconditionHelper, createClassPrecondition,
								moveFeaturePrecondition))
				.with(ModelVisualizerAdapter.builder()
						.withOutputpath("test_output")
						.withFormat(FileFormat.DOT)
						.withFormat(FileFormat.SVG)
						.saveStates()
						.saveDesignSpace()
				)
				.with(DesignSpaceExplorationAdapter.builder()
						.transformations(assignFeatureRule, deleteEmptyClassRule, createClassRule, moveFeatureRule)
						.objectives(new AlwaysSatisfiedRandomHardObjective())
//						.strategy(new DepthFirstStrategy().withDepthLimit(3).continueIfHardObjectivesFulfilled()
						.strategy(new BestFirstStrategy().withDepthLimit(6).continueIfHardObjectivesFulfilled()
//								.goOnOnlyIfFitnessIsBetter()
						))
				.build();

		var model = store.createEmptyModel();
		var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
//		dseAdapter.setRandom(1);
		var queryEngine = model.getAdapter(ModelQueryAdapter.class);

//		var modelInterpretation = model.getInterpretation(classModel);
		var nameInterpretation = model.getInterpretation(name);
		var methodInterpretation = model.getInterpretation(method);
		var attributeInterpretation = model.getInterpretation(attribute);
		var dataDependencyInterpretation = model.getInterpretation(dataDependency);
		var functionalDependencyInterpretation = model.getInterpretation(functionalDependency);

//		var modelElement = dseAdapter.createObject();
		var method1 = dseAdapter.createObject();
		var method1Id = method1.get(0);
		var method2 = dseAdapter.createObject();
		var method2Id = method2.get(0);
		var method3 = dseAdapter.createObject();
		var method3Id = method3.get(0);
		var method4 = dseAdapter.createObject();
		var method4Id = method4.get(0);
		var attribute1 = dseAdapter.createObject();
		var attribute1Id = attribute1.get(0);
		var attribute2 = dseAdapter.createObject();
		var attribute2Id = attribute2.get(0);
		var attribute3 = dseAdapter.createObject();
		var attribute3Id = attribute3.get(0);
		var attribute4 = dseAdapter.createObject();
		var attribute4Id = attribute4.get(0);
		var attribute5 = dseAdapter.createObject();
		var attribute5Id = attribute5.get(0);

		nameInterpretation.put(method1, "M1");
		nameInterpretation.put(method2, "M2");
		nameInterpretation.put(method3, "M3");
		nameInterpretation.put(method4, "M4");
		nameInterpretation.put(attribute1, "A1");
		nameInterpretation.put(attribute2, "A2");
		nameInterpretation.put(attribute3, "A3");
		nameInterpretation.put(attribute4, "A4");
		nameInterpretation.put(attribute5, "A5");



//		modelInterpretation.put(modelElement, true);
		methodInterpretation.put(method1, true);
		methodInterpretation.put(method2, true);
		methodInterpretation.put(method3, true);
		methodInterpretation.put(method4, true);
		attributeInterpretation.put(attribute1, true);
		attributeInterpretation.put(attribute2, true);
		attributeInterpretation.put(attribute3, true);
		attributeInterpretation.put(attribute4, true);
		attributeInterpretation.put(attribute5, true);

		dataDependencyInterpretation.put(Tuple.of(method1Id, attribute1Id), true);
		dataDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true);
		dataDependencyInterpretation.put(Tuple.of(method2Id, attribute2Id), true);
		dataDependencyInterpretation.put(Tuple.of(method3Id, attribute3Id), true);
		dataDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true);
		dataDependencyInterpretation.put(Tuple.of(method4Id, attribute3Id), true);
		dataDependencyInterpretation.put(Tuple.of(method4Id, attribute5Id), true);

		functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute3Id), true);
		functionalDependencyInterpretation.put(Tuple.of(method1Id, attribute4Id), true);
		functionalDependencyInterpretation.put(Tuple.of(method2Id, attribute1Id), true);
		functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute1Id), true);
		functionalDependencyInterpretation.put(Tuple.of(method3Id, attribute4Id), true);
		functionalDependencyInterpretation.put(Tuple.of(method4Id, attribute2Id), true);

		queryEngine.flushChanges();

		var states = dseAdapter.explore();
		System.out.println("states size: " + states.size());
	}

}