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
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.query.literal;
import tools.refinery.store.query.Constraint;
import tools.refinery.store.query.equality.LiteralEqualityHelper;
import tools.refinery.store.query.substitution.Substitution;
import tools.refinery.store.query.term.DataVariable;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.query.term.int_.IntTerms;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class CountLiteral extends AbstractCallLiteral {
private final DataVariable<Integer> resultVariable;
public CountLiteral(DataVariable<Integer> resultVariable, Constraint target, List<Variable> arguments) {
super(target, arguments);
if (!resultVariable.getType().equals(Integer.class)) {
throw new IllegalArgumentException("Count result variable %s must be of type %s, got %s instead".formatted(
resultVariable, Integer.class.getName(), resultVariable.getType().getName()));
}
if (arguments.contains(resultVariable)) {
throw new IllegalArgumentException("Count result variable %s must not appear in the argument list"
.formatted(resultVariable));
}
this.resultVariable = resultVariable;
}
public DataVariable<Integer> getResultVariable() {
return resultVariable;
}
@Override
public Set<Variable> getOutputVariables() {
return Set.of(resultVariable);
}
@Override
public Literal reduce() {
var reduction = getTarget().getReduction();
return switch (reduction) {
case ALWAYS_FALSE -> getResultVariable().assign(IntTerms.constant(0));
// The only way a constant {@code true} predicate can be called in a negative position is to have all of
// its arguments bound as input variables. Thus, there will only be a single match.
case ALWAYS_TRUE -> getResultVariable().assign(IntTerms.constant(1));
case NOT_REDUCIBLE -> this;
};
}
@Override
protected Literal doSubstitute(Substitution substitution, List<Variable> substitutedArguments) {
return new CountLiteral(substitution.getTypeSafeSubstitute(resultVariable), getTarget(), substitutedArguments);
}
@Override
public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) {
if (!super.equalsWithSubstitution(helper, other)) {
return false;
}
var otherCountLiteral = (CountLiteral) other;
return helper.variableEqual(resultVariable, otherCountLiteral.resultVariable);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
CountLiteral that = (CountLiteral) o;
return resultVariable.equals(that.resultVariable);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), resultVariable);
}
@Override
public String toString() {
var builder = new StringBuilder();
builder.append(resultVariable);
builder.append(" is count ");
builder.append(getTarget().toReferenceString());
builder.append("(");
var argumentIterator = getArguments().iterator();
if (argumentIterator.hasNext()) {
builder.append(argumentIterator.next());
while (argumentIterator.hasNext()) {
builder.append(", ").append(argumentIterator.next());
}
}
builder.append(")");
return builder.toString();
}
}
|