From 10be0b17e9e0014cc43ab50618327cfef3e54340 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 11 Apr 2018 17:16:45 +0200 Subject: [PATCH] Oder-Constraints generieren --- src/de/dhbwstuttgart/sat/asp/Clingo.java | 1 + .../dhbwstuttgart/sat/asp/model/ASPRule.java | 5 +- .../sat/asp/writer/ASPFactory.java | 77 ++++++++++++++++--- test/asp/UnifyWithoutWildcards.java | 20 ++++- .../unifywithoutwildcards/ASPFactoryTest.java | 45 +++++++++++ 5 files changed, 134 insertions(+), 14 deletions(-) create mode 100644 test/asp/unifywithoutwildcards/ASPFactoryTest.java diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index c7608d47..78cb694e 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -23,6 +23,7 @@ public class Clingo { programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/result.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/cartesian.lp")); } public Clingo(List inputFiles){ diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java index 20d221f9..acd297ca 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -8,7 +8,10 @@ public enum ASPRule { ASP_PARAMLIST_END_POINTER("null"), ASP_TYPE("type"), ASP_FCTYPE("typeFC"), - ASP_TYPE_VAR("typeVar"); + ASP_TYPE_VAR("typeVar"), ASP_ODER("oder"), + ASP_CONSTRAINT("constraint"), + ASP_LIST_NAME("list"), + ASP_LIST_ENDPOINTER("null"); private final String text; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 3de3e1e3..3e3c70ad 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -24,23 +24,71 @@ public class ASPFactory implements TypeVisitor{ for(Pair p : constraints.getUndConstraints()){ factory.convertPair(p); } - for(Set> constraint : constraints.getOderConstraints()){ - for(Constraint oder : constraint){ - factory.convertOderConstraint(oder); - } + for(Set> oder : constraints.getOderConstraints()){ + factory.convertOderConstraint(oder); } return factory.writer.getASPFile(); } - private void convertOderConstraint(Constraint oder) { - throw new NotImplementedException(); + /** + * Wandelt eine Reihe von Constraints zu einem Und-Constraint - also einer Liste - um. + * Dieser kann dann in Oder-Constraints verwendet werden. + * @param undCons - Eine Liste von Constraints. Bsp: equals(..), smallerDot(..) + * @return - list(equals, list(smallerDot(..., null) + */ + protected ASPStatement convertListToUndConstraint(List undCons) { + if(undCons.size() == 0)throw new NullPointerException(); + if(undCons.size() == 1){ + return undCons.get(0); + } + ASPStatement list = makeStatement(ASPRule.ASP_LIST_ENDPOINTER.toString()); + for(ASPStatement con : undCons){ + list = makeStatement(ASPRule.ASP_LIST_NAME.toString(), con.getASP(), list.getASP()); + } + return list; } - ASPWriter writer = new ASPWriter(); + protected ASPStatement convertListToUndConstraint(Constraint undCons) { + List convert = new ArrayList<>(); + for(Pair p : undCons){ + convert.add(generatePairStmt(p)); + } + return convertListToUndConstraint(convert); + } + + protected void convertOderConstraint(Set> oder) { + if(oder.size() < 2)throw new NotImplementedException(); + List ret = new ArrayList<>(); + Iterator> it = oder.iterator(); + String pointer1 = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + ASPStatement stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), pointer1, + convertListToUndConstraint(it.next()).getASP()); + ret.add(stmt); + while(it.hasNext()){ + String pointer2 = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Constraint cons = it.next(); + stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), pointer2, + convertListToUndConstraint(cons).getASP()); + ret.add(stmt); + ASPStatement oderStmt = makeStatement(ASPRule.ASP_ODER.toString(), pointer1, pointer2); + if(it.hasNext()){ + String oderPointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), oderPointer, oderStmt.getASP()); + ret.add(stmt); + pointer1 = oderPointer; + }else{ + ret.add(oderStmt); + } + } + //Alle erstellten Constraints schreiben: + writer.addAll(ret); + } + + protected ASPWriter writer = new ASPWriter(); boolean isFCType = false; - private void convertFC(Collection classes) throws ClassNotFoundException { + protected void convertFC(Collection classes) throws ClassNotFoundException { Set fc = FCGenerator.toFC(classes); isFCType = true; for(Pair fcp : fc){ @@ -49,7 +97,12 @@ public class ASPFactory implements TypeVisitor{ isFCType = false; } - private void convertPair(Pair p){ + protected void convertPair(Pair p){ + ASPStatement pairStmt = generatePairStmt(p); + writer.add(pairStmt); + } + + protected ASPStatement generatePairStmt(Pair p) { String ls = p.TA1.acceptTV(this); String rs = p.TA2.acceptTV(this); ASPStatement pairStmt = null; @@ -60,10 +113,10 @@ public class ASPFactory implements TypeVisitor{ }else if(p.GetOperator().equals(PairOperator.SMALLER)){ pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); }else throw new NotImplementedException(); - writer.add(pairStmt); + return pairStmt; } - private ASPStatement makeStatement(String rule, String... params){ + protected ASPStatement makeStatement(String rule, String... params){ String stmt = rule + "("; for(String param : params){ stmt += param + ","; @@ -73,7 +126,7 @@ public class ASPFactory implements TypeVisitor{ return new ASPStatement(stmt); } - private String convertParameterlist(List pointers){ + protected String convertParameterlist(List pointers){ String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); Iterator it = pointers.iterator(); String p = pointer; diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index b4ebd7d9..d425261b 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -77,6 +77,24 @@ public class UnifyWithoutWildcards { assert resultSet.results.size()==1; } + @Test + public void oderConstraintTest() throws InterruptedException, IOException, ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken()); + RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test2"), new ArrayList<>(), new NullToken()); + Set> oderCons = new HashSet<>(); + Constraint cons1 = new Constraint<>(); + cons1.add(new Pair(t1, t2, PairOperator.EQUALSDOT)); + oderCons.add(cons1); + Constraint cons2 = new Constraint<>(); + cons2.add(new Pair(t1, t3, PairOperator.EQUALSDOT)); + oderCons.add(cons2); + testSet.addOderConstraint(oderCons); + ResultSet resultSet = run(testSet, getFC()); + assert resultSet.results.size() == 1; + } + @Test public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet testSet = new ConstraintSet<>(); @@ -95,7 +113,7 @@ public class UnifyWithoutWildcards { testSet.addOderConstraint(oderCons); } ResultSet resultSet = run(testSet, getFC()); - System.out.println(resultSet.results.size()); + assert resultSet.results.size() == 1; } public ResultSet run(ConstraintSet toTest, Collection fc) throws IOException, InterruptedException, ClassNotFoundException { diff --git a/test/asp/unifywithoutwildcards/ASPFactoryTest.java b/test/asp/unifywithoutwildcards/ASPFactoryTest.java new file mode 100644 index 00000000..16cd150f --- /dev/null +++ b/test/asp/unifywithoutwildcards/ASPFactoryTest.java @@ -0,0 +1,45 @@ +package asp.unifywithoutwildcards; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; +import de.dhbwstuttgart.sat.asp.writer.ASPStatement; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class ASPFactoryTest extends ASPFactory { + @Test + public void undConstraintTest(){ + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + Constraint toTest = new Constraint(); + for(int i = 0; i<5;i++){ + toTest.add(new Pair(tph, tph, PairOperator.EQUALSDOT)); + } + List undCons = new ArrayList<>(); + for(Pair p : toTest){ + undCons.add(generatePairStmt(p)); + } + ASPStatement ret = convertListToUndConstraint(undCons); + System.out.println(ret.getASP()); + } + + @Test + public void oderConstraintTest(){ + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + Set> oderTest = new HashSet<>(); + for(int i = 0; i<5;i++){ + Constraint toTest = new Constraint(); + toTest.add(new Pair(tph, tph, PairOperator.EQUALSDOT)); + oderTest.add(toTest); + } + this.convertOderConstraint(oderTest); + System.out.println(this.writer.getASPFile()); + } +}