aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java')
-rw-r--r--subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java
new file mode 100644
index 00000000..a46ebe31
--- /dev/null
+++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java
@@ -0,0 +1,79 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.query.term;
7
8import tools.refinery.store.query.equality.LiteralEqualityHelper;
9import tools.refinery.store.query.substitution.Substitution;
10import tools.refinery.store.query.valuation.Valuation;
11
12import java.util.Objects;
13import java.util.Set;
14
15public abstract class UnaryTerm<R, T> extends AbstractTerm<R> {
16 private final Class<T> bodyType;
17 private final Term<T> body;
18
19 protected UnaryTerm(Class<R> type, Class<T> bodyType, Term<T> body) {
20 super(type);
21 if (!body.getType().equals(bodyType)) {
22 throw new IllegalArgumentException("Expected body %s to be of type %s, got %s instead".formatted(body,
23 bodyType.getName(), body.getType().getName()));
24 }
25 this.bodyType = bodyType;
26 this.body = body;
27 }
28
29 public Class<T> getBodyType() {
30 return bodyType;
31 }
32
33 public Term<T> getBody() {
34 return body;
35 }
36
37 @Override
38 public R evaluate(Valuation valuation) {
39 var bodyValue = body.evaluate(valuation);
40 return bodyValue == null ? null : doEvaluate(bodyValue);
41 }
42
43 protected abstract R doEvaluate(T bodyValue);
44
45 @Override
46 public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) {
47 if (!super.equalsWithSubstitution(helper, other)) {
48 return false;
49 }
50 var otherUnaryTerm = (UnaryTerm<?, ?>) other;
51 return bodyType.equals(otherUnaryTerm.bodyType) && body.equalsWithSubstitution(helper, otherUnaryTerm.body);
52 }
53
54 @Override
55 public Term<R> substitute(Substitution substitution) {
56 return doSubstitute(substitution, body.substitute(substitution));
57 }
58
59 protected abstract Term<R> doSubstitute(Substitution substitution, Term<T> substitutedBody);
60
61 @Override
62 public Set<AnyDataVariable> getInputVariables() {
63 return body.getInputVariables();
64 }
65
66 @Override
67 public boolean equals(Object o) {
68 if (this == o) return true;
69 if (o == null || getClass() != o.getClass()) return false;
70 if (!super.equals(o)) return false;
71 UnaryTerm<?, ?> unaryTerm = (UnaryTerm<?, ?>) o;
72 return Objects.equals(bodyType, unaryTerm.bodyType) && Objects.equals(body, unaryTerm.body);
73 }
74
75 @Override
76 public int hashCode() {
77 return Objects.hash(super.hashCode(), bodyType, body);
78 }
79}