ASP Factory für Algorithmus von Herr Gencay

This commit is contained in:
JanUlrich 2018-03-23 16:54:32 +01:00
parent 41c87e3085
commit daf684deef
4 changed files with 232 additions and 1 deletions

View File

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

View File

@ -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<String> {
ASPWriter writer = new ASPWriter();
boolean isFCType = false;
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
ASPGencayFactory factory = new ASPGencayFactory();
factory.convertFC(fcClasses);
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
for(Constraint<Pair> constraint : constraints1){
for(Pair p : constraint){
factory.convertPair(p);
}
}
return factory.writer.getASPFile();
}
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> 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<String> pointers){
if(pointers.size()==1)return pointers.get(0);
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
Iterator<String> 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<String> 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<String> generateParameter(RefType type){
List<String> 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();
}
}

View File

@ -81,7 +81,8 @@ public class UnifyWithoutWildcards {
return new ArrayList<>(); return new ArrayList<>();
} }
} }
private Collection<TypePlaceholder> getInvolvedTPHS(ConstraintSet<Pair> toTest) {
protected Collection<TypePlaceholder> getInvolvedTPHS(ConstraintSet<Pair> toTest) {
List<TypePlaceholder> ret = new ArrayList<>(); List<TypePlaceholder> ret = new ArrayList<>();
toTest.map((Pair p)-> { toTest.map((Pair p)-> {
ret.addAll(p.TA1.acceptTV(new TPHExtractor())); ret.addAll(p.TA1.acceptTV(new TPHExtractor()));

View File

@ -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<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
List<RefTypeOrTPHOrWildcardOrGeneric> 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<ClassOrInterface> getFC() {
Set<ClassOrInterface> 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<Vector<Integer>> {}