aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java
blob: be675a802eaf62c489c617601693751245fd0140 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.semantics;

import com.google.inject.Inject;
import com.google.inject.Provider;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tools.refinery.language.model.tests.utils.ProblemParseHelper;
import tools.refinery.language.tests.ProblemInjectorProvider;
import tools.refinery.store.dse.propagation.PropagationAdapter;
import tools.refinery.store.dse.strategy.BestFirstStoreManager;
import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.query.interpreter.QueryInterpreterAdapter;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.ReasoningStoreAdapter;
import tools.refinery.store.reasoning.literal.Concreteness;
import tools.refinery.store.statecoding.StateCoderAdapter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@ExtendWith(InjectionExtension.class)
@InjectWith(ProblemInjectorProvider.class)
class SolutionSerializerTest {
	@Inject
	private ProblemParseHelper parseHelper;

	@Inject
	private Provider<ModelInitializer> initializerProvider;

	@Inject
	private Provider<SolutionSerializer> serializerProvider;

	@ParameterizedTest
	@MethodSource
	void solutionSerializerTest(String prefix, String input, String expectedOutput) throws IOException {
		var problem = parseHelper.parse(prefix + "\n" + input).problem();
		var storeBuilder = ModelStore.builder()
				.with(QueryInterpreterAdapter.builder())
				.with(PropagationAdapter.builder())
				.with(StateCoderAdapter.builder())
				.with(DesignSpaceExplorationAdapter.builder())
				.with(ReasoningAdapter.builder()
						.requiredInterpretations(Set.of(Concreteness.CANDIDATE)));
		var initializer = initializerProvider.get();
		var modelSeed = initializer.createModel(problem, storeBuilder);
		var store = storeBuilder.build();
		var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed);
		var initialVersion = model.commit();
		var bestFirst = new BestFirstStoreManager(store, 1);
		bestFirst.startExploration(initialVersion, 0);
		model.restore(bestFirst.getSolutionStore().getSolutions().getFirst().version());
		var serializer = serializerProvider.get();
		var solution = serializer.serializeSolution(initializer.getProblemTrace(), model);
		String actualOutput;
		try (var outputStream = new ByteArrayOutputStream()) {
			solution.eResource().save(outputStream, Map.of());
			actualOutput = outputStream.toString();
		}
		assertThat(actualOutput, is(prefix + "\n" + expectedOutput));
	}

	static Stream<Arguments> solutionSerializerTest() {
		return Stream.of(Arguments.of("""
				class Foo.
				""", """
				scope Foo = 3.
				""", """
				declare foo1, foo2, foo3.
				!exists(Foo::new).
				Foo(foo1).
				Foo(foo2).
				Foo(foo3).
				"""), Arguments.of("""
				class Foo {
					contains Bar[2] bars
				}

				class Bar.
				""", """
				scope Foo = 1.
				""", """
				declare foo1, bar1, bar2.
				!exists(Foo::new).
				!exists(Bar::new).
				Foo(foo1).
				Bar(bar1).
				Bar(bar2).
				bars(foo1, bar1).
				bars(foo1, bar2).
				"""), Arguments.of("""
				class Foo {
					Bar[2] bars opposite foo
				}

				class Bar {
					Foo[1] foo opposite bars
				}
				""", """
				scope Foo = 1, Bar = 2.
				""", """
				declare foo1, bar1, bar2.
				!exists(Foo::new).
				!exists(Bar::new).
				Foo(foo1).
				Bar(bar1).
				Bar(bar2).
				default !bars(*, *).
				bars(foo1, bar1).
				bars(foo1, bar2).
				"""), Arguments.of("""
				class Person {
					Person[2] friend opposite friend
				}
				""", """
				friend(a, b).
				friend(a, c).
				friend(b, c).

				scope Person += 0.
				""", """
				declare a, b, c.
				!exists(Person::new).
				Person(a).
				Person(b).
				Person(c).
				default !friend(*, *).
				friend(a, b).
				friend(a, c).
				friend(b, a).
				friend(b, c).
				friend(c, a).
				friend(c, b).
				"""), Arguments.of("""
				class Foo {
					Bar bar
				}

				enum Bar {
					BAR_A,
					BAR_B
				}
				""", """
				bar(foo, BAR_A).

				scope Foo += 0.
				""", """
				declare foo.
				!exists(Foo::new).
				Foo(foo).
				default !bar(*, *).
				bar(foo, BAR_A).
				"""), Arguments.of("""
				class Foo.
				class Bar extends Foo.
				""", """
				scope Foo = 1, Bar = 0.
				""", """
				declare foo1.
				!exists(Foo::new).
				!exists(Bar::new).
				Foo(foo1).
				!Bar(foo1).
				"""), Arguments.of("""
				class Foo {
					Foo[] ref
				}
				""", """
				ref(a, b).
				!exists(b).

				scope Foo += 0.
				""", """
				declare a.
				!exists(Foo::new).
				Foo(a).
				default !ref(*, *).
				"""), Arguments.of("""
				atom a.
				class Foo.
				""", """
				Foo(a).
				scope Foo += 0.
				""", """
				!exists(Foo::new).
				Foo(a).
				"""), Arguments.of("""
				multi a.
				class Foo.
				""", """
				Foo(a).
				!exists(Foo::new).
				scope Foo = 2.
				""", """
				declare foo1, foo2.
				!exists(a).
				!exists(Foo::new).
				Foo(foo1).
				Foo(foo2).
				"""), Arguments.of("""
				declare a.
				class Foo.
				""", """
				Foo(a).
				?exists(a).
				scope Foo = 2, Foo += 1.
				""", """
				declare foo1.
				!exists(Foo::new).
				Foo(a).
				Foo(foo1).
				"""), Arguments.of("""
				declare a.
				class Foo.
				""", """
				Foo(a).
				?exists(a).
				scope Foo = 1, Foo += 1.
				""", """
				declare foo1.
				!exists(a).
				!exists(Foo::new).
				Foo(foo1).
				"""));
	}
}