aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/multiobject/ExistsRefiner.java
blob: f134fe921a2dc5e23fe6a33ebba6beac67e29697 (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
/*
 * 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 ExistsRefiner extends AbstractPartialInterpretationRefiner<TruthValue, Boolean> {
	private final Interpretation<CardinalityInterval> countInterpretation;

	private ExistsRefiner(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) {
		var currentCount = countInterpretation.get(key);
		if (currentCount == null) {
			return false;
		}
		CardinalityInterval newCount;
		switch (value) {
		case UNKNOWN -> {
			return true;
		}
		case TRUE -> newCount = currentCount.meet(CardinalityIntervals.SOME);
		case FALSE -> newCount = currentCount.meet(CardinalityIntervals.NONE);
		case ERROR -> {
			return false;
		}
		default -> throw new IllegalArgumentException("Unknown TruthValue: " + value);
		}
		if (newCount.isEmpty()) {
			return false;
		}
		countInterpretation.put(key, newCount);
		return true;
	}

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