diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/PreservedType.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/PreservedType.java | 141 |
1 files changed, 0 insertions, 141 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/PreservedType.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/PreservedType.java deleted file mode 100644 index 0696f4c3..00000000 --- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/PreservedType.java +++ /dev/null | |||
@@ -1,141 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-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.reasoning.representation.PartialRelation; | ||
9 | import tools.refinery.store.representation.TruthValue; | ||
10 | |||
11 | import java.util.*; | ||
12 | |||
13 | final class PreservedType implements TypeAnalysisResult { | ||
14 | private final ExtendedTypeInfo extendedTypeInfo; | ||
15 | private final List<PartialRelation> directSubtypes; | ||
16 | private final List<ExtendedTypeInfo> allExternalTypeInfoList; | ||
17 | private final InferredType inferredType; | ||
18 | |||
19 | public PreservedType(ExtendedTypeInfo extendedTypeInfo, List<ExtendedTypeInfo> allExternalTypeInfoList) { | ||
20 | this.extendedTypeInfo = extendedTypeInfo; | ||
21 | directSubtypes = List.copyOf(extendedTypeInfo.getDirectSubtypes()); | ||
22 | this.allExternalTypeInfoList = allExternalTypeInfoList; | ||
23 | inferredType = propagateMust(extendedTypeInfo.getAllSupertypesAndSelf(), | ||
24 | extendedTypeInfo.getConcreteSubtypesAndSelf()); | ||
25 | } | ||
26 | |||
27 | public PartialRelation type() { | ||
28 | return extendedTypeInfo.getType(); | ||
29 | } | ||
30 | |||
31 | public List<PartialRelation> getDirectSubtypes() { | ||
32 | return directSubtypes; | ||
33 | } | ||
34 | |||
35 | public boolean isAbstractType() { | ||
36 | return extendedTypeInfo.isAbstractType(); | ||
37 | } | ||
38 | |||
39 | public boolean isVacuous() { | ||
40 | return isAbstractType() && directSubtypes.isEmpty(); | ||
41 | } | ||
42 | |||
43 | public InferredType asInferredType() { | ||
44 | return inferredType; | ||
45 | } | ||
46 | |||
47 | public InferredType merge(InferredType inferredType, TruthValue value) { | ||
48 | return switch (value) { | ||
49 | case UNKNOWN -> inferredType; | ||
50 | case TRUE -> addMust(inferredType); | ||
51 | case FALSE -> removeMay(inferredType); | ||
52 | case ERROR -> addError(inferredType); | ||
53 | }; | ||
54 | } | ||
55 | |||
56 | private InferredType addMust(InferredType inferredType) { | ||
57 | var originalMustTypes = inferredType.mustTypes(); | ||
58 | if (originalMustTypes.contains(type())) { | ||
59 | return inferredType; | ||
60 | } | ||
61 | var mustTypes = new HashSet<>(originalMustTypes); | ||
62 | extendedTypeInfo.addMust(mustTypes); | ||
63 | var originalMayConcreteTypes = inferredType.mayConcreteTypes(); | ||
64 | var mayConcreteTypes = new LinkedHashSet<>(originalMayConcreteTypes); | ||
65 | Set<PartialRelation> mayConcreteTypesResult; | ||
66 | if (mayConcreteTypes.retainAll(extendedTypeInfo.getConcreteSubtypesAndSelf())) { | ||
67 | mayConcreteTypesResult = mayConcreteTypes; | ||
68 | } else { | ||
69 | mayConcreteTypesResult = originalMayConcreteTypes; | ||
70 | } | ||
71 | return propagateMust(mustTypes, mayConcreteTypesResult); | ||
72 | } | ||
73 | |||
74 | private InferredType removeMay(InferredType inferredType) { | ||
75 | var originalMayConcreteTypes = inferredType.mayConcreteTypes(); | ||
76 | var mayConcreteTypes = new LinkedHashSet<>(originalMayConcreteTypes); | ||
77 | if (!mayConcreteTypes.removeAll(extendedTypeInfo.getConcreteSubtypesAndSelf())) { | ||
78 | return inferredType; | ||
79 | } | ||
80 | return propagateMust(inferredType.mustTypes(), mayConcreteTypes); | ||
81 | } | ||
82 | |||
83 | private InferredType addError(InferredType inferredType) { | ||
84 | var originalMustTypes = inferredType.mustTypes(); | ||
85 | if (originalMustTypes.contains(type())) { | ||
86 | if (inferredType.mayConcreteTypes().isEmpty()) { | ||
87 | return inferredType; | ||
88 | } | ||
89 | return new InferredType(originalMustTypes, Set.of(), null); | ||
90 | } | ||
91 | var mustTypes = new HashSet<>(originalMustTypes); | ||
92 | extendedTypeInfo.addMust(mustTypes); | ||
93 | return new InferredType(mustTypes, Set.of(), null); | ||
94 | } | ||
95 | |||
96 | private InferredType propagateMust(Set<PartialRelation> originalMustTypes, | ||
97 | Set<PartialRelation> mayConcreteTypes) { | ||
98 | // It is possible that there is not type at all, do not force one by propagation. | ||
99 | var maybeUntyped = originalMustTypes.isEmpty(); | ||
100 | // Para-consistent case, do not propagate must types to avoid logical explosion. | ||
101 | var paraConsistentOrSurelyUntyped = mayConcreteTypes.isEmpty(); | ||
102 | if (maybeUntyped || paraConsistentOrSurelyUntyped) { | ||
103 | return new InferredType(originalMustTypes, mayConcreteTypes, null); | ||
104 | } | ||
105 | var currentType = computeCurrentType(mayConcreteTypes); | ||
106 | var mustTypes = new HashSet<>(originalMustTypes); | ||
107 | boolean changed = false; | ||
108 | for (var newMustExtendedTypeInfo : allExternalTypeInfoList) { | ||
109 | var newMustType = newMustExtendedTypeInfo.getType(); | ||
110 | if (mustTypes.contains(newMustType)) { | ||
111 | continue; | ||
112 | } | ||
113 | if (newMustExtendedTypeInfo.allowsAllConcreteTypes(mayConcreteTypes)) { | ||
114 | newMustExtendedTypeInfo.addMust(mustTypes); | ||
115 | changed = true; | ||
116 | } | ||
117 | } | ||
118 | if (!changed) { | ||
119 | return new InferredType(originalMustTypes, mayConcreteTypes, currentType); | ||
120 | } | ||
121 | return new InferredType(mustTypes, mayConcreteTypes, currentType); | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * Returns a concrete type that is allowed by a (consistent, i.e., nonempty) set of <b>may</b> concrete types. | ||
126 | * | ||
127 | * @param mayConcreteTypes The set of allowed concrete types. Must not be empty. | ||
128 | * @return The first concrete type that is allowed by {@code matConcreteTypes}. | ||
129 | */ | ||
130 | private PartialRelation computeCurrentType(Set<PartialRelation> mayConcreteTypes) { | ||
131 | for (var concreteExtendedTypeInfo : allExternalTypeInfoList) { | ||
132 | var concreteType = concreteExtendedTypeInfo.getType(); | ||
133 | if (!concreteExtendedTypeInfo.isAbstractType() && mayConcreteTypes.contains(concreteType)) { | ||
134 | return concreteType; | ||
135 | } | ||
136 | } | ||
137 | // We have already filtered out the para-consistent case in {@link #propagateMust(Set<PartialRelation>, | ||
138 | // Set<PartialRelation>}. | ||
139 | throw new AssertionError("No concrete type in %s".formatted(mayConcreteTypes)); | ||
140 | } | ||
141 | } | ||