diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java index 28ba7625..7343f709 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/EquivalenceLiteral.java | |||
@@ -5,16 +5,44 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.query.literal; | 6 | package tools.refinery.store.query.literal; |
7 | 7 | ||
8 | import tools.refinery.store.query.InvalidQueryException; | ||
8 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | 9 | import tools.refinery.store.query.equality.LiteralEqualityHelper; |
10 | import tools.refinery.store.query.equality.LiteralHashCodeHelper; | ||
9 | import tools.refinery.store.query.substitution.Substitution; | 11 | import tools.refinery.store.query.substitution.Substitution; |
10 | import tools.refinery.store.query.term.NodeVariable; | ||
11 | import tools.refinery.store.query.term.Variable; | 12 | import tools.refinery.store.query.term.Variable; |
12 | 13 | ||
13 | import java.util.Objects; | 14 | import java.util.Objects; |
14 | import java.util.Set; | 15 | import java.util.Set; |
15 | 16 | ||
16 | public record EquivalenceLiteral(boolean positive, NodeVariable left, NodeVariable right) | 17 | // {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}. |
17 | implements CanNegate<EquivalenceLiteral> { | 18 | @SuppressWarnings("squid:S2160") |
19 | public final class EquivalenceLiteral extends AbstractLiteral implements CanNegate<EquivalenceLiteral> { | ||
20 | private final boolean positive; | ||
21 | private final Variable left; | ||
22 | private final Variable right; | ||
23 | |||
24 | public EquivalenceLiteral(boolean positive, Variable left, Variable right) { | ||
25 | if (!left.tryGetType().equals(right.tryGetType())) { | ||
26 | throw new InvalidQueryException("Variables %s and %s of different type cannot be equivalent" | ||
27 | .formatted(left, right)); | ||
28 | } | ||
29 | this.positive = positive; | ||
30 | this.left = left; | ||
31 | this.right = right; | ||
32 | } | ||
33 | |||
34 | public boolean isPositive() { | ||
35 | return positive; | ||
36 | } | ||
37 | |||
38 | public Variable getLeft() { | ||
39 | return left; | ||
40 | } | ||
41 | |||
42 | public Variable getRight() { | ||
43 | return right; | ||
44 | } | ||
45 | |||
18 | @Override | 46 | @Override |
19 | public Set<Variable> getOutputVariables() { | 47 | public Set<Variable> getOutputVariables() { |
20 | return Set.of(left); | 48 | return Set.of(left); |
@@ -37,8 +65,8 @@ public record EquivalenceLiteral(boolean positive, NodeVariable left, NodeVariab | |||
37 | 65 | ||
38 | @Override | 66 | @Override |
39 | public EquivalenceLiteral substitute(Substitution substitution) { | 67 | public EquivalenceLiteral substitute(Substitution substitution) { |
40 | return new EquivalenceLiteral(positive, substitution.getTypeSafeSubstitute(left), | 68 | return new EquivalenceLiteral(positive, substitution.getSubstitute(left), |
41 | substitution.getTypeSafeSubstitute(right)); | 69 | substitution.getSubstitute(right)); |
42 | } | 70 | } |
43 | 71 | ||
44 | @Override | 72 | @Override |
@@ -55,27 +83,18 @@ public record EquivalenceLiteral(boolean positive, NodeVariable left, NodeVariab | |||
55 | return false; | 83 | return false; |
56 | } | 84 | } |
57 | var otherEquivalenceLiteral = (EquivalenceLiteral) other; | 85 | var otherEquivalenceLiteral = (EquivalenceLiteral) other; |
58 | return helper.variableEqual(left, otherEquivalenceLiteral.left) && helper.variableEqual(right, | 86 | return helper.variableEqual(left, otherEquivalenceLiteral.left) && |
59 | otherEquivalenceLiteral.right); | 87 | helper.variableEqual(right, otherEquivalenceLiteral.right); |
60 | } | ||
61 | |||
62 | @Override | ||
63 | public String toString() { | ||
64 | return "%s %s %s".formatted(left, positive ? "===" : "!==", right); | ||
65 | } | 88 | } |
66 | 89 | ||
67 | @Override | 90 | @Override |
68 | public boolean equals(Object obj) { | 91 | public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) { |
69 | if (obj == this) return true; | 92 | return Objects.hash(super.hashCodeWithSubstitution(helper), helper.getVariableHashCode(left), |
70 | if (obj == null || obj.getClass() != this.getClass()) return false; | 93 | helper.getVariableHashCode(right)); |
71 | var that = (EquivalenceLiteral) obj; | ||
72 | return this.positive == that.positive && | ||
73 | Objects.equals(this.left, that.left) && | ||
74 | Objects.equals(this.right, that.right); | ||
75 | } | 94 | } |
76 | 95 | ||
77 | @Override | 96 | @Override |
78 | public int hashCode() { | 97 | public String toString() { |
79 | return Objects.hash(getClass(), positive, left, right); | 98 | return "%s %s %s".formatted(left, positive ? "===" : "!==", right); |
80 | } | 99 | } |
81 | } | 100 | } |