From a71c1f38935785918a3e5b595c5a64be6dee4ecf Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 5 Jul 2018 16:33:20 +0200 Subject: [PATCH] Gencoy ASP Generator fertig stellen --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 7 +++++- .../sat/asp/writer/ASPFactory.java | 25 +++++++++++++++++++ ...actory.java => ASPFactoryAlternative.java} | 10 +++++--- test/asp/gencay/GeneratorTest.java | 14 +++++------ 4 files changed, 43 insertions(+), 13 deletions(-) rename src/de/dhbwstuttgart/sat/asp/writer/{ASPGencayFactory.java => ASPFactoryAlternative.java} (96%) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 92c1d98b..80c11870 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -133,12 +133,17 @@ public class JavaTXCompiler { } public List aspTypeInference() throws ClassNotFoundException, IOException, InterruptedException { - List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); + Collection 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()); } + HashMap classes = new HashMap<>(); + for(ClassOrInterface cl : allClasses){ + classes.put(cl.getClassName().toString(), cl); + } + allClasses = classes.values(); final ConstraintSet cons = getConstraints(); diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index adf75ec0..9755945b 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -1,12 +1,16 @@ package de.dhbwstuttgart.sat.asp.writer; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; +import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -94,9 +98,30 @@ public class ASPFactory implements TypeVisitor{ protected void convertFC(Collection classes) throws ClassNotFoundException { Collection fc = FCGenerator.toFC(classes); + HashMap set = new HashMap<>(); for(Pair fcp : fc){ + StringBuilder output = new StringBuilder(); + OutputGenerator generator = new OutputGenerator(output); + fcp.TA1.accept(generator); + output.append("<"); + fcp.TA2.accept(generator); + set.put(output.toString(), fcp); + } + for(Pair fcp : set.values()){ convertPair(fcp); } + for(ClassOrInterface cl : classes){ + RefType t = fromClassOrInterface(cl); + convertPair(new Pair(t,t,PairOperator.SMALLER)); + } + } + + private RefType fromClassOrInterface(ClassOrInterface cl){ + List params = new ArrayList<>(); + for(GenericTypeVar gtv : cl.getGenerics()){ + params.add(TypePlaceholder.fresh(new NullToken())); + } + return new RefType(cl.getClassName(), params, new NullToken()); } protected void convertPair(Pair p){ diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactoryAlternative.java similarity index 96% rename from src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPFactoryAlternative.java index 8c7e3ad5..2927e667 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactoryAlternative.java @@ -15,7 +15,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import java.util.*; -public class ASPGencayFactory implements TypeVisitor { +public class ASPFactoryAlternative implements TypeVisitor { /* TODO: * Alle TPHs müssen als var(tph) definiert sein * Wenn es eine Variable ist, dann direkt in die type-Regel schreiben: type(p, type, tph) @@ -56,7 +56,7 @@ public class ASPGencayFactory implements TypeVisitor { } public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ - ASPGencayFactory factory = new ASPGencayFactory(); + ASPFactoryAlternative factory = new ASPFactoryAlternative(); factory.convertFC(fcClasses); List> constraints1 = constraints.cartesianProduct().iterator().next(); for(Constraint constraint : constraints1){ @@ -148,8 +148,10 @@ public class ASPGencayFactory implements TypeVisitor { String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); currentFCTypePointer = pointer; String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); - if(refType.getParaList().size() == 1 && (refType.getParaList().get(0) instanceof TypePlaceholder)){ - TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); + if(refType.getParaList().size() == 1 + && (refType.getParaList().get(0) instanceof TypePlaceholder + || refType.getParaList().get(0) instanceof WildcardType)){ + RefTypeOrTPHOrWildcardOrGeneric typePlaceholder = refType.getParaList().get(0); paramPointer = typePlaceholder.acceptTV(this); }else{ List params = null; diff --git a/test/asp/gencay/GeneratorTest.java b/test/asp/gencay/GeneratorTest.java index 488c8629..5c3f0a9f 100644 --- a/test/asp/gencay/GeneratorTest.java +++ b/test/asp/gencay/GeneratorTest.java @@ -3,7 +3,7 @@ package asp.gencay; import asp.UnifyWithoutWildcards; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.writer.ASPGencayFactory; +import de.dhbwstuttgart.sat.asp.writer.ASPFactoryAlternative; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; @@ -12,12 +12,9 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import de.dhbwstuttgart.typeinference.unify.model.WildcardType; import org.junit.Test; -import java.lang.reflect.Type; import java.util.*; public class GeneratorTest extends UnifyWithoutWildcards{ @@ -29,7 +26,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{ RefType t1 = new RefType(new JavaClassName("java.util.List"), list1, new NullToken()); RefType t2 = new RefType(new JavaClassName("java.util.List"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - String resultSet = ASPGencayFactory.generateASP(testSet, + String resultSet = ASPFactoryAlternative.generateASP(testSet, new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class)))); System.out.println(resultSet); } @@ -42,7 +39,7 @@ public class GeneratorTest extends 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)); - String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC()); + String resultSet = ASPFactoryAlternative.generateASP(testSet, this.getFC()); System.out.println(resultSet); } @@ -65,7 +62,7 @@ public class GeneratorTest extends UnifyWithoutWildcards{ fc.add(ASTFactory.createClass(AbstractList.class)); fc.add(ASTFactory.createClass(Integer.class)); - String resultSet = ASPGencayFactory.generateASP(testSet, fc); + String resultSet = ASPFactoryAlternative.generateASP(testSet, fc); System.out.println(resultSet); } @@ -99,7 +96,8 @@ public class GeneratorTest extends UnifyWithoutWildcards{ fc.add(ASTFactory.createClass(Integer.class)); fc.add(ASTFactory.createClass(Number.class)); - String resultSet = ASPGencayFactory.generateASP(testSet, fc); + String resultSet = ASPFactoryAlternative.generateASP(testSet, fc); + //String resultSet = ASPFactoryAlternative.generateASP(testSet, new HashSet<>()); System.out.println(resultSet); }