Generics Test erzeugt falsches ConstraintSet

This commit is contained in:
JanUlrich 2017-12-20 13:34:34 +01:00
parent e8757a179f
commit 09bdaa6a21
11 changed files with 133 additions and 16 deletions

View File

@ -82,7 +82,7 @@ public class JavaTXCompiler {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for(SourceFile sf : this.sourceFiles.values()) { for(SourceFile sf : this.sourceFiles.values()) {
allClasses.addAll(getAvailableClasses(sf)); //allClasses.addAll(getAvailableClasses(sf));
} }
final ConstraintSet<Pair> cons = getConstraints(); final ConstraintSet<Pair> cons = getConstraints();
@ -98,9 +98,9 @@ public class JavaTXCompiler {
xConsSet.addAll(constraint); xConsSet.addAll(constraint);
} }
//System.out.println(xConsSet); System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure); Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
//System.out.println("RESULT: " + result); System.out.println("RESULT: " + result);
results.addAll(result); results.addAll(result);
} }
return results.stream().map((unifyPairs -> return results.stream().map((unifyPairs ->

View File

@ -1,11 +1,19 @@
package de.dhbwstuttgart.sat.asp; package de.dhbwstuttgart.sat.asp;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.model.*; import de.dhbwstuttgart.sat.asp.model.*;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar; 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.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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -15,25 +23,45 @@ public class ASPGenerator {
ASPWriter writer = new ASPWriter(); ASPWriter writer = new ASPWriter();
private final String asp; private final String asp;
public ASPGenerator(ConstraintSet constraints, Collection<ClassOrInterface> fcClasses){ public ASPGenerator(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses){
asp = toASP(constraints, fcClasses); List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
List<Pair> constraintPairs = new ArrayList<>();
for(Constraint<Pair> constraint : constraints1){
System.out.println(UnifyTypeFactory.convert(constraint));
constraintPairs.addAll(constraint);
}
asp = toASP(constraintPairs, fcClasses);
} }
public String getASP(){ public String getASP(){
return asp; return asp;
} }
private String toASP(ConstraintSet constraintSet, Collection<ClassOrInterface> fcClasses){ private String toASP(List<Pair> constraintSet, Collection<ClassOrInterface> fcClasses){
TypeConverter converter = new TypeConverter();
for(ClassOrInterface cl : fcClasses){ for(ClassOrInterface cl : fcClasses){
Optional<ClassOrInterface> superClass = ASPType superClass = cl.getSuperClass().acceptTV(converter);
fcClasses.stream().filter(c -> c.getSuperClass().getName().equals(cl.getClassName())).findAny(); ASPPairSmaller fcEntry = new ASPPairSmaller(convert(cl), superClass);
//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)));
writer.add(new ASPStatement(fcEntry.toASP())); writer.add(new ASPStatement(fcEntry.toASP()));
} }
for(Pair cons : constraintSet){
writer.add(new ASPStatement(convert(cons).toASP()));
}
return writer.getASPFile(); 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){ private ASPType convert(ClassOrInterface cl){
List<ASPType> paramList = new ArrayList<>(); List<ASPType> paramList = new ArrayList<>();
for(GenericTypeVar gtv : cl.getGenerics()){ for(GenericTypeVar gtv : cl.getGenerics()){
@ -43,7 +71,45 @@ public class ASPGenerator {
return new ASPRefType(toConstant(cl.getClassName()), params); 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(".", "_"); return "c" + name.toString().replace(".", "_");
} }
private class TypeConverter implements TypeVisitor<ASPType>{
@Override
public ASPType visit(RefType type) {
List<ASPType> 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));
}
}
} }

View File

@ -1,7 +1,14 @@
package de.dhbwstuttgart.sat.asp.model; package de.dhbwstuttgart.sat.asp.model;
public class ASPGenericType implements ASPType{ 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 + ")";
} }
} }

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.sat.asp.model; package de.dhbwstuttgart.sat.asp.model;
import de.dhbwstuttgart.sat.asp.ASPGenerator;
import de.dhbwstuttgart.sat.asp.ASPWriter; import de.dhbwstuttgart.sat.asp.ASPWriter;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
@ -19,13 +20,13 @@ public class ASPParameterList {
if(types.size() == 0){ if(types.size() == 0){
name = ASP_PARAMLIST_END_POINTER; name = ASP_PARAMLIST_END_POINTER;
}else{ }else{
name = NameGenerator.makeNewName(); name = newName();
String nextPointer = name; String nextPointer = name;
Iterator<ASPType> it = types.iterator(); Iterator<ASPType> it = types.iterator();
while(it.hasNext()){ while(it.hasNext()){
ASPType t = it.next(); ASPType t = it.next();
String param = nextPointer + "," + t.toString() + ","; String param = nextPointer + "," + t.toString() + ",";
nextPointer = NameGenerator.makeNewName(); nextPointer = newName();
if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER;
param += nextPointer; param += nextPointer;
writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); 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(){ public String toString(){
return name; return name;
} }

View File

@ -1,7 +1,14 @@
package de.dhbwstuttgart.sat.asp.model; package de.dhbwstuttgart.sat.asp.model;
public class ASPTypeVar implements ASPType{ 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 +")";
} }
} }

View File

@ -12,6 +12,7 @@ import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.syntaxtree.type.WildcardType; import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH; import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH;
@ -169,6 +170,10 @@ public class UnifyTypeFactory {
return constraints.map(UnifyTypeFactory::convert); return constraints.map(UnifyTypeFactory::convert);
} }
public static Constraint<UnifyPair> convert(Constraint<Pair> constraint){
return constraint.stream().map(UnifyTypeFactory::convert).collect(Collectors.toCollection(Constraint::new));
}
public static UnifyPair convert(Pair p) { public static UnifyPair convert(Pair p) {
if(p.GetOperator().equals(PairOperator.SMALLERDOT)) { if(p.GetOperator().equals(PairOperator.SMALLERDOT)) {
UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1) UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1)

View File

@ -104,5 +104,8 @@ public class Pair implements Serializable
return eOperator; return eOperator;
} }
public boolean OperatorSmallerDot() {
return eOperator == PairOperator.SMALLERDOT;
}
} }
// ino.end // ino.end

View File

@ -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");
}
}

View File

@ -1,6 +1,7 @@
class Generics<B> { class Generics<B> {
<A extends B> A mt1(A a, B b){ //<A extends B> A mt1(A a, B b){
B mt1(B a, B b){
return mt1(a, a); return mt1(a, a);
} }
} }

View File

@ -2,6 +2,7 @@ package typeinference;
import java.io.File; import java.io.File;
//TODO: Hier gibt es einen Fehler. Das erstellte ConstraintSet stimmt nicht
public class GenericsTest extends JavaTXCompilerTest{ public class GenericsTest extends JavaTXCompilerTest{
public GenericsTest() { public GenericsTest() {
this.fileToTest = new File(rootDirectory+"Generics.jav"); this.fileToTest = new File(rootDirectory+"Generics.jav");