blob: 4929e74cbcf31d84ae4339a20df43d0cad172f2d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.logic.literal;
import tools.refinery.logic.InvalidQueryException;
import tools.refinery.logic.equality.LiteralEqualityHelper;
import tools.refinery.logic.equality.LiteralHashCodeHelper;
import tools.refinery.logic.substitution.Substitution;
import tools.refinery.logic.term.DataVariable;
import tools.refinery.logic.term.Term;
import tools.refinery.logic.term.Variable;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
// {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}.
@SuppressWarnings("squid:S2160")
public class AssignLiteral<T> extends AbstractLiteral {
private final DataVariable<T> variable;
private final Term<T> term;
public AssignLiteral(DataVariable<T> variable, Term<T> term) {
if (!term.getType().equals(variable.getType())) {
throw new InvalidQueryException("Term %s must be of type %s, got %s instead".formatted(
term, variable.getType().getName(), term.getType().getName()));
}
var inputVariables = term.getInputVariables();
if (inputVariables.contains(variable)) {
throw new InvalidQueryException("Result variable %s must not appear in the term %s".formatted(
variable, term));
}
this.variable = variable;
this.term = term;
}
public DataVariable<T> getVariable() {
return variable;
}
public Term<T> getTerm() {
return term;
}
@Override
public Set<Variable> getOutputVariables() {
return Set.of(variable);
}
@Override
public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) {
return Collections.unmodifiableSet(term.getInputVariables());
}
@Override
public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of();
}
@Override
public Literal substitute(Substitution substitution) {
return new AssignLiteral<>(substitution.getTypeSafeSubstitute(variable), term.substitute(substitution));
}
@Override
public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) {
if (other == null || getClass() != other.getClass()) {
return false;
}
var otherAssignLiteral = (AssignLiteral<?>) other;
return helper.variableEqual(variable, otherAssignLiteral.variable) &&
term.equalsWithSubstitution(helper, otherAssignLiteral.term);
}
@Override
public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
return Objects.hash(super.hashCodeWithSubstitution(helper), helper.getVariableHashCode(variable),
term.hashCodeWithSubstitution(helper));
}
@Override
public String toString() {
return "%s is (%s)".formatted(variable, term);
}
}
|