diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CheckLiteral.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CheckLiteral.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CheckLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CheckLiteral.java new file mode 100644 index 00000000..dfedd2cb --- /dev/null +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CheckLiteral.java | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.literal; | ||
7 | |||
8 | import tools.refinery.store.query.InvalidQueryException; | ||
9 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | ||
10 | import tools.refinery.store.query.equality.LiteralHashCodeHelper; | ||
11 | import tools.refinery.store.query.substitution.Substitution; | ||
12 | import tools.refinery.store.query.term.ConstantTerm; | ||
13 | import tools.refinery.store.query.term.Term; | ||
14 | import tools.refinery.store.query.term.Variable; | ||
15 | import tools.refinery.store.query.term.bool.BoolNotTerm; | ||
16 | import tools.refinery.store.query.term.bool.BoolTerms; | ||
17 | |||
18 | import java.util.Collections; | ||
19 | import java.util.Objects; | ||
20 | import java.util.Set; | ||
21 | |||
22 | // {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}. | ||
23 | @SuppressWarnings("squid:S2160") | ||
24 | public class CheckLiteral extends AbstractLiteral implements CanNegate<CheckLiteral> { | ||
25 | private final Term<Boolean> term; | ||
26 | |||
27 | public CheckLiteral(Term<Boolean> term) { | ||
28 | if (!term.getType().equals(Boolean.class)) { | ||
29 | throw new InvalidQueryException("Term %s must be of type %s, got %s instead".formatted( | ||
30 | term, Boolean.class.getName(), term.getType().getName())); | ||
31 | } | ||
32 | this.term = term; | ||
33 | } | ||
34 | |||
35 | public Term<Boolean> getTerm() { | ||
36 | return term; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public Set<Variable> getOutputVariables() { | ||
41 | return Set.of(); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) { | ||
46 | return Collections.unmodifiableSet(term.getInputVariables()); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) { | ||
51 | return Set.of(); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public Literal substitute(Substitution substitution) { | ||
56 | return new CheckLiteral(term.substitute(substitution)); | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public CheckLiteral negate() { | ||
61 | if (term instanceof BoolNotTerm notTerm) { | ||
62 | return new CheckLiteral(notTerm.getBody()); | ||
63 | } | ||
64 | return new CheckLiteral(BoolTerms.not(term)); | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) { | ||
69 | if (other == null || getClass() != other.getClass()) { | ||
70 | return false; | ||
71 | } | ||
72 | var otherAssumeLiteral = (CheckLiteral) other; | ||
73 | return term.equalsWithSubstitution(helper, otherAssumeLiteral.term); | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) { | ||
78 | return Objects.hash(super.hashCodeWithSubstitution(helper), term.hashCodeWithSubstitution(helper)); | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public Literal reduce() { | ||
83 | if (term instanceof ConstantTerm<Boolean> constantTerm) { | ||
84 | // Return {@link BooleanLiteral#FALSE} for {@code false} or {@code null} literals. | ||
85 | return Boolean.TRUE.equals(constantTerm.getValue()) ? BooleanLiteral.TRUE : | ||
86 | BooleanLiteral.FALSE; | ||
87 | } | ||
88 | return this; | ||
89 | } | ||
90 | |||
91 | @Override | ||
92 | public String toString() { | ||
93 | return "(%s)".formatted(term); | ||
94 | } | ||
95 | } | ||