diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/containment/InferredContainment.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/containment/InferredContainment.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/containment/InferredContainment.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/containment/InferredContainment.java new file mode 100644 index 00000000..8df23d9a --- /dev/null +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/containment/InferredContainment.java | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.reasoning.translator.containment; | ||
7 | |||
8 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
9 | import tools.refinery.store.representation.TruthValue; | ||
10 | |||
11 | import java.util.Objects; | ||
12 | import java.util.Set; | ||
13 | |||
14 | final class InferredContainment { | ||
15 | public static final InferredContainment UNKNOWN = new InferredContainment( | ||
16 | TruthValue.UNKNOWN, Set.of(), Set.of()); | ||
17 | private final TruthValue contains; | ||
18 | private final Set<PartialRelation> mustLinks; | ||
19 | private final Set<PartialRelation> forbiddenLinks; | ||
20 | private final int hashCode; | ||
21 | |||
22 | public InferredContainment(TruthValue contains, Set<PartialRelation> mustLinks, | ||
23 | Set<PartialRelation> forbiddenLinks) { | ||
24 | this.contains = adjustContains(contains, mustLinks, forbiddenLinks); | ||
25 | this.mustLinks = mustLinks; | ||
26 | this.forbiddenLinks = forbiddenLinks; | ||
27 | hashCode = Objects.hash(contains, mustLinks, forbiddenLinks); | ||
28 | } | ||
29 | |||
30 | private static TruthValue adjustContains(TruthValue contains, Set<PartialRelation> mustLinks, | ||
31 | Set<PartialRelation> forbiddenLinks) { | ||
32 | var result = contains; | ||
33 | if (!mustLinks.isEmpty()) { | ||
34 | result = result.merge(TruthValue.TRUE); | ||
35 | } | ||
36 | boolean hasErrorLink = mustLinks.stream().anyMatch(forbiddenLinks::contains); | ||
37 | if (mustLinks.size() >= 2 || hasErrorLink) { | ||
38 | result = result.merge(TruthValue.ERROR); | ||
39 | } | ||
40 | return result; | ||
41 | } | ||
42 | |||
43 | public TruthValue contains() { | ||
44 | return contains; | ||
45 | } | ||
46 | |||
47 | public Set<PartialRelation> mustLinks() { | ||
48 | return mustLinks; | ||
49 | } | ||
50 | |||
51 | public Set<PartialRelation> forbiddenLinks() { | ||
52 | return forbiddenLinks; | ||
53 | } | ||
54 | |||
55 | @Override | ||
56 | public boolean equals(Object obj) { | ||
57 | if (obj == this) return true; | ||
58 | if (obj == null || obj.getClass() != this.getClass()) return false; | ||
59 | var that = (InferredContainment) obj; | ||
60 | return Objects.equals(this.contains, that.contains) && | ||
61 | Objects.equals(this.mustLinks, that.mustLinks) && | ||
62 | Objects.equals(this.forbiddenLinks, that.forbiddenLinks); | ||
63 | } | ||
64 | |||
65 | @Override | ||
66 | public int hashCode() { | ||
67 | return hashCode; | ||
68 | } | ||
69 | |||
70 | @Override | ||
71 | public String toString() { | ||
72 | return "InferredContainment[" + | ||
73 | "contains=" + contains + ", " + | ||
74 | "mustLinks=" + mustLinks + ", " + | ||
75 | "forbiddenLinks=" + forbiddenLinks + ']'; | ||
76 | } | ||
77 | } | ||