diff options
Diffstat (limited to 'subprojects/logic/src/main/java/tools/refinery/logic/term/UnaryTerm.java')
-rw-r--r-- | subprojects/logic/src/main/java/tools/refinery/logic/term/UnaryTerm.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/UnaryTerm.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/UnaryTerm.java new file mode 100644 index 00000000..e173de3e --- /dev/null +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/UnaryTerm.java | |||
@@ -0,0 +1,74 @@ | |||
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.logic.term; | ||
7 | |||
8 | import tools.refinery.logic.InvalidQueryException; | ||
9 | import tools.refinery.logic.equality.LiteralEqualityHelper; | ||
10 | import tools.refinery.logic.equality.LiteralHashCodeHelper; | ||
11 | import tools.refinery.logic.substitution.Substitution; | ||
12 | import tools.refinery.logic.valuation.Valuation; | ||
13 | |||
14 | import java.util.Objects; | ||
15 | import java.util.Set; | ||
16 | |||
17 | // {@link Object#equals(Object)} is implemented by {@link AbstractTerm}. | ||
18 | @SuppressWarnings("squid:S2160") | ||
19 | public abstract class UnaryTerm<R, T> extends AbstractTerm<R> { | ||
20 | private final Class<T> bodyType; | ||
21 | private final Term<T> body; | ||
22 | |||
23 | protected UnaryTerm(Class<R> type, Class<T> bodyType, Term<T> body) { | ||
24 | super(type); | ||
25 | if (!body.getType().equals(bodyType)) { | ||
26 | throw new InvalidQueryException("Expected body %s to be of type %s, got %s instead".formatted(body, | ||
27 | bodyType.getName(), body.getType().getName())); | ||
28 | } | ||
29 | this.bodyType = bodyType; | ||
30 | this.body = body; | ||
31 | } | ||
32 | |||
33 | public Class<T> getBodyType() { | ||
34 | return bodyType; | ||
35 | } | ||
36 | |||
37 | public Term<T> getBody() { | ||
38 | return body; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public R evaluate(Valuation valuation) { | ||
43 | var bodyValue = body.evaluate(valuation); | ||
44 | return bodyValue == null ? null : doEvaluate(bodyValue); | ||
45 | } | ||
46 | |||
47 | protected abstract R doEvaluate(T bodyValue); | ||
48 | |||
49 | @Override | ||
50 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) { | ||
51 | if (!super.equalsWithSubstitution(helper, other)) { | ||
52 | return false; | ||
53 | } | ||
54 | var otherUnaryTerm = (UnaryTerm<?, ?>) other; | ||
55 | return bodyType.equals(otherUnaryTerm.bodyType) && body.equalsWithSubstitution(helper, otherUnaryTerm.body); | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) { | ||
60 | return Objects.hash(super.hashCodeWithSubstitution(helper), bodyType, body.hashCodeWithSubstitution(helper)); | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public Term<R> substitute(Substitution substitution) { | ||
65 | return doSubstitute(substitution, body.substitute(substitution)); | ||
66 | } | ||
67 | |||
68 | protected abstract Term<R> doSubstitute(Substitution substitution, Term<T> substitutedBody); | ||
69 | |||
70 | @Override | ||
71 | public Set<AnyDataVariable> getInputVariables() { | ||
72 | return body.getInputVariables(); | ||
73 | } | ||
74 | } | ||