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
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.reasoning.seed;
import tools.refinery.store.map.Cursor;
import tools.refinery.store.reasoning.representation.PartialSymbol;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.tuple.Tuple;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public interface Seed<T> {
int arity();
Class<T> valueType();
T majorityValue();
T get(Tuple key);
Cursor<Tuple, T> getCursor(T defaultValue, int nodeCount);
static <T> Builder<T> builder(int arity, Class<T> valueType, T reducedValue) {
return new Builder<>(arity, valueType, reducedValue);
}
static <T> Builder<T> builder(Symbol<T> symbol) {
return builder(symbol.arity(), symbol.valueType(), symbol.defaultValue());
}
static <T> Builder<T> builder(PartialSymbol<T, ?> partialSymbol) {
return builder(partialSymbol.arity(), partialSymbol.abstractDomain().abstractType(),
partialSymbol.defaultValue());
}
@SuppressWarnings("UnusedReturnValue")
class Builder<T> {
private final int arity;
private final Class<T> valueType;
private T reducedValue;
private final Map<Tuple, T> map = new LinkedHashMap<>();
private Builder(int arity, Class<T> valueType, T reducedValue) {
this.arity = arity;
this.valueType = valueType;
this.reducedValue = reducedValue;
}
public Builder<T> reducedValue(T reducedValue) {
this.reducedValue = reducedValue;
return this;
}
public Builder<T> put(Tuple key, T value) {
if (key.getSize() != arity) {
throw new IllegalArgumentException("Expected %s to have %d elements".formatted(key, arity));
}
map.put(key, value);
return this;
}
public Builder<T> putAll(Map<Tuple, T> map) {
for (var entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this;
}
public Seed<T> build() {
return new MapBasedSeed<>(arity, valueType, reducedValue, Collections.unmodifiableMap(map));
}
}
}
|