diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyInitializer.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyInitializer.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyInitializer.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyInitializer.java new file mode 100644 index 00000000..233e43f0 --- /dev/null +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyInitializer.java | |||
@@ -0,0 +1,64 @@ | |||
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.translator.typehierarchy; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.reasoning.refinement.PartialModelInitializer; | ||
10 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
11 | import tools.refinery.store.reasoning.seed.ModelSeed; | ||
12 | import tools.refinery.store.representation.Symbol; | ||
13 | import tools.refinery.store.representation.TruthValue; | ||
14 | import tools.refinery.store.tuple.Tuple; | ||
15 | |||
16 | import java.util.Arrays; | ||
17 | import java.util.HashMap; | ||
18 | import java.util.function.Function; | ||
19 | |||
20 | public class TypeHierarchyInitializer implements PartialModelInitializer { | ||
21 | private final TypeHierarchy typeHierarchy; | ||
22 | private final Symbol<InferredType> typeSymbol; | ||
23 | |||
24 | public TypeHierarchyInitializer(TypeHierarchy typeHierarchy, Symbol<InferredType> typeSymbol) { | ||
25 | this.typeHierarchy = typeHierarchy; | ||
26 | this.typeSymbol = typeSymbol; | ||
27 | } | ||
28 | |||
29 | @Override | ||
30 | public void initialize(Model model, ModelSeed modelSeed) { | ||
31 | var inferredTypes = new InferredType[modelSeed.getNodeCount()]; | ||
32 | Arrays.fill(inferredTypes, typeHierarchy.getUnknownType()); | ||
33 | for (var type : typeHierarchy.getAllTypes()) { | ||
34 | model.checkCancelled(); | ||
35 | initializeType(type, inferredTypes, model, modelSeed); | ||
36 | } | ||
37 | var typeInterpretation = model.getInterpretation(typeSymbol); | ||
38 | var uniqueTable = new HashMap<InferredType, InferredType>(); | ||
39 | for (int i = 0; i < inferredTypes.length; i++) { | ||
40 | model.checkCancelled(); | ||
41 | var uniqueType = uniqueTable.computeIfAbsent(inferredTypes[i], Function.identity()); | ||
42 | typeInterpretation.put(Tuple.of(i), uniqueType); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | private void initializeType(PartialRelation type, InferredType[] inferredTypes, Model model, ModelSeed modelSeed) { | ||
47 | var cursor = modelSeed.getCursor(type, TruthValue.UNKNOWN); | ||
48 | var analysisResult = typeHierarchy.getAnalysisResult(type); | ||
49 | while (cursor.move()) { | ||
50 | model.checkCancelled(); | ||
51 | var i = cursor.getKey().get(0); | ||
52 | checkNodeId(inferredTypes, i); | ||
53 | var value = cursor.getValue(); | ||
54 | inferredTypes[i] = analysisResult.merge(inferredTypes[i], value); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | private void checkNodeId(InferredType[] inferredTypes, int nodeId) { | ||
59 | if (nodeId < 0 || nodeId >= inferredTypes.length) { | ||
60 | throw new IllegalArgumentException("Expected node id %d to be lower than model size %d" | ||
61 | .formatted(nodeId, inferredTypes.length)); | ||
62 | } | ||
63 | } | ||
64 | } | ||