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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.query.literal;
import tools.refinery.store.query.InvalidQueryException;
import tools.refinery.store.query.equality.LiteralEqualityHelper;
import tools.refinery.store.query.equality.LiteralHashCodeHelper;
import tools.refinery.store.query.substitution.Substitution;
import tools.refinery.store.query.term.Variable;
import java.util.Objects;
import java.util.Set;
// {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}.
@SuppressWarnings("squid:S2160")
public final class EquivalenceLiteral extends AbstractLiteral implements CanNegate<EquivalenceLiteral> {
private final boolean positive;
private final Variable left;
private final Variable right;
public EquivalenceLiteral(boolean positive, Variable left, Variable right) {
if (!left.tryGetType().equals(right.tryGetType())) {
throw new InvalidQueryException("Variables %s and %s of different type cannot be equivalent"
.formatted(left, right));
}
this.positive = positive;
this.left = left;
this.right = right;
}
public boolean isPositive() {
return positive;
}
public Variable getLeft() {
return left;
}
public Variable getRight() {
return right;
}
@Override
public Set<Variable> getOutputVariables() {
return Set.of(left);
}
@Override
public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of(right);
}
@Override
public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of();
}
@Override
public EquivalenceLiteral negate() {
return new EquivalenceLiteral(!positive, left, right);
}
@Override
public EquivalenceLiteral substitute(Substitution substitution) {
return new EquivalenceLiteral(positive, substitution.getSubstitute(left),
substitution.getSubstitute(right));
}
@Override
public Literal reduce() {
if (left.equals(right)) {
return positive ? BooleanLiteral.TRUE : BooleanLiteral.FALSE;
}
return this;
}
@Override
public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) {
if (other.getClass() != getClass()) {
return false;
}
var otherEquivalenceLiteral = (EquivalenceLiteral) other;
return helper.variableEqual(left, otherEquivalenceLiteral.left) &&
helper.variableEqual(right, otherEquivalenceLiteral.right);
}
@Override
public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
return Objects.hash(super.hashCodeWithSubstitution(helper), helper.getVariableHashCode(left),
helper.getVariableHashCode(right));
}
@Override
public String toString() {
return "%s %s %s".formatted(left, positive ? "===" : "!==", right);
}
}
|