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
|
package tools.refinery.store.query.view;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.dnf.FunctionalDependency;
import tools.refinery.store.query.term.DataSort;
import tools.refinery.store.query.term.NodeSort;
import tools.refinery.store.query.term.Sort;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.tuple.Tuple;
import tools.refinery.store.tuple.Tuple1;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class FunctionalRelationView<T> extends RelationView<T> {
private final T defaultValue;
public FunctionalRelationView(Symbol<T> symbol, String name) {
super(symbol, name);
defaultValue = symbol.defaultValue();
}
public FunctionalRelationView(Symbol<T> symbol) {
super(symbol);
defaultValue = symbol.defaultValue();
}
@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 !Objects.equals(defaultValue, value);
}
@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;
}
@Override
public List<Sort> getSorts() {
var sorts = new Sort[arity()];
int valueIndex = sorts.length - 1;
for (int i = 0; i < valueIndex; i++) {
sorts[i] = NodeSort.INSTANCE;
}
sorts[valueIndex] = new DataSort<>(getSymbol().valueType());
return List.of(sorts);
}
@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;
FunctionalRelationView<?> that = (FunctionalRelationView<?>) o;
return Objects.equals(defaultValue, that.defaultValue);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), defaultValue);
}
}
|