aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/logic/src/test/java/tools/refinery/logic/literal/AggregationLiteralTest.java
diff options
context:
space:
mode:
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.java89
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 */
6package tools.refinery.logic.literal;
7
8import org.junit.jupiter.api.Test;
9import tools.refinery.logic.Constraint;
10import tools.refinery.logic.InvalidQueryException;
11import tools.refinery.logic.dnf.Dnf;
12import tools.refinery.logic.dnf.InvalidClauseException;
13import tools.refinery.logic.term.*;
14
15import java.util.List;
16import java.util.Set;
17
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.*;
20import static org.junit.jupiter.api.Assertions.assertAll;
21import static org.junit.jupiter.api.Assertions.assertThrows;
22import static tools.refinery.logic.literal.Literals.not;
23import static tools.refinery.logic.term.int_.IntTerms.INT_SUM;
24import static tools.refinery.logic.term.int_.IntTerms.constant;
25
26class 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}