diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/refinement/DefaultStorageRefiner.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/refinement/DefaultStorageRefiner.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/refinement/DefaultStorageRefiner.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/refinement/DefaultStorageRefiner.java new file mode 100644 index 00000000..d4ec2e8b --- /dev/null +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/refinement/DefaultStorageRefiner.java | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.reasoning.refinement; | ||
7 | |||
8 | import tools.refinery.store.model.Interpretation; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | import tools.refinery.store.tuple.Tuple; | ||
12 | |||
13 | public class DefaultStorageRefiner<T> implements StorageRefiner { | ||
14 | private static final StorageRefiner.Factory<Object> FACTORY = DefaultStorageRefiner::new; | ||
15 | |||
16 | private final Interpretation<T> interpretation; | ||
17 | |||
18 | public DefaultStorageRefiner(Symbol<T> symbol, Model model) { | ||
19 | interpretation = model.getInterpretation(symbol); | ||
20 | } | ||
21 | |||
22 | @Override | ||
23 | public boolean split(int parentNode, int childNode) { | ||
24 | var symbol = interpretation.getSymbol(); | ||
25 | int arity = symbol.arity(); | ||
26 | for (int i = 0; i < arity; i++) { | ||
27 | int adjacentSize = interpretation.getAdjacentSize(i, parentNode); | ||
28 | if (adjacentSize == 0) { | ||
29 | continue; | ||
30 | } | ||
31 | var toSetKeys = new Tuple[adjacentSize]; | ||
32 | // This is safe, because we won't pass the array to the outside. | ||
33 | @SuppressWarnings("unchecked") | ||
34 | var toSetValues = (T[]) new Object[adjacentSize]; | ||
35 | var cursor = interpretation.getAdjacent(i, parentNode); | ||
36 | int j = 0; | ||
37 | while (cursor.move()) { | ||
38 | toSetKeys[j] = cursor.getKey().set(i, childNode); | ||
39 | toSetValues[j] = cursor.getValue(); | ||
40 | j++; | ||
41 | } | ||
42 | for (j = 0; j < adjacentSize; j++) { | ||
43 | interpretation.put(toSetKeys[j], toSetValues[j]); | ||
44 | } | ||
45 | } | ||
46 | return true; | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public boolean cleanup(int nodeToDelete) { | ||
51 | var symbol = interpretation.getSymbol(); | ||
52 | int arity = symbol.arity(); | ||
53 | var defaultValue = symbol.defaultValue(); | ||
54 | for (int i = 0; i < arity; i++) { | ||
55 | int adjacentSize = interpretation.getAdjacentSize(i, nodeToDelete); | ||
56 | if (adjacentSize == 0) { | ||
57 | continue; | ||
58 | } | ||
59 | var toDelete = new Tuple[adjacentSize]; | ||
60 | var cursor = interpretation.getAdjacent(i, nodeToDelete); | ||
61 | int j = 0; | ||
62 | while (cursor.move()) { | ||
63 | toDelete[j] = cursor.getKey(); | ||
64 | j++; | ||
65 | } | ||
66 | for (j = 0; j < adjacentSize; j++) { | ||
67 | interpretation.put(toDelete[j], defaultValue); | ||
68 | } | ||
69 | } | ||
70 | return true; | ||
71 | } | ||
72 | |||
73 | public static <T> StorageRefiner.Factory<T> factory() { | ||
74 | // This is safe, because {@code FACTORY} doesn't depend on {@code T} at all. | ||
75 | @SuppressWarnings("unchecked") | ||
76 | var typedFactory = (StorageRefiner.Factory<T>) FACTORY; | ||
77 | return typedFactory; | ||
78 | } | ||
79 | } | ||