diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java new file mode 100644 index 00000000..867acd58 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -0,0 +1,25 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPGencayRule { + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("sub"), + ASP_PAIR_SMALLER_DOT_NAME("subEq"), + ASP_PARAMLIST_NAME("paramEq"), + ASP_FC_PARAMLIST_NAME("param"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("typeEq"), + ASP_FCTYPE("type"), + ASP_TYPE_VAR("var"), + ASP_GENERIC_VAR("pph"); + + private final String text; + + private ASPGencayRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java new file mode 100644 index 00000000..5a56c7a7 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -0,0 +1,155 @@ +package de.dhbwstuttgart.sat.asp.writer; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPGencayRule; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; +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.PairOperator; + +import java.sql.Ref; +import java.util.*; + +public class ASPGencayFactory implements TypeVisitor { + + ASPWriter writer = new ASPWriter(); + boolean isFCType = false; + + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPGencayFactory factory = new ASPGencayFactory(); + factory.convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + + return factory.writer.getASPFile(); + } + + private void convertFC(Collection classes) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes); + isFCType = true; + for(Pair fcp : fc){ + generateTheta((RefType) fcp.TA1); + generateTheta((RefType) fcp.TA2); + convertPair(fcp); + } + isFCType = false; + } + + private void generateTheta(RefType t){ + String statement = "theta"+t.getParaList().size() + "("; + statement += ASPStringConverter.toConstant(t.getName()); + statement+=")"; + writer.add(new ASPStatement(statement)); + } + + private void convertPair(Pair p){ + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); + ASPStatement pairStmt = null; + if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_EQUALS_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); + }else throw new NotImplementedException(); + writer.add(pairStmt); + } + + private ASPStatement makeStatement(String rule, String... params){ + String stmt = rule + "("; + for(String param : params){ + stmt += param + ","; + } + stmt = stmt.substring(0,stmt.length()-1); + stmt += ")"; + return new ASPStatement(stmt); + } + + private String convertParameterlist(List pointers){ + if(pointers.size()==1)return pointers.get(0); + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Iterator it = pointers.iterator(); + String p = pointer; + String paramname = ASPGencayRule.ASP_PARAMLIST_NAME.toString(); + if(this.isFCType)paramname = ASPGencayRule.ASP_FC_PARAMLIST_NAME.toString(); + if(!it.hasNext()){ + return ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + } + while (it.hasNext()){ + ASPStatement stmt; + String type = it.next(); + String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + if(it.hasNext()){ + stmt = makeStatement(paramname, p, type, nextP); + }else{ + stmt = makeStatement(paramname, p, type, + ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString()); + } + p = nextP; + writer.add(stmt); + } + return pointer; + } + + @Override + public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + List params = new ArrayList<>(); + params = generateParameter(refType); + params.remove(0); //Das erste ist der eigentliche Typ kein parameter + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); + writer.add(stmt); + return pointer; + } + + /** + * Erstellt die Parameterliste, wobei type auch schon als Parameter betrachtet wird. + */ + List generateParameter(RefType type){ + List ret = new ArrayList<>(); + ret.add(ASPStringConverter.toConstant(type.getName())); + for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){ + if(param instanceof RefType){ + ret.addAll(generateParameter((RefType) param)); + }else if(param instanceof TypePlaceholder){ + ret.add(param.acceptTV(this)); + }else throw new NotImplementedException(); + } + return ret; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + String name = ASPStringConverter.toConstant(typePlaceholder.getName()); + ASPStatement stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); + writer.add(stmt); + return name; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + } +} diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index f4422ff7..6f69d639 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -81,7 +81,8 @@ public class UnifyWithoutWildcards { return new ArrayList<>(); } } - private Collection getInvolvedTPHS(ConstraintSet toTest) { + + protected Collection getInvolvedTPHS(ConstraintSet toTest) { List ret = new ArrayList<>(); toTest.map((Pair p)-> { ret.addAll(p.TA1.acceptTV(new TPHExtractor())); diff --git a/test/asp/gencay/GeneratorTest.java b/test/asp/gencay/GeneratorTest.java new file mode 100644 index 00000000..d7e457b2 --- /dev/null +++ b/test/asp/gencay/GeneratorTest.java @@ -0,0 +1,50 @@ +package asp.gencay; + +import asp.UnifyWithoutWildcards; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; +import de.dhbwstuttgart.sat.asp.writer.ASPGencayFactory; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.*; +import java.util.concurrent.ArrayBlockingQueue; + +public class GeneratorTest extends UnifyWithoutWildcards{ + @Test + public void matrix() throws InterruptedException, IOException, ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); + 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()); + System.out.println(resultSet); + } + + private Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createClass(Matrix.class)); + //ret.add(ASTFactory.createObjectClass()); + //ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } +} + +class Matrix extends Vector> {} \ No newline at end of file