diff options
Diffstat (limited to 'subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTest.java')
-rw-r--r-- | subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTest.java | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTest.java b/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTest.java new file mode 100644 index 00000000..931c62dd --- /dev/null +++ b/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTest.java | |||
@@ -0,0 +1,224 @@ | |||
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 org.hamcrest.Matchers; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
11 | import tools.refinery.store.reasoning.translator.TranslationException; | ||
12 | import tools.refinery.store.representation.TruthValue; | ||
13 | |||
14 | import java.util.Set; | ||
15 | |||
16 | import static org.hamcrest.MatcherAssert.assertThat; | ||
17 | import static org.hamcrest.Matchers.hasEntry; | ||
18 | import static org.junit.jupiter.api.Assertions.assertAll; | ||
19 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
20 | |||
21 | class TypeHierarchyTest { | ||
22 | @Test | ||
23 | void directSupertypesTest() { | ||
24 | var c1 = new PartialRelation("C1", 1); | ||
25 | var c2 = new PartialRelation("C2", 1); | ||
26 | var c3 = new PartialRelation("C3", 1); | ||
27 | |||
28 | var sut = TypeHierarchy.builder() | ||
29 | .type(c1, c2, c3) | ||
30 | .type(c2, c3) | ||
31 | .type(c3) | ||
32 | .build(); | ||
33 | var tester = new TypeHierarchyTester(sut); | ||
34 | |||
35 | assertAll( | ||
36 | () -> tester.assertConcreteType(c1), | ||
37 | () -> tester.assertConcreteType(c2, c1), | ||
38 | () -> tester.assertConcreteType(c3, c2) | ||
39 | ); | ||
40 | } | ||
41 | |||
42 | @Test | ||
43 | void typeEliminationAbstractToConcreteTest() { | ||
44 | var c1 = new PartialRelation("C1", 1); | ||
45 | var c2 = new PartialRelation("C2", 1); | ||
46 | var a11 = new PartialRelation("A11", 1); | ||
47 | var a12 = new PartialRelation("A12", 1); | ||
48 | var a21 = new PartialRelation("A21", 1); | ||
49 | var a22 = new PartialRelation("A22", 1); | ||
50 | var a3 = new PartialRelation("A3", 1); | ||
51 | |||
52 | var sut = TypeHierarchy.builder() | ||
53 | .type(a3, true) | ||
54 | .type(a21, true, a3) | ||
55 | .type(a22, true, a3) | ||
56 | .type(a11, true, a21, a22) | ||
57 | .type(a12, true, a21, a22) | ||
58 | .type(c1, a11, a12) | ||
59 | .type(c2, a3) | ||
60 | .build(); | ||
61 | var tester = new TypeHierarchyTester(sut); | ||
62 | |||
63 | assertAll( | ||
64 | () -> tester.assertConcreteType(c1), | ||
65 | () -> tester.assertConcreteType(c2), | ||
66 | () -> tester.assertEliminatedType(a11, c1), | ||
67 | () -> tester.assertEliminatedType(a12, c1), | ||
68 | () -> tester.assertEliminatedType(a21, c1), | ||
69 | () -> tester.assertEliminatedType(a22, c1), | ||
70 | () -> tester.assertAbstractType(a3, c1, c2) | ||
71 | ); | ||
72 | } | ||
73 | |||
74 | @Test | ||
75 | void typeEliminationConcreteToAbstractTest() { | ||
76 | var c1 = new PartialRelation("C1", 1); | ||
77 | var c2 = new PartialRelation("C2", 1); | ||
78 | var a11 = new PartialRelation("A11", 1); | ||
79 | var a12 = new PartialRelation("A12", 1); | ||
80 | var a21 = new PartialRelation("A21", 1); | ||
81 | var a22 = new PartialRelation("A22", 1); | ||
82 | var a3 = new PartialRelation("A3", 1); | ||
83 | |||
84 | var sut = TypeHierarchy.builder() | ||
85 | .type(c1, a11, a12) | ||
86 | .type(c2, a3) | ||
87 | .type(a11, true, a21, a22) | ||
88 | .type(a12, true, a21, a22) | ||
89 | .type(a21, true, a3) | ||
90 | .type(a22, true, a3) | ||
91 | .type(a3, true) | ||
92 | .build(); | ||
93 | var tester = new TypeHierarchyTester(sut); | ||
94 | |||
95 | assertAll( | ||
96 | () -> tester.assertConcreteType(c1), | ||
97 | () -> tester.assertConcreteType(c2), | ||
98 | () -> tester.assertEliminatedType(a11, c1), | ||
99 | () -> tester.assertEliminatedType(a12, c1), | ||
100 | () -> tester.assertEliminatedType(a21, c1), | ||
101 | () -> tester.assertEliminatedType(a22, c1), | ||
102 | () -> tester.assertAbstractType(a3, c1, c2) | ||
103 | ); | ||
104 | } | ||
105 | |||
106 | @Test | ||
107 | void preserveConcreteTypeTest() { | ||
108 | var c1 = new PartialRelation("C1", 1); | ||
109 | var a1 = new PartialRelation("A1", 1); | ||
110 | var c2 = new PartialRelation("C2", 1); | ||
111 | var a2 = new PartialRelation("A2", 1); | ||
112 | |||
113 | var sut = TypeHierarchy.builder() | ||
114 | .type(c1, a1) | ||
115 | .type(a1, true, c2) | ||
116 | .type(c2, a2) | ||
117 | .type(a2, true) | ||
118 | .build(); | ||
119 | var tester = new TypeHierarchyTester(sut); | ||
120 | |||
121 | assertAll( | ||
122 | () -> tester.assertConcreteType(c1), | ||
123 | () -> tester.assertEliminatedType(a1, c1), | ||
124 | () -> tester.assertConcreteType(c2, c1), | ||
125 | () -> tester.assertEliminatedType(a2, c2) | ||
126 | ); | ||
127 | } | ||
128 | |||
129 | @Test | ||
130 | void mostGeneralCurrentTypeTest() { | ||
131 | var c1 = new PartialRelation("C1", 1); | ||
132 | var c2 = new PartialRelation("C2", 1); | ||
133 | var c3 = new PartialRelation("C3", 1); | ||
134 | |||
135 | var sut = TypeHierarchy.builder() | ||
136 | .type(c1, c3) | ||
137 | .type(c2, c3) | ||
138 | .type(c3) | ||
139 | .build(); | ||
140 | var tester = new TypeHierarchyTester(sut); | ||
141 | var c3Result = tester.getPreservedType(c3); | ||
142 | |||
143 | var expected = new InferredType(Set.of(c3), Set.of(c1, c2, c3), c3); | ||
144 | assertAll( | ||
145 | () -> assertThat(tester.getInferredType(c3), Matchers.is(expected)), | ||
146 | () -> assertThat(c3Result.merge(sut.getUnknownType(), TruthValue.TRUE), Matchers.is(expected)) | ||
147 | ); | ||
148 | } | ||
149 | |||
150 | @Test | ||
151 | void preferFirstConcreteTypeTest() { | ||
152 | var a1 = new PartialRelation("A1", 1); | ||
153 | var c1 = new PartialRelation("C1", 1); | ||
154 | var c2 = new PartialRelation("C2", 1); | ||
155 | var c3 = new PartialRelation("C3", 1); | ||
156 | var c4 = new PartialRelation("C4", 1); | ||
157 | |||
158 | var sut = TypeHierarchy.builder() | ||
159 | .type(c1, a1) | ||
160 | .type(c2, a1) | ||
161 | .type(c3, a1) | ||
162 | .type(c4, c3) | ||
163 | .type(a1, true) | ||
164 | .build(); | ||
165 | var tester = new TypeHierarchyTester(sut); | ||
166 | var c1Result = tester.getPreservedType(c1); | ||
167 | var a1Result = tester.getPreservedType(a1); | ||
168 | |||
169 | assertThat(c1Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
170 | Matchers.is(new InferredType(Set.of(a1), Set.of(c2, c3, c4), c2))); | ||
171 | } | ||
172 | |||
173 | @Test | ||
174 | void preferFirstMostGeneralConcreteTypeTest() { | ||
175 | var a1 = new PartialRelation("A1", 1); | ||
176 | var c1 = new PartialRelation("C1", 1); | ||
177 | var c2 = new PartialRelation("C2", 1); | ||
178 | var c3 = new PartialRelation("C3", 1); | ||
179 | var c4 = new PartialRelation("C4", 1); | ||
180 | |||
181 | var sut = TypeHierarchy.builder() | ||
182 | .type(c4, c3) | ||
183 | .type(c3, a1) | ||
184 | .type(c2, a1) | ||
185 | .type(c1, a1) | ||
186 | .type(a1, true) | ||
187 | .build(); | ||
188 | var tester = new TypeHierarchyTester(sut); | ||
189 | var c1Result = tester.getPreservedType(c1); | ||
190 | var a1Result = tester.getPreservedType(a1); | ||
191 | |||
192 | assertThat(c1Result.merge(a1Result.asInferredType(), TruthValue.FALSE), | ||
193 | Matchers.is(new InferredType(Set.of(a1), Set.of(c2, c3, c4), c3))); | ||
194 | } | ||
195 | |||
196 | @Test | ||
197 | void circularTypeHierarchyTest() { | ||
198 | var c1 = new PartialRelation("C1", 1); | ||
199 | var c2 = new PartialRelation("C2", 1); | ||
200 | var builder = TypeHierarchy.builder() | ||
201 | .type(c1, c2) | ||
202 | .type(c2, c1); | ||
203 | |||
204 | assertThrows(TranslationException.class, builder::build); | ||
205 | } | ||
206 | |||
207 | @Test | ||
208 | void chainedEliminationTest() { | ||
209 | var a1 = new PartialRelation("A1", 1); | ||
210 | var a2 = new PartialRelation("A2", 1); | ||
211 | var c1 = new PartialRelation("C1", 1); | ||
212 | |||
213 | var sut = TypeHierarchy.builder() | ||
214 | .type(a1, true) | ||
215 | .type(a2, true, a1) | ||
216 | .type(c1, a2) | ||
217 | .build(); | ||
218 | |||
219 | assertAll( | ||
220 | () -> assertThat(sut.getEliminatedTypes(), hasEntry(a1, c1)), | ||
221 | () -> assertThat(sut.getEliminatedTypes(), hasEntry(a2, c1)) | ||
222 | ); | ||
223 | } | ||
224 | } | ||