aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/multiobject/EqualsRefiner.java
blob: d8db4ec4a882f10f06e994f9097e4751c61a5cc6 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.translator.multiobject;

import tools.refinery.store.model.Interpretation;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.refinement.AbstractPartialInterpretationRefiner;
import tools.refinery.store.reasoning.representation.PartialSymbol;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.representation.cardinality.CardinalityInterval;
import tools.refinery.store.representation.cardinality.CardinalityIntervals;
import tools.refinery.store.tuple.Tuple;

public class EqualsRefiner extends AbstractPartialInterpretationRefiner<TruthValue, Boolean> {
	private final Interpretation<CardinalityInterval> countInterpretation;

	private EqualsRefiner(ReasoningAdapter adapter, PartialSymbol<TruthValue, Boolean> partialSymbol,
                          Symbol<CardinalityInterval> countSymbol) {
		super(adapter, partialSymbol);
		countInterpretation = adapter.getModel().getInterpretation(countSymbol);
	}

	@Override
	public boolean merge(Tuple key, TruthValue value) {
		if (value == TruthValue.UNKNOWN) {
			return true;
		}
		if (value == TruthValue.ERROR) {
			return false;
		}
		int left = key.get(0);
		int right = key.get(1);
		boolean isDiagonal = left == right;
		if (isDiagonal && value == TruthValue.FALSE) {
			return false;
		}
		if (!isDiagonal) {
			return !value.may();
		}
		if (value != TruthValue.TRUE) {
			throw new IllegalArgumentException("Unknown TruthValue: " + value);
		}
		// {@code isDiagonal} is true, so this could be {@code left} or {@code right}.
		var unaryKey = Tuple.of(left);
		var currentCount = countInterpretation.get(unaryKey);
		if (currentCount == null) {
			return false;
		}
		var newCount = currentCount.meet(CardinalityIntervals.LONE);
		if (newCount.isEmpty()) {
			return false;
		}
		countInterpretation.put(unaryKey, newCount);
		return true;
	}

	public static Factory<TruthValue, Boolean> of(Symbol<CardinalityInterval> countSymbol) {
		return (adapter, partialSymbol) -> new EqualsRefiner(adapter, partialSymbol, countSymbol);
	}
}