aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/test/java/tools/refinery/store/reasoning/translator/metamodel/MetamodelTest.java
blob: eabbdffec106770da2e7d0a09d222964741cea86 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.translator.metamodel;

import org.junit.jupiter.api.Test;
import tools.refinery.store.model.Model;
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.containment.ContainmentHierarchyTranslator;
import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator;
import tools.refinery.store.reasoning.translator.multiplicity.ConstrainedMultiplicity;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.representation.cardinality.CardinalityIntervals;
import tools.refinery.store.tuple.Tuple;

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

class MetamodelTest {
	private final PartialRelation person = new PartialRelation("Person", 1);
	private final PartialRelation student = new PartialRelation("Student", 1);
	private final PartialRelation teacher = new PartialRelation("Teacher", 1);
	private final PartialRelation university = new PartialRelation("University", 1);
	private final PartialRelation course = new PartialRelation("Course", 1);
	private final PartialRelation courses = new PartialRelation("courses", 2);
	private final PartialRelation location = new PartialRelation("location", 2);
	private final PartialRelation lecturer = new PartialRelation("lecturer", 2);
	private final PartialRelation invalidLecturerCount = new PartialRelation("invalidLecturerCount", 1);
	private final PartialRelation enrolledStudents = new PartialRelation("enrolledStudents", 2);
	private final PartialRelation invalidStudentCount = new PartialRelation("invalidStudentCount", 1);

	@Test
	void metamodelTest() {
		var metamodel = Metamodel.builder()
				.type(person, true)
				.type(student, person)
				.type(teacher, person)
				.type(university)
				.type(course)
				.reference(courses, university, true, course, location)
				.reference(location, course, university, courses)
				.reference(lecturer, course,
						ConstrainedMultiplicity.of(CardinalityIntervals.ONE, invalidLecturerCount), teacher)
				.reference(enrolledStudents, course,
						ConstrainedMultiplicity.of(CardinalityIntervals.SOME, invalidStudentCount), student)
				.build();

		var seed = ModelSeed.builder(5)
				.seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder
						.reducedValue(CardinalityIntervals.ONE)
						.put(Tuple.of(1), CardinalityIntervals.SET)
						.put(Tuple.of(4), CardinalityIntervals.SET))
				.seed(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, builder -> builder
						.reducedValue(TruthValue.UNKNOWN))
				.seed(ContainmentHierarchyTranslator.CONTAINS_SYMBOL, builder -> builder
						.reducedValue(TruthValue.UNKNOWN))
				.seed(person, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(student, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(teacher, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(university, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0), TruthValue.TRUE))
				.seed(course, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(2), TruthValue.TRUE))
				.seed(courses, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.seed(location, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(1, 0), TruthValue.TRUE))
				.seed(lecturer, builder -> builder
						.reducedValue(TruthValue.FALSE)
						.put(Tuple.of(1, 3), TruthValue.TRUE))
				.seed(enrolledStudents, builder -> builder.reducedValue(TruthValue.UNKNOWN))
				.build();

		var model = createModel(metamodel, seed);
		var reasoningAdapter = model.getAdapter(ReasoningAdapter.class);

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

		var invalidLecturerCountInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL,
				invalidLecturerCount);
		assertThat(invalidLecturerCountInterpretation.get(Tuple.of(1)), is(TruthValue.FALSE));
		assertThat(invalidLecturerCountInterpretation.get(Tuple.of(2)), is(TruthValue.ERROR));

		var enrolledStudentsInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL,
				enrolledStudents);
		assertThat(enrolledStudentsInterpretation.get(Tuple.of(1, 3)), is(TruthValue.FALSE));
		assertThat(enrolledStudentsInterpretation.get(Tuple.of(1, 4)), is(TruthValue.UNKNOWN));
	}

	@Test
	void simpleContainmentTest() {
		var metamodel = Metamodel.builder()
				.type(university)
				.type(course)
				.reference(courses, university, true, course)
				.build();


		var seed = ModelSeed.builder(4)
				.seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder
						.reducedValue(CardinalityIntervals.ONE)
						.put(Tuple.of(0), CardinalityIntervals.SET)
						.put(Tuple.of(1), CardinalityIntervals.SET))
				.seed(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, builder -> builder
						.reducedValue(TruthValue.UNKNOWN))
				.seed(ContainmentHierarchyTranslator.CONTAINS_SYMBOL, builder -> builder
						.reducedValue(TruthValue.UNKNOWN))
				.seed(university, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(0), TruthValue.TRUE))
				.seed(course, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(1), TruthValue.TRUE))
				.seed(courses, builder -> builder
						.reducedValue(TruthValue.UNKNOWN)
						.put(Tuple.of(2, 3), TruthValue.TRUE))
				.build();

		var model = createModel(metamodel, seed);
		var coursesInterpretation = model.getAdapter(ReasoningAdapter.class)
				.getPartialInterpretation(Concreteness.PARTIAL, courses);

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

	private static Model createModel(Metamodel metamodel, ModelSeed seed) {
		var store = ModelStore.builder()
				.with(ViatraModelQueryAdapter.builder())
				.with(ReasoningAdapter.builder())
				.with(new MultiObjectTranslator())
				.with(new MetamodelTranslator(metamodel))
				.build();

		return store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(seed);
	}
}