aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/typehierarchy/TypeHierarchyPartialModelTest.java
blob: e87b268425628b1fbd6c66ef92eb4361c9fbbff0 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.translator.typehierarchy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tools.refinery.store.model.Model;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.interpreter.QueryInterpreterAdapter;
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.logic.term.truthvalue.TruthValue;
import tools.refinery.store.tuple.Tuple;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class TypeHierarchyPartialModelTest {
	private final PartialRelation person = new PartialRelation("Person", 1);
	private final PartialRelation member = new PartialRelation("Member", 1);
	private final PartialRelation student = new PartialRelation("Student", 1);
	private final PartialRelation teacher = new PartialRelation("Teacher", 1);
	private final PartialRelation pet = new PartialRelation("Pet", 1);

	private Model model;

	@BeforeEach
	void beforeEach() {
		var typeHierarchy = TypeHierarchy.builder()
				.type(person, true)
				.type(member, true, person)
				.type(student, member)
				.type(teacher, member)
				.type(pet)
				.build();

		var store = ModelStore.builder()
				.with(QueryInterpreterAdapter.builder())
				.with(ReasoningAdapter.builder())
				.with(new TypeHierarchyTranslator(typeHierarchy))
				.build();

		var seed = ModelSeed.builder(4)
				.seed(person, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(3), TruthValue.FALSE))
				.seed(member, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(1), TruthValue.TRUE)
						.put(Tuple.of(2), TruthValue.TRUE))
				.seed(student, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0), TruthValue.TRUE)
						.put(Tuple.of(2), TruthValue.FALSE))
				.seed(teacher, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(pet, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.build();
		model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed);
	}

	@Test
	void initialModelTest() {
		var reasoningAdapter = model.getAdapter(ReasoningAdapter.class);

		var personInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, person);
		assertThat(personInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(personInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(personInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(personInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var memberInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, member);
		assertThat(memberInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(memberInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(memberInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(memberInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var studentInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, student);
		assertThat(studentInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(studentInterpretation.get(Tuple.of(1)), is(TruthValue.UNKNOWN));
		assertThat(studentInterpretation.get(Tuple.of(2)), is(TruthValue.FALSE));
		assertThat(studentInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var teacherInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, teacher);
		assertThat(teacherInterpretation.get(Tuple.of(0)), is(TruthValue.FALSE));
		assertThat(teacherInterpretation.get(Tuple.of(1)), is(TruthValue.UNKNOWN));
		assertThat(teacherInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(teacherInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var petInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, pet);
		assertThat(petInterpretation.get(Tuple.of(0)), is(TruthValue.FALSE));
		assertThat(petInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(petInterpretation.get(Tuple.of(2)), is(TruthValue.FALSE));
		assertThat(petInterpretation.get(Tuple.of(3)), is(TruthValue.UNKNOWN));
	}

	@Test
	void initialModelCandidateTest() {
		var reasoningAdapter = model.getAdapter(ReasoningAdapter.class);

		var personCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, person);
		assertThat(personCandidateInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(personCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(personCandidateInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(personCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var memberCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, member);
		assertThat(memberCandidateInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(memberCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(memberCandidateInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(memberCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var studentCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, student);
		assertThat(studentCandidateInterpretation.get(Tuple.of(0)), is(TruthValue.TRUE));
		assertThat(studentCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(studentCandidateInterpretation.get(Tuple.of(2)), is(TruthValue.FALSE));
		assertThat(studentCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var teacherCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, teacher);
		assertThat(teacherCandidateInterpretation.get(Tuple.of(0)), is(TruthValue.FALSE));
		assertThat(teacherCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(teacherCandidateInterpretation.get(Tuple.of(2)), is(TruthValue.TRUE));
		assertThat(teacherCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var petCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, pet);
		assertThat(petCandidateInterpretation.get(Tuple.of(0)), is(TruthValue.FALSE));
		assertThat(petCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(petCandidateInterpretation.get(Tuple.of(2)), is(TruthValue.FALSE));
		assertThat(petCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));
	}

	@Test
	void refinedModelTest() {
		var reasoningAdapter = model.getAdapter(ReasoningAdapter.class);
		var studentRefiner = reasoningAdapter.getRefiner(student);
		studentRefiner.merge(Tuple.of(1), TruthValue.FALSE);
		studentRefiner.merge(Tuple.of(3), TruthValue.TRUE);
		model.getAdapter(ModelQueryAdapter.class).flushChanges();

		var personInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, person);
		assertThat(personInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(personInterpretation.get(Tuple.of(3)), is(TruthValue.ERROR));

		var personCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, person);
		assertThat(personCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(personCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var memberInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, member);
		assertThat(memberInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(memberInterpretation.get(Tuple.of(3)), is(TruthValue.ERROR));

		var memberCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, member);
		assertThat(memberCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(memberCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var studentInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, student);
		assertThat(studentInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(studentInterpretation.get(Tuple.of(3)), is(TruthValue.ERROR));

		var studentCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, student);
		assertThat(studentCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(studentCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var teacherInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, teacher);
		assertThat(teacherInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(teacherInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var teacherCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, teacher);
		assertThat(teacherCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.TRUE));
		assertThat(teacherCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var petInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, pet);
		assertThat(petInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(petInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));

		var petCandidateInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, pet);
		assertThat(petCandidateInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(petCandidateInterpretation.get(Tuple.of(3)), is(TruthValue.FALSE));
	}
}