aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/test/java/tools/refinery/store/dse/DebugTest.java
blob: c6da896c9bb901358e6312518cc396c7ac7b09b3 (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
/*
 * 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.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;

class DebugTest {
	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> isEncapsulatedBy = Symbol.of("IsEncapsulatedBy", 2);
	private static final Symbol<Boolean> encapsulates = Symbol.of("Encapsulates", 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 isEncapsulatedByView = new KeyOnlyView<>(isEncapsulatedBy);
	private static final AnySymbolView encapsulatesView = new KeyOnlyView<>(encapsulates);
	private static final AnySymbolView featuresView = new KeyOnlyView<>(features);
	private static final AnySymbolView classesView = new KeyOnlyView<>(classes);


	@Test
	@Disabled("This test is only for debugging purposes")
	void BFSTest() {
		var createClassPrecondition = Query.of("CreateClassPrecondition",
				(builder, model) -> builder.clause(
						classModelView.call(model)
				));

		var createClassRule = new TransformationRule("CreateClass",
				createClassPrecondition,
				(model) -> {
					var classesInterpretation = model.getInterpretation(classes);
					var classElementInterpretation = model.getInterpretation(classElement);
					return ((Tuple activation) -> {
						var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
						var modelElement = activation.get(0);

						var newClassElement = dseAdapter.createObject();
						var newClassElementId = newClassElement.get(0);

						classesInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
						classElementInterpretation.put(Tuple.of(newClassElementId), true);
					});
				});

		var createFeaturePrecondition = Query.of("CreateFeaturePrecondition",
				(builder, model) -> builder.clause(
						classModelView.call(model)
				));

		var createFeatureRule = new TransformationRule("CreateFeature",
				createFeaturePrecondition,
				(model) -> {
					var featuresInterpretation = model.getInterpretation(features);
					var featureInterpretation = model.getInterpretation(feature);
					return ((Tuple activation) -> {
						var dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
						var modelElement = activation.get(0);

						var newClassElement = dseAdapter.createObject();
						var newClassElementId = newClassElement.get(0);

						featuresInterpretation.put(Tuple.of(modelElement, newClassElementId), true);
						featureInterpretation.put(Tuple.of(newClassElementId), true);
					});
				});

		var store = ModelStore.builder()
				.symbols(classModel, classElement, feature, isEncapsulatedBy, encapsulates, classes, features)
				.with(ViatraModelQueryAdapter.builder()
						.queries(createClassPrecondition, createFeaturePrecondition))
				.with(ModelVisualizerAdapter.builder()
						.withOutputpath("test_output")
						.withFormat(FileFormat.DOT)
						.withFormat(FileFormat.SVG)
						.saveStates()
						.saveDesignSpace()
				)
				.with(DesignSpaceExplorationAdapter.builder()
						.transformations(createClassRule, createFeatureRule)
						.objectives(new AlwaysSatisfiedRandomHardObjective())
						.strategy(new DepthFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled()
//						.strategy(new BestFirstStrategy().withDepthLimit(4).continueIfHardObjectivesFulfilled()
//								.goOnOnlyIfFitnessIsBetter()
						))
				.build();

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

		var modelElementInterpretation = model.getInterpretation(classModel);
		var classElementInterpretation = model.getInterpretation(classElement);
		var modelElement = dseAdapter.createObject();
		modelElementInterpretation.put(modelElement, true);
		classElementInterpretation.put(modelElement, true);
		queryEngine.flushChanges();


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

	}
}