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
127
128
129
130
|
package tools.refinery.language.semantics.model.internal;
import org.eclipse.collections.api.LazyIntIterable;
import org.eclipse.collections.api.factory.primitive.IntObjectMaps;
import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
import org.eclipse.collections.api.tuple.primitive.IntObjectPair;
import tools.refinery.store.model.Tuple;
import tools.refinery.store.model.representation.TruthValue;
final class IntermediateNode extends DecisionTreeNode {
private final MutableIntObjectMap<DecisionTreeNode> children;
private final DecisionTreeNode otherwise;
IntermediateNode(MutableIntObjectMap<DecisionTreeNode> children, DecisionTreeNode otherwise) {
this.children = children;
this.otherwise = otherwise;
}
private DecisionTreeNode getChild(int index) {
var child = children.get(index);
return child == null ? otherwise : child;
}
@Override
public DecisionTreeValue getValue(int level, Tuple tuple) {
return getChild(tuple.get(level)).getValue(level - 1, tuple);
}
@Override
public DecisionTreeNode deepCopy() {
var newChildren = IntObjectMaps.mutable.from(children.keyValuesView(), IntObjectPair::getOne,
pair -> pair.getTwo().deepCopy());
var newOtherwise = otherwise.deepCopy();
return new IntermediateNode(newChildren, newOtherwise);
}
@Override
protected void mergeAllValues(int nextLevel, Tuple tuple, TruthValue value) {
otherwise.mergeValue(nextLevel, tuple, value);
for (var child : children) {
child.mergeValue(nextLevel, tuple, value);
}
reduceChildren();
}
@Override
protected void mergeSingleValue(int key, int nextLevel, Tuple tuple, TruthValue value) {
var otherwiseReduced = getOtherwiseReducedValue();
var child = children.get(key);
if (child == null) {
var newChild = otherwise.withMergedValue(nextLevel, tuple, value);
if (otherwiseReduced == null || newChild.getReducedValue() != otherwiseReduced) {
children.put(key, newChild);
}
return;
}
child.mergeValue(nextLevel, tuple, value);
if (otherwiseReduced != null && child.getReducedValue() == otherwiseReduced) {
children.remove(key);
}
}
@Override
public void overwriteValues(DecisionTreeNode values) {
if (values instanceof IntermediateNode intermediateValues) {
otherwise.overwriteValues(intermediateValues.otherwise);
for (var pair : children.keyValuesView()) {
pair.getTwo().overwriteValues(intermediateValues.getChild(pair.getOne()));
}
for (var pair : intermediateValues.children.keyValuesView()) {
children.getIfAbsentPut(pair.getOne(), () -> otherwise.withOverwrittenValues(pair.getTwo()));
}
reduceChildren();
} else {
throw new IllegalArgumentException("Level mismatch");
}
}
private void reduceChildren() {
var otherwiseReduced = getOtherwiseReducedValue();
if (otherwiseReduced == null) {
return;
}
var iterator = children.iterator();
while (iterator.hasNext()) {
var child = iterator.next();
if (child.getReducedValue() == otherwiseReduced) {
iterator.remove();
}
}
}
@Override
protected DecisionTreeValue getOtherwiseReducedValue() {
return otherwise.getReducedValue();
}
@Override
protected LazyIntIterable getChildKeys() {
return children.keysView();
}
protected boolean moveNextSparse(int level, DecisionTreeCursor cursor, int startIndex, int[] sortedChildren) {
for (int i = startIndex; i < sortedChildren.length; i++) {
var key = sortedChildren[i];
var child = getChild(key);
var found = child.moveNext(level - 1, cursor);
if (found) {
cursor.iterationState[level] = i;
cursor.rawTuple[level] = key;
return true;
}
}
return false;
}
protected boolean moveNextDense(int level, DecisionTreeCursor cursor, int startIndex) {
for (int i = startIndex; i < cursor.nodeCount; i++) {
var child = getChild(i);
var found = child.moveNext(level - 1, cursor);
if (found) {
cursor.iterationState[level] = i;
cursor.rawTuple[level] = i;
return true;
}
}
return false;
}
}
|