diff options
author | Márton Golej <golejmarci@gmail.com> | 2021-10-17 19:59:09 +0200 |
---|---|---|
committer | Márton Golej <golejmarci@gmail.com> | 2021-10-17 20:10:26 +0200 |
commit | 63e0207f9b044f24a4a07c99bf731ee92e08cfab (patch) | |
tree | bc2ea36d2057d3d60346eb27a60e7d20030f439c /language-to-store | |
parent | build: fix artifact coordinates (diff) | |
download | refinery-63e0207f9b044f24a4a07c99bf731ee92e08cfab.tar.gz refinery-63e0207f9b044f24a4a07c99bf731ee92e08cfab.tar.zst refinery-63e0207f9b044f24a4a07c99bf731ee92e08cfab.zip |
Language to store implementation added
Diffstat (limited to 'language-to-store')
4 files changed, 688 insertions, 8 deletions
diff --git a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java index a2cab671..5dacc8cb 100644 --- a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java +++ b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java | |||
@@ -1,11 +1,231 @@ | |||
1 | package tools.refinery.language.mapping; | 1 | package tools.refinery.language.mapping; |
2 | 2 | ||
3 | import java.util.HashMap; | ||
4 | import java.util.HashSet; | ||
5 | import java.util.Map; | ||
6 | import java.util.Optional; | ||
7 | |||
8 | import org.eclipse.emf.common.util.EList; | ||
9 | |||
10 | import tools.refinery.language.ProblemUtil; | ||
11 | import tools.refinery.language.model.problem.Assertion; | ||
12 | import tools.refinery.language.model.problem.AssertionArgument; | ||
13 | import tools.refinery.language.model.problem.ClassDeclaration; | ||
14 | import tools.refinery.language.model.problem.EnumDeclaration; | ||
15 | import tools.refinery.language.model.problem.LogicValue; | ||
16 | import tools.refinery.language.model.problem.Node; | ||
17 | import tools.refinery.language.model.problem.NodeAssertionArgument; | ||
18 | import tools.refinery.language.model.problem.PredicateDefinition; | ||
3 | import tools.refinery.language.model.problem.Problem; | 19 | import tools.refinery.language.model.problem.Problem; |
20 | import tools.refinery.language.model.problem.ReferenceDeclaration; | ||
21 | import tools.refinery.language.model.problem.Statement; | ||
22 | import tools.refinery.language.model.problem.UniqueDeclaration; | ||
4 | import tools.refinery.store.model.Model; | 23 | import tools.refinery.store.model.Model; |
24 | import tools.refinery.store.model.ModelStore; | ||
25 | import tools.refinery.store.model.ModelStoreImpl; | ||
26 | import tools.refinery.store.model.Tuple; | ||
27 | import tools.refinery.store.model.representation.Relation; | ||
28 | import tools.refinery.store.model.representation.TruthValue; | ||
29 | |||
5 | 30 | ||
6 | public class PartialModelMapper { | 31 | public class PartialModelMapper { |
7 | public Model transformProblem(Problem problem) { | 32 | |
8 | // TODO @Marci Implement this | 33 | private int nodeIter; |
9 | throw new UnsupportedOperationException(); | 34 | public PartialModelMapper() { |
35 | this.nodeIter = 0; | ||
36 | } | ||
37 | |||
38 | public PartialModelMapperDTO transformProblem(Problem problem) throws Exception { | ||
39 | PartialModelMapperDTO pmmDTO = initTransform(problem); | ||
40 | |||
41 | Optional<Problem> builtinProblem = ProblemUtil.getBuiltInLibrary(problem); | ||
42 | if (builtinProblem.isEmpty()) throw new Exception("builtin.problem not found"); | ||
43 | PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); | ||
44 | pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); | ||
45 | pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); | ||
46 | pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); //így most valami nem stimmel | ||
47 | pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); | ||
48 | pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); | ||
49 | |||
50 | //Definition of store and model | ||
51 | ModelStore store = new ModelStoreImpl(new HashSet<>(pmmDTO.getRelationMap().values())); | ||
52 | Model model = store.createModel(); | ||
53 | pmmDTO.setModel(model); | ||
54 | |||
55 | Map<Node,Integer> allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), | ||
56 | pmmDTO.getUniqueNodeMap(), | ||
57 | pmmDTO.getNewNodeMap(), | ||
58 | pmmDTO.getNodeMap()); | ||
59 | |||
60 | //Filling up the relations with unknown truth values | ||
61 | for (tools.refinery.language.model.problem.Relation relation : pmmDTO.getRelationMap().keySet()) { | ||
62 | if(!(relation instanceof PredicateDefinition pd && pd.isError())) { | ||
63 | Relation<TruthValue> r = pmmDTO.getRelationMap().get(relation); | ||
64 | if(r.getArity() == 1) | ||
65 | for(Integer i : allNodesMap.values()) { | ||
66 | pmmDTO.getModel().put(r, Tuple.of(i), TruthValue.UNKNOWN); | ||
67 | } | ||
68 | else if(r.getArity() == 2) | ||
69 | for(Integer i : allNodesMap.values()) { | ||
70 | for (Integer j : allNodesMap.values()) { | ||
71 | pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); | ||
72 | } | ||
73 | } | ||
74 | else throw new Exception("Relation with arity above 2 is not supported"); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | //Filling up the exists | ||
79 | tools.refinery.language.model.problem.Relation existsRelation = null; | ||
80 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | ||
81 | if (r.getName().equals("exists")) existsRelation = r; | ||
82 | } | ||
83 | if(existsRelation.equals(null)) throw new Exception("exists not found"); | ||
84 | for (Node n : allNodesMap.keySet()) { | ||
85 | if(pmmDTO.getNewNodeMap().containsKey(n)) { | ||
86 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
87 | Tuple.of(allNodesMap.get(n)), | ||
88 | TruthValue.UNKNOWN); | ||
89 | } | ||
90 | else { | ||
91 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
92 | Tuple.of(allNodesMap.get(n)), | ||
93 | TruthValue.TRUE); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | //Filling up the equals | ||
98 | tools.refinery.language.model.problem.Relation equalsRelation = null; | ||
99 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | ||
100 | if (r.getName().equals("equals")) equalsRelation = r; | ||
101 | } | ||
102 | if(equalsRelation.equals(null)) throw new Exception("equals not found"); | ||
103 | for (Node n1 : allNodesMap.keySet()) { | ||
104 | for(Node n2 : allNodesMap.keySet()) { | ||
105 | if(n1.equals(n2)) { | ||
106 | if(pmmDTO.getNewNodeMap().containsKey(n1)) { | ||
107 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
108 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
109 | TruthValue.UNKNOWN); | ||
110 | } | ||
111 | else { | ||
112 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
113 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
114 | TruthValue.TRUE); | ||
115 | } | ||
116 | } | ||
117 | else { | ||
118 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
119 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
120 | TruthValue.FALSE); | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | //Transforming the assertions | ||
126 | processAssertions(problem, pmmDTO, allNodesMap); | ||
127 | processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); | ||
128 | |||
129 | //throw new UnsupportedOperationException(); | ||
130 | return pmmDTO; | ||
131 | } | ||
132 | |||
133 | private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map<Node, Integer> allNodesMap) { | ||
134 | for(Statement s : problem.getStatements()) { | ||
135 | if(s instanceof Assertion assertion) { | ||
136 | Relation<TruthValue> r1 = pmmDTO.getRelationMap().get(assertion.getRelation()); | ||
137 | int i = 0; | ||
138 | int[] integers = new int[assertion.getArguments().size()]; | ||
139 | for (AssertionArgument aa : assertion.getArguments()) { | ||
140 | if (aa instanceof NodeAssertionArgument nas) { | ||
141 | integers[i] = allNodesMap.get(nas.getNode()); | ||
142 | i++; | ||
143 | } | ||
144 | } | ||
145 | pmmDTO.getModel().put(r1, Tuple.of(integers), logicValueToTruthValue(assertion.getValue())); | ||
146 | } | ||
147 | else if (s instanceof ClassDeclaration cd) { | ||
148 | if(!cd.isAbstract()) | ||
149 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(cd), | ||
150 | Tuple.of(pmmDTO.getNewNodeMap().get(cd.getNewNode())), | ||
151 | TruthValue.TRUE); | ||
152 | } | ||
153 | else if (s instanceof EnumDeclaration ed) { | ||
154 | for (Node n : ed.getLiterals()) { | ||
155 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(ed), | ||
156 | Tuple.of(pmmDTO.getEnumNodeMap().get(n)), | ||
157 | TruthValue.TRUE); | ||
158 | } | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | public PartialModelMapperDTO initTransform(Problem problem) { | ||
164 | //Defining needed Maps | ||
165 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap = new HashMap<>(); | ||
166 | Map<Node, Integer> enumNodeMap = new HashMap<>(); | ||
167 | Map<Node, Integer> uniqueNodeMap = new HashMap<>(); | ||
168 | Map<Node, Integer> newNodeMap = new HashMap<>(); | ||
169 | |||
170 | //Definition of Relations, filling up the enumNodeMap, uniqueNodeMap, newNodeMap | ||
171 | EList<Statement> statements = problem.getStatements(); | ||
172 | for (Statement s : statements) { | ||
173 | if (s instanceof ClassDeclaration cd) { | ||
174 | Relation<TruthValue> r1 = new Relation<>(cd.getName(), 1, TruthValue.FALSE); | ||
175 | relationMap.put(cd, r1); | ||
176 | if(!cd.isAbstract()) newNodeMap.put(cd.getNewNode(), this.nodeIter++); | ||
177 | EList<ReferenceDeclaration> refDeclList = cd.getReferenceDeclarations(); | ||
178 | for (ReferenceDeclaration refDec : refDeclList) { | ||
179 | Relation<TruthValue> r2 = new Relation<>(refDec.getName(), 2, TruthValue.FALSE); | ||
180 | relationMap.put(refDec, r2); | ||
181 | } | ||
182 | } | ||
183 | else if (s instanceof EnumDeclaration ed) { | ||
184 | Relation<TruthValue> r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); | ||
185 | relationMap.put(ed, r); | ||
186 | EList<Node> nodeList = ed.getLiterals(); | ||
187 | for (Node n : ed.getLiterals()) { | ||
188 | enumNodeMap.put(n, nodeIter++); | ||
189 | } | ||
190 | } | ||
191 | else if (s instanceof UniqueDeclaration ud) { | ||
192 | for (Node n : ud.getNodes()) { | ||
193 | uniqueNodeMap.put(n, this.nodeIter++); | ||
194 | } | ||
195 | } | ||
196 | else if (s instanceof PredicateDefinition pd) { | ||
197 | Relation<TruthValue> r = new Relation<>(pd.getName(), 1, TruthValue.FALSE); | ||
198 | relationMap.put(pd, r); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | |||
203 | |||
204 | |||
205 | //Filling the nodeMap up | ||
206 | Map<Node, Integer> nodeMap = new HashMap<>(); | ||
207 | for(Node n : problem.getNodes()) { | ||
208 | nodeMap.put(n, this.nodeIter++); | ||
209 | } | ||
210 | |||
211 | return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); | ||
212 | } | ||
213 | |||
214 | private Map<Node, Integer> mergeNodeMaps(Map<Node, Integer> enumNodeMap, | ||
215 | Map<Node, Integer> uniqueNodeMap, | ||
216 | Map<Node, Integer> newNodeMap, | ||
217 | Map<Node, Integer> nodeMap) { | ||
218 | Map<Node, Integer> out = new HashMap<>(enumNodeMap); | ||
219 | for (Node n : uniqueNodeMap.keySet()) out.put(n, uniqueNodeMap.get(n)); | ||
220 | for (Node n : newNodeMap.keySet()) out.put(n, newNodeMap.get(n)); | ||
221 | for (Node n : nodeMap.keySet()) out.put(n, nodeMap.get(n)); | ||
222 | return out; | ||
223 | } | ||
224 | |||
225 | private TruthValue logicValueToTruthValue(LogicValue value) { | ||
226 | if(value.equals(LogicValue.TRUE)) return TruthValue.TRUE; | ||
227 | else if(value.equals(LogicValue.FALSE)) return TruthValue.FALSE; | ||
228 | else if(value.equals(LogicValue.UNKNOWN)) return TruthValue.UNKNOWN; | ||
229 | else return TruthValue.ERROR; | ||
10 | } | 230 | } |
11 | } | 231 | } |
diff --git a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java new file mode 100644 index 00000000..3397b4bd --- /dev/null +++ b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java | |||
@@ -0,0 +1,54 @@ | |||
1 | package tools.refinery.language.mapping; | ||
2 | |||
3 | import java.util.Map; | ||
4 | |||
5 | import tools.refinery.language.model.problem.Node; | ||
6 | import tools.refinery.store.model.Model; | ||
7 | import tools.refinery.store.model.representation.Relation; | ||
8 | import tools.refinery.store.model.representation.TruthValue; | ||
9 | |||
10 | public class PartialModelMapperDTO { | ||
11 | private Model model; | ||
12 | private Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap; | ||
13 | private Map<Node, Integer> nodeMap; | ||
14 | private Map<Node, Integer> enumNodeMap; | ||
15 | private Map<Node, Integer> uniqueNodeMap; | ||
16 | private Map<Node, Integer> newNodeMap; | ||
17 | |||
18 | public PartialModelMapperDTO(Model model, | ||
19 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap, | ||
20 | Map<Node, Integer> nodeMap, | ||
21 | Map<Node, Integer> enumNodeMap, | ||
22 | Map<Node, Integer> uniqueNodeMap, | ||
23 | Map<Node, Integer> newNodeMap) { | ||
24 | this.model = model; | ||
25 | this.relationMap = relationMap; | ||
26 | this.nodeMap = nodeMap; | ||
27 | this.enumNodeMap = enumNodeMap; | ||
28 | this.uniqueNodeMap = uniqueNodeMap; | ||
29 | this.newNodeMap = newNodeMap; | ||
30 | } | ||
31 | |||
32 | public Model getModel() { | ||
33 | return this.model; | ||
34 | } | ||
35 | public Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> getRelationMap(){ | ||
36 | return this.relationMap; | ||
37 | } | ||
38 | public Map<Node, Integer> getNodeMap() { | ||
39 | return this.nodeMap; | ||
40 | } | ||
41 | public Map<Node, Integer> getEnumNodeMap() { | ||
42 | return this.enumNodeMap; | ||
43 | } | ||
44 | public Map<Node, Integer> getUniqueNodeMap() { | ||
45 | return this.uniqueNodeMap; | ||
46 | } | ||
47 | public Map<Node, Integer> getNewNodeMap() { | ||
48 | return this.newNodeMap; | ||
49 | } | ||
50 | |||
51 | public void setModel(Model model) { | ||
52 | this.model = model; | ||
53 | } | ||
54 | } | ||
diff --git a/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend b/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend index 41a7b7fd..a9c88223 100644 --- a/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend +++ b/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend | |||
@@ -1,25 +1,31 @@ | |||
1 | package tools.refinery.language.mapping.tests | 1 | package tools.refinery.language.mapping.tests |
2 | 2 | ||
3 | import com.google.inject.Inject | 3 | import com.google.inject.Inject |
4 | import org.eclipse.emf.ecore.util.EcoreUtil | ||
4 | import org.eclipse.xtext.testing.InjectWith | 5 | import org.eclipse.xtext.testing.InjectWith |
5 | import org.eclipse.xtext.testing.extensions.InjectionExtension | 6 | import org.eclipse.xtext.testing.extensions.InjectionExtension |
6 | import org.eclipse.xtext.testing.util.ParseHelper | 7 | import org.eclipse.xtext.testing.util.ParseHelper |
7 | import org.junit.jupiter.api.BeforeEach | 8 | import org.junit.jupiter.api.BeforeEach |
8 | import org.junit.jupiter.api.Disabled | ||
9 | import org.junit.jupiter.api.Test | 9 | import org.junit.jupiter.api.Test |
10 | import org.junit.jupiter.api.^extension.ExtendWith | 10 | import org.junit.jupiter.api.^extension.ExtendWith |
11 | import tools.refinery.language.mapping.PartialModelMapper | 11 | import tools.refinery.language.mapping.PartialModelMapper |
12 | import tools.refinery.language.model.problem.Problem | 12 | import tools.refinery.language.model.problem.Problem |
13 | import tools.refinery.language.tests.ProblemInjectorProvider | 13 | import tools.refinery.language.tests.ProblemInjectorProvider |
14 | import tools.refinery.store.model.Tuple | ||
15 | import tools.refinery.store.model.representation.TruthValue | ||
14 | 16 | ||
15 | import static org.hamcrest.MatcherAssert.assertThat | 17 | import static org.hamcrest.MatcherAssert.assertThat |
16 | import static org.hamcrest.Matchers.* | 18 | import static org.hamcrest.Matchers.* |
19 | import static org.junit.jupiter.api.Assertions.assertTrue | ||
17 | 20 | ||
18 | @ExtendWith(InjectionExtension) | 21 | @ExtendWith(InjectionExtension) |
19 | @InjectWith(ProblemInjectorProvider) | 22 | @InjectWith(ProblemInjectorProvider) |
20 | class PartialModelMapperTest { | 23 | class PartialModelMapperTest { |
21 | @Inject | 24 | @Inject |
22 | ParseHelper<Problem> parseHelper | 25 | ParseHelper<Problem> parseHelper |
26 | |||
27 | @Inject | ||
28 | extension ProblemTestUtil | ||
23 | 29 | ||
24 | PartialModelMapper mapper | 30 | PartialModelMapper mapper |
25 | 31 | ||
@@ -29,8 +35,7 @@ class PartialModelMapperTest { | |||
29 | } | 35 | } |
30 | 36 | ||
31 | @Test | 37 | @Test |
32 | @Disabled("Method not yet implemented") | 38 | def void relationTest() { |
33 | def void exampleTest() { | ||
34 | val problem = parseHelper.parse(''' | 39 | val problem = parseHelper.parse(''' |
35 | class Person { | 40 | class Person { |
36 | Person[0..*] friend | 41 | Person[0..*] friend |
@@ -38,7 +43,294 @@ class PartialModelMapperTest { | |||
38 | 43 | ||
39 | friend(a, b). | 44 | friend(a, b). |
40 | ''') | 45 | ''') |
41 | val model = mapper.transformProblem(problem) | 46 | EcoreUtil.resolveAll(problem) |
42 | assertThat(model, notNullValue()) | 47 | |
48 | val modelAndMaps = mapper.transformProblem(problem) | ||
49 | assertThat(modelAndMaps, notNullValue()) | ||
50 | |||
51 | val model = modelAndMaps.model | ||
52 | val relationMap = modelAndMaps.relationMap | ||
53 | val nodeMap = modelAndMaps.nodeMap | ||
54 | |||
55 | val person = problem.findClass("Person") | ||
56 | val friend = problem.findClass("Person").reference("friend") | ||
57 | val a = problem.node("a") | ||
58 | val b = problem.node("b") | ||
59 | |||
60 | assertTrue(model.getDataRepresentations().contains(relationMap.get(person))); | ||
61 | assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))); | ||
62 | assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)); | ||
63 | } | ||
64 | |||
65 | @Test | ||
66 | def void classTest() { | ||
67 | val problem = parseHelper.parse(''' | ||
68 | class Person { | ||
69 | Person[0..*] friend | ||
70 | } | ||
71 | |||
72 | Person(a). | ||
73 | ''') | ||
74 | EcoreUtil.resolveAll(problem) | ||
75 | |||
76 | val modelAndMaps = mapper.transformProblem(problem) | ||
77 | assertThat(modelAndMaps, notNullValue()) | ||
78 | |||
79 | val model = modelAndMaps.model | ||
80 | val relationMap = modelAndMaps.relationMap | ||
81 | val nodeMap = modelAndMaps.nodeMap | ||
82 | |||
83 | val person = problem.findClass("Person") | ||
84 | val friend = problem.findClass("Person").reference("friend") | ||
85 | val a = problem.node("a") | ||
86 | |||
87 | assertTrue(model.getDataRepresentations().contains(relationMap.get(person))); | ||
88 | assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))); | ||
89 | |||
90 | assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); | ||
91 | } | ||
92 | |||
93 | @Test | ||
94 | def void equalsAndExistTest() { | ||
95 | val problem = parseHelper.parse(''' | ||
96 | node(a). | ||
97 | node(b). | ||
98 | |||
99 | class Person. | ||
100 | ''') | ||
101 | EcoreUtil.resolveAll(problem) | ||
102 | val builtin = problem.builtin; | ||
103 | |||
104 | val modelAndMaps = mapper.transformProblem(problem) | ||
105 | assertThat(modelAndMaps, notNullValue()) | ||
106 | |||
107 | val model = modelAndMaps.model | ||
108 | val relationMap = modelAndMaps.relationMap | ||
109 | val nodeMap = modelAndMaps.nodeMap | ||
110 | val newNodeMap = modelAndMaps.newNodeMap | ||
111 | |||
112 | val a = problem.node("a") | ||
113 | val b = problem.node("b") | ||
114 | val Person = problem.findClass("Person") | ||
115 | val PersonNew = problem.findClass("Person").newNode | ||
116 | val exists = builtin.pred("exists") | ||
117 | val equals = builtin.findClass("node").reference("equals") | ||
118 | |||
119 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) | ||
120 | assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))) | ||
121 | assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))) | ||
122 | |||
123 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
124 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
125 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
126 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
127 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
128 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
129 | |||
130 | assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
131 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
132 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
133 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
134 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
135 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
136 | } | ||
137 | |||
138 | @Test | ||
139 | def void equalsAndExistTest2() { | ||
140 | val problem = parseHelper.parse(''' | ||
141 | class Person. | ||
142 | |||
143 | Person(a). | ||
144 | Person(b). | ||
145 | ''') | ||
146 | val builtin = problem.builtin; | ||
147 | EcoreUtil.resolveAll(problem) | ||
148 | |||
149 | val modelAndMaps = mapper.transformProblem(problem) | ||
150 | assertThat(modelAndMaps, notNullValue()) | ||
151 | |||
152 | val model = modelAndMaps.model | ||
153 | val relationMap = modelAndMaps.relationMap | ||
154 | val nodeMap = modelAndMaps.nodeMap | ||
155 | val newNodeMap = modelAndMaps.newNodeMap | ||
156 | |||
157 | val a = problem.node("a") | ||
158 | val b = problem.node("b") | ||
159 | val Person = problem.findClass("Person") | ||
160 | val PersonNew = problem.findClass("Person").newNode | ||
161 | val exists = builtin.pred("exists") | ||
162 | val equals = builtin.findClass("node").reference("equals") | ||
163 | |||
164 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); | ||
165 | assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))); | ||
166 | assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))); | ||
167 | |||
168 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); | ||
169 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)); | ||
170 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)); | ||
171 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)); | ||
172 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)); | ||
173 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)); | ||
174 | |||
175 | assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)); | ||
176 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)); | ||
177 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)); | ||
178 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)); | ||
179 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); | ||
180 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); | ||
181 | } | ||
182 | |||
183 | @Test | ||
184 | def void newNodeTest(){ | ||
185 | val problem = parseHelper.parse(''' | ||
186 | class Person. | ||
187 | abstract class Family. | ||
188 | ''') | ||
189 | EcoreUtil.resolveAll(problem) | ||
190 | |||
191 | val modelAndMaps = mapper.transformProblem(problem); | ||
192 | assertThat(modelAndMaps, notNullValue()) | ||
193 | |||
194 | val model = modelAndMaps.model | ||
195 | val relationMap = modelAndMaps.relationMap | ||
196 | val newNodeMap = modelAndMaps.newNodeMap | ||
197 | |||
198 | val Person = problem.findClass("Person") | ||
199 | val Family = problem.findClass("Family") | ||
200 | val PersonNew = problem.findClass("Person").newNode | ||
201 | |||
202 | |||
203 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); | ||
204 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))); | ||
205 | |||
206 | assertTrue(newNodeMap.size.equals(4)) //3 from builtin.problem, 1 from Person | ||
207 | assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)); | ||
208 | } | ||
209 | |||
210 | @Test | ||
211 | def void enumTest(){ | ||
212 | val problem = parseHelper.parse(''' | ||
213 | enum TaxStatus { | ||
214 | child, student, adult, retired | ||
215 | } | ||
216 | ''') | ||
217 | EcoreUtil.resolveAll(problem) | ||
218 | |||
219 | val modelAndMaps = mapper.transformProblem(problem) | ||
220 | assertThat(modelAndMaps, notNullValue()) | ||
221 | |||
222 | val model = modelAndMaps.model | ||
223 | val relationMap = modelAndMaps.relationMap | ||
224 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
225 | |||
226 | val TaxStatus = problem.findEnum("TaxStatus") | ||
227 | val child = problem.findEnum("TaxStatus").literal("child") | ||
228 | val student = problem.findEnum("TaxStatus").literal("student") | ||
229 | val adult = problem.findEnum("TaxStatus").literal("adult") | ||
230 | val retired = problem.findEnum("TaxStatus").literal("retired") | ||
231 | |||
232 | assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))); | ||
233 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(child))).equals(TruthValue.TRUE)); | ||
234 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(student))).equals(TruthValue.TRUE)); | ||
235 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(adult))).equals(TruthValue.TRUE)); | ||
236 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)); | ||
237 | } | ||
238 | |||
239 | @Test | ||
240 | def void builtinBoolTest(){ | ||
241 | val problem = parseHelper.parse(''' | ||
242 | class Person. | ||
243 | ''') | ||
244 | EcoreUtil.resolveAll(problem) | ||
245 | val builtin = problem.builtin; | ||
246 | |||
247 | val modelAndMaps = mapper.transformProblem(problem) | ||
248 | assertThat(modelAndMaps, notNullValue()) | ||
249 | |||
250 | val model = modelAndMaps.model | ||
251 | val relationMap = modelAndMaps.relationMap | ||
252 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
253 | |||
254 | val bool = builtin.findEnum("bool") | ||
255 | val trueEnum = builtin.findEnum("bool").literal("true") //Emiatt nem sikerül a teszt | ||
256 | val falseEnum = builtin.findEnum("bool").literal("false") | ||
257 | |||
258 | assertTrue(model.getDataRepresentations().contains(relationMap.get(bool))); | ||
259 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(trueEnum))).equals(TruthValue.TRUE)); | ||
260 | //TODO maradék assert | ||
261 | } | ||
262 | |||
263 | @Test | ||
264 | def void compositeTest() { | ||
265 | val problem = parseHelper.parse(''' | ||
266 | class Family { | ||
267 | contains Person[] members | ||
268 | } | ||
269 | |||
270 | class Person { | ||
271 | Person[0..*] children | ||
272 | Person[0..1] parent | ||
273 | TaxStatus taxStatus | ||
274 | } | ||
275 | |||
276 | enum TaxStatus { | ||
277 | child, student, adult, retired | ||
278 | } | ||
279 | |||
280 | % A child cannot have any dependents. | ||
281 | error invalidTaxStatus(Person p) <-> | ||
282 | taxStatus(p, child), children(p, _q). | ||
283 | |||
284 | unique family. | ||
285 | Family(family). | ||
286 | members(family, anne): true. | ||
287 | members(family, bob). | ||
288 | members(family, ciri). | ||
289 | children(anne, ciri). | ||
290 | ?children(bob, ciri). | ||
291 | taxStatus(anne, adult). | ||
292 | ''') | ||
293 | EcoreUtil.resolveAll(problem) | ||
294 | |||
295 | val modelAndMaps = mapper.transformProblem(problem) | ||
296 | assertThat(modelAndMaps, notNullValue()) | ||
297 | |||
298 | val model = modelAndMaps.model | ||
299 | val relationMap = modelAndMaps.relationMap | ||
300 | val nodeMap = modelAndMaps.nodeMap | ||
301 | val uniqueNodeMap = modelAndMaps.uniqueNodeMap | ||
302 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
303 | |||
304 | val Family = problem.findClass("Family") | ||
305 | val members = problem.findClass("Family").reference("members") | ||
306 | val Person = problem.findClass("Person") | ||
307 | val children = problem.findClass("Person").reference("children") | ||
308 | val parent = problem.findClass("Person").reference("parent") | ||
309 | val taxStatus = problem.findClass("Person").reference("taxStatus") | ||
310 | val TaxStatus = problem.findEnum("TaxStatus") | ||
311 | val invalidTaxStatus = problem.pred("invalidTaxStatus") | ||
312 | |||
313 | val anne = problem.node("anne") | ||
314 | val bob = problem.node("bob") | ||
315 | val ciri = problem.node("ciri") | ||
316 | val family = problem.uniqueNode("family") | ||
317 | val adult = problem.findEnum("TaxStatus").literal("adult") | ||
318 | |||
319 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))); | ||
320 | assertTrue(model.getDataRepresentations().contains(relationMap.get(members))); | ||
321 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); | ||
322 | assertTrue(model.getDataRepresentations().contains(relationMap.get(children))); | ||
323 | assertTrue(model.getDataRepresentations().contains(relationMap.get(parent))); | ||
324 | assertTrue(model.getDataRepresentations().contains(relationMap.get(taxStatus))); | ||
325 | assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))); | ||
326 | assertTrue(model.getDataRepresentations().contains(relationMap.get(invalidTaxStatus))); | ||
327 | |||
328 | assertTrue(model.get(relationMap.get(Family), Tuple.of(uniqueNodeMap.get(family))).equals(TruthValue.TRUE)); | ||
329 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(anne))).equals(TruthValue.TRUE)); | ||
330 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(bob))).equals(TruthValue.TRUE)); | ||
331 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(ciri))).equals(TruthValue.TRUE)); | ||
332 | assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(anne),nodeMap.get(ciri))).equals(TruthValue.TRUE)); | ||
333 | assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(bob),nodeMap.get(ciri))).equals(TruthValue.UNKNOWN)); | ||
334 | assertTrue(model.get(relationMap.get(taxStatus), Tuple.of(nodeMap.get(anne),enumNodeMap.get(adult))).equals(TruthValue.TRUE)); | ||
43 | } | 335 | } |
44 | } | 336 | } |
diff --git a/language-to-store/src/test/java/tools/refinery/language/mapping/tests/ProblemTestUtil.xtend b/language-to-store/src/test/java/tools/refinery/language/mapping/tests/ProblemTestUtil.xtend new file mode 100644 index 00000000..b3b8145e --- /dev/null +++ b/language-to-store/src/test/java/tools/refinery/language/mapping/tests/ProblemTestUtil.xtend | |||
@@ -0,0 +1,114 @@ | |||
1 | package tools.refinery.language.mapping.tests | ||
2 | |||
3 | import tools.refinery.language.ProblemUtil | ||
4 | import tools.refinery.language.model.problem.Argument | ||
5 | import tools.refinery.language.model.problem.Assertion | ||
6 | import tools.refinery.language.model.problem.AssertionArgument | ||
7 | import tools.refinery.language.model.problem.Atom | ||
8 | import tools.refinery.language.model.problem.ClassDeclaration | ||
9 | import tools.refinery.language.model.problem.Conjunction | ||
10 | import tools.refinery.language.model.problem.EnumDeclaration | ||
11 | import tools.refinery.language.model.problem.Literal | ||
12 | import tools.refinery.language.model.problem.NegativeLiteral | ||
13 | import tools.refinery.language.model.problem.Node | ||
14 | import tools.refinery.language.model.problem.NodeAssertionArgument | ||
15 | import tools.refinery.language.model.problem.NodeValueAssertion | ||
16 | import tools.refinery.language.model.problem.PredicateDefinition | ||
17 | import tools.refinery.language.model.problem.Problem | ||
18 | import tools.refinery.language.model.problem.UniqueDeclaration | ||
19 | import tools.refinery.language.model.problem.Variable | ||
20 | import tools.refinery.language.model.problem.VariableOrNodeArgument | ||
21 | |||
22 | class ProblemTestUtil { | ||
23 | def builtin(Problem it) { | ||
24 | ProblemUtil.getBuiltInLibrary(it).get | ||
25 | } | ||
26 | |||
27 | def errors(Problem it) { | ||
28 | eResource.errors | ||
29 | } | ||
30 | |||
31 | def nodeNames(Problem it) { | ||
32 | nodes.map[name] | ||
33 | } | ||
34 | |||
35 | def pred(Problem it, String name) { | ||
36 | statements.filter(PredicateDefinition).findFirst[it.name == name] | ||
37 | } | ||
38 | |||
39 | def param(PredicateDefinition it, int i) { | ||
40 | parameters.get(i) | ||
41 | } | ||
42 | |||
43 | def conj(PredicateDefinition it, int i) { | ||
44 | bodies.get(i) | ||
45 | } | ||
46 | |||
47 | def lit(Conjunction it, int i) { | ||
48 | literals.get(i) | ||
49 | } | ||
50 | |||
51 | def negated(Literal it) { | ||
52 | (it as NegativeLiteral).atom | ||
53 | } | ||
54 | |||
55 | def relation(Literal it) { | ||
56 | (it as Atom).relation | ||
57 | } | ||
58 | |||
59 | def arg(Atom it, int i) { | ||
60 | it.arguments.get(i) | ||
61 | } | ||
62 | |||
63 | def arg(Literal it, int i) { | ||
64 | (it as Atom).arg(i) | ||
65 | } | ||
66 | |||
67 | def variable(Argument it) { | ||
68 | (it as VariableOrNodeArgument).variableOrNode as Variable | ||
69 | } | ||
70 | |||
71 | def node(Argument it) { | ||
72 | (it as VariableOrNodeArgument).variableOrNode as Node | ||
73 | } | ||
74 | |||
75 | def assertion(Problem it, int i) { | ||
76 | statements.filter(Assertion).get(i) | ||
77 | } | ||
78 | |||
79 | def nodeValueAssertion(Problem it, int i) { | ||
80 | statements.filter(NodeValueAssertion).get(i) | ||
81 | } | ||
82 | |||
83 | def arg(Assertion it, int i) { | ||
84 | arguments.get(i) | ||
85 | } | ||
86 | |||
87 | def node(AssertionArgument it) { | ||
88 | (it as NodeAssertionArgument).node | ||
89 | } | ||
90 | |||
91 | def node(Problem it, String name) { | ||
92 | nodes.findFirst[it.name == name] | ||
93 | } | ||
94 | |||
95 | def uniqueNode(Problem it, String name) { | ||
96 | statements.filter(UniqueDeclaration).flatMap[nodes].findFirst[it.name == name] | ||
97 | } | ||
98 | |||
99 | def findClass(Problem it, String name) { | ||
100 | statements.filter(ClassDeclaration).findFirst[it.name == name] | ||
101 | } | ||
102 | |||
103 | def reference(ClassDeclaration it, String name) { | ||
104 | it.referenceDeclarations.findFirst[it.name == name] | ||
105 | } | ||
106 | |||
107 | def findEnum(Problem it, String name) { | ||
108 | statements.filter(EnumDeclaration).findFirst[it.name == name] | ||
109 | } | ||
110 | |||
111 | def literal(EnumDeclaration it, String name) { | ||
112 | literals.findFirst[it.name == name] | ||
113 | } | ||
114 | } | ||