diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 51974ce8..bac09f8b 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -29,7 +29,7 @@ import java.util.stream.Collectors; public class JavaTXCompiler { - final CompilationEnvironment environment; + final CompilationEnvironment environment; public final Map sourceFiles = new HashMap<>(); public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { @@ -38,56 +38,83 @@ public class JavaTXCompiler { public JavaTXCompiler(List sources) throws IOException, ClassNotFoundException { environment = new CompilationEnvironment(sources); - for(File s : sources){ - sourceFiles.put(s,parse(s)); + for (File s : sources) { + sourceFiles.put(s, parse(s)); } } - public List typeInference() throws ClassNotFoundException { + public ConstraintSet getConstraints() throws ClassNotFoundException { List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); - for(SourceFile sf : sourceFiles.values()){ + for (SourceFile sf : sourceFiles.values()) { allClasses.addAll(sf.getClasses()); } List importedClasses = new ArrayList<>(); //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC - for(File forSourceFile : sourceFiles.keySet()) - for(JavaClassName name : sourceFiles.get(forSourceFile).getImports()){ - //TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet + for (File forSourceFile : sourceFiles.keySet()) + for (JavaClassName name : sourceFiles.get(forSourceFile).getImports()) { + //TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet ClassOrInterface importedClass = ASTFactory.createClass( ClassLoader.getSystemClassLoader().loadClass(name.toString())); importedClasses.add(importedClass); } allClasses.addAll(importedClasses); - FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); - final ConstraintSet cons = new TYPE(sourceFiles.values(), allClasses).getConstraints(); + return new TYPE(sourceFiles.values(), allClasses).getConstraints(); + } + + public List getAvailableClasses(SourceFile forSourceFile) throws ClassNotFoundException { + List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); + for (SourceFile sf : sourceFiles.values()) { + allClasses.addAll(sf.getClasses()); + } + List importedClasses = new ArrayList<>(); + for (JavaClassName name : forSourceFile.getImports()) { + //TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet + ClassOrInterface importedClass = ASTFactory.createClass( + ClassLoader.getSystemClassLoader().loadClass(name.toString())); + importedClasses.add(importedClass); + allClasses.addAll(importedClasses); + } + return allClasses; + } + + public List typeInference() throws ClassNotFoundException { + 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)); + } + + final ConstraintSet cons = getConstraints(); + + FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); Set> results = new HashSet<>(); - for(List> xCons : unifyCons.cartesianProduct()){ + for (List> xCons : unifyCons.cartesianProduct()) { Set xConsSet = new HashSet<>(); - for(Constraint constraint : xCons){ + for (Constraint constraint : xCons) { xConsSet.addAll(constraint); } System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); - //System.out.println("RESULT: " + result); + System.out.println("RESULT: " + result); results.addAll(result); } return results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); - } + } - private Map generateTPHMap(ConstraintSet constraints){ + private Map generateTPHMap(ConstraintSet constraints) { HashMap ret = new HashMap<>(); - constraints.map((Pair p)->{ - if(p.TA1 instanceof TypePlaceholder){ - ret.put(((TypePlaceholder)p.TA1).getName(), (TypePlaceholder) p.TA1); + constraints.map((Pair p) -> { + if (p.TA1 instanceof TypePlaceholder) { + ret.put(((TypePlaceholder) p.TA1).getName(), (TypePlaceholder) p.TA1); } - if(p.TA2 instanceof TypePlaceholder){ - ret.put(((TypePlaceholder)p.TA2).getName(), (TypePlaceholder) p.TA2); + if (p.TA2 instanceof TypePlaceholder) { + ret.put(((TypePlaceholder) p.TA2).getName(), (TypePlaceholder) p.TA2); } return null; }); @@ -97,8 +124,8 @@ public class JavaTXCompiler { private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException { CompilationUnitContext tree = JavaTXParser.parse(sourceFile); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile), new GenericsRegistry(null)); - SourceFile ret = generator.convert(tree, environment.packageCrawler); + SourceFile ret = generator.convert(tree, environment.packageCrawler); return ret; } - -} + +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 2cf41b43..d54b682a 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -43,7 +43,7 @@ public class TypeGenerator { throw new NotImplementedException(); }else if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ - System.out.println(unannTypeContext.getText()); + //System.out.println(unannTypeContext.getText()); throw new NotImplementedException(); }else if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){ diff --git a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java index 26ab0957..1b942ad0 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java @@ -1,24 +1,115 @@ package de.dhbwstuttgart.sat.asp; +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import java.sql.Ref; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.Optional; public class ASPGenerator { - public static String toASP(ConstraintSet constraintSet, Collection fcClasses){ - String ret = ""; - for(ClassOrInterface cl : fcClasses){ - String className = toConstant(cl.getClassName()); - String superClassName = toConstant(cl.getSuperClass().getName()); + ASPWriter writer = new ASPWriter(); + private final String asp; + public ASPGenerator(ConstraintSet constraints, Collection fcClasses){ + List> constraints1 = constraints.cartesianProduct().iterator().next(); + List constraintPairs = new ArrayList<>(); + for(Constraint constraint : constraints1){ + System.out.println(UnifyTypeFactory.convert(constraint)); + constraintPairs.addAll(constraint); } - return ret; + asp = toASP(constraintPairs, fcClasses); } + public String getASP(){ + return asp; + } + + private String toASP(List constraintSet, Collection fcClasses){ + TypeConverter converter = new TypeConverter(); + for(ClassOrInterface cl : fcClasses){ + ASPType superClass = cl.getSuperClass().acceptTV(converter); + ASPPairSmaller fcEntry = new ASPPairSmaller(convert(cl), superClass); + writer.add(new ASPStatement(fcEntry.toASP())); + } + for(Pair cons : constraintSet){ + writer.add(new ASPStatement(convert(cons).toASP())); + } + + return writer.getASPFile(); + } + + private ASPPair convert(Pair pair){ + TypeConverter converter = new TypeConverter(); + ASPType ls = pair.TA1.acceptTV(converter); + ASPType rs = pair.TA2.acceptTV(converter); + if(pair.OperatorEqual()){ + return new ASPPairEquals(ls, rs); + }else if(pair.OperatorSmallerDot()){ + return new ASPPairSmallerDot(ls, rs); + }else throw new NotImplementedException(); + } + + private ASPType convert(ClassOrInterface cl){ + List paramList = new ArrayList<>(); + for(GenericTypeVar gtv : cl.getGenerics()){ + paramList.add(new ASPGenericType(toConstant(gtv.getName()))); + } + ASPParameterList params = new ASPParameterList(paramList, writer); + return new ASPRefType(toConstant(cl.getClassName()), params); + } public static String toConstant(JavaClassName name){ + return toConstant(name.toString().replace(".", "_")); + } + + public static String toConstant(String name){ return "c" + name.toString().replace(".", "_"); } + + private class TypeConverter implements TypeVisitor{ + + @Override + public ASPType visit(RefType type) { + List paramList = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric gtv : type.getParaList()){ + paramList.add(gtv.acceptTV(this)); + } + ASPParameterList params = new ASPParameterList(paramList, writer); + return new ASPRefType(toConstant(type.getName()), params); + } + + @Override + public ASPType visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public ASPType visit(TypePlaceholder typePlaceholder) { + return new ASPTypeVar(toConstant(typePlaceholder.getName())); + } + + @Override + public ASPType visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public ASPType visit(GenericRefType genericRefType) { + return new ASPRefType(toConstant(genericRefType.getName()), + new ASPParameterList(new ArrayList<>(), writer)); + } + } } diff --git a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/ASPWriter.java new file mode 100644 index 00000000..4d33cbbf --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/ASPWriter.java @@ -0,0 +1,24 @@ +package de.dhbwstuttgart.sat.asp; + +import de.dhbwstuttgart.sat.asp.model.ASPRefType; +import de.dhbwstuttgart.sat.asp.model.ASPStatement; +import de.dhbwstuttgart.sat.asp.model.ASPType; + +import java.util.HashSet; + +public class ASPWriter { + + private HashSet content = new HashSet<>(); + + public void add(ASPStatement stmt){ + content.add(stmt); + } + + public String getASPFile(){ + String ret = ""; + for(ASPStatement statement : content){ + ret += statement.getASP() + ".\n"; + } + return ret; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java new file mode 100644 index 00000000..26e6ea02 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPGenericType implements ASPType{ + public static final String ASP_GENERIC_TYPE_NAME = "genericType"; + private final String name; + + public ASPGenericType(String name){ + this.name = name; + } + + public String toString(){ + return ASP_GENERIC_TYPE_NAME + "(" + name + ")"; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java new file mode 100644 index 00000000..bbd3ff3f --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java @@ -0,0 +1,21 @@ +package de.dhbwstuttgart.sat.asp.model; + +public abstract class ASPPair { + public final ASPType leftSide; + public final ASPType rightSide; + + public ASPPair(ASPType ls, ASPType rs){ + this.leftSide = ls; + this.rightSide = rs; + } + + public String toASP(){ + return this.getRuleName() + "(" + leftSide + ","+ rightSide + ")"; + } + + public String toString(){ + return toASP(); + } + + protected abstract String getRuleName(); +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java new file mode 100644 index 00000000..b67920bd --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java @@ -0,0 +1,13 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPPairEquals extends ASPPair{ + private final static String ASP_PAIR_EQUALS_NAME = "equals"; + public ASPPairEquals(ASPType ls, ASPType rs){ + super(ls, rs); + } + + @Override + protected String getRuleName() { + return ASP_PAIR_EQUALS_NAME; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java new file mode 100644 index 00000000..882054cc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java @@ -0,0 +1,13 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPPairSmaller extends ASPPair{ + private final static String ASP_PAIR_SMALLER_NAME = "smaller"; + public ASPPairSmaller(ASPType ls, ASPType rs){ + super(ls, rs); + } + + @Override + protected String getRuleName() { + return ASP_PAIR_SMALLER_NAME; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java new file mode 100644 index 00000000..0e6598c1 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java @@ -0,0 +1,13 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPPairSmallerDot extends ASPPair{ + private final static String ASP_PAIR_SMALLER_NAME = "smallerDot"; + public ASPPairSmallerDot(ASPType ls, ASPType rs){ + super(ls, rs); + } + + @Override + protected String getRuleName() { + return ASP_PAIR_SMALLER_NAME; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java new file mode 100644 index 00000000..f0c06744 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java @@ -0,0 +1,45 @@ +package de.dhbwstuttgart.sat.asp.model; + +import de.dhbwstuttgart.sat.asp.ASPGenerator; +import de.dhbwstuttgart.sat.asp.ASPWriter; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +public class ASPParameterList { + private final static String ASP_PARAMLIST_NAME = "param"; + private final static String ASP_PARAMLIST_END_POINTER = "null"; + public final String name; + private final List types; + + public ASPParameterList(List types, ASPWriter writer){ + this.types = types; + if(types.size() == 0){ + name = ASP_PARAMLIST_END_POINTER; + }else{ + name = newName(); + String nextPointer = name; + Iterator it = types.iterator(); + while(it.hasNext()){ + ASPType t = it.next(); + String param = nextPointer + "," + t.toString() + ","; + nextPointer = newName(); + if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; + param += nextPointer; + writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); + //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); + } + } + } + + private String newName() { + return ASPGenerator.toConstant(NameGenerator.makeNewName()); + } + + public String toString(){ + return name; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java new file mode 100644 index 00000000..ba318876 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPRefType implements ASPType { + public static final String ASP_TYPE = "type"; + private final ASPParameterList params; + private final String name; + + public ASPRefType(String name, ASPParameterList params){ + this.name = name; + this.params = params; + } + + public ASPParameterList getParams() { + return params; + } + + public String toString(){ + return ASP_TYPE + "(" + name +"," + params.name + ")"; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java new file mode 100644 index 00000000..8e4a1d35 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java @@ -0,0 +1,27 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPStatement { + private final String stmt; + public ASPStatement(String stmt) { + this.stmt = stmt; + } + + public String toString(){ + return stmt; + } + + @Override + public int hashCode() { + return stmt.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof ASPStatement)return stmt.equals(((ASPStatement) obj).stmt); + return false; + } + + public String getASP() { + return stmt; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPType.java index 4de8759d..e703596c 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPType.java @@ -1,9 +1,4 @@ package de.dhbwstuttgart.sat.asp.model; -import java.util.List; - -public class ASPType { - public ASPType(String name, List params){ - - } +public interface ASPType { } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java new file mode 100644 index 00000000..e2f87636 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.sat.asp.model; + +public class ASPTypeVar implements ASPType{ + private final String name; + + public ASPTypeVar(String name){ + this.name = name; + } + + @Override + public String toString() { + return "typeVar("+ name +")"; + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 361a7583..6e48c797 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -12,6 +12,7 @@ import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.WildcardType; +import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH; @@ -169,6 +170,10 @@ public class UnifyTypeFactory { return constraints.map(UnifyTypeFactory::convert); } + public static Constraint convert(Constraint constraint){ + return constraint.stream().map(UnifyTypeFactory::convert).collect(Collectors.toCollection(Constraint::new)); + } + public static UnifyPair convert(Pair p) { if(p.GetOperator().equals(PairOperator.SMALLERDOT)) { UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1) diff --git a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java index c7e1f6b6..ab0cb3ea 100644 --- a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java +++ b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java @@ -104,5 +104,8 @@ public class Pair implements Serializable return eOperator; } + public boolean OperatorSmallerDot() { + return eOperator == PairOperator.SMALLERDOT; + } } // ino.end diff --git a/src/de/dhbwstuttgart/typeinference/result/ResultSet.java b/src/de/dhbwstuttgart/typeinference/result/ResultSet.java index 9c1982ce..22da67fc 100644 --- a/src/de/dhbwstuttgart/typeinference/result/ResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/result/ResultSet.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.typeinference.result; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.*; import java.util.HashSet; @@ -15,6 +16,7 @@ public class ResultSet { public ResolvedType resolveType(RefTypeOrTPHOrWildcardOrGeneric type) { if(type instanceof TypePlaceholder) return new Resolver(this).resolve((TypePlaceholder)type); + if(type instanceof GenericRefType)new ResolvedType(type, new HashSet<>()); if(type instanceof RefType){ RelatedTypeWalker related = new RelatedTypeWalker(null, this); type.accept(related); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index f40ecb2f..dc90847b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -153,8 +153,8 @@ public final class TypeParams implements Iterable{ return false; for(int i = 0; i < this.size(); i++){ - if(this.get(i) == null) - System.out.print("s"); + //if(this.get(i) == null) + //System.out.print("s"); } for(int i = 0; i < this.size(); i++) if(!(this.get(i).equals(other.get(i)))) diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java new file mode 100644 index 00000000..5074e4f3 --- /dev/null +++ b/test/asp/typeinference/ASPTest.java @@ -0,0 +1,68 @@ +package asp.typeinference; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.sat.asp.ASPGenerator; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class ASPTest { + + public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; + private static final List filesToTest = new ArrayList<>(); + protected File fileToTest = null; + + public ASPTest(){ + } + + + @Test + public void test() throws IOException, ClassNotFoundException { + if(fileToTest != null)filesToTest.add(fileToTest); + else return; + //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + //filesToTest.add(new File(rootDirectory+"test.jav")); + filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); + //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")); + JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + List allClasses = new ArrayList<>(); + for(SourceFile sf : compiler.sourceFiles.values()) { + //allClasses.addAll(compiler.getAvailableClasses(sf)); + } + for(SourceFile sf : compiler.sourceFiles.values()) { + allClasses.addAll(sf.getClasses()); + } + + final ConstraintSet cons = compiler.getConstraints(); + ASPGenerator generator = new ASPGenerator(cons, allClasses); + System.out.println(generator.getASP()); + } + + static String readFile(String path, Charset encoding) + throws IOException + { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded, encoding); + } + +} + diff --git a/test/asp/typeinference/GenericsTest.java b/test/asp/typeinference/GenericsTest.java new file mode 100644 index 00000000..b0f0330a --- /dev/null +++ b/test/asp/typeinference/GenericsTest.java @@ -0,0 +1,9 @@ +package asp.typeinference; + +import java.io.File; + +public class GenericsTest extends ASPTest { + public GenericsTest() { + this.fileToTest = new File(rootDirectory+"Generics.jav"); + } +} \ No newline at end of file diff --git a/test/asp/typeinference/VectorTest.java b/test/asp/typeinference/VectorTest.java new file mode 100644 index 00000000..77ff4959 --- /dev/null +++ b/test/asp/typeinference/VectorTest.java @@ -0,0 +1,9 @@ +package asp.typeinference; + +import java.io.File; + +public class VectorTest extends ASPTest { + public VectorTest() { + this.fileToTest = new File(rootDirectory+"Vector.jav"); + } +} \ No newline at end of file diff --git a/test/javFiles/Generics.jav b/test/javFiles/Generics.jav index 958025e6..c76b40aa 100644 --- a/test/javFiles/Generics.jav +++ b/test/javFiles/Generics.jav @@ -1,6 +1,7 @@ class Generics { - A mt1(A a, B b){ + // A mt1(A a, B b){ + B mt1(B a, B b){ return mt1(a, a); } } diff --git a/test/typeinference/GenericsTest.java b/test/typeinference/GenericsTest.java index 3811883d..abcef013 100644 --- a/test/typeinference/GenericsTest.java +++ b/test/typeinference/GenericsTest.java @@ -2,6 +2,7 @@ package typeinference; import java.io.File; +//TODO: Hier gibt es einen Fehler. Das erstellte ConstraintSet stimmt nicht public class GenericsTest extends JavaTXCompilerTest{ public GenericsTest() { this.fileToTest = new File(rootDirectory+"Generics.jav"); diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 8e60c803..fbbed9af 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -1,11 +1,23 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.ASPGenerator; +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.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; +import de.dhbwstuttgart.typeinference.unify.TypeUnify; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import org.junit.Test; import java.io.File; @@ -17,8 +29,10 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class JavaTXCompilerTest { @@ -28,11 +42,10 @@ public class JavaTXCompilerTest { public JavaTXCompilerTest(){ } - @Test - public void test() throws IOException, java.lang.ClassNotFoundException { + public void test() throws IOException, ClassNotFoundException { if(fileToTest != null)filesToTest.add(fileToTest); - else return; + else return; //filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); //filesToTest.add(new File(rootDirectory+"test.jav")); @@ -47,6 +60,7 @@ public class JavaTXCompilerTest { //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + List results = compiler.typeInference(); for(File f : compiler.sourceFiles.keySet()){ @@ -54,14 +68,18 @@ public class JavaTXCompilerTest { System.out.println(ASTTypePrinter.print(sf)); //List results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen assert results.size()>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){ - System.out.println(tip.insert(content)); + insertedTypes.add(tip.insert(content)); } } + for(String s : insertedTypes){ + System.out.println(s); + } } }