From b6d67a1c519e86a4ed4d9df390fc7b40758ea0fd Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 12 Apr 2018 19:31:32 +0200 Subject: [PATCH] Beginnen mit Tests von UnifyWithoutWildcards --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 73 ++++++++- .../sat/asp/writer/ASPFactory.java | 24 ++- .../JavaTXCompilerASPTest.java | 141 ++++++++++++++++++ 3 files changed, 229 insertions(+), 9 deletions(-) create mode 100644 test/asp/unifywithoutwildcards/JavaTXCompilerASPTest.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 9a66543a..eb1d1db7 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -8,11 +8,14 @@ import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -22,8 +25,7 @@ import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.*; import java.util.stream.Collectors; @@ -130,4 +132,69 @@ public class JavaTXCompiler { return ret; } + public List aspTypeInference() throws ClassNotFoundException, IOException, InterruptedException { + List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); + //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC + for(SourceFile sf : this.sourceFiles.values()) { + allClasses.addAll(getAvailableClasses(sf)); + allClasses.addAll(sf.getClasses()); + } + + final ConstraintSet cons = getConstraints(); + + + String content = ""; + content = ASPFactory.generateASP(cons, allClasses); + final String tempDirectory = "/tmp/"; + 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(cons)); + return Arrays.asList(resultSet); + } + + private static class TPHExtractor implements TypeVisitor> { + @Override + public List visit(RefType refType) { + ArrayList ret = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + ret.addAll(param.acceptTV(this)); + } + return ret; + } + + @Override + public List visit(SuperWildcardType superWildcardType) { + return superWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(TypePlaceholder typePlaceholder) { + return Arrays.asList(typePlaceholder); + } + + @Override + public List visit(ExtendsWildcardType extendsWildcardType) { + return extendsWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(GenericRefType genericRefType) { + return new ArrayList<>(); + } + } + + protected Collection getInvolvedTPHS(ConstraintSet toTest) { + List ret = new ArrayList<>(); + toTest.map((Pair p)-> { + ret.addAll(p.TA1.acceptTV(new TPHExtractor())); + ret.addAll(p.TA2.acceptTV(new TPHExtractor())); + return p; + }); + return ret; + } + } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 3e3c70ad..aafb8166 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -42,7 +42,7 @@ public class ASPFactory implements TypeVisitor{ if(undCons.size() == 1){ return undCons.get(0); } - ASPStatement list = makeStatement(ASPRule.ASP_LIST_ENDPOINTER.toString()); + ASPStatement list = new ASPStatement(ASPRule.ASP_LIST_ENDPOINTER.toString()); for(ASPStatement con : undCons){ list = makeStatement(ASPRule.ASP_LIST_NAME.toString(), con.getASP(), list.getASP()); } @@ -58,7 +58,11 @@ public class ASPFactory implements TypeVisitor{ } protected void convertOderConstraint(Set> oder) { - if(oder.size() < 2)throw new NotImplementedException(); + if(oder.size() < 2){//Oder-Setgröße darf nicht null sein. Sonst gibt es sowieso kein Ergebnis: + for(Pair p : oder.iterator().next()){ + this.convertPair(p); //Einfach als und Constraints behandeln, wenn es nur einen Oder-Zweig gibt + } + } List ret = new ArrayList<>(); Iterator> it = oder.iterator(); String pointer1 = ASPStringConverter.toConstant(NameGenerator.makeNewName()); @@ -118,10 +122,12 @@ public class ASPFactory implements TypeVisitor{ protected ASPStatement makeStatement(String rule, String... params){ String stmt = rule + "("; - for(String param : params){ - stmt += param + ","; + Iterator it = Arrays.asList(params).iterator(); + while(it.hasNext()){ + String param = it.next(); + stmt += param; + if(it.hasNext())stmt+=","; } - stmt = stmt.substring(0,stmt.length()-1); stmt += ")"; return new ASPStatement(stmt); } @@ -183,6 +189,12 @@ public class ASPFactory implements TypeVisitor{ @Override public String visit(GenericRefType genericRefType) { - throw new NotImplementedException(); + //Kann eigentlich wie ein normaler RefType behandelt werden + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + String typeName = ASPStringConverter.toConstant(genericRefType.getParsedName()); + String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, ASPRule.ASP_PARAMLIST_END_POINTER.toString()); + writer.add(stmt); + return pointer; } } diff --git a/test/asp/unifywithoutwildcards/JavaTXCompilerASPTest.java b/test/asp/unifywithoutwildcards/JavaTXCompilerASPTest.java new file mode 100644 index 00000000..b9ac3e02 --- /dev/null +++ b/test/asp/unifywithoutwildcards/JavaTXCompilerASPTest.java @@ -0,0 +1,141 @@ +package asp.unifywithoutwildcards; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; +import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typedeployment.TypeInsert; +import de.dhbwstuttgart.typedeployment.TypeInsertFactory; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class JavaTXCompilerASPTest { + + public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; + + @Test + public void finiteClosure() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"fc.jav")); + } + @Test + public void lambda() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Lambda.jav")); + } + @Test + public void lambda2() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Lambda2.jav")); + } + @Test + public void lambda3() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Lambda3.jav")); + } + @Test + public void mathStruc() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"mathStruc.jav")); + } + @Test + public void generics() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Generics.jav")); + } + @Test + public void genericsMethodCall() throws IOException, ClassNotFoundException, InterruptedException { + TestResultSet result = execute(new File(rootDirectory+"MethodCallGenerics.jav")); + //TODO: Hier sollte der Rückgabetyp der Methode String sein + } + @Test + public void faculty() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Faculty.jav")); + } + @Test + public void facultyTyped() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"FacultyTyped.jav")); + } + @Test + public void matrix() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Matrix.jav")); + } + @Test + public void packageTests() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Package.jav")); + } + @Test + public void vector() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Vector.jav")); + } + @Test + public void lambdaRunnable() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"LambdaRunnable.jav")); + } + @Test + public void expressions() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"Expressions.jav")); + } + @Test + public void addLong() throws IOException, ClassNotFoundException, InterruptedException { + execute(new File(rootDirectory+"AddLong.jav")); + } + + private static class TestResultSet{ + + } + + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InterruptedException { + //filesToTest.add(new File(rootDirectory+"fc.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); + //filesToTest.add(new File(rootDirectory+"Vector.jav")); + //filesToTest.add(new File(rootDirectory+"Generics.jav")); + //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); + //filesToTest.add(new File(rootDirectory+"Matrix.jav")); + //filesToTest.add(new File(rootDirectory+"Import.jav")); + // //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + // //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + // //filesToTest.add(new File(rootDirectory+"test.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + + List results = compiler.aspTypeInference(); + + for(File f : compiler.sourceFiles.keySet()){ + SourceFile sf = compiler.sourceFiles.get(f); + System.out.println(ASTTypePrinter.print(sf)); + System.out.println(ASTPrinter.print(sf)); + //List results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen + assert results.size()>0; + System.out.println(ResultSetPrinter.print(results.get(0))); + Set insertedTypes = new HashSet<>(); + for(ResultSet resultSet : results){ + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); + assert result.size()>0; + String content = readFile(f.getPath(), StandardCharsets.UTF_8); + for(TypeInsert tip : result){ + insertedTypes.add(tip.insert(content)); + } + } + for(String s : insertedTypes){ + System.out.println(s); + } + } + return new TestResultSet(); + } + + static String readFile(String path, Charset encoding) + throws IOException + { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded, encoding); + } + +} +