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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* SPDX-FileCopyrightText: 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.InvalidQueryException;
import tools.refinery.store.query.equality.LiteralEqualityHelper;
import tools.refinery.store.query.equality.LiteralHashCodeHelper;
import tools.refinery.store.query.substitution.Substitution;
import tools.refinery.store.query.term.NodeVariable;
import tools.refinery.store.query.term.ParameterDirection;
import tools.refinery.store.query.term.Variable;
import java.util.List;
import java.util.Set;
// {@link Object#equals(Object)} is implemented by {@link AbstractLiteral}.
@SuppressWarnings("squid:S2160")
public class RepresentativeElectionLiteral extends AbstractCallLiteral {
private final Connectivity connectivity;
public RepresentativeElectionLiteral(Connectivity connectivity, Constraint target, NodeVariable specific,
NodeVariable representative) {
this(connectivity, target, List.of(specific, representative));
}
private RepresentativeElectionLiteral(Connectivity connectivity, Constraint target, List<Variable> arguments) {
super(target, arguments);
this.connectivity = connectivity;
var parameters = target.getParameters();
int arity = target.arity();
if (arity != 2) {
throw new InvalidQueryException("SCCs can only take binary relations");
}
if (parameters.get(0).isDataVariable() || parameters.get(1).isDataVariable()) {
throw new InvalidQueryException("SCCs can only be computed over nodes");
}
if (parameters.get(0).getDirection() != ParameterDirection.OUT ||
parameters.get(1).getDirection() != ParameterDirection.OUT) {
throw new InvalidQueryException("SCCs cannot take input parameters");
}
}
public Connectivity getConnectivity() {
return connectivity;
}
@Override
protected Literal doSubstitute(Substitution substitution, List<Variable> substitutedArguments) {
return new RepresentativeElectionLiteral(connectivity, getTarget(), substitutedArguments);
}
@Override
public Set<Variable> getOutputVariables() {
return getArgumentsOfDirection(ParameterDirection.OUT);
}
@Override
public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of();
}
@Override
public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) {
return Set.of();
}
@Override
public Literal reduce() {
var reduction = getTarget().getReduction();
return switch (reduction) {
case ALWAYS_FALSE -> BooleanLiteral.FALSE;
case ALWAYS_TRUE -> throw new InvalidQueryException(
"Trying to elect representatives over an infinite set");
case NOT_REDUCIBLE -> this;
};
}
@Override
public AbstractCallLiteral withArguments(Constraint newTarget, List<Variable> newArguments) {
return new RepresentativeElectionLiteral(connectivity, newTarget, newArguments);
}
@Override
public boolean equalsWithSubstitution(LiteralEqualityHelper helper, Literal other) {
if (!super.equalsWithSubstitution(helper, other)) {
return false;
}
var otherRepresentativeElectionLiteral = (RepresentativeElectionLiteral) other;
return connectivity.equals(otherRepresentativeElectionLiteral.connectivity);
}
@Override
public int hashCodeWithSubstitution(LiteralHashCodeHelper helper) {
return super.hashCodeWithSubstitution(helper) * 31 + connectivity.hashCode();
}
@Override
public String toString() {
var builder = new StringBuilder();
builder.append("@Representative(\"");
builder.append(connectivity);
builder.append("\") ");
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();
}
}
|