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

import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.logic.term.truthvalue.TruthValue;

import java.util.Objects;
import java.util.Set;

final class InferredContainment {
	public static final InferredContainment UNKNOWN = new InferredContainment(
			TruthValue.UNKNOWN, Set.of(), Set.of());
	private final TruthValue contains;
	private final Set<PartialRelation> mustLinks;
	private final Set<PartialRelation> forbiddenLinks;
	private final int hashCode;

	public InferredContainment(TruthValue contains, Set<PartialRelation> mustLinks,
							   Set<PartialRelation> forbiddenLinks) {
		this.contains = adjustContains(contains, mustLinks, forbiddenLinks);
		this.mustLinks = mustLinks;
		this.forbiddenLinks = forbiddenLinks;
		hashCode = Objects.hash(this.contains, mustLinks, forbiddenLinks);
	}

	private static TruthValue adjustContains(TruthValue contains, Set<PartialRelation> mustLinks,
											 Set<PartialRelation> forbiddenLinks) {
		var result = contains;
		if (!mustLinks.isEmpty()) {
			result = result.merge(TruthValue.TRUE);
		}
		boolean hasErrorLink = mustLinks.stream().anyMatch(forbiddenLinks::contains);
		if (mustLinks.size() >= 2 || hasErrorLink) {
			result = result.merge(TruthValue.ERROR);
		}
		return result;
	}

	public TruthValue contains() {
		return contains;
	}

	public Set<PartialRelation> mustLinks() {
		return mustLinks;
	}

	public Set<PartialRelation> forbiddenLinks() {
		return forbiddenLinks;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) return true;
		if (obj == null || obj.getClass() != this.getClass()) return false;
		var that = (InferredContainment) obj;
		return Objects.equals(this.contains, that.contains) &&
				Objects.equals(this.mustLinks, that.mustLinks) &&
				Objects.equals(this.forbiddenLinks, that.forbiddenLinks);
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public String toString() {
		return "InferredContainment[" +
				"contains=" + contains + ", " +
				"mustLinks=" + mustLinks + ", " +
				"forbiddenLinks=" + forbiddenLinks + ']';
	}
}