aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java
blob: 3d278a8bd4260e705fe919aff4c52cbe4c1def43 (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
package tools.refinery.store.query.view;

import tools.refinery.store.model.Model;
import tools.refinery.store.query.FunctionalDependency;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.tuple.Tuple;
import tools.refinery.store.tuple.Tuple1;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class FunctionalRelationView<T> extends RelationView<T> {
	public FunctionalRelationView(Symbol<T> symbol, String name) {
		super(symbol, name);
	}

	public FunctionalRelationView(Symbol<T> symbol) {
		super(symbol);
	}

	@Override
	public Set<FunctionalDependency<Integer>> getFunctionalDependencies() {
		var arity = getSymbol().arity();
		var forEach = IntStream.range(0, arity).boxed().collect(Collectors.toUnmodifiableSet());
		var unique = Set.of(arity);
		return Set.of(new FunctionalDependency<>(forEach, unique));
	}

	@Override
	public Set<RelationViewImplication> getImpliedRelationViews() {
		var symbol = getSymbol();
		var impliedIndices = IntStream.range(0, symbol.arity()).boxed().toList();
		var keyOnlyRelationView = new KeyOnlyRelationView<>(symbol);
		return Set.of(new RelationViewImplication(this, keyOnlyRelationView, impliedIndices));
	}

	@Override
	public boolean filter(Tuple key, T value) {
		return true;
	}

	@Override
	public Object[] forwardMap(Tuple key, T value) {
		int size = key.getSize();
		Object[] result = new Object[size + 1];
		for (int i = 0; i < size; i++) {
			result[i] = Tuple.of(key.get(i));
		}
		result[key.getSize()] = value;
		return result;
	}

	@Override
	public boolean get(Model model, Object[] tuple) {
		int[] content = new int[tuple.length - 1];
		for (int i = 0; i < tuple.length - 1; i++) {
			content[i] = ((Tuple1) tuple[i]).value0();
		}
		Tuple key = Tuple.of(content);
		@SuppressWarnings("unchecked")
		T valueInTuple = (T) tuple[tuple.length - 1];
		T valueInMap = model.getInterpretation(getSymbol()).get(key);
		return valueInTuple.equals(valueInMap);
	}

	@Override
	public int arity() {
		return getSymbol().arity() + 1;
	}
}