aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java')
-rw-r--r--subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java
new file mode 100644
index 00000000..7ef69d05
--- /dev/null
+++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/DataVariable.java
@@ -0,0 +1,103 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.logic.term;
7
8import org.jetbrains.annotations.Nullable;
9import tools.refinery.logic.InvalidQueryException;
10import tools.refinery.logic.equality.LiteralEqualityHelper;
11import tools.refinery.logic.equality.LiteralHashCodeHelper;
12import tools.refinery.logic.literal.EquivalenceLiteral;
13import tools.refinery.logic.literal.Literal;
14import tools.refinery.logic.substitution.Substitution;
15import tools.refinery.logic.valuation.Valuation;
16
17import java.util.Objects;
18
19public final class DataVariable<T> extends AnyDataVariable implements Term<T> {
20 private final Class<T> type;
21
22 DataVariable(String name, Class<T> type) {
23 super(name);
24 this.type = type;
25 }
26
27 @Override
28 public Class<T> getType() {
29 return type;
30 }
31
32 @Override
33 public DataVariable<T> renew(@Nullable String name) {
34 return new DataVariable<>(name, type);
35 }
36
37 @Override
38 public DataVariable<T> renew() {
39 return renew(getExplicitName());
40 }
41
42 @Override
43 public <U> DataVariable<U> asDataVariable(Class<U> newType) {
44 if (!getType().equals(newType)) {
45 throw new InvalidQueryException("%s is not of type %s but of type %s"
46 .formatted(this, newType.getName(), getType().getName()));
47 }
48 @SuppressWarnings("unchecked")
49 var result = (DataVariable<U>) this;
50 return result;
51 }
52
53 @Override
54 public T evaluate(Valuation valuation) {
55 return valuation.getValue(this);
56 }
57
58 @Override
59 public Term<T> substitute(Substitution substitution) {
60 return substitution.getTypeSafeSubstitute(this);
61 }
62
63 @Override
64 public boolean equalsWithSubstitution(LiteralEqualityHelper helper, AnyTerm other) {
65 return other instanceof DataVariable<?> dataVariable && helper.variableEqual(this, dataVariable);
66 }
67
68 @Override
69 public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
70 return helper.getVariableHashCode(this);
71 }
72
73 @Override
74 public int hashCodeWithSubstitution(int sequenceNumber) {
75 return Objects.hash(type, sequenceNumber);
76 }
77
78 public Literal assign(AssignedValue<T> value) {
79 return value.toLiteral(this);
80 }
81
82 @Override
83 public boolean equals(Object o) {
84 if (this == o) return true;
85 if (o == null || getClass() != o.getClass()) return false;
86 if (!super.equals(o)) return false;
87 DataVariable<?> that = (DataVariable<?>) o;
88 return type.equals(that.type);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(super.hashCode(), type);
94 }
95
96 public EquivalenceLiteral isEquivalent(DataVariable<T> other) {
97 return new EquivalenceLiteral(true, this, other);
98 }
99
100 public EquivalenceLiteral notEquivalent(DataVariable<T> other) {
101 return new EquivalenceLiteral(false, this, other);
102 }
103}