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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.literal.AbstractCallLiteral;
import tools.refinery.store.query.literal.Reduction;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.query.view.AnySymbolView;
import tools.refinery.store.query.view.KeyOnlyView;
import tools.refinery.store.representation.Symbol;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static tools.refinery.store.query.literal.Literals.not;

class DuplicateDnfRemoverTest {
	private final static Symbol<Boolean> friend = Symbol.of("friend", 2);
	private final static AnySymbolView friendView = new KeyOnlyView<>(friend);

	private DuplicateDnfRemover sut;

	@BeforeEach
	void beforeEach() {
		sut = new DuplicateDnfRemover();
	}

	@Test
	void removeDuplicateSimpleTest() {
		var one = Query.of("One", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var two = Query.of("Two", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));

		var oneResult = sut.rewrite(one);
		var twoResult = sut.rewrite(two);

		assertThat(oneResult, is(twoResult));
		assertThat(one, is(oneResult));
	}

	@Test
	void notDuplicateSimpleTest() {
		var one = Query.of("One", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var two = Query.of("Two", (builder, x, y) -> builder.clause((z) -> List.of(
				friendView.call(x, y),
				friendView.call(y, z)
		)));

		var oneResult = sut.rewrite(one);
		var twoResult = sut.rewrite(two);

		assertThat(one, is(oneResult));
		assertThat(two, is(twoResult));
	}

	@Test
	void removeDuplicateRecursiveTest() {
		var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var one = Query.of("One", (builder, x) -> builder.clause(
				oneSubQuery.call(x, Variable.of())
		));
		var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var two = Query.of("Two", (builder, x) -> builder.clause(
				twoSubQuery.call(x, Variable.of())
		));

		var oneResult = sut.rewrite(one);
		var twoResult = sut.rewrite(two);

		assertThat(oneResult, is(twoResult));
		assertThat(one, is(oneResult));
	}

	@Test
	void notDuplicateRecursiveTest() {
		var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var one = Query.of("One", (builder, x) -> builder.clause(
				oneSubQuery.call(x, Variable.of())
		));
		var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var two = Query.of("Two", (builder, x) -> builder.clause(
				twoSubQuery.call(Variable.of(), x)
		));

		var oneResult = sut.rewrite(one);
		var twoResult = sut.rewrite(two);

		assertThat(one, is(oneResult));
		assertThat(oneResult, is(not(twoResult)));

		var oneCall = (AbstractCallLiteral) oneResult.getDnf().getClauses().get(0).literals().get(0);
		var twoCall = (AbstractCallLiteral) twoResult.getDnf().getClauses().get(0).literals().get(0);

		assertThat(oneCall.getTarget(), is(twoCall.getTarget()));
	}

	@Test
	void removeContradictionTest() {
		var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var query = Query.of("Contradiction", (builder, x, y) -> builder.clause(
				oneSubQuery.call(x, y),
				not(twoSubQuery.call(x, y))
		));

		var result = sut.rewrite(query);

		assertThat(result.getDnf().getReduction(), is(Reduction.ALWAYS_FALSE));
	}

	@Test
	void removeQuantifiedContradictionTest() {
		var oneSubQuery = Query.of("OneSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var twoSubQuery = Query.of("TwoSubQuery", (builder, x, y) -> builder.clause(
				friendView.call(x, y),
				friendView.call(y, x)
		));
		var query = Query.of("Contradiction", (builder, x) -> builder.clause(
				oneSubQuery.call(x, Variable.of()),
				not(twoSubQuery.call(x, Variable.of()))
		));

		var result = sut.rewrite(query);

		assertThat(result.getDnf().getReduction(), is(Reduction.ALWAYS_FALSE));
	}
}