diff options
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.java | 68 |
1 files changed, 68 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..4083111a --- /dev/null +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/UnaryTerm.java | |||
@@ -0,0 +1,68 @@ | |||
1 | package tools.refinery.store.query.term; | ||
2 | |||
3 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | ||
4 | import tools.refinery.store.query.substitution.Substitution; | ||
5 | import tools.refinery.store.query.valuation.Valuation; | ||
6 | |||
7 | import java.util.Objects; | ||
8 | import java.util.Set; | ||
9 | |||
10 | public abstract class UnaryTerm<R, T> implements Term<R> { | ||
11 | private final Term<T> body; | ||
12 | |||
13 | protected UnaryTerm(Term<T> body) { | ||
14 | if (!body.getType().equals(getBodyType())) { | ||
15 | throw new IllegalArgumentException("Expected body %s to be of type %s, got %s instead".formatted(body, | ||
16 | getBodyType().getName(), body.getType().getName())); | ||
17 | } | ||
18 | this.body = body; | ||
19 | } | ||
20 | |||
21 | public abstract Class<T> getBodyType(); | ||
22 | |||
23 | public Term<T> getBody() { | ||
24 | return body; | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public R evaluate(Valuation valuation) { | ||
29 | var bodyValue = body.evaluate(valuation); | ||
30 | return bodyValue == null ? null : doEvaluate(bodyValue); | ||
31 | } | ||
32 | |||
33 | protected abstract R doEvaluate(T bodyValue); | ||
34 | |||
35 | @Override | ||
36 | public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) { | ||
37 | if (getClass() != other.getClass()) { | ||
38 | return false; | ||
39 | } | ||
40 | var otherUnaryTerm = (UnaryTerm<?, ?>) other; | ||
41 | return body.equalsWithSubstitution(helper, otherUnaryTerm.body); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public Term<R> substitute(Substitution substitution) { | ||
46 | return doSubstitute(substitution, body.substitute(substitution)); | ||
47 | } | ||
48 | |||
49 | protected abstract Term<R> doSubstitute(Substitution substitution, Term<T> substitutedBody); | ||
50 | |||
51 | @Override | ||
52 | public Set<AnyDataVariable> getInputVariables() { | ||
53 | return body.getInputVariables(); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public boolean equals(Object o) { | ||
58 | if (this == o) return true; | ||
59 | if (o == null || getClass() != o.getClass()) return false; | ||
60 | UnaryTerm<?, ?> unaryTerm = (UnaryTerm<?, ?>) o; | ||
61 | return body.equals(unaryTerm.body); | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public int hashCode() { | ||
66 | return Objects.hash(getClass(), body); | ||
67 | } | ||
68 | } | ||