aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/containment/ContainmentHierarchyTranslatorTest.java
blob: bbfaff8426392673ec929ed4380866443c4f6124 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.translator.containment;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.query.viatra.ViatraModelQueryAdapter;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.ReasoningStoreAdapter;
import tools.refinery.store.reasoning.literal.Concreteness;
import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.seed.ModelSeed;
import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator;
import tools.refinery.store.reasoning.translator.multiplicity.UnconstrainedMultiplicity;
import tools.refinery.store.reasoning.translator.typehierarchy.TypeHierarchy;
import tools.refinery.store.reasoning.translator.typehierarchy.TypeHierarchyTranslator;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.representation.cardinality.CardinalityIntervals;
import tools.refinery.store.tuple.Tuple;

import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static tools.refinery.store.reasoning.translator.containment.ContainmentHierarchyTranslator.CONTAINED_SYMBOL;
import static tools.refinery.store.reasoning.translator.containment.ContainmentHierarchyTranslator.CONTAINS_SYMBOL;
import static tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator.COUNT_SYMBOL;

class ContainmentHierarchyTranslatorTest {
	private final PartialRelation c1 = new PartialRelation("C1", 1);
	private final PartialRelation c2 = new PartialRelation("C2", 1);
	private final PartialRelation entry = new PartialRelation("entry", 2);

	private ModelStore store;

	@BeforeEach
	void beforeEach() {

		var typeHierarchy = TypeHierarchy.builder()
				.type(CONTAINED_SYMBOL, true)
				.type(c1)
				.type(c2, c1, CONTAINED_SYMBOL)
				.build();

		var containmentHierarchy = Map.of(
				entry,
				new ContainmentInfo(c1, UnconstrainedMultiplicity.INSTANCE, c2)
		);

		store = ModelStore.builder()
				.with(ViatraModelQueryAdapter.builder())
				.with(ReasoningAdapter.builder())
				.with(new MultiObjectTranslator())
				.with(new TypeHierarchyTranslator(typeHierarchy))
				.with(new ContainmentHierarchyTranslator(containmentHierarchy))
				.build();
	}

	@Test
	void treeTest() {
		var modelSeed = ModelSeed.builder(3)
				.seed(COUNT_SYMBOL, builder -> builder.reducedValue(CardinalityIntervals.ONE))
				.seed(CONTAINED_SYMBOL, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(CONTAINS_SYMBOL, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(c1, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0), TruthValue.TRUE))
				.seed(c2, builder -> builder
						.put(Tuple.of(1), TruthValue.TRUE)
						.put(Tuple.of(2), TruthValue.TRUE))
				.seed(entry, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0, 1), TruthValue.TRUE)
						.put(Tuple.of(0, 2), TruthValue.TRUE))
				.build();

		var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed);
		var interpretation = model.getAdapter(ReasoningAdapter.class).getPartialInterpretation(Concreteness.PARTIAL,
				entry);

		assertThat(interpretation.get(Tuple.of(0, 0)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(0, 1)), is(TruthValue.TRUE));
		assertThat(interpretation.get(Tuple.of(0, 2)), is(TruthValue.TRUE));
		assertThat(interpretation.get(Tuple.of(1, 0)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(1, 1)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(1, 2)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(2, 0)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(2, 1)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(2, 2)), is(TruthValue.FALSE));
	}

	@Test
	void loopTest() {
		var modelSeed = ModelSeed.builder(3)
				.seed(COUNT_SYMBOL, builder -> builder.reducedValue(CardinalityIntervals.ONE))
				.seed(CONTAINED_SYMBOL, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(CONTAINS_SYMBOL, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(c1, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(c2, builder -> builder
						.put(Tuple.of(0), TruthValue.TRUE)
						.put(Tuple.of(1), TruthValue.TRUE)
						.put(Tuple.of(2), TruthValue.TRUE))
				.seed(entry, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0, 1), TruthValue.TRUE)
						.put(Tuple.of(1, 2), TruthValue.TRUE)
						.put(Tuple.of(2, 0), TruthValue.TRUE))
				.build();

		var model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed);
		var interpretation = model.getAdapter(ReasoningAdapter.class).getPartialInterpretation(Concreteness.PARTIAL,
				entry);

		assertThat(interpretation.get(Tuple.of(0, 0)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(0, 1)), is(TruthValue.ERROR));
		assertThat(interpretation.get(Tuple.of(0, 2)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(1, 0)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(1, 1)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(1, 2)), is(TruthValue.ERROR));
		assertThat(interpretation.get(Tuple.of(2, 0)), is(TruthValue.ERROR));
		assertThat(interpretation.get(Tuple.of(2, 1)), is(TruthValue.FALSE));
		assertThat(interpretation.get(Tuple.of(2, 2)), is(TruthValue.FALSE));
	}
}