aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/ClausePostProcessor.java
blob: b5e7092b6f44f93181aa03292c0d0dc11c5e8385 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.dnf;

import org.jetbrains.annotations.NotNull;
import tools.refinery.store.query.literal.BooleanLiteral;
import tools.refinery.store.query.literal.EquivalenceLiteral;
import tools.refinery.store.query.literal.Literal;
import tools.refinery.store.query.substitution.MapBasedSubstitution;
import tools.refinery.store.query.substitution.StatelessSubstitution;
import tools.refinery.store.query.substitution.Substitution;
import tools.refinery.store.query.term.NodeVariable;
import tools.refinery.store.query.term.ParameterDirection;
import tools.refinery.store.query.term.Variable;

import java.util.*;
import java.util.function.Function;

class ClausePostProcessor {
	private final Map<Variable, ParameterInfo> parameters;
	private final List<Literal> literals;
	private final Map<NodeVariable, NodeVariable> representatives = new LinkedHashMap<>();
	private final Map<NodeVariable, Set<NodeVariable>> equivalencePartition = new HashMap<>();
	private List<Literal> substitutedLiterals;
	private final Set<Variable> existentiallyQuantifiedVariables = new LinkedHashSet<>();
	private Set<Variable> positiveVariables;
	private Map<Variable, Set<SortableLiteral>> variableToLiteralInputMap;
	private PriorityQueue<SortableLiteral> literalsWithAllInputsBound;
	private LinkedHashSet<Literal> topologicallySortedLiterals;

	public ClausePostProcessor(Map<Variable, ParameterInfo> parameters, List<Literal> literals) {
		this.parameters = parameters;
		this.literals = literals;
	}

	public Result postProcessClause() {
		mergeEquivalentNodeVariables();
		substitutedLiterals = new ArrayList<>(literals.size());
		keepParameterEquivalences();
		substituteLiterals();
		computeExistentiallyQuantifiedVariables();
		computePositiveVariables();
		validatePositiveRepresentatives();
		validatePrivateVariables();
		topologicallySortLiterals();
		var filteredLiterals = new ArrayList<Literal>(topologicallySortedLiterals.size());
		for (var literal : topologicallySortedLiterals) {
			var reducedLiteral = literal.reduce();
			if (BooleanLiteral.FALSE.equals(reducedLiteral)) {
				return ConstantResult.ALWAYS_FALSE;
			} else if (!BooleanLiteral.TRUE.equals(reducedLiteral)) {
				filteredLiterals.add(reducedLiteral);
			}
		}
		if (filteredLiterals.isEmpty()) {
			return ConstantResult.ALWAYS_TRUE;
		}
		var clause = new DnfClause(Collections.unmodifiableSet(positiveVariables),
				Collections.unmodifiableList(filteredLiterals));
		return new ClauseResult(clause);
	}

	private void mergeEquivalentNodeVariables() {
		for (var literal : literals) {
			if (isPositiveEquivalence(literal)) {
				var equivalenceLiteral = (EquivalenceLiteral) literal;
				mergeVariables(equivalenceLiteral.left(), equivalenceLiteral.right());
			}
		}
	}

	private static boolean isPositiveEquivalence(Literal literal) {
		return literal instanceof EquivalenceLiteral equivalenceLiteral && equivalenceLiteral.positive();
	}

	private void mergeVariables(NodeVariable left, NodeVariable right) {
		var leftRepresentative = getRepresentative(left);
		var rightRepresentative = getRepresentative(right);
		var leftInfo = parameters.get(leftRepresentative);
		var rightInfo = parameters.get(rightRepresentative);
		if (leftInfo != null && (rightInfo == null || leftInfo.index() <= rightInfo.index())) {
			// Prefer the variable occurring earlier in the parameter list as a representative.
			doMergeVariables(leftRepresentative, rightRepresentative);
		} else {
			doMergeVariables(rightRepresentative, leftRepresentative);
		}
	}

