Gencoy ASP Generator fertig stellen

This commit is contained in:
JanUlrich 2018-07-05 16:33:20 +02:00
parent 1f8421db27
commit a71c1f3893
4 changed files with 43 additions and 13 deletions

View File

@ -133,12 +133,17 @@ public class JavaTXCompiler {
}
public List<ResultSet> aspTypeInference() throws ClassNotFoundException, IOException, InterruptedException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
Collection<ClassOrInterface> 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<String, ClassOrInterface> classes = new HashMap<>();
for(ClassOrInterface cl : allClasses){
classes.put(cl.getClassName().toString(), cl);
}
allClasses = classes.values();
final ConstraintSet<Pair> cons = getConstraints();

View File

@ -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<String>{
protected void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Collection<Pair> fc = FCGenerator.toFC(classes);
HashMap<String, Pair> 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<RefTypeOrTPHOrWildcardOrGeneric> 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){

View File

@ -15,7 +15,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import java.util.*;
public class ASPGencayFactory implements TypeVisitor<String> {
public class ASPFactoryAlternative implements TypeVisitor<String> {
/* 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<String> {
}
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
ASPGencayFactory factory = new ASPGencayFactory();
ASPFactoryAlternative factory = new ASPFactoryAlternative();
factory.convertFC(fcClasses);
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
for(Constraint<Pair> constraint : constraints1){
@ -148,8 +148,10 @@ public class ASPGencayFactory implements TypeVisitor<String> {
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<String> params = null;

View File

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