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

import com.google.inject.Inject;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
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.problem.*;
import tools.refinery.language.model.tests.utils.ProblemParseHelper;
import tools.refinery.language.tests.ProblemInjectorProvider;
import tools.refinery.language.utils.ContainmentRole;
import tools.refinery.language.utils.ProblemDesugarer;

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

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

	@Inject
	private ProblemDesugarer desugarer;

	@Test
	void implicitNodeTest() {
		var problem = parseHelper.parse("""
				exists(a).
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var node = problem.node("a");
		assertThat(collectedSymbols.nodes(), hasKey(node));
		assertThat(collectedSymbols.nodes().get(node).individual(), is(false));
	}

	@Test
	void individualNodeTest() {
		var problem = parseHelper.parse("""
				indiv a.
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var node = problem.individualNode("a");
		assertThat(collectedSymbols.nodes(), hasKey(node));
		assertThat(collectedSymbols.nodes().get(node).individual(), is(true));
	}

	@Test
	void classTest() {
		var problem = parseHelper.parse("""
				class Foo.
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var classDeclaration = problem.findClass("Foo").get();
		assertThat(collectedSymbols.relations(), hasKey(classDeclaration));
		var classInfo = collectedSymbols.relations().get(classDeclaration);
		assertThat(classInfo.parameters(), hasSize(1));
		assertThat(classInfo.containmentRole(), is(ContainmentRole.CONTAINED));
		assertThat(classInfo.hasDefinition(), is(false));
		var newNode = classDeclaration.getNewNode();
		assertThat(collectedSymbols.nodes(), hasKey(newNode));
		assertThat(collectedSymbols.nodes().get(newNode).individual(), is(false));
		assertThat(classInfo.assertions(), assertsNode(newNode, LogicValue.TRUE));
		assertThat(collectedSymbols.relations().get(problem.builtinSymbols().exists()).assertions(),
				assertsNode(newNode, LogicValue.UNKNOWN));
		assertThat(collectedSymbols.relations().get(problem.builtinSymbols().equals()).assertions(),
				assertsNode(newNode, LogicValue.UNKNOWN));
	}

	@Test
	void abstractClassTest() {
		var problem = parseHelper.parse("""
				abstract class Foo.
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.findClass("Foo").get()).assertions(), hasSize(0));
	}

	@Test
	void referenceTest() {
		var problem = parseHelper.parse("""
				class Foo {
					refers Foo[] bar opposite quux
					refers Foo quux opposite bar
				}
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var fooClass = problem.findClass("Foo");
		var barReference = fooClass.feature("bar");
		var barInfo = collectedSymbols.relations().get(barReference);
		var quuxReference = fooClass.feature("quux");
		var quuxInfo = collectedSymbols.relations().get(quuxReference);
		assertThat(barInfo.containmentRole(), is(ContainmentRole.NONE));
		assertThat(barInfo.opposite(), is(quuxReference));
		assertThat(barInfo.multiplicity(), is(instanceOf(UnboundedMultiplicity.class)));
		assertThat(barInfo.hasDefinition(), is(false));
		assertThat(quuxInfo.containmentRole(), is(ContainmentRole.NONE));
		assertThat(quuxInfo.opposite(), is(barReference));
		assertThat(quuxInfo.multiplicity(), is(instanceOf(ExactMultiplicity.class)));
		assertThat(quuxInfo.multiplicity(), hasProperty("exactValue", is(1)));
		assertThat(quuxInfo.hasDefinition(), is(false));
	}

	@Test
	void containmentReferenceTest() {
		var problem = parseHelper.parse("""
				class Foo {
					contains Foo[] bar
				}
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.findClass("Foo").feature("bar")).containmentRole(),
				is(ContainmentRole.CONTAINMENT));
	}

	@Disabled("TODO: Rework numerical references")
	@Test
	void dataReferenceTest() {
		var problem = parseHelper.parse("""
				class Foo {
					int bar
				}
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.findClass("Foo").feature("bar")).containmentRole(),
				is(ContainmentRole.CONTAINMENT));
	}

	@Test
	void enumTest() {
		var problem = parseHelper.parse("""
				enum Foo {
					bar, quux
				}
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var enumDeclaration = problem.findEnum("Foo");
		var enumInfo = collectedSymbols.relations().get(enumDeclaration.get());
		assertThat(enumInfo.containmentRole(), is(ContainmentRole.NONE));
		assertThat(enumInfo.assertions(), assertsNode(enumDeclaration.literal("bar"), LogicValue.TRUE));
		assertThat(enumInfo.assertions(), assertsNode(enumDeclaration.literal("quux"), LogicValue.TRUE));
	}

	@ParameterizedTest
	@MethodSource
	void predicateTest(String keyword, ContainmentRole containmentRole) {
		var problem = parseHelper.parse(keyword + " foo(node x) <-> domain(x); data(x).");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var predicateInfo = collectedSymbols.relations().get(problem.pred("foo").get());
		assertThat(predicateInfo.containmentRole(), is(containmentRole));
		assertThat(predicateInfo.parameters(), hasSize(1));
		assertThat(predicateInfo.bodies(), hasSize(2));
		assertThat(predicateInfo.hasDefinition(), is(true));
	}

	static Stream<Arguments> predicateTest() {
		return Stream.of(Arguments.of("pred", ContainmentRole.NONE), Arguments.of("error", ContainmentRole.NONE),
				Arguments.of("contained pred", ContainmentRole.CONTAINED), Arguments.of("containment pred",
						ContainmentRole.CONTAINMENT));
	}

	@ParameterizedTest
	@MethodSource("logicValues")
	void assertionTest(String keyword, LogicValue value) {
		var problem = parseHelper.parse("""
				pred foo(node x).
				foo(a): %s.
				""".formatted(keyword));
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(),
				assertsNode(problem.node("a"), value));
	}

	@ParameterizedTest
	@MethodSource("logicValues")
	void defaultAssertionTest(String keyword, LogicValue value) {
		var problem = parseHelper.parse("""
				pred foo(node x).
				default foo(a): %s.
				""".formatted(keyword));
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(),
				assertsNode(problem.node("a"), value));
	}

	static Stream<Arguments> logicValues() {
		return Stream.of(Arguments.of("true", LogicValue.TRUE), Arguments.of("false", LogicValue.FALSE),
				Arguments.of("unknown", LogicValue.UNKNOWN), Arguments.of("error", LogicValue.ERROR));
	}

	@Test
	void invalidAssertionArityTest() {
		var problem = parseHelper.parse("""
				pred foo(node x).
				foo(a, b).
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(), hasSize(0));
	}

	@Test
	void invalidProblemTest() {
		var problem = parseHelper.parse("""
				class Foo {
					bar[] opposite quux
					Foo quux opposite bar
				}
				""").get();
		assertDoesNotThrow(() -> desugarer.collectSymbols(problem));
	}

	@Test
	void errorAssertionTest() {
		var problem = parseHelper.parse("""
				error foo(node a, node b) <-> equals(a, b).
				""");
		var collectedSymbols = desugarer.collectSymbols(problem.get());
		var fooInfo = collectedSymbols.relations().get(problem.pred("foo").get());
		assertThat(fooInfo.assertions(), hasSize(1));
		var assertion = fooInfo.assertions().stream().findFirst().orElseThrow();
		assertThat(assertion.getValue(), hasProperty("logicValue", is(LogicValue.FALSE)));
		assertThat(assertion.getArguments(), hasSize(2));
		assertThat(assertion.getArguments(), everyItem(instanceOf(WildcardAssertionArgument.class)));
	}

	private static Matcher<Iterable<? super Assertion>> assertsNode(Node node, LogicValue value) {
		return hasItem(allOf(hasProperty("arguments", hasItem(hasProperty("node", is(node)))), hasProperty("value",
				hasProperty("logicValue", is(value)))));
	}
}