	private void doMergeVariables(NodeVariable parentRepresentative, NodeVariable newChildRepresentative) {
		var parentSet = getEquivalentVariables(parentRepresentative);
		var childSet = getEquivalentVariables(newChildRepresentative);
		parentSet.addAll(childSet);
		equivalencePartition.remove(newChildRepresentative);
		for (var childEquivalentNodeVariable : childSet) {
			representatives.put(childEquivalentNodeVariable, parentRepresentative);
		}
	}

	private NodeVariable getRepresentative(NodeVariable variable) {
		return representatives.computeIfAbsent(variable, Function.identity());
	}

	private Set<NodeVariable> getEquivalentVariables(NodeVariable variable) {
		var representative = getRepresentative(variable);
		if (!representative.equals(variable)) {
			throw new AssertionError("NodeVariable %s already has a representative %s"
					.formatted(variable, representative));
		}
		return equivalencePartition.computeIfAbsent(variable, key -> {
			var set = new HashSet<NodeVariable>(1);
			set.add(key);
			return set;
		});
	}

	private void keepParameterEquivalences() {
		for (var pair : representatives.entrySet()) {
			var left = pair.getKey();
			var right = pair.getValue();
			if (!left.equals(right) && parameters.containsKey(left) && parameters.containsKey(right)) {
				substitutedLiterals.add(left.isEquivalent(right));
			}
		}
	}

	private void substituteLiterals() {
		Substitution substitution;
		if (representatives.isEmpty()) {
			substitution = null;
		} else {
			substitution = new MapBasedSubstitution(Collections.unmodifiableMap(representatives),
					StatelessSubstitution.IDENTITY);
		}
		for (var literal : literals) {
			if (isPositiveEquivalence(literal)) {
				// We already retained all equivalences that cannot be replaced with substitutions in
				// {@link#keepParameterEquivalences()}.
				continue;
			}
			var substitutedLiteral = substitution == null ? literal : literal.substitute(substitution);
			substitutedLiterals.add(substitutedLiteral);
		}
	}

	private void computeExistentiallyQuantifiedVariables() {
		for (var literal : substitutedLiterals) {
			for (var variable : literal.getOutputVariables()) {
				boolean added = existentiallyQuantifiedVariables.add(variable);
				if (!variable.isUnifiable()) {
					var parameterInfo = parameters.get(variable);
					if (parameterInfo != null && parameterInfo.direction() == ParameterDirection.IN) {
						throw new IllegalArgumentException("Trying to bind %s parameter %s"
								.formatted(ParameterDirection.IN, variable));
					}
					if (!added) {
						throw new IllegalArgumentException("Variable %s has multiple assigned values"
								.formatted(variable));
					}
				}
			}
		}
	}

	private void computePositiveVariables() {
		positiveVariables = new LinkedHashSet<>();
		for (var pair : parameters.entrySet()) {
			var variable = pair.getKey();
			if (pair.getValue().direction() == ParameterDirection.IN) {
				// Inputs count as positive, because they are already bound when we evaluate literals.
				positiveVariables.add(variable);
			} else if (!existentiallyQuantifiedVariables.contains(variable)) {
				throw new IllegalArgumentException("Unbound %s parameter %s"
						.formatted(ParameterDirection.OUT, variable));
			}
		}
		positiveVariables.addAll(existentiallyQuantifiedVariables);
	}

	private void validatePositiveRepresentatives() {
		for (var pair : equivalencePartition.entrySet()) {
			var representative = pair.getKey();
			if (!positiveVariables.contains(representative)) {
				var variableSet = pair.getValue();
				throw new IllegalArgumentException("Variables %s were merged by equivalence but are not bound"
						.formatted(variableSet));
			}
		}
	}

