diff options
Diffstat (limited to 'subprojects/logic/src/main/java/tools/refinery/logic/term/Variable.java')
-rw-r--r-- | subprojects/logic/src/main/java/tools/refinery/logic/term/Variable.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/Variable.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/Variable.java new file mode 100644 index 00000000..28aad21b --- /dev/null +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/Variable.java | |||
@@ -0,0 +1,88 @@ | |||
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 org.jetbrains.annotations.Nullable; | ||
9 | import tools.refinery.logic.dnf.DnfUtils; | ||
10 | |||
11 | import java.util.Objects; | ||
12 | import java.util.Optional; | ||
13 | |||
14 | public abstract sealed class Variable permits AnyDataVariable, NodeVariable { | ||
15 | private final String explicitName; | ||
16 | private final String uniqueName; | ||
17 | |||
18 | protected Variable(String name) { | ||
19 | this.explicitName = name; | ||
20 | uniqueName = DnfUtils.generateUniqueName(name); | ||
21 | } | ||
22 | |||
23 | public abstract Optional<Class<?>> tryGetType(); | ||
24 | |||
25 | public String getName() { | ||
26 | return explicitName == null ? uniqueName : explicitName; | ||
27 | } | ||
28 | |||
29 | protected String getExplicitName() { | ||
30 | return explicitName; | ||
31 | } | ||
32 | |||
33 | public boolean isExplicitlyNamed() { | ||
34 | return explicitName != null; | ||
35 | } | ||
36 | |||
37 | public String getUniqueName() { | ||
38 | return uniqueName; | ||
39 | } | ||
40 | |||
41 | public abstract Variable renew(@Nullable String name); | ||
42 | |||
43 | public abstract Variable renew(); | ||
44 | |||
45 | public abstract boolean isNodeVariable(); | ||
46 | |||
47 | public abstract boolean isDataVariable(); | ||
48 | |||
49 | public abstract NodeVariable asNodeVariable(); | ||
50 | |||
51 | public abstract <T> DataVariable<T> asDataVariable(Class<T> type); | ||
52 | |||
53 | public abstract int hashCodeWithSubstitution(int sequenceNumber); | ||
54 | |||
55 | @Override | ||
56 | public String toString() { | ||
57 | return getName(); | ||
58 | } | ||
59 | |||
60 | @Override | ||
61 | public boolean equals(Object o) { | ||
62 | if (this == o) return true; | ||
63 | if (o == null || getClass() != o.getClass()) return false; | ||
64 | Variable variable = (Variable) o; | ||
65 | return Objects.equals(uniqueName, variable.uniqueName); | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public int hashCode() { | ||
70 | return Objects.hash(uniqueName); | ||
71 | } | ||
72 | |||
73 | public static NodeVariable of(@Nullable String name) { | ||
74 | return new NodeVariable(name); | ||
75 | } | ||
76 | |||
77 | public static NodeVariable of() { | ||
78 | return of((String) null); | ||
79 | } | ||
80 | |||
81 | public static <T> DataVariable<T> of(@Nullable String name, Class<T> type) { | ||
82 | return new DataVariable<>(name, type); | ||
83 | } | ||
84 | |||
85 | public static <T> DataVariable<T> of(Class<T> type) { | ||
86 | return of(null, type); | ||
87 | } | ||
88 | } | ||