From 277dac20e7c49dda3746a7a92b12dd5df86893ee Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 11 Apr 2018 14:13:15 +0200 Subject: [PATCH] Tests anpassen. Beginnen mit Oder Constraints --- src/de/dhbwstuttgart/sat/asp/Clingo.java | 1 - .../sat/asp/writer/ASPFactory.java | 16 ++++-- .../constraints/ConstraintSet.java | 8 +++ test/asp/UnifyWithoutWildcards.java | 51 ++++++++++++++++--- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index 771da890..c7608d47 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -49,7 +49,6 @@ public class Clingo { InputStream output = clingo.getInputStream(); clingo.waitFor(); String result = IOUtils.toString(output, StandardCharsets.UTF_8); - System.out.println(result); return result; } } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 1777eabd..3de3e1e3 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -12,6 +12,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import javax.management.Notification; import java.util.*; public class ASPFactory implements TypeVisitor{ @@ -19,16 +20,23 @@ public class ASPFactory implements TypeVisitor{ public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ ASPFactory factory = new ASPFactory(); factory.convertFC(fcClasses); - List> constraints1 = constraints.cartesianProduct().iterator().next(); - for(Constraint constraint : constraints1){ - for(Pair p : constraint){ - factory.convertPair(p); + //List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Pair p : constraints.getUndConstraints()){ + factory.convertPair(p); + } + for(Set> constraint : constraints.getOderConstraints()){ + for(Constraint oder : constraint){ + factory.convertOderConstraint(oder); } } return factory.writer.getASPFile(); } + private void convertOderConstraint(Constraint oder) { + throw new NotImplementedException(); + } + ASPWriter writer = new ASPWriter(); boolean isFCType = false; diff --git a/src/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java b/src/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java index 01356fe9..13c62e47 100644 --- a/src/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java +++ b/src/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java @@ -52,4 +52,12 @@ public class ConstraintSet { ret.oderConstraints = newOder; return ret; } + + public Constraint getUndConstraints() { + return undConstraints; + } + + public List>> getOderConstraints() { + return oderConstraints; + } } diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 40ea0bc7..b4ebd7d9 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -8,7 +8,7 @@ import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResolvedType; @@ -19,7 +19,6 @@ import org.junit.Test; import java.io.File; import java.io.IOException; import java.io.PrintWriter; -import java.sql.Ref; import java.util.*; public class UnifyWithoutWildcards { @@ -34,7 +33,7 @@ public class UnifyWithoutWildcards { RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - ResultSet resultSet = run(testSet); + ResultSet resultSet = run(testSet, getFC()); //System.out.println(ResultSetPrinter.print(resultSet)); assert resultSet.results.size() > 0; ResolvedType rsType = resultSet.resolveType(list2.get(1)); @@ -48,7 +47,7 @@ public class UnifyWithoutWildcards { RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken()); RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - ResultSet resultSet = run(testSet); + ResultSet resultSet = run(testSet, getFC()); //System.out.println(ResultSetPrinter.print(resultSet)); assert resultSet.results.size() == 0; //Hier gibt es keine Lösung. //TODO: Kann in zukünfigen Fällen eine Lösung geben @@ -60,19 +59,55 @@ public class UnifyWithoutWildcards { TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken()); RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - ResultSet resultSet = run(testSet); + ResultSet resultSet = run(testSet, getFC()); System.out.println(resultSet.results.size()); } - public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { + @Test + public void failTest() 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()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + testSet.addUndConstraint(new Pair(t1, t3, PairOperator.SMALLERDOT)); + Set fc = new HashSet<>(); + fc.add(ASTFactory.createClass(Test2.class)); + ResultSet resultSet = run(testSet, fc); + assert resultSet.results.size()==1; + } + + @Test + public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + for(int i = 0; i<1; i++){ + TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken()); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken()); + Set> oderCons = new HashSet<>(); + Constraint cons1 = new Constraint<>(); + cons1.add(new Pair(t1, t2, PairOperator.SMALLERDOT)); + oderCons.add(cons1); + Constraint cons2 = new Constraint<>(); + cons2.add(new Pair(t1, t3, PairOperator.SMALLERDOT)); + oderCons.add(cons2); + testSet.addOderConstraint(oderCons); + } + ResultSet resultSet = run(testSet, getFC()); + System.out.println(resultSet.results.size()); + } + + public ResultSet run(ConstraintSet toTest, Collection fc) throws IOException, InterruptedException, ClassNotFoundException { String content = ""; - content = ASPFactory.generateASP(toTest, this.getFC()); + content = ASPFactory.generateASP(toTest, fc); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); writer.println(content); writer.close(); Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); String result = clingo.runClingo(); + System.out.println(result); ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest)); return resultSet; } @@ -129,4 +164,6 @@ public class UnifyWithoutWildcards { private class Matrix extends HashMap>{} private class Test1 extends Object{} + + private class Test2 extends Object{} }