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
120
121
122
123
124
125
126
|
/*
* SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.logic.dnf;
import tools.refinery.logic.InvalidQueryException;
import tools.refinery.logic.literal.CallPolarity;
import tools.refinery.logic.term.Aggregator;
import tools.refinery.logic.term.AssignedValue;
import tools.refinery.logic.term.NodeVariable;
import tools.refinery.logic.term.Variable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public final class FunctionalQuery<T> extends Query<T> {
private final Class<T> type;
FunctionalQuery(Dnf dnf, Class<T> type) {
super(dnf);
var parameters = dnf.getSymbolicParameters();
int outputIndex = dnf.arity() - 1;
for (int i = 0; i < outputIndex; i++) {
var parameter = parameters.get(i);
var parameterType = parameter.tryGetType();
if (parameterType.isPresent()) {
throw new InvalidQueryException("Expected parameter %s of %s to be a node variable, got %s instead"
.formatted(parameter, dnf, parameterType.get().getName()));
}
}
var outputParameter = parameters.get(outputIndex);
var outputParameterType = outputParameter.tryGetType();
if (outputParameterType.isEmpty() || !outputParameterType.get().equals(type)) {
throw new InvalidQueryException("Expected parameter %s of %s to be %s, but got %s instead".formatted(
outputParameter, dnf, type, outputParameterType.map(Class::getName).orElse("node")));
}
this.type = type;
}
@Override
public int arity() {
return getDnf().arity() - 1;
}
@Override
public Class<T> valueType() {
return type;
}
@Override
public T defaultValue() {
return null;
}
@Override
protected FunctionalQuery<T> withDnfInternal(Dnf newDnf) {
return newDnf.asFunction(type);
}
@Override
public FunctionalQuery<T> withDnf(Dnf newDnf) {
return (FunctionalQuery<T>) super.withDnf(newDnf);
}
public AssignedValue<T> call(List<NodeVariable> arguments) {
return targetVariable -> {
var argumentsWithTarget = new ArrayList<Variable>(arguments.size() + 1);
argumentsWithTarget.addAll(arguments);
argumentsWithTarget.add(targetVariable);
return getDnf().call(CallPolarity.POSITIVE, argumentsWithTarget);
};
}
public AssignedValue<T> call(NodeVariable... arguments) {
return call(List.of(arguments));
}
public <R> AssignedValue<R> aggregate(Aggregator<R, T> aggregator, List<NodeVariable> arguments) {
return targetVariable -> {
var placeholderVariable = Variable.of(type);
var argumentsWithPlaceholder = new ArrayList<Variable>(arguments.size() + 1);
argumentsWithPlaceholder.addAll(arguments);
argumentsWithPlaceholder.add(placeholderVariable);
return getDnf()
.aggregateBy(placeholderVariable, aggregator, argumentsWithPlaceholder)
.toLiteral(targetVariable);
};
}
public <R> AssignedValue<R> aggregate(Aggregator<R, T> aggregator, NodeVariable... arguments) {
return aggregate(aggregator, List.of(arguments));
}
public AssignedValue<T> leftJoin(T defaultValue, List<NodeVariable> arguments) {
return targetVariable -> {
var placeholderVariable = Variable.of(type);
var argumentsWithPlaceholder = new ArrayList<Variable>(arguments.size() + 1);
argumentsWithPlaceholder.addAll(arguments);
argumentsWithPlaceholder.add(placeholderVariable);
return getDnf()
.leftJoinBy(placeholderVariable, defaultValue, argumentsWithPlaceholder)
.toLiteral(targetVariable);
};
}
public AssignedValue<T> leftJoin(T defaultValue, NodeVariable... arguments) {
return leftJoin(defaultValue, List.of(arguments));
}
@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;
FunctionalQuery<?> that = (FunctionalQuery<?>) o;
return Objects.equals(type, that.type);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), type);
}
}
|