diff options
Diffstat (limited to 'subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java')
-rw-r--r-- | subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java b/subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java new file mode 100644 index 00000000..76639e18 --- /dev/null +++ b/subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.logic.literal; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.logic.Constraint; | ||
10 | import tools.refinery.logic.InvalidQueryException; | ||
11 | import tools.refinery.logic.dnf.Dnf; | ||
12 | import tools.refinery.logic.dnf.InvalidClauseException; | ||
13 | import tools.refinery.logic.term.*; | ||
14 | |||
15 | import java.util.List; | ||
16 | import java.util.Set; | ||
17 | |||
18 | import static org.hamcrest.MatcherAssert.assertThat; | ||
19 | import static org.hamcrest.Matchers.*; | ||
20 | import static org.junit.jupiter.api.Assertions.assertAll; | ||
21 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
22 | import static tools.refinery.logic.literal.Literals.not; | ||
23 | import static tools.refinery.logic.term.int_.IntTerms.INT_SUM; | ||
24 | import static tools.refinery.logic.term.int_.IntTerms.constant; | ||
25 | |||
26 | class AggregationLiteralTest { | ||
27 | private static final NodeVariable p = Variable.of("p"); | ||
28 | private static final DataVariable<Integer> x = Variable.of("x", Integer.class); | ||
29 | private static final DataVariable<Integer> y = Variable.of("y", Integer.class); | ||
30 | private static final DataVariable<Integer> z = Variable.of("z", Integer.class); | ||
31 | private static final Constraint fakeConstraint = new Constraint() { | ||
32 | @Override | ||
33 | public String name() { | ||
34 | return getClass().getName(); | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public List<Parameter> getParameters() { | ||
39 | return List.of( | ||
40 | new Parameter(null, ParameterDirection.OUT), | ||
41 | new Parameter(Integer.class, ParameterDirection.OUT) | ||
42 | ); | ||
43 | } | ||
44 | }; | ||
45 | |||
46 | @Test | ||
47 | void parameterDirectionTest() { | ||
48 | var literal = x.assign(fakeConstraint.aggregateBy(y, INT_SUM, p, y)); | ||
49 | assertAll( | ||
50 | () -> assertThat(literal.getOutputVariables(), containsInAnyOrder(x)), | ||
51 | () -> assertThat(literal.getInputVariables(Set.of()), empty()), | ||
52 | () -> assertThat(literal.getInputVariables(Set.of(p)), containsInAnyOrder(p)), | ||
53 | () -> assertThat(literal.getPrivateVariables(Set.of()), containsInAnyOrder(p, y)), | ||
54 | () -> assertThat(literal.getPrivateVariables(Set.of(p)), containsInAnyOrder(y)) | ||
55 | ); | ||
56 | } | ||
57 | |||
58 | @Test | ||
59 | void missingAggregationVariableTest() { | ||
60 | var aggregation = fakeConstraint.aggregateBy(y, INT_SUM, p, z); | ||
61 | assertThrows(InvalidQueryException.class, () -> x.assign(aggregation)); | ||
62 | } | ||
63 | |||
64 | @Test | ||
65 | void circularAggregationVariableTest() { | ||
66 | var aggregation = fakeConstraint.aggregateBy(x, INT_SUM, p, x); | ||
67 | assertThrows(InvalidQueryException.class, () -> x.assign(aggregation)); | ||
68 | } | ||
69 | |||
70 | @Test | ||
71 | void unboundTwiceVariableTest() { | ||
72 | var builder = Dnf.builder() | ||
73 | .clause( | ||
74 | not(fakeConstraint.call(p, y)), | ||
75 | x.assign(fakeConstraint.aggregateBy(y, INT_SUM, p, y)) | ||
76 | ); | ||
77 | assertThrows(InvalidClauseException.class, builder::build); | ||
78 | } | ||
79 | |||
80 | @Test | ||
81 | void unboundBoundVariableTest() { | ||
82 | var builder = Dnf.builder() | ||
83 | .clause( | ||
84 | y.assign(constant(27)), | ||
85 | x.assign(fakeConstraint.aggregateBy(y, INT_SUM, p, y)) | ||
86 | ); | ||
87 | assertThrows(InvalidClauseException.class, builder::build); | ||
88 | } | ||
89 | } | ||