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

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tools.refinery.store.query.literal.BooleanLiteral;
import tools.refinery.store.query.literal.Literal;
import tools.refinery.store.query.term.DataVariable;
import tools.refinery.store.query.term.NodeVariable;
import tools.refinery.store.query.term.ParameterDirection;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.query.view.AnySymbolView;
import tools.refinery.store.query.view.FunctionView;
import tools.refinery.store.query.view.KeyOnlyView;
import tools.refinery.store.representation.Symbol;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static tools.refinery.store.query.literal.Literals.not;
import static tools.refinery.store.query.term.int_.IntTerms.INT_SUM;

class VariableDirectionTest {
	private static final Symbol<Boolean> person = Symbol.of("Person", 1);
	private static final Symbol<Boolean> friend = Symbol.of("friend", 2);
	private static final Symbol<Integer> age = Symbol.of("age", 1, Integer.class);
	private static final AnySymbolView personView = new KeyOnlyView<>(person);
	private static final AnySymbolView friendView = new KeyOnlyView<>(friend);
	private static final FunctionView<Integer> ageView = new FunctionView<>(age);
	private static final NodeVariable p = Variable.of("p");
	private static final NodeVariable q = Variable.of("q");
	private static final DataVariable<Integer> x = Variable.of("x", Integer.class);
	private static final DataVariable<Integer> y = Variable.of("y", Integer.class);
	private static final DataVariable<Integer> z = Variable.of("z", Integer.class);

	@ParameterizedTest
	@MethodSource("clausesWithVariableInput")
	void unboundOutVariableTest(List<? extends Literal> clause) {
		var builder = Dnf.builder().parameter(p, ParameterDirection.OUT).clause(clause);
		assertThrows(InvalidClauseException.class, builder::build);
	}

	@ParameterizedTest
	@MethodSource("clausesWithVariableInput")
	void unboundInVariableTest(List<? extends Literal> clause) {
		var builder = Dnf.builder().parameter(p, ParameterDirection.IN).clause(clause);
		var dnf = assertDoesNotThrow(builder::build);
		var clauses = dnf.getClauses();
		if (clauses.size() > 0) {
			assertThat(clauses.get(0).positiveVariables(), hasItem(p));
		}
	}

	@ParameterizedTest
	@MethodSource("clausesWithVariableInput")
	void boundPrivateVariableTest(List<? extends Literal> clause) {
		var clauseWithBinding = new ArrayList<Literal>(clause);
		clauseWithBinding.add(personView.call(p));
		var builder = Dnf.builder().clause(clauseWithBinding);
		var dnf = assertDoesNotThrow(builder::build);
		var clauses = dnf.getClauses();
		if (clauses.size() > 0) {
			assertThat(clauses.get(0).positiveVariables(), hasItem(p));
		}
	}

	static Stream<Arguments> clausesWithVariableInput() {
		return Stream.concat(
				clausesNotBindingVariable(),
				literalToClauseArgumentStream(literalsWithRequiredVariableInput())
		);
	}

	@ParameterizedTest
	@MethodSource("clausesNotBindingVariable")
	void unboundPrivateVariableTest(List<? extends Literal> clause) {
		var builder = Dnf.builder().clause(clause);
		var dnf = assertDoesNotThrow(builder::build);
		var clauses = dnf.getClauses();
		if (clauses.size() > 0) {
			assertThat(clauses.get(0).positiveVariables(), not(hasItem(p)));
		}
	}

	@ParameterizedTest
	@MethodSource("clausesNotBindingVariable")
	void unboundByEquivalencePrivateVariableTest(List<? extends Literal> clause) {
		var r = Variable.of("r");
		var clauseWithEquivalence = new ArrayList<Literal>(clause);
		clauseWithEquivalence.add(r.isEquivalent(p));
		var builder = Dnf.builder().clause(clauseWithEquivalence);
		assertThrows(InvalidClauseException.class, builder::build);
	}

	static Stream<Arguments> clausesNotBindingVariable() {
		return Stream.concat(
				Stream.of(
					Arguments.of(List.of()),
					Arguments.of(List.of(BooleanLiteral.TRUE)),
					Arguments.of(List.of(BooleanLiteral.FALSE))
				),
				literalToClauseArgumentStream(literalsWithPrivateVariable())
		);
	}

	@ParameterizedTest
	@MethodSource("literalsWithPrivateVariable")
	void unboundTwicePrivateVariableTest(Literal literal) {
		var builder = Dnf.builder().clause(not(personView.call(p)), literal);
		assertThrows(InvalidClauseException.class, builder::build);
	}

	@ParameterizedTest
	@MethodSource("literalsWithPrivateVariable")
	void unboundTwiceByEquivalencePrivateVariableTest(Literal literal) {
		var r = Variable.of("r");
		var builder = Dnf.builder().clause(not(personView.call(r)), r.isEquivalent(p), literal);
		assertThrows(InvalidClauseException.class, builder::build);
	}

	static Stream<Arguments> literalsWithPrivateVariable() {
		var dnfWithOutput = Dnf.builder("WithOutput")
				.parameter(p, ParameterDirection.OUT)
				.parameter(q, ParameterDirection.OUT)
				.clause(friendView.call(p, q))
				.build();
		var dnfWithOutputToAggregate = Dnf.builder("WithOutputToAggregate")
				.parameter(p, ParameterDirection.OUT)
				.parameter(q, ParameterDirection.OUT)
				.parameter(x, ParameterDirection.OUT)
				.clause(
						friendView.call(p, q),
						ageView.call(q, x)
				)
				.build();

		return Stream.of(
				Arguments.of(not(friendView.call(p, q))),
				Arguments.of(y.assign(friendView.count(p, q))),
				Arguments.of(y.assign(ageView.aggregate(INT_SUM, p))),
				Arguments.of(not(dnfWithOutput.call(p, q))),
				Arguments.of(y.assign(dnfWithOutput.count(p, q))),
				Arguments.of(y.assign(dnfWithOutputToAggregate.aggregateBy(z, INT_SUM, p, q, z)))
		);
	}

	@ParameterizedTest
	@MethodSource("literalsWithRequiredVariableInput")
	void unboundPrivateVariableTest(Literal literal) {
		var builder = Dnf.builder().clause(literal);
		assertThrows(InvalidClauseException.class, builder::build);
	}

	@ParameterizedTest
	@MethodSource("literalsWithRequiredVariableInput")
	void boundPrivateVariableInputTest(Literal literal) {
		var builder = Dnf.builder().clause(personView.call(p), literal);
		var dnf = assertDoesNotThrow(builder::build);
		assertThat(dnf.getClauses().get(0).positiveVariables(), hasItem(p));
	}

	static Stream<Arguments> literalsWithRequiredVariableInput() {
		var dnfWithInput = Dnf.builder("WithInput")
				.parameter(p, ParameterDirection.IN)
				.parameter(q, ParameterDirection.OUT)
				.clause(friendView.call(p, q)).build();
		var dnfWithInputToAggregate = Dnf.builder("WithInputToAggregate")
				.parameter(p, ParameterDirection.IN)
				.parameter(q, ParameterDirection.OUT)
				.parameter(x, ParameterDirection.OUT)
				.clause(
						friendView.call(p, q),
						ageView.call(q, x)
				).build();

		return Stream.of(
				Arguments.of(dnfWithInput.call(p, q)),
				Arguments.of(dnfWithInput.call(p, p)),
				Arguments.of(not(dnfWithInput.call(p, q))),
				Arguments.of(not(dnfWithInput.call(p, p))),
				Arguments.of(y.assign(dnfWithInput.count(p, q))),
				Arguments.of(y.assign(dnfWithInput.count(p, p))),
				Arguments.of(y.assign(dnfWithInputToAggregate.aggregateBy(z, INT_SUM, p, q, z))),
				Arguments.of(y.assign(dnfWithInputToAggregate.aggregateBy(z, INT_SUM, p, p, z)))
		);
	}

	@ParameterizedTest
	@MethodSource("literalsWithVariableOutput")
	void boundParameterTest(Literal literal) {
		var builder = Dnf.builder().parameter(p, ParameterDirection.OUT).clause(literal);
		var dnf = assertDoesNotThrow(builder::build);
		assertThat(dnf.getClauses().get(0).positiveVariables(), hasItem(p));
	}

	@ParameterizedTest
	@MethodSource("literalsWithVariableOutput")
	void boundTwiceParameterTest(Literal literal) {
		var builder = Dnf.builder().parameter(p, ParameterDirection.IN).clause(literal);
		var dnf = assertDoesNotThrow(builder::build);
		assertThat(dnf.getClauses().get(0).positiveVariables(), hasItem(p));
	}

	@ParameterizedTest
	@MethodSource("literalsWithVariableOutput")
	void boundPrivateVariableOutputTest(Literal literal) {
		var dnfWithInput = Dnf.builder("WithInput")
				.parameter(p, ParameterDirection.IN)
				.clause(personView.call(p))
				.build();
		var builder = Dnf.builder().clause(dnfWithInput.call(p), literal);
		var dnf = assertDoesNotThrow(builder::build);
		assertThat(dnf.getClauses().get(0).positiveVariables(), hasItem(p));
	}

	@ParameterizedTest
	@MethodSource("literalsWithVariableOutput")
	void boundTwicePrivateVariableOutputTest(Literal literal) {
		var builder = Dnf.builder().clause(personView.call(p), literal);
		var dnf = assertDoesNotThrow(builder::build);
		assertThat(dnf.getClauses().get(0).positiveVariables(), hasItem(p));
	}

	static Stream<Arguments> literalsWithVariableOutput() {
		var dnfWithOutput = Dnf.builder("WithOutput")
				.parameter(p, ParameterDirection.OUT)
				.parameter(q, ParameterDirection.OUT)
				.clause(friendView.call(p, q))
				.build();

		return Stream.of(
				Arguments.of(friendView.call(p, q)),
				Arguments.of(dnfWithOutput.call(p, q))
		);
	}

	private static Stream<Arguments> literalToClauseArgumentStream(Stream<Arguments> literalArgumentsStream) {
		return literalArgumentsStream.map(arguments -> Arguments.of(List.of(arguments.get()[0])));
	}
}