aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/FunctionalQuery.java
blob: 680fa631491405e424f723adad49486b484dccbe (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.dnf;

import tools.refinery.store.query.literal.CallPolarity;
import tools.refinery.store.query.term.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public final class FunctionalQuery<T> implements Query<T> {
	private final Dnf dnf;
	private final Class<T> type;

	FunctionalQuery(Dnf dnf, Class<T> type) {
		var parameters = dnf.getParameters();
		int outputIndex = dnf.arity() - 1;
		for (int i = 0; i < outputIndex; i++) {
			var parameter = parameters.get(i);
			if (!(parameter instanceof NodeVariable)) {
				throw new IllegalArgumentException("Expected parameter %s of %s to be of sort %s, but got %s instead"
						.formatted(parameter, dnf, NodeSort.INSTANCE, parameter.getSort()));
			}
		}
		var outputParameter = parameters.get(outputIndex);
		if (!(outputParameter instanceof DataVariable<?> dataOutputParameter) ||
				!dataOutputParameter.getType().equals(type)) {
			throw new IllegalArgumentException("Expected parameter %s of %s to be of sort %s, but got %s instead"
					.formatted(outputParameter, dnf, type, outputParameter.getSort()));
		}
		this.dnf = dnf;
		this.type = type;
	}

	@Override
	public String name() {
		return dnf.name();
	}

	@Override
	public int arity() {
		return dnf.arity() - 1;
	}

	@Override
	public Class<T> valueType() {
		return type;
	}

	@Override
	public T defaultValue() {
		return null;
	}

	@Override
	public Dnf getDnf() {
		return dnf;
	}

	public AssignedValue<T> call(List<NodeVariable> arguments) {
		return targetVariable -> {
			var argumentsWithTarget = new ArrayList<Variable>(arguments.size() + 1);
			argumentsWithTarget.addAll(arguments);
			argumentsWithTarget.add(targetVariable);
			return dnf.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 dnf.aggregate(placeholderVariable, aggregator, argumentsWithPlaceholder).toLiteral(targetVariable);
		};
	}

	public <R> AssignedValue<R> aggregate(Aggregator<R, T> aggregator, NodeVariable... arguments) {
		return aggregate(aggregator, List.of(arguments));
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		FunctionalQuery<?> that = (FunctionalQuery<?>) o;
		return dnf.equals(that.dnf) && type.equals(that.type);
	}

	@Override
	public int hashCode() {
		return Objects.hash(dnf, type);
	}

	@Override
	public String toString() {
		return dnf.toString();
	}
}