blob: dfedd2cb52829efc268da999a50bec3018188908 (
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* 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.ConstantTerm;
import tools.refinery.store.query.term.Term;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.query.term.bool.BoolNotTerm;
import tools.refinery.store.query.term.bool.BoolTerms;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
// {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}.
@SuppressWarnings("squid:S2160")
public class CheckLiteral extends AbstractLiteral implements CanNegate<CheckLiteral> {
private final Term<Boolean> term;
public CheckLiteral(Term<Boolean> term) {
if (!term.getType().equals(Boolean.class)) {
throw new InvalidQueryException("Term %s must be of type %s, got %s instead".formatted(
term, Boolean.class.getName(), term.getType().getName()));
}
this.term = term;
}
public Term<Boolean> getTerm() {
return term;
}
@Override
public Set<Variable> getOutputVariables() {
return Set.of();
}
@Override
public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) {
return Collections.unmodifiableSet(term.getInputVariables());
}
@Override
public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of();
}
@Override
public Literal substitute(Substitution substitution) {
return new CheckLiteral(term.substitute(substitution));
}
@Override
public CheckLiteral negate() {
if (term instanceof BoolNotTerm notTerm) {
return new CheckLiteral(notTerm.getBody());
}
return new CheckLiteral(BoolTerms.not(term));
}
@Override
public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) {
if (other == null || getClass() != other.getClass()) {
return false;
}
var otherAssumeLiteral = (CheckLiteral) other;
return term.equalsWithSubstitution(helper, otherAssumeLiteral.term);
}
@Override
public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
return Objects.hash(super.hashCodeWithSubstitution(helper), term.hashCodeWithSubstitution(helper));
}
@Override
public Literal reduce() {
if (term instanceof ConstantTerm<Boolean> constantTerm) {
// Return {@link BooleanLiteral#FALSE} for {@code false} or {@code null} literals.
return Boolean.TRUE.equals(constantTerm.getValue()) ? BooleanLiteral.TRUE :
BooleanLiteral.FALSE;
}
return this;
}
@Override
public String toString() {
return "(%s)".formatted(term);
}
}
|