From 09bdaa6a218dbce8bfee4cdd15e9a3bb1a2b808e Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 20 Dec 2017 13:34:34 +0100 Subject: [PATCH] Generics Test erzeugt falsches ConstraintSet --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 6 +- .../dhbwstuttgart/sat/asp/ASPGenerator.java | 82 +++++++++++++++++-- .../sat/asp/model/ASPGenericType.java | 9 +- .../sat/asp/model/ASPPairSmallerDot.java | 13 +++ .../sat/asp/model/ASPParameterList.java | 9 +- .../sat/asp/model/ASPTypeVar.java | 9 +- .../syntaxtree/factory/UnifyTypeFactory.java | 5 ++ .../typeinference/constraints/Pair.java | 3 + test/asp/typeinference/GenericsTest.java | 9 ++ test/javFiles/Generics.jav | 3 +- test/typeinference/GenericsTest.java | 1 + 11 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java create mode 100644 test/asp/typeinference/GenericsTest.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index cce811074..bac09f8b6 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -82,7 +82,7 @@ public class JavaTXCompiler { 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(getAvailableClasses(sf)); } final ConstraintSet cons = getConstraints(); @@ -98,9 +98,9 @@ public class JavaTXCompiler { xConsSet.addAll(constraint); } - //System.out.println(xConsSet); + 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 -> diff --git a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java index 07e0e8dd9..1b942ad0f 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java @@ -1,11 +1,19 @@ 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; @@ -15,25 +23,45 @@ public class ASPGenerator { ASPWriter writer = new ASPWriter(); private final String asp; - public ASPGenerator(ConstraintSet constraints, Collection fcClasses){ - asp = toASP(constraints, fcClasses); + 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); + } + asp = toASP(constraintPairs, fcClasses); } public String getASP(){ return asp; } - private String toASP(ConstraintSet constraintSet, Collection fcClasses){ + private String toASP(List constraintSet, Collection fcClasses){ + TypeConverter converter = new TypeConverter(); for(ClassOrInterface cl : fcClasses){ - Optional superClass = - fcClasses.stream().filter(c -> c.getSuperClass().getName().equals(cl.getClassName())).findAny(); - //Für den Fall das es keinen Supertyp in den fcClasses gibt, wird die Klasse immer noch als ihr eigener Subtyp angefügt: - ASPPairSmaller fcEntry = new ASPPairSmaller(convert(cl), convert(superClass.orElse(cl))); + 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()){ @@ -43,7 +71,45 @@ public class ASPGenerator { return new ASPRefType(toConstant(cl.getClassName()), params); } - private String toConstant(JavaClassName name){ + 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/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java index 729d678a6..26e6ea02a 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java @@ -1,7 +1,14 @@ package de.dhbwstuttgart.sat.asp.model; public class ASPGenericType implements ASPType{ - public ASPGenericType(String name){ + 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/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java new file mode 100644 index 000000000..0e6598c1a --- /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 index b38f8a38b..f0c067444 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.sat.asp.model; +import de.dhbwstuttgart.sat.asp.ASPGenerator; import de.dhbwstuttgart.sat.asp.ASPWriter; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; @@ -19,13 +20,13 @@ public class ASPParameterList { if(types.size() == 0){ name = ASP_PARAMLIST_END_POINTER; }else{ - name = NameGenerator.makeNewName(); + name = newName(); String nextPointer = name; Iterator it = types.iterator(); while(it.hasNext()){ ASPType t = it.next(); String param = nextPointer + "," + t.toString() + ","; - nextPointer = NameGenerator.makeNewName(); + nextPointer = newName(); if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; param += nextPointer; writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); @@ -34,6 +35,10 @@ public class ASPParameterList { } } + private String newName() { + return ASPGenerator.toConstant(NameGenerator.makeNewName()); + } + public String toString(){ return name; } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java index 16b79f91e..e2f87636b 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java @@ -1,7 +1,14 @@ package de.dhbwstuttgart.sat.asp.model; public class ASPTypeVar implements ASPType{ - public ASPTypeVar(String name){ + 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 66b750a40..e5eea03ce 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 c7e1f6b65..ab0cb3ea7 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/test/asp/typeinference/GenericsTest.java b/test/asp/typeinference/GenericsTest.java new file mode 100644 index 000000000..b0f0330a9 --- /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/javFiles/Generics.jav b/test/javFiles/Generics.jav index 958025e61..c76b40aa5 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 3811883d8..abcef0133 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");