From 63e0207f9b044f24a4a07c99bf731ee92e08cfab Mon Sep 17 00:00:00 2001 From: Márton Golej Date: Sun, 17 Oct 2021 19:59:09 +0200 Subject: Language to store implementation added --- .../language/mapping/PartialModelMapper.java | 226 ++++++++++++++- .../language/mapping/PartialModelMapperDTO.java | 54 ++++ .../mapping/tests/PartialModelMapperTest.xtend | 302 ++++++++++++++++++++- .../language/mapping/tests/ProblemTestUtil.xtend | 114 ++++++++ 4 files changed, 688 insertions(+), 8 deletions(-) create mode 100644 language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java create mode 100644 language-to-store/src/test/java/tools/refinery/language/mapping/tests/ProblemTestUtil.xtend (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 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 @@ package tools.refinery.language.mapping; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; + +import org.eclipse.emf.common.util.EList; + +import tools.refinery.language.ProblemUtil; +import tools.refinery.language.model.problem.Assertion; +import tools.refinery.language.model.problem.AssertionArgument; +import tools.refinery.language.model.problem.ClassDeclaration; +import tools.refinery.language.model.problem.EnumDeclaration; +import tools.refinery.language.model.problem.LogicValue; +import tools.refinery.language.model.problem.Node; +import tools.refinery.language.model.problem.NodeAssertionArgument; +import tools.refinery.language.model.problem.PredicateDefinition; import tools.refinery.language.model.problem.Problem; +import tools.refinery.language.model.problem.ReferenceDeclaration; +import tools.refinery.language.model.problem.Statement; +import tools.refinery.language.model.problem.UniqueDeclaration; import tools.refinery.store.model.Model; +import tools.refinery.store.model.ModelStore; +import tools.refinery.store.model.ModelStoreImpl; +import tools.refinery.store.model.Tuple; +import tools.refinery.store.model.representation.Relation; +import tools.refinery.store.model.representation.TruthValue; + public class PartialModelMapper { - public Model transformProblem(Problem problem) { - // TODO @Marci Implement this - throw new UnsupportedOperationException(); + + private int nodeIter; + public PartialModelMapper() { + this.nodeIter = 0; + } + + public PartialModelMapperDTO transformProblem(Problem problem) throws Exception { + PartialModelMapperDTO pmmDTO = initTransform(problem); + + Optional builtinProblem = ProblemUtil.getBuiltInLibrary(problem); + if (builtinProblem.isEmpty()) throw new Exception("builtin.problem not found"); + PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); + pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); + pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); + pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); //így most valami nem stimmel + pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); + pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); + + //Definition of store and model + ModelStore store = new ModelStoreImpl(new HashSet<>(pmmDTO.getRelationMap().values())); + Model model = store.createModel(); + pmmDTO.setModel(model); + + Map allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), + pmmDTO.getUniqueNodeMap(), + pmmDTO.getNewNodeMap(), + pmmDTO.getNodeMap()); + + //Filling up the relations with unknown truth values + for (tools.refinery.language.model.problem.Relation relation : pmmDTO.getRelationMap().keySet()) { + if(!(relation instanceof PredicateDefinition pd && pd.isError())) { + Relation r = pmmDTO.getRelationMap().get(relation); + if(r.getArity() == 1) + for(Integer i : allNodesMap.values()) { + pmmDTO.getModel().put(r, Tuple.of(i), TruthValue.UNKNOWN); + } + else if(r.getArity() == 2) + for(Integer i : allNodesMap.values()) { + for (Integer j : allNodesMap.values()) { + pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); + } + } + else throw new Exception("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"); + for (Node n : allNodesMap.keySet()) { + if(pmmDTO.getNewNodeMap().containsKey(n)) { + pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), + Tuple.of(allNodesMap.get(n)), + TruthValue.UNKNOWN); + } + else { + pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), + Tuple.of(allNodesMap.get(n)), + TruthValue.TRUE); + } + } + + //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"); + for (Node n1 : allNodesMap.keySet()) { + for(Node n2 : allNodesMap.keySet()) { + if(n1.equals(n2)) { + if(pmmDTO.getNewNodeMap().containsKey(n1)) { + pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), + Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), + TruthValue.UNKNOWN); + } + else { + pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), + Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), + TruthValue.TRUE); + } + } + else { + pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), + Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), + TruthValue.FALSE); + } + } + } + + //Transforming the assertions + processAssertions(problem, pmmDTO, allNodesMap); + processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); + + //throw new UnsupportedOperationException(); + return pmmDTO; + } + + private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map allNodesMap) { + for(Statement s : problem.getStatements()) { + if(s instanceof Assertion assertion) { + Relation r1 = pmmDTO.getRelationMap().get(assertion.getRelation()); + int i = 0; + int[] integers = new int[assertion.getArguments().size()]; + for (AssertionArgument aa : assertion.getArguments()) { + if (aa instanceof NodeAssertionArgument nas) { + integers[i] = allNodesMap.get(nas.getNode()); + i++; + } + } + pmmDTO.getModel().put(r1, Tuple.of(integers), logicValueToTruthValue(assertion.getValue())); + } + else if (s instanceof ClassDeclaration cd) { + if(!cd.isAbstract()) + pmmDTO.getModel().put(pmmDTO.getRelationMap().get(cd), + Tuple.of(pmmDTO.getNewNodeMap().get(cd.getNewNode())), + TruthValue.TRUE); + } + else if (s instanceof EnumDeclaration ed) { + for (Node n : ed.getLiterals()) { + pmmDTO.getModel().put(pmmDTO.getRelationMap().get(ed), + Tuple.of(pmmDTO.getEnumNodeMap().get(n)), + TruthValue.TRUE); + } + } + } + } + + public PartialModelMapperDTO initTransform(Problem problem) { + //Defining needed Maps + Map> relationMap = new HashMap<>(); + Map enumNodeMap = new HashMap<>(); + Map uniqueNodeMap = new HashMap<>(); + Map newNodeMap = new HashMap<>(); + + //Definition of Relations, filling up the enumNodeMap, uniqueNodeMap, newNodeMap + EList statements = problem.getStatements(); + for (Statement s : statements) { + if (s instanceof ClassDeclaration cd) { + Relation r1 = new Relation<>(cd.getName(), 1, TruthValue.FALSE); + relationMap.put(cd, r1); + if(!cd.isAbstract()) newNodeMap.put(cd.getNewNode(), this.nodeIter++); + EList refDeclList = cd.getReferenceDeclarations(); + for (ReferenceDeclaration refDec : refDeclList) { + Relation r2 = new Relation<>(refDec.getName(), 2, TruthValue.FALSE); + relationMap.put(refDec, r2); + } + } + 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++); + } + } + else if (s instanceof UniqueDeclaration ud) { + for (Node n : ud.getNodes()) { + uniqueNodeMap.put(n, this.nodeIter++); + } + } + else if (s instanceof PredicateDefinition pd) { + Relation r = new Relation<>(pd.getName(), 1, TruthValue.FALSE); + relationMap.put(pd, r); + } + } + + + + + //Filling the nodeMap up + Map nodeMap = new HashMap<>(); + for(Node n : problem.getNodes()) { + nodeMap.put(n, this.nodeIter++); + } + + return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); + } + + 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)); + return out; + } + + 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; } } 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 @@ +package tools.refinery.language.mapping; + +import java.util.Map; + +import tools.refinery.language.model.problem.Node; +import tools.refinery.store.model.Model; +import tools.refinery.store.model.representation.Relation; +import tools.refinery.store.model.representation.TruthValue; + +public class PartialModelMapperDTO { + private Model model; + private Map> relationMap; + private Map nodeMap; + private Map enumNodeMap; + private Map uniqueNodeMap; + private Map newNodeMap; + + public PartialModelMapperDTO(Model model, + Map> relationMap, + Map nodeMap, + Map enumNodeMap, + Map uniqueNodeMap, + Map newNodeMap) { + this.model = model; + this.relationMap = relationMap; + this.nodeMap = nodeMap; + this.enumNodeMap = enumNodeMap; + this.uniqueNodeMap = uniqueNodeMap; + this.newNodeMap = newNodeMap; + } + + public Model getModel() { + return this.model; + } + public Map> getRelationMap(){ + return this.relationMap; + } + public Map getNodeMap() { + return this.nodeMap; + } + public Map getEnumNodeMap() { + return this.enumNodeMap; + } + public Map getUniqueNodeMap() { + return this.uniqueNodeMap; + } + public Map getNewNodeMap() { + return this.newNodeMap; + } + + public void setModel(Model model) { + this.model = model; + } +} 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 @@ package tools.refinery.language.mapping.tests import com.google.inject.Inject +import org.eclipse.emf.ecore.util.EcoreUtil import org.eclipse.xtext.testing.InjectWith import org.eclipse.xtext.testing.extensions.InjectionExtension import org.eclipse.xtext.testing.util.ParseHelper import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.junit.jupiter.api.^extension.ExtendWith import tools.refinery.language.mapping.PartialModelMapper import tools.refinery.language.model.problem.Problem import tools.refinery.language.tests.ProblemInjectorProvider +import tools.refinery.store.model.Tuple +import tools.refinery.store.model.representation.TruthValue import static org.hamcrest.MatcherAssert.assertThat import static org.hamcrest.Matchers.* +import static org.junit.jupiter.api.Assertions.assertTrue @ExtendWith(InjectionExtension) @InjectWith(ProblemInjectorProvider) class PartialModelMapperTest { @Inject ParseHelper parseHelper + + @Inject + extension ProblemTestUtil PartialModelMapper mapper @@ -29,8 +35,7 @@ class PartialModelMapperTest { } @Test - @Disabled("Method not yet implemented") - def void exampleTest() { + def void relationTest() { val problem = parseHelper.parse(''' class Person { Person[0..*] friend @@ -38,7 +43,294 @@ class PartialModelMapperTest { friend(a, b). ''') - val model = mapper.transformProblem(problem) - assertThat(model, notNullValue()) + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val nodeMap = modelAndMaps.nodeMap + + val person = problem.findClass("Person") + val friend = problem.findClass("Person").reference("friend") + val a = problem.node("a") + val b = problem.node("b") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(person))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))); + assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)); + } + + @Test + def void classTest() { + val problem = parseHelper.parse(''' + class Person { + Person[0..*] friend + } + + Person(a). + ''') + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val nodeMap = modelAndMaps.nodeMap + + val person = problem.findClass("Person") + val friend = problem.findClass("Person").reference("friend") + val a = problem.node("a") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(person))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))); + + assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); + } + + @Test + def void equalsAndExistTest() { + val problem = parseHelper.parse(''' + node(a). + node(b). + + class Person. + ''') + EcoreUtil.resolveAll(problem) + val builtin = problem.builtin; + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val nodeMap = modelAndMaps.nodeMap + val newNodeMap = modelAndMaps.newNodeMap + + val a = problem.node("a") + val b = problem.node("b") + val Person = problem.findClass("Person") + val PersonNew = problem.findClass("Person").newNode + val exists = builtin.pred("exists") + val equals = builtin.findClass("node").reference("equals") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) + assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))) + assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))) + + assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)) + assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)) + + assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) + } + + @Test + def void equalsAndExistTest2() { + val problem = parseHelper.parse(''' + class Person. + + Person(a). + Person(b). + ''') + val builtin = problem.builtin; + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val nodeMap = modelAndMaps.nodeMap + val newNodeMap = modelAndMaps.newNodeMap + + val a = problem.node("a") + val b = problem.node("b") + val Person = problem.findClass("Person") + val PersonNew = problem.findClass("Person").newNode + val exists = builtin.pred("exists") + val equals = builtin.findClass("node").reference("equals") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))); + + assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)); + + assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); + assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)); + } + + @Test + def void newNodeTest(){ + val problem = parseHelper.parse(''' + class Person. + abstract class Family. + ''') + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem); + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val newNodeMap = modelAndMaps.newNodeMap + + val Person = problem.findClass("Person") + val Family = problem.findClass("Family") + val PersonNew = problem.findClass("Person").newNode + + + assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))); + + assertTrue(newNodeMap.size.equals(4)) //3 from builtin.problem, 1 from Person + assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)); + } + + @Test + def void enumTest(){ + val problem = parseHelper.parse(''' + enum TaxStatus { + child, student, adult, retired + } + ''') + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val enumNodeMap = modelAndMaps.enumNodeMap + + val TaxStatus = problem.findEnum("TaxStatus") + val child = problem.findEnum("TaxStatus").literal("child") + val student = problem.findEnum("TaxStatus").literal("student") + val adult = problem.findEnum("TaxStatus").literal("adult") + val retired = problem.findEnum("TaxStatus").literal("retired") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))); + assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(child))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(student))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(adult))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)); + } + + @Test + def void builtinBoolTest(){ + val problem = parseHelper.parse(''' + class Person. + ''') + EcoreUtil.resolveAll(problem) + val builtin = problem.builtin; + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val enumNodeMap = modelAndMaps.enumNodeMap + + val bool = builtin.findEnum("bool") + val trueEnum = builtin.findEnum("bool").literal("true") //Emiatt nem sikerül a teszt + val falseEnum = builtin.findEnum("bool").literal("false") + + 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 + } + + @Test + def void compositeTest() { + val problem = parseHelper.parse(''' + class Family { + contains Person[] members + } + + class Person { + Person[0..*] children + Person[0..1] parent + TaxStatus taxStatus + } + + enum TaxStatus { + child, student, adult, retired + } + + % A child cannot have any dependents. + error invalidTaxStatus(Person p) <-> + taxStatus(p, child), children(p, _q). + + unique family. + Family(family). + members(family, anne): true. + members(family, bob). + members(family, ciri). + children(anne, ciri). + ?children(bob, ciri). + taxStatus(anne, adult). + ''') + EcoreUtil.resolveAll(problem) + + val modelAndMaps = mapper.transformProblem(problem) + assertThat(modelAndMaps, notNullValue()) + + val model = modelAndMaps.model + val relationMap = modelAndMaps.relationMap + val nodeMap = modelAndMaps.nodeMap + val uniqueNodeMap = modelAndMaps.uniqueNodeMap + val enumNodeMap = modelAndMaps.enumNodeMap + + val Family = problem.findClass("Family") + val members = problem.findClass("Family").reference("members") + val Person = problem.findClass("Person") + val children = problem.findClass("Person").reference("children") + val parent = problem.findClass("Person").reference("parent") + val taxStatus = problem.findClass("Person").reference("taxStatus") + val TaxStatus = problem.findEnum("TaxStatus") + val invalidTaxStatus = problem.pred("invalidTaxStatus") + + val anne = problem.node("anne") + val bob = problem.node("bob") + val ciri = problem.node("ciri") + val family = problem.uniqueNode("family") + val adult = problem.findEnum("TaxStatus").literal("adult") + + assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(members))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(children))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(parent))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(taxStatus))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))); + assertTrue(model.getDataRepresentations().contains(relationMap.get(invalidTaxStatus))); + + assertTrue(model.get(relationMap.get(Family), Tuple.of(uniqueNodeMap.get(family))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(anne))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(bob))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(ciri))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(anne),nodeMap.get(ciri))).equals(TruthValue.TRUE)); + assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(bob),nodeMap.get(ciri))).equals(TruthValue.UNKNOWN)); + assertTrue(model.get(relationMap.get(taxStatus), Tuple.of(nodeMap.get(anne),enumNodeMap.get(adult))).equals(TruthValue.TRUE)); } } 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 @@ +package tools.refinery.language.mapping.tests + +import tools.refinery.language.ProblemUtil +import tools.refinery.language.model.problem.Argument +import tools.refinery.language.model.problem.Assertion +import tools.refinery.language.model.problem.AssertionArgument +import tools.refinery.language.model.problem.Atom +import tools.refinery.language.model.problem.ClassDeclaration +import tools.refinery.language.model.problem.Conjunction +import tools.refinery.language.model.problem.EnumDeclaration +import tools.refinery.language.model.problem.Literal +import tools.refinery.language.model.problem.NegativeLiteral +import tools.refinery.language.model.problem.Node +import tools.refinery.language.model.problem.NodeAssertionArgument +import tools.refinery.language.model.problem.NodeValueAssertion +import tools.refinery.language.model.problem.PredicateDefinition +import tools.refinery.language.model.problem.Problem +import tools.refinery.language.model.problem.UniqueDeclaration +import tools.refinery.language.model.problem.Variable +import tools.refinery.language.model.problem.VariableOrNodeArgument + +class ProblemTestUtil { + def builtin(Problem it) { + ProblemUtil.getBuiltInLibrary(it).get + } + + def errors(Problem it) { + eResource.errors + } + + def nodeNames(Problem it) { + nodes.map[name] + } + + def pred(Problem it, String name) { + statements.filter(PredicateDefinition).findFirst[it.name == name] + } + + def param(PredicateDefinition it, int i) { + parameters.get(i) + } + + def conj(PredicateDefinition it, int i) { + bodies.get(i) + } + + def lit(Conjunction it, int i) { + literals.get(i) + } + + def negated(Literal it) { + (it as NegativeLiteral).atom + } + + def relation(Literal it) { + (it as Atom).relation + } + + def arg(Atom it, int i) { + it.arguments.get(i) + } + + def arg(Literal it, int i) { + (it as Atom).arg(i) + } + + def variable(Argument it) { + (it as VariableOrNodeArgument).variableOrNode as Variable + } + + def node(Argument it) { + (it as VariableOrNodeArgument).variableOrNode as Node + } + + def assertion(Problem it, int i) { + statements.filter(Assertion).get(i) + } + + def nodeValueAssertion(Problem it, int i) { + statements.filter(NodeValueAssertion).get(i) + } + + def arg(Assertion it, int i) { + arguments.get(i) + } + + def node(AssertionArgument it) { + (it as NodeAssertionArgument).node + } + + def node(Problem it, String name) { + nodes.findFirst[it.name == name] + } + + def uniqueNode(Problem it, String name) { + statements.filter(UniqueDeclaration).flatMap[nodes].findFirst[it.name == name] + } + + def findClass(Problem it, String name) { + statements.filter(ClassDeclaration).findFirst[it.name == name] + } + + def reference(ClassDeclaration it, String name) { + it.referenceDeclarations.findFirst[it.name == name] + } + + def findEnum(Problem it, String name) { + statements.filter(EnumDeclaration).findFirst[it.name == name] + } + + def literal(EnumDeclaration it, String name) { + literals.findFirst[it.name == name] + } +} -- cgit v1.2.3-54-g00ecf