From f566affc42c3207eebbbce46726df6e582035763 Mon Sep 17 00:00:00 2001 From: Márton Golej Date: Mon, 18 Oct 2021 16:12:10 +0200 Subject: Commments added, code cleanup: --- .../language/mapping/PartialModelMapper.java | 68 ++++++++++++++-------- .../mapping/tests/PartialModelMapperTest.xtend | 10 +++- 2 files changed, 53 insertions(+), 25 deletions(-) (limited to 'language-to-store') 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 { this.nodeIter = 0; } - public PartialModelMapperDTO transformProblem(Problem problem) throws Exception { + public PartialModelMapperDTO transformProblem(Problem problem) throws PartialModelMapperException { + //Getting the relations and the nodes from the given problem PartialModelMapperDTO pmmDTO = initTransform(problem); + //Getting the relations and the nodes from the built in problem Optional builtinProblem = ProblemUtil.getBuiltInLibrary(problem); - if (builtinProblem.isEmpty()) throw new Exception("builtin.problem not found"); + if (builtinProblem.isEmpty()) throw new PartialModelMapperException("builtin.problem not found"); PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); + + //Merging the relation and the nodes from the given problem and from the built in problem pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); - pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); //így most valami nem stimmel + pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); @@ -52,6 +56,7 @@ public class PartialModelMapper { Model model = store.createModel(); pmmDTO.setModel(model); + //Collecting all the nodes in one map Map allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), pmmDTO.getUniqueNodeMap(), pmmDTO.getNewNodeMap(), @@ -71,16 +76,13 @@ public class PartialModelMapper { pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); } } - else throw new Exception("Relation with arity above 2 is not supported"); + else throw new PartialModelMapperException("Relation with arity above 2 is not supported"); } } //Filling up the exists - tools.refinery.language.model.problem.Relation existsRelation = null; - for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { - if (r.getName().equals("exists")) existsRelation = r; - } - if(existsRelation.equals(null)) throw new Exception("exists not found"); + tools.refinery.language.model.problem.Relation existsRelation = findingRelationInDTO(builtinProblemDTO, "exists", + "The exists not found in built in problem"); for (Node n : allNodesMap.keySet()) { if(pmmDTO.getNewNodeMap().containsKey(n)) { pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), @@ -95,11 +97,8 @@ public class PartialModelMapper { } //Filling up the equals - tools.refinery.language.model.problem.Relation equalsRelation = null; - for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { - if (r.getName().equals("equals")) equalsRelation = r; - } - if(equalsRelation.equals(null)) throw new Exception("equals not found"); + tools.refinery.language.model.problem.Relation equalsRelation = findingRelationInDTO(builtinProblemDTO, "equals", + "The equals not found in built in problem"); for (Node n1 : allNodesMap.keySet()) { for(Node n2 : allNodesMap.keySet()) { if(n1.equals(n2)) { @@ -126,10 +125,21 @@ public class PartialModelMapper { processAssertions(problem, pmmDTO, allNodesMap); processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); - //throw new UnsupportedOperationException(); return pmmDTO; } + //Searches for and gives back a relation in a PartialModelMapperDTO + private tools.refinery.language.model.problem.Relation findingRelationInDTO(PartialModelMapperDTO partialModelMapperDTO, String searchedRelation, String errorText) + throws PartialModelMapperException { + tools.refinery.language.model.problem.Relation relation = null; + for (tools.refinery.language.model.problem.Relation r : partialModelMapperDTO.getRelationMap().keySet()) { + if (r.getName().equals(searchedRelation)) relation = r; + } + if(relation.equals(null)) throw new PartialModelMapperException(errorText); + return relation; + } + + //Processing assertions and placing them in the model private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map allNodesMap) { for(Statement s : problem.getStatements()) { if(s instanceof Assertion assertion) { @@ -160,6 +170,7 @@ public class PartialModelMapper { } } + //Getting the relations and nodes from the problem public PartialModelMapperDTO initTransform(Problem problem) { //Defining needed Maps Map> relationMap = new HashMap<>(); @@ -183,7 +194,6 @@ public class PartialModelMapper { else if (s instanceof EnumDeclaration ed) { Relation r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); relationMap.put(ed, r); - EList nodeList = ed.getLiterals(); for (Node n : ed.getLiterals()) { enumNodeMap.put(n, nodeIter++); } @@ -199,9 +209,6 @@ public class PartialModelMapper { } } - - - //Filling the nodeMap up Map nodeMap = new HashMap<>(); for(Node n : problem.getNodes()) { @@ -210,22 +217,35 @@ public class PartialModelMapper { return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); } - + + //Merging the maps of nodes into one map private Map mergeNodeMaps(Map enumNodeMap, Map uniqueNodeMap, Map newNodeMap, Map nodeMap) { - Map out = new HashMap<>(enumNodeMap); - for (Node n : uniqueNodeMap.keySet()) out.put(n, uniqueNodeMap.get(n)); - for (Node n : newNodeMap.keySet()) out.put(n, newNodeMap.get(n)); - for (Node n : nodeMap.keySet()) out.put(n, nodeMap.get(n)); + Map out = new HashMap<>(); + out.putAll(enumNodeMap); + out.putAll(uniqueNodeMap); + out.putAll(newNodeMap); + out.putAll(nodeMap); return out; } + //Exchange method from LogicValue to TruthValue private TruthValue logicValueToTruthValue(LogicValue value) { if(value.equals(LogicValue.TRUE)) return TruthValue.TRUE; else if(value.equals(LogicValue.FALSE)) return TruthValue.FALSE; else if(value.equals(LogicValue.UNKNOWN)) return TruthValue.UNKNOWN; else return TruthValue.ERROR; } + + public class PartialModelMapperException extends Exception{ + private static final long serialVersionUID = 1L; + public PartialModelMapperException(String errorText) { + super(errorText); + } + public PartialModelMapperException() { + super(); + } + } } 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 { mapper = new PartialModelMapper } + //Testing the relation @Test def void relationTest() { val problem = parseHelper.parse(''' @@ -62,6 +63,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)); } + //Testing the class @Test def void classTest() { val problem = parseHelper.parse(''' @@ -90,6 +92,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); } + //Testing the equals and exists from the built in problem @Test def void equalsAndExistTest() { val problem = parseHelper.parse(''' @@ -135,6 +138,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) } + //Testing the equals and exists from the built in problem with a different example @Test def void equalsAndExistTest2() { val problem = parseHelper.parse(''' @@ -180,6 +184,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); } + //Testing the behavior of the newNodes @Test def void newNodeTest(){ val problem = parseHelper.parse(''' @@ -207,6 +212,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)); } + //Testing the behavior of enumerations @Test def void enumTest(){ val problem = parseHelper.parse(''' @@ -236,6 +242,7 @@ class PartialModelMapperTest { assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)); } + //Testing the bool from the built in problem @Test def void builtinBoolTest(){ val problem = parseHelper.parse(''' @@ -257,9 +264,10 @@ class PartialModelMapperTest { assertTrue(model.getDataRepresentations().contains(relationMap.get(bool))); assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(trueEnum))).equals(TruthValue.TRUE)); - //TODO maradék assert + assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(falseEnum))).equals(TruthValue.TRUE)); } + //Testing different aspects of the behavior @Test def void compositeTest() { val problem = parseHelper.parse(''' -- cgit v1.2.3-54-g00ecf