diff options
author | 2023-08-13 23:33:03 +0200 | |
---|---|---|
committer | 2023-08-13 23:33:03 +0200 | |
commit | 6b0c83a05cd55d3508ca7dc7d7d1648d602bc613 (patch) | |
tree | 64f6fa67f43524ac32c8b552dd1f09c015d5f7eb | |
parent | feat: metamodel translator (diff) | |
download | refinery-6b0c83a05cd55d3508ca7dc7d7d1648d602bc613.tar.gz refinery-6b0c83a05cd55d3508ca7dc7d7d1648d602bc613.tar.zst refinery-6b0c83a05cd55d3508ca7dc7d7d1648d602bc613.zip |
fix: concrete supertype translation
2 files changed, 149 insertions, 5 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTranslator.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTranslator.java index 4bff4557..67e8035f 100644 --- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTranslator.java +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyTranslator.java | |||
@@ -45,13 +45,12 @@ public class TypeHierarchyTranslator implements ModelStoreConfiguration { | |||
45 | 45 | ||
46 | private ModelStoreConfiguration createPreservedTypeTranslator(PartialRelation type, TypeAnalysisResult result) { | 46 | private ModelStoreConfiguration createPreservedTypeTranslator(PartialRelation type, TypeAnalysisResult result) { |
47 | var may = Query.of(type.name() + "#partial#may", (builder, p1) -> { | 47 | var may = Query.of(type.name() + "#partial#may", (builder, p1) -> { |
48 | if (result.isAbstractType()) { | 48 | if (!result.isAbstractType()) { |
49 | for (var subtype : result.getDirectSubtypes()) { | ||
50 | builder.clause(PartialLiterals.may(subtype.call(p1))); | ||
51 | } | ||
52 | } else { | ||
53 | builder.clause(new MayTypeView(typeSymbol, type).call(p1)); | 49 | builder.clause(new MayTypeView(typeSymbol, type).call(p1)); |
54 | } | 50 | } |
51 | for (var subtype : result.getDirectSubtypes()) { | ||
52 | builder.clause(PartialLiterals.may(subtype.call(p1))); | ||
53 | } | ||
55 | }); | 54 | }); |
56 | 55 | ||
57 | var must = Query.of(type.name() + "#partial#must", (builder, p1) -> builder.clause( | 56 | var must = Query.of(type.name() + "#partial#must", (builder, p1) -> builder.clause( |
diff --git a/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/ConcreteSupertypeTest.java b/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/ConcreteSupertypeTest.java new file mode 100644 index 00000000..3658d603 --- /dev/null +++ b/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/ConcreteSupertypeTest.java | |||
@@ -0,0 +1,145 @@ | |||
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 org.junit.jupiter.api.BeforeEach; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | import tools.refinery.store.model.ModelStore; | ||
11 | import tools.refinery.store.query.viatra.ViatraModelQueryAdapter; | ||
12 | import tools.refinery.store.reasoning.ReasoningAdapter; | ||
13 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
14 | import tools.refinery.store.reasoning.literal.Concreteness; | ||
15 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
16 | import tools.refinery.store.reasoning.seed.ModelSeed; | ||
17 | import tools.refinery.store.representation.TruthValue; | ||
18 | import tools.refinery.store.tuple.Tuple; | ||
19 | |||
20 | import static org.hamcrest.MatcherAssert.assertThat; | ||
21 | import static org.hamcrest.Matchers.is; | ||
22 | |||
23 | class ConcreteSupertypeTest { | ||
24 | private final PartialRelation c1 = new PartialRelation("C1", 1); | ||
25 | private final PartialRelation c2 = new PartialRelation("C2", 1); | ||
26 | |||
27 | private ModelStore store; | ||
28 | |||
29 | @BeforeEach | ||
30 | void beforeEach() { | ||
31 | var typeHierarchy = TypeHierarchy.builder() | ||
32 | .type(c1) | ||
33 | .type(c2, c1) | ||
34 | .build(); | ||
35 | |||
36 | store = ModelStore.builder() | ||
37 | .with(ViatraModelQueryAdapter.builder()) | ||
38 | .with(ReasoningAdapter.builder()) | ||
39 | .with(new TypeHierarchyTranslator(typeHierarchy)) | ||
40 | .build(); | ||
41 | } | ||
42 | |||
43 | @Test | ||
44 | void inheritedTypeTrueTest() { | ||
45 | var seed = ModelSeed.builder(1) | ||
46 | .seed(c1, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
47 | .seed(c2, builder -> builder | ||
48 | .reducedValue(TruthValue.UNKNOWN) | ||
49 | .put(Tuple.of(0), TruthValue.TRUE)) | ||
50 | .build(); | ||
51 | |||
52 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
53 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
54 | |||
55 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), is(TruthValue.TRUE)); | ||
56 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), is(TruthValue.TRUE)); | ||
57 | } | ||
58 | |||
59 | @Test | ||
60 | void inheritedTypeFalseTest() { | ||
61 | var seed = ModelSeed.builder(1) | ||
62 | .seed(c1, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
63 | .seed(c2, builder -> builder | ||
64 | .reducedValue(TruthValue.UNKNOWN) | ||
65 | .put(Tuple.of(0), TruthValue.FALSE)) | ||
66 | .build(); | ||
67 | |||
68 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
69 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
70 | |||
71 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), | ||
72 | is(TruthValue.UNKNOWN)); | ||
73 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), is(TruthValue.FALSE)); | ||
74 | } | ||
75 | |||
76 | @Test | ||
77 | void supertypeTrueTest() { | ||
78 | var seed = ModelSeed.builder(1) | ||
79 | .seed(c1, builder -> builder | ||
80 | .reducedValue(TruthValue.UNKNOWN) | ||
81 | .put(Tuple.of(0), TruthValue.TRUE)) | ||
82 | .seed(c2, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
83 | .build(); | ||
84 | |||
85 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
86 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
87 | |||
88 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), is(TruthValue.TRUE)); | ||
89 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), | ||
90 | is(TruthValue.UNKNOWN)); | ||
91 | } | ||
92 | |||
93 | @Test | ||
94 | void supertypeFalseTest() { | ||
95 | var seed = ModelSeed.builder(1) | ||
96 | .seed(c1, builder -> builder | ||
97 | .reducedValue(TruthValue.UNKNOWN) | ||
98 | .put(Tuple.of(0), TruthValue.FALSE)) | ||
99 | .seed(c2, builder -> builder.reducedValue(TruthValue.UNKNOWN)) | ||
100 | .build(); | ||
101 | |||
102 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
103 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
104 | |||
105 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), is(TruthValue.FALSE)); | ||
106 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), is(TruthValue.FALSE)); | ||
107 | } | ||
108 | |||
109 | @Test | ||
110 | void supertypeOnlyTest() { | ||
111 | var seed = ModelSeed.builder(1) | ||
112 | .seed(c1, builder -> builder | ||
113 | .reducedValue(TruthValue.UNKNOWN) | ||
114 | .put(Tuple.of(0), TruthValue.TRUE)) | ||
115 | .seed(c2, builder -> builder | ||
116 | .reducedValue(TruthValue.UNKNOWN) | ||
117 | .put(Tuple.of(0), TruthValue.FALSE)) | ||
118 | .build(); | ||
119 | |||
120 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
121 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
122 | |||
123 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), is(TruthValue.TRUE)); | ||
124 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), is(TruthValue.FALSE)); | ||
125 | } | ||
126 | |||
127 | |||
128 | @Test | ||
129 | void inheritedTypeErrorTest() { | ||
130 | var seed = ModelSeed.builder(1) | ||
131 | .seed(c1, builder -> builder | ||
132 | .reducedValue(TruthValue.UNKNOWN) | ||
133 | .put(Tuple.of(0), TruthValue.FALSE)) | ||
134 | .seed(c2, builder -> builder | ||
135 | .reducedValue(TruthValue.UNKNOWN) | ||
136 | .put(Tuple.of(0), TruthValue.TRUE)) | ||
137 | .build(); | ||
138 | |||
139 | var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed); | ||
140 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
141 | |||
142 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c1).get(Tuple.of(0)), is(TruthValue.ERROR)); | ||
143 | assertThat(adapter.getPartialInterpretation(Concreteness.PARTIAL, c2).get(Tuple.of(0)), is(TruthValue.ERROR)); | ||
144 | } | ||
145 | } | ||