diff options
author | 2021-10-18 16:12:10 +0200 | |
---|---|---|
committer | 2021-10-18 16:12:10 +0200 | |
commit | f566affc42c3207eebbbce46726df6e582035763 (patch) | |
tree | da11f218d4030b579874c0283cec31c7961821a3 /language-to-store/src | |
parent | Language to store implementation added (diff) | |
download | refinery-f566affc42c3207eebbbce46726df6e582035763.tar.gz refinery-f566affc42c3207eebbbce46726df6e582035763.tar.zst refinery-f566affc42c3207eebbbce46726df6e582035763.zip |
Commments added, code cleanup:
Diffstat (limited to 'language-to-store/src')
2 files changed, 53 insertions, 25 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 5dacc8cb..53fa78e3 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 | |||
@@ -35,15 +35,19 @@ public class PartialModelMapper { | |||
35 | this.nodeIter = 0; | 35 | this.nodeIter = 0; |
36 | } | 36 | } |
37 | 37 | ||
38 | public PartialModelMapperDTO transformProblem(Problem problem) throws Exception { | 38 | public PartialModelMapperDTO transformProblem(Problem problem) throws PartialModelMapperException { |
39 | //Getting the relations and the nodes from the given problem | ||
39 | PartialModelMapperDTO pmmDTO = initTransform(problem); | 40 | PartialModelMapperDTO pmmDTO = initTransform(problem); |
40 | 41 | ||
42 | //Getting the relations and the nodes from the built in problem | ||
41 | Optional<Problem> builtinProblem = ProblemUtil.getBuiltInLibrary(problem); | 43 | Optional<Problem> builtinProblem = ProblemUtil.getBuiltInLibrary(problem); |
42 | if (builtinProblem.isEmpty()) throw new Exception("builtin.problem not found"); | 44 | if (builtinProblem.isEmpty()) throw new PartialModelMapperException("builtin.problem not found"); |
43 | PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); | 45 | PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); |
46 | |||
47 | //Merging the relation and the nodes from the given problem and from the built in problem | ||
44 | pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); | 48 | pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); |
45 | pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); | 49 | pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); |
46 | pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); //így most valami nem stimmel | 50 | pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); |
47 | pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); | 51 | pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); |
48 | pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); | 52 | pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); |
49 | 53 | ||
@@ -52,6 +56,7 @@ public class PartialModelMapper { | |||
52 | Model model = store.createModel(); | 56 | Model model = store.createModel(); |
53 | pmmDTO.setModel(model); | 57 | pmmDTO.setModel(model); |
54 | 58 | ||
59 | //Collecting all the nodes in one map | ||
55 | Map<Node,Integer> allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), | 60 | Map<Node,Integer> allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), |
56 | pmmDTO.getUniqueNodeMap(), | 61 | pmmDTO.getUniqueNodeMap(), |
57 | pmmDTO.getNewNodeMap(), | 62 | pmmDTO.getNewNodeMap(), |
@@ -71,16 +76,13 @@ public class PartialModelMapper { | |||
71 | pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); | 76 | pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); |
72 | } | 77 | } |
73 | } | 78 | } |
74 | else throw new Exception("Relation with arity above 2 is not supported"); | 79 | else throw new PartialModelMapperException("Relation with arity above 2 is not supported"); |
75 | } | 80 | } |
76 | } | 81 | } |
77 | 82 | ||
78 | //Filling up the exists | 83 | //Filling up the exists |
79 | tools.refinery.language.model.problem.Relation existsRelation = null; | 84 | tools.refinery.language.model.problem.Relation existsRelation = findingRelationInDTO(builtinProblemDTO, "exists", |
80 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | 85 | "The exists not found in built in problem"); |
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()) { | 86 | for (Node n : allNodesMap.keySet()) { |
85 | if(pmmDTO.getNewNodeMap().containsKey(n)) { | 87 | if(pmmDTO.getNewNodeMap().containsKey(n)) { |
86 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | 88 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), |
@@ -95,11 +97,8 @@ public class PartialModelMapper { | |||
95 | } | 97 | } |
96 | 98 | ||
97 | //Filling up the equals | 99 | //Filling up the equals |
98 | tools.refinery.language.model.problem.Relation equalsRelation = null; | 100 | tools.refinery.language.model.problem.Relation equalsRelation = findingRelationInDTO(builtinProblemDTO, "equals", |
99 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | 101 | "The equals not found in built in problem"); |
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()) { | 102 | for (Node n1 : allNodesMap.keySet()) { |
104 | for(Node n2 : allNodesMap.keySet()) { | 103 | for(Node n2 : allNodesMap.keySet()) { |
105 | if(n1.equals(n2)) { | 104 | if(n1.equals(n2)) { |
@@ -126,10 +125,21 @@ public class PartialModelMapper { | |||
126 | processAssertions(problem, pmmDTO, allNodesMap); | 125 | processAssertions(problem, pmmDTO, allNodesMap); |
127 | processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); | 126 | processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); |
128 | 127 | ||
129 | //throw new UnsupportedOperationException(); | ||
130 | return pmmDTO; | 128 | return pmmDTO; |
131 | } | 129 | } |
132 | 130 | ||
131 | //Searches for and gives back a relation in a PartialModelMapperDTO | ||
132 | private tools.refinery.language.model.problem.Relation findingRelationInDTO(PartialModelMapperDTO partialModelMapperDTO, String searchedRelation, String errorText) | ||
133 | throws PartialModelMapperException { | ||
134 | tools.refinery.language.model.problem.Relation relation = null; | ||
135 | for (tools.refinery.language.model.problem.Relation r : partialModelMapperDTO.getRelationMap().keySet()) { | ||
136 | if (r.getName().equals(searchedRelation)) relation = r; | ||
137 | } | ||
138 | if(relation.equals(null)) throw new PartialModelMapperException(errorText); | ||
139 | return relation; | ||
140 | } | ||
141 | |||
142 | //Processing assertions and placing them in the model | ||
133 | private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map<Node, Integer> allNodesMap) { | 143 | private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map<Node, Integer> allNodesMap) { |
134 | for(Statement s : problem.getStatements()) { | 144 | for(Statement s : problem.getStatements()) { |
135 | if(s instanceof Assertion assertion) { | 145 | if(s instanceof Assertion assertion) { |
@@ -160,6 +170,7 @@ public class PartialModelMapper { | |||
160 | } | 170 | } |
161 | } | 171 | } |
162 | 172 | ||
173 | //Getting the relations and nodes from the problem | ||
163 | public PartialModelMapperDTO initTransform(Problem problem) { | 174 | public PartialModelMapperDTO initTransform(Problem problem) { |
164 | //Defining needed Maps | 175 | //Defining needed Maps |
165 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap = new HashMap<>(); | 176 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap = new HashMap<>(); |
@@ -183,7 +194,6 @@ public class PartialModelMapper { | |||
183 | else if (s instanceof EnumDeclaration ed) { | 194 | else if (s instanceof EnumDeclaration ed) { |
184 | Relation<TruthValue> r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); | 195 | Relation<TruthValue> r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); |
185 | relationMap.put(ed, r); | 196 | relationMap.put(ed, r); |
186 | EList<Node> nodeList = ed.getLiterals(); | ||
187 | for (Node n : ed.getLiterals()) { | 197 | for (Node n : ed.getLiterals()) { |
188 | enumNodeMap.put(n, nodeIter++); | 198 | enumNodeMap.put(n, nodeIter++); |
189 | } | 199 | } |
@@ -199,9 +209,6 @@ public class PartialModelMapper { | |||
199 | } | 209 | } |
200 | } | 210 | } |
201 | 211 | ||
202 | |||
203 | |||
204 | |||
205 | //Filling the nodeMap up | 212 | //Filling the nodeMap up |
206 | Map<Node, Integer> nodeMap = new HashMap<>(); | 213 | Map<Node, Integer> nodeMap = new HashMap<>(); |
207 | for(Node n : problem.getNodes()) { | 214 | for(Node n : problem.getNodes()) { |
@@ -210,22 +217,35 @@ public class PartialModelMapper { | |||
210 | 217 | ||
211 | return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); | 218 | return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); |
212 | } | 219 | } |
213 | 220 | ||
221 | //Merging the maps of nodes into one map | ||
214 | private Map<Node, Integer> mergeNodeMaps(Map<Node, Integer> enumNodeMap, | 222 | private Map<Node, Integer> mergeNodeMaps(Map<Node, Integer> enumNodeMap, |
215 | Map<Node, Integer> uniqueNodeMap, | 223 | Map<Node, Integer> uniqueNodeMap, |
216 | Map<Node, Integer> newNodeMap, | 224 | Map<Node, Integer> newNodeMap, |
217 | Map<Node, Integer> nodeMap) { | 225 | Map<Node, Integer> nodeMap) { |
218 | Map<Node, Integer> out = new HashMap<>(enumNodeMap); | 226 | Map<Node, Integer> out = new HashMap<>(); |
219 | for (Node n : uniqueNodeMap.keySet()) out.put(n, uniqueNodeMap.get(n)); | 227 | out.putAll(enumNodeMap); |
220 | for (Node n : newNodeMap.keySet()) out.put(n, newNodeMap.get(n)); | 228 | out.putAll(uniqueNodeMap); |
221 | for (Node n : nodeMap.keySet()) out.put(n, nodeMap.get(n)); | 229 | out.putAll(newNodeMap); |
230 | out.putAll(nodeMap); | ||
222 | return out; | 231 | return out; |
223 | } | 232 | } |
224 | 233 | ||
234 | //Exchange method from LogicValue to TruthValue | ||
225 | private TruthValue logicValueToTruthValue(LogicValue value) { | 235 | private TruthValue logicValueToTruthValue(LogicValue value) { |
226 | if(value.equals(LogicValue.TRUE)) return TruthValue.TRUE; | 236 | if(value.equals(LogicValue.TRUE)) return TruthValue.TRUE; |
227 | else if(value.equals(LogicValue.FALSE)) return TruthValue.FALSE; | 237 | else if(value.equals(LogicValue.FALSE)) return TruthValue.FALSE; |
228 | else if(value.equals(LogicValue.UNKNOWN)) return TruthValue.UNKNOWN; | 238 | else if(value.equals(LogicValue.UNKNOWN)) return TruthValue.UNKNOWN; |
229 | else return TruthValue.ERROR; | 239 | else return TruthValue.ERROR; |
230 | } | 240 | } |
241 | |||
242 | public class PartialModelMapperException extends Exception{ | ||
243 | private static final long serialVersionUID = 1L; | ||
244 | public PartialModelMapperException(String errorText) { | ||
245 | super(errorText); | ||
246 | } | ||
247 | public PartialModelMapperException() { | ||
248 | super(); | ||
249 | } | ||
250 | } | ||
231 | } | 251 | } |
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 a9c88223..86dd47ae 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 | |||
@@ -34,6 +34,7 @@ class PartialModelMapperTest { | |||
34 | mapper = new PartialModelMapper | 34 | mapper = new PartialModelMapper |
35 | } | 35 | } |
36 | 36 | ||
37 | //Testing the relation | ||
37 | @Test | 38 | @Test |
38 | def void relationTest() { | 39 | def void relationTest() { |
39 | val problem = parseHelper.parse(''' | 40 | val problem = parseHelper.parse(''' |
@@ -62,6 +63,7 @@ class PartialModelMapperTest { | |||
62 | assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)); | 63 | assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)); |
63 | } | 64 | } |
64 | 65 | ||
66 | //Testing the class | ||
65 | @Test | 67 | @Test |
66 | def void classTest() { | 68 | def void classTest() { |
67 | val problem = parseHelper.parse(''' | 69 | val problem = parseHelper.parse(''' |
@@ -90,6 +92,7 @@ class PartialModelMapperTest { | |||
90 | assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); | 92 | assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); |
91 | } | 93 | } |
92 | 94 | ||
95 | //Testing the equals and exists from the built in problem | ||
93 | @Test | 96 | @Test |
94 | def void equalsAndExistTest() { | 97 | def void equalsAndExistTest() { |
95 | val problem = parseHelper.parse(''' | 98 | val problem = parseHelper.parse(''' |
@@ -135,6 +138,7 @@ class PartialModelMapperTest { | |||
135 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | 138 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) |
136 | } | 139 | } |
137 | 140 | ||
141 | //Testing the equals and exists from the built in problem with a different example | ||
138 | @Test | 142 | @Test |
139 | def void equalsAndExistTest2() { | 143 | def void equalsAndExistTest2() { |
140 | val problem = parseHelper.parse(''' | 144 | val problem = parseHelper.parse(''' |
@@ -180,6 +184,7 @@ class PartialModelMapperTest { | |||
180 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); | 184 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); |
181 | } | 185 | } |
182 | 186 | ||
187 | //Testing the behavior of the newNodes | ||
183 | @Test | 188 | @Test |
184 | def void newNodeTest(){ | 189 | def void newNodeTest(){ |
185 | val problem = parseHelper.parse(''' | 190 | val problem = parseHelper.parse(''' |
@@ -207,6 +212,7 @@ class PartialModelMapperTest { | |||
207 | assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)); | 212 | assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)); |
208 | } | 213 | } |
209 | 214 | ||
215 | //Testing the behavior of enumerations | ||
210 | @Test | 216 | @Test |
211 | def void enumTest(){ | 217 | def void enumTest(){ |
212 | val problem = parseHelper.parse(''' | 218 | val problem = parseHelper.parse(''' |
@@ -236,6 +242,7 @@ class PartialModelMapperTest { | |||
236 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)); | 242 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)); |
237 | } | 243 | } |
238 | 244 | ||
245 | //Testing the bool from the built in problem | ||
239 | @Test | 246 | @Test |
240 | def void builtinBoolTest(){ | 247 | def void builtinBoolTest(){ |
241 | val problem = parseHelper.parse(''' | 248 | val problem = parseHelper.parse(''' |
@@ -257,9 +264,10 @@ class PartialModelMapperTest { | |||
257 | 264 | ||
258 | assertTrue(model.getDataRepresentations().contains(relationMap.get(bool))); | 265 | assertTrue(model.getDataRepresentations().contains(relationMap.get(bool))); |
259 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(trueEnum))).equals(TruthValue.TRUE)); | 266 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(trueEnum))).equals(TruthValue.TRUE)); |
260 | //TODO maradék assert | 267 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(falseEnum))).equals(TruthValue.TRUE)); |
261 | } | 268 | } |
262 | 269 | ||
270 | //Testing different aspects of the behavior | ||
263 | @Test | 271 | @Test |
264 | def void compositeTest() { | 272 | def void compositeTest() { |
265 | val problem = parseHelper.parse(''' | 273 | val problem = parseHelper.parse(''' |