diff options
Diffstat (limited to 'subprojects/logic/src/test/java/tools/refinery/logic/rewriter/DuplicateDnfRemoverTest.java')
-rw-r--r-- | subprojects/logic/src/test/java/tools/refinery/logic/rewriter/DuplicateDnfRemoverTest.java | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/subprojects/logic/src/test/java/tools/refinery/logic/rewriter/DuplicateDnfRemoverTest.java b/subprojects/logic/src/test/java/tools/refinery/logic/rewriter/DuplicateDnfRemoverTest.java new file mode 100644 index 00000000..7b2ce8b2 --- /dev/null +++ b/subprojects/logic/src/test/java/tools/refinery/logic/rewriter/DuplicateDnfRemoverTest.java | |||
@@ -0,0 +1,162 @@ | |||
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.rewriter; | ||
7 | |||
8 | import org.junit.jupiter.api.BeforeEach; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | import tools.refinery.logic.Constraint; | ||
11 | import tools.refinery.logic.dnf.Query; | ||
12 | import tools.refinery.logic.literal.AbstractCallLiteral; | ||
13 | import tools.refinery.logic.literal.Reduction; | ||
14 | import tools.refinery.logic.term.Variable; | ||
15 | import tools.refinery.logic.tests.FakeKeyOnlyView; | ||
16 | |||
17 | import java.util.List; | ||
18 | |||
19 | import static org.hamcrest.MatcherAssert.assertThat; | ||
20 | import static org.hamcrest.Matchers.is; | ||
21 | import static org.hamcrest.Matchers.not; | ||
22 | import static tools.refinery.logic.literal.Literals.not; | ||
23 | |||
24 | class DuplicateDnfRemoverTest { | ||
25 | private final static Constraint friendView = new FakeKeyOnlyView("friend", 2); | ||
26 | |||
27 | private DuplicateDnfRemover sut; | ||
28 | |||
29 | @BeforeEach | ||
30 | void beforeEach() { | ||
31 | sut = new DuplicateDnfRemover(); | ||
32 | } | ||
33 | |||
34 | @Test | ||
35 | void removeDuplicateSimpleTest() { | ||
36 | var one = Query.of("One", (builder, x, y) -> builder.clause( | ||
37 | friendView.call(x, y), | ||
38 | friendView.call(y, x) | ||
39 | )); | ||
40 | var two = Query.of("Two", (builder, x, y) -> builder.clause( | ||
41 | friendView.call(x, y), | ||
42 | friendView.call(y, x) | ||
43 | )); | ||
44 | |||
45 | var oneResult = sut.rewrite(one); | ||
46 | var twoResult = sut.rewrite(two); | ||
47 | |||
48 | assertThat(oneResult, is(twoResult)); | ||
49 | assertThat(one, is(oneResult)); | ||
50 | } | ||
51 | |||
52 | @Test | ||
53 | void notDuplicateSimpleTest() { | ||
54 | var one = Query.of("One", (builder, x, y) -> builder.clause( | ||
55 | friendView.call(x, y), | ||
56 | friendView.call(y, x) | ||
57 | )); | ||
58 | var two = Query.of("Two", (builder, x, y) -> builder.clause((z) -> List.of( | ||
59 | friendView.call(x, y), | ||
60 | friendView.call(y, z) | ||
61 | ))); | ||
62 | |||
63 | var oneResult = sut.rewrite(one); | ||
64 | var twoResult = sut.rewrite(two); | ||
65 | |||
66 | assertThat(one, is(oneResult)); | ||
67 | assertThat(two, is(twoResult)); | ||
68 | } | ||
69 | |||
70 | @Test | ||
71 | void removeDuplicateRecursiveTest() { | ||
72 | var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause( | ||
73 | friendView.call(x, y), | ||
74 | friendView.call(y, x) | ||
75 | )); | ||
76 | var one = Query.of("One", (builder, x) -> builder.clause( | ||
77 | oneSubQuery.call(x, Variable.of()) | ||
78 | )); | ||
79 | var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause( | ||
80 | friendView.call(x, y), | ||
81 | friendView.call(y, x) | ||
82 | )); | ||
83 | var two = Query.of("Two", (builder, x) -> builder.clause( | ||
84 | twoSubQuery.call(x, Variable.of()) | ||
85 | )); | ||
86 | |||
87 | var oneResult = sut.rewrite(one); | ||
88 | var twoResult = sut.rewrite(two); | ||
89 | |||
90 | assertThat(oneResult, is(twoResult)); | ||
91 | assertThat(one, is(oneResult)); | ||
92 | } | ||
93 | |||
94 | @Test | ||
95 | void notDuplicateRecursiveTest() { | ||
96 | var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause( | ||
97 | friendView.call(x, y), | ||
98 | friendView.call(y, x) | ||
99 | )); | ||
100 | var one = Query.of("One", (builder, x) -> builder.clause( | ||
101 | oneSubQuery.call(x, Variable.of()) | ||
102 | )); | ||
103 | var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause( | ||
104 | friendView.call(x, y), | ||
105 | friendView.call(y, x) | ||
106 | )); | ||
107 | var two = Query.of("Two", (builder, x) -> builder.clause( | ||
108 | twoSubQuery.call(Variable.of(), x) | ||
109 | )); | ||
110 | |||
111 | var oneResult = sut.rewrite(one); | ||
112 | var twoResult = sut.rewrite(two); | ||
113 | |||
114 | assertThat(one, is(oneResult)); | ||
115 | assertThat(oneResult, is(not(twoResult))); | ||
116 | |||
117 | var oneCall = (AbstractCallLiteral) oneResult.getDnf().getClauses().getFirst().literals().getFirst(); | ||
118 | var twoCall = (AbstractCallLiteral) twoResult.getDnf().getClauses().getFirst().literals().getFirst(); | ||
119 | |||
120 | assertThat(oneCall.getTarget(), is(twoCall.getTarget())); | ||
121 | } | ||
122 | |||
123 | @Test | ||
124 | void removeContradictionTest() { | ||
125 | var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause( | ||
126 | friendView.call(x, y), | ||
127 | friendView.call(y, x) | ||
128 | )); | ||
129 | var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause( | ||
130 | friendView.call(x, y), | ||
131 | friendView.call(y, x) | ||
132 | )); | ||
133 | var query = Query.of("Contradiction", (builder, x, y) -> builder.clause( | ||
134 | oneSubQuery.call(x, y), | ||
135 | not(twoSubQuery.call(x, y)) | ||
136 | )); | ||
137 | |||
138 | var result = sut.rewrite(query); | ||
139 | |||
140 | assertThat(result.getDnf().getReduction(), is(Reduction.ALWAYS_FALSE)); | ||
141 | } | ||
142 | |||
143 | @Test | ||
144 | void removeQuantifiedContradictionTest() { | ||
145 | var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause( | ||
146 | friendView.call(x, y), | ||
147 | friendView.call(y, x) | ||
148 | )); | ||
149 | var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause( | ||
150 | friendView.call(x, y), | ||
151 | friendView.call(y, x) | ||
152 | )); | ||
153 | var query = Query.of("Contradiction", (builder, x) -> builder.clause( | ||
154 | oneSubQuery.call(x, Variable.of()), | ||
155 | not(twoSubQuery.call(x, Variable.of())) | ||
156 | )); | ||
157 | |||
158 | var result = sut.rewrite(query); | ||
159 | |||
160 | assertThat(result.getDnf().getReduction(), is(Reduction.ALWAYS_FALSE)); | ||
161 | } | ||
162 | } | ||