diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java index 8cc3fae6..4da92b90 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AssignLiteral.java | |||
@@ -9,21 +9,43 @@ import tools.refinery.store.query.equality.LiteralEqualityHelper; | |||
9 | import tools.refinery.store.query.substitution.Substitution; | 9 | import tools.refinery.store.query.substitution.Substitution; |
10 | import tools.refinery.store.query.term.DataVariable; | 10 | import tools.refinery.store.query.term.DataVariable; |
11 | import tools.refinery.store.query.term.Term; | 11 | import tools.refinery.store.query.term.Term; |
12 | import tools.refinery.store.query.term.Variable; | ||
13 | 12 | ||
14 | import java.util.Set; | 13 | import java.util.Objects; |
15 | 14 | ||
16 | public record AssignLiteral<T>(DataVariable<T> variable, Term<T> term) implements Literal { | 15 | public final class AssignLiteral<T> implements Literal { |
17 | public AssignLiteral { | 16 | private final DataVariable<T> variable; |
17 | private final Term<T> term; | ||
18 | private final VariableBinder variableBinder; | ||
19 | |||
20 | public AssignLiteral(DataVariable<T> variable, Term<T> term) { | ||
18 | if (!term.getType().equals(variable.getType())) { | 21 | if (!term.getType().equals(variable.getType())) { |
19 | throw new IllegalArgumentException("Term %s must be of type %s, got %s instead".formatted( | 22 | throw new IllegalArgumentException("Term %s must be of type %s, got %s instead".formatted( |
20 | term, variable.getType().getName(), term.getType().getName())); | 23 | term, variable.getType().getName(), term.getType().getName())); |
21 | } | 24 | } |
25 | this.variable = variable; | ||
26 | this.term = term; | ||
27 | var inputVariables = term.getInputVariables(); | ||
28 | if (inputVariables.contains(variable)) { | ||
29 | throw new IllegalArgumentException("Result variable %s must not appear in the term %s".formatted( | ||
30 | variable, term)); | ||
31 | } | ||
32 | variableBinder = VariableBinder.builder() | ||
33 | .variable(variable, VariableDirection.OUT) | ||
34 | .variables(inputVariables, VariableDirection.IN) | ||
35 | .build(); | ||
36 | } | ||
37 | |||
38 | public DataVariable<T> getTargetVariable() { | ||
39 | return variable; | ||
40 | } | ||
41 | |||
42 | public Term<T> getTerm() { | ||
43 | return term; | ||
22 | } | 44 | } |
23 | 45 | ||
24 | @Override | 46 | @Override |
25 | public Set<Variable> getBoundVariables() { | 47 | public VariableBinder getVariableBinder() { |
26 | return Set.of(variable); | 48 | return variableBinder; |
27 | } | 49 | } |
28 | 50 | ||
29 | @Override | 51 | @Override |
@@ -41,9 +63,22 @@ public record AssignLiteral<T>(DataVariable<T> variable, Term<T> term) implement | |||
41 | otherLetLiteral.term); | 63 | otherLetLiteral.term); |
42 | } | 64 | } |
43 | 65 | ||
44 | |||
45 | @Override | 66 | @Override |
46 | public String toString() { | 67 | public String toString() { |
47 | return "%s is (%s)".formatted(variable, term); | 68 | return "%s is (%s)".formatted(variable, term); |
48 | } | 69 | } |
70 | |||
71 | @Override | ||
72 | public boolean equals(Object obj) { | ||
73 | if (obj == this) return true; | ||
74 | if (obj == null || obj.getClass() != this.getClass()) return false; | ||
75 | var that = (AssignLiteral<?>) obj; | ||
76 | return Objects.equals(this.variable, that.variable) && | ||
77 | Objects.equals(this.term, that.term); | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public int hashCode() { | ||
82 | return Objects.hash(getClass(), variable, term); | ||
83 | } | ||
49 | } | 84 | } |