diff options
author | 2021-08-16 04:30:18 +0200 | |
---|---|---|
committer | 2021-08-16 04:30:18 +0200 | |
commit | fb3381d87d49e879ef2f7913b2b49323ff59fe61 (patch) | |
tree | 76d5b3c13766c06c2378d329dbaebf22728eb329 /model-data | |
parent | Included EMF for rete recipies (hopefully temporally) (diff) | |
download | refinery-fb3381d87d49e879ef2f7913b2b49323ff59fe61.tar.gz refinery-fb3381d87d49e879ef2f7913b2b49323ff59fe61.tar.zst refinery-fb3381d87d49e879ef2f7913b2b49323ff59fe61.zip |
First working test for querying models.
Co-authored-by: Attila Ficsor <ficsorattila96@gmail.com>
Diffstat (limited to 'model-data')
-rw-r--r-- | model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java b/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java new file mode 100644 index 00000000..50fcf3f4 --- /dev/null +++ b/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java | |||
@@ -0,0 +1,93 @@ | |||
1 | package org.eclipse.viatra.solver.data.query.test; | ||
2 | |||
3 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
4 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
5 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
6 | |||
7 | import java.util.Collection; | ||
8 | import java.util.Set; | ||
9 | |||
10 | import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine; | ||
11 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatch; | ||
12 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatcher; | ||
13 | import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification; | ||
14 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
15 | import org.eclipse.viatra.solver.data.model.Model; | ||
16 | import org.eclipse.viatra.solver.data.model.ModelStore; | ||
17 | import org.eclipse.viatra.solver.data.model.ModelStoreImpl; | ||
18 | import org.eclipse.viatra.solver.data.model.Tuple; | ||
19 | import org.eclipse.viatra.solver.data.model.representation.Relation; | ||
20 | import org.eclipse.viatra.solver.data.model.representation.TruthValue; | ||
21 | import org.eclipse.viatra.solver.data.query.RelationalQuery; | ||
22 | import org.eclipse.viatra.solver.data.query.RelationalScope; | ||
23 | import org.eclipse.viatra.solver.data.query.view.FilteredRelationView; | ||
24 | import org.eclipse.viatra.solver.data.query.view.FunctionalRelationView; | ||
25 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
26 | import org.eclipse.viatra.solver.data.query.view.TupleRelationView; | ||
27 | import org.junit.jupiter.api.Test; | ||
28 | |||
29 | class QueryTest { | ||
30 | @Test | ||
31 | void minimalTest() { | ||
32 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
33 | |||
34 | ModelStore store = new ModelStoreImpl(Set.of(person)); | ||
35 | Model model = store.createModel(); | ||
36 | |||
37 | model.put(person, Tuple.of(0), true); | ||
38 | model.put(person, Tuple.of(1), true); | ||
39 | |||
40 | RelationView<Boolean> persionView = new TupleRelationView(model, person); | ||
41 | |||
42 | RelationalScope scope = new RelationalScope(Set.of(persionView)); | ||
43 | |||
44 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) | ||
45 | .addParameter("p",persionView) | ||
46 | .addConstraint(persionView, "p").build(); | ||
47 | |||
48 | ViatraQueryEngine engine = AdvancedViatraQueryEngine.on(scope); | ||
49 | GenericPatternMatcher personMatcher = engine.getMatcher(personQuery); | ||
50 | |||
51 | assertEquals(2, personMatcher.countMatches()); | ||
52 | } | ||
53 | |||
54 | void modelBuildingTest() { | ||
55 | Relation<Boolean> person = new Relation<>("Person", 1, false); | ||
56 | Relation<Integer> age = new Relation<Integer>("age", 1, null); | ||
57 | Relation<TruthValue> friend = new Relation<>("friend", 2, TruthValue.False); | ||
58 | |||
59 | ModelStore store = new ModelStoreImpl(Set.of(person, age, friend)); | ||
60 | Model model = store.createModel(); | ||
61 | |||
62 | model.put(person, Tuple.of(0), true); | ||
63 | model.put(person, Tuple.of(1), true); | ||
64 | model.put(age, Tuple.of(0), 3); | ||
65 | model.put(age, Tuple.of(1), 1); | ||
66 | model.put(friend, Tuple.of(0, 1), TruthValue.True); | ||
67 | model.put(friend, Tuple.of(1, 0), TruthValue.Unknown); | ||
68 | |||
69 | // Sanity check | ||
70 | assertTrue(model.get(person, Tuple.of(0))); | ||
71 | assertTrue(model.get(person, Tuple.of(1))); | ||
72 | assertFalse(model.get(person, Tuple.of(2))); | ||
73 | |||
74 | RelationView<Boolean> persionView = new TupleRelationView(model, person); | ||
75 | RelationView<Integer> ageView = new FunctionalRelationView<>(model, age); | ||
76 | RelationView<TruthValue> friendMustView = new FilteredRelationView<TruthValue>(model, friend, | ||
77 | (k, v) -> v.must()); | ||
78 | RelationView<TruthValue> friendMayView = new FilteredRelationView<TruthValue>(model, friend, (k, v) -> v.may()); | ||
79 | |||
80 | RelationalScope scope = new RelationalScope(Set.of(persionView, ageView, friendMustView, friendMayView)); | ||
81 | |||
82 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) | ||
83 | .addParameter("p",persionView) | ||
84 | .addConstraint(persionView, "p").build(); | ||
85 | |||
86 | ViatraQueryEngine engine = AdvancedViatraQueryEngine.on(scope); | ||
87 | GenericPatternMatcher personMatcher = engine.getMatcher(personQuery); | ||
88 | Collection<GenericPatternMatch> personMatches = personMatcher.getAllMatches(); | ||
89 | for(GenericPatternMatch personMatch : personMatches) { | ||
90 | System.out.println(personMatch); | ||
91 | } | ||
92 | } | ||
93 | } \ No newline at end of file | ||