	private void validatePrivateVariables() {
		var negativeVariablesMap = new HashMap<Variable, Literal>();
		for (var literal : substitutedLiterals) {
			for (var variable : literal.getPrivateVariables(positiveVariables)) {
				var oldLiteral = negativeVariablesMap.put(variable, literal);
				if (oldLiteral != null) {
					throw new IllegalArgumentException("Unbound variable %s appears in multiple literals %s and %s"
							.formatted(variable, oldLiteral, literal));
				}
			}
		}
	}

	private void topologicallySortLiterals() {
		topologicallySortedLiterals = new LinkedHashSet<>(substitutedLiterals.size());
		variableToLiteralInputMap = new HashMap<>();
		literalsWithAllInputsBound = new PriorityQueue<>();
		int size = substitutedLiterals.size();
		for (int i = 0; i < size; i++) {
			var literal = substitutedLiterals.get(i);
			var sortableLiteral = new SortableLiteral(i, literal);
			sortableLiteral.enqueue();
		}
		while (!literalsWithAllInputsBound.isEmpty()) {
			var variable = literalsWithAllInputsBound.remove();
			variable.addToSortedLiterals();
		}
		if (!variableToLiteralInputMap.isEmpty()) {
			throw new IllegalArgumentException("Unbound input variables %s"
					.formatted(variableToLiteralInputMap.keySet()));
		}
	}

	private class SortableLiteral implements Comparable<SortableLiteral> {
		private final int index;
		private final Literal literal;
		private final Set<Variable> remainingInputs;

		private SortableLiteral(int index, Literal literal) {
			this.index = index;
			this.literal = literal;
			remainingInputs = new HashSet<>(literal.getInputVariables(positiveVariables));
			for (var pair : parameters.entrySet()) {
				if (pair.getValue().direction() == ParameterDirection.IN) {
					remainingInputs.remove(pair.getKey());
				}
			}
		}

		public void enqueue() {
			if (allInputsBound()) {
				addToAllInputsBoundQueue();
			} else {
				addToVariableToLiteralInputMap();
			}
		}

		private void bindVariable(Variable input) {
			if (!remainingInputs.remove(input)) {
				throw new AssertionError("Already processed input %s of literal %s".formatted(input, literal));
			}
			if (allInputsBound()) {
				addToAllInputsBoundQueue();
			}
		}

		private boolean allInputsBound() {
			return remainingInputs.isEmpty();
		}

		private void addToVariableToLiteralInputMap() {
			for (var inputVariable : remainingInputs) {
				var literalSetForInput = variableToLiteralInputMap.computeIfAbsent(
						inputVariable, key -> new HashSet<>());
				literalSetForInput.add(this);
			}
		}

		private void addToAllInputsBoundQueue() {
			literalsWithAllInputsBound.add(this);
		}

		public void addToSortedLiterals() {
			if (!allInputsBound()) {
				throw new AssertionError("Inputs %s of %s are not yet bound".formatted(remainingInputs, literal));
			}
			// Add literal if we haven't yet added a duplicate of this literal.
			topologicallySortedLiterals.add(literal);
			for (var variable : literal.getOutputVariables()) {
				var literalSetForInput = variableToLiteralInputMap.remove(variable);
				if (literalSetForInput == null) {
					continue;
				}
				for (var targetSortableLiteral : literalSetForInput) {
					targetSortableLiteral.bindVariable(variable);
				}
			}
		}

		@Override
		public int compareTo(@NotNull ClausePostProcessor.SortableLiteral other) {
			return Integer.compare(index, other.index);
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;
			SortableLiteral that = (SortableLiteral) o;
			return index == that.index && Objects.equals(literal, that.literal);
		}

		@Override
		public int hashCode() {
			return Objects.hash(index, literal);
		}
	}

	public sealed interface Result permits ClauseResult, ConstantResult {
	}

	public record ClauseResult(DnfClause clause) implements Result {
	}

	public enum ConstantResult implements Result {
		ALWAYS_TRUE,
		ALWAYS_FALSE
	}

	public record ParameterInfo(ParameterDirection direction, int index) {
	}
}