From 000c16b90779cddf914bc930d1f515aafbe0ee3c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 27 Mar 2018 23:56:12 +0200 Subject: [PATCH] Gencoy ASP Generator implementieren --- .../sat/asp/model/ASPGencayRule.java | 3 +- .../sat/asp/writer/ASPGencayFactory.java | 128 ++++++++++++++---- .../sat/asp/writer/ASPWriter.java | 6 + 3 files changed, 111 insertions(+), 26 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java index 867acd58..e16592cc 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -10,7 +10,8 @@ public enum ASPGencayRule { ASP_TYPE("typeEq"), ASP_FCTYPE("type"), ASP_TYPE_VAR("var"), - ASP_GENERIC_VAR("pph"); + ASP_GENERIC_VAR("pph"), + ASP_PARAMLIST_ORDER("paramOrder"); private final String text; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java index 77ea36f3..11408c25 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -22,11 +22,39 @@ public class ASPGencayFactory implements TypeVisitor { * Für die FCTypen eindeutige Namen für die pph-Regeln * (ergibt sich von selbst, weil man einfach den Namen der TPH in der FC verwenden kann) * paramOrder wird benötigt! - * + * Nur bei parameterlisten > 1 + * paramOrder(paralistPointer, parameter, num) + * (ähnlich meiner paramNum) + * Trennung von FC und Eq: + * sub -> type -> param + * ...Eq -> typeEq -> paramEq + * type..(_,_,_p): p kann sein: + * 1. Variable + * 2. ParameterPointer + * 3. null */ ASPWriter writer = new ASPWriter(); boolean isFCType = false; + private static List generateVar(ConstraintSet constraints){ + List ret = new ArrayList<>(); + for(TypePlaceholder tph : getInvolvedTPHS(constraints)){ + ret.add(makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), + ASPStringConverter.toConstant(tph.getName()))); + } + return ret; + } + + protected static Collection getInvolvedTPHS(ConstraintSet toTest) { + List ret = new ArrayList<>(); + toTest.map((Pair p)-> { + ret.addAll(p.TA1.acceptTV(new TPHExtractor())); + ret.addAll(p.TA2.acceptTV(new TPHExtractor())); + return p; + }); + return ret; + } + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ ASPGencayFactory factory = new ASPGencayFactory(); factory.convertFC(fcClasses); @@ -36,6 +64,7 @@ public class ASPGencayFactory implements TypeVisitor { factory.convertPair(p); } } + factory.writer.addAll(generateVar(constraints)); return factory.writer.getASPFile(); } @@ -58,8 +87,8 @@ public class ASPGencayFactory implements TypeVisitor { } private void convertPair(Pair p){ - String ls = generateFCType((RefType) p.TA1); - String rs = generateFCType((RefType) p.TA2); + 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); @@ -71,25 +100,7 @@ public class ASPGencayFactory implements TypeVisitor { writer.add(pairStmt); } - private String generateFCType(RefType refType){ - String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); - String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); - if(refType.getParaList().size() > 1)throw new NotImplementedException(); - if(refType.getParaList().size() > 0){ - TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); - String pphName = ASPStringConverter.toConstant(typePlaceholder.getName()); - ASPStatement paramStmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), - pointer, pphName); - writer.add(paramStmt); - } - String typeName = ASPStringConverter.toConstant(refType.getName()); - String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); - ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); - writer.add(stmt); - return pointer; - } - - private ASPStatement makeStatement(String rule, String... params){ + private static ASPStatement makeStatement(String rule, String... params){ String stmt = rule + "("; for(String param : params){ stmt += param + ","; @@ -125,8 +136,38 @@ public class ASPGencayFactory implements TypeVisitor { return pointer; } + //Wird zum erstellen der pph(..) Regeln gebraucht + private String currentFCTypePointer = ""; @Override public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + currentFCTypePointer = pointer; + String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + if(refType.getParaList().size() == 1 ){ + if(refType.getParaList().get(0) instanceof TypePlaceholder){ + TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); + paramPointer = typePlaceholder.acceptTV(this); + } + }else{ + List params = null; + params = generateParameter(refType); + params.remove(0); + paramPointer = convertParameterlist(params); + if(refType.getParaList().size()>1){ + //paramOrder generieren: + for(String param : params) { + ASPStatement pOstmt = makeStatement(ASPGencayRule.ASP_PARAMLIST_ORDER.toString(), + paramPointer, param); + writer.add(pOstmt); + } + } + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); + writer.add(stmt); + return pointer; + /* String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); List params = null; params = generateParameter(refType); @@ -136,12 +177,16 @@ public class ASPGencayFactory implements TypeVisitor { ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); writer.add(stmt); return pointer; + */ } /** * Erstellt die Parameterliste, wobei type auch schon als Parameter betrachtet wird. + * Die RefTypes werden nicht zu extra type-Regeln umgewandelt. Es wird nur ihr Name als Konstante benutzt + * Das funktioniert, weil nacher das ParamOrder zuteilt, welche Typen zusammenhängen. + * Diese funktion nur verwenden, wenn auch ein paramOrder generiert wird */ - List generateParameter(RefType type){ + private List generateParameter(RefType type){ List ret = new ArrayList<>(); ret.add(ASPStringConverter.toConstant(type.getName())); for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){ @@ -165,7 +210,8 @@ public class ASPGencayFactory implements TypeVisitor { ASPStatement stmt = null; if(isFCType){ - stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), name); + stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), + currentFCTypePointer, name); } else { stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); } @@ -183,4 +229,36 @@ public class ASPGencayFactory implements TypeVisitor { public String visit(GenericRefType genericRefType) { throw new NotImplementedException(); } -} + + private static class TPHExtractor implements TypeVisitor>{ + @Override + public List visit(RefType refType) { + ArrayList ret = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + ret.addAll(param.acceptTV(this)); + } + return ret; + } + + @Override + public List visit(SuperWildcardType superWildcardType) { + return superWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(TypePlaceholder typePlaceholder) { + return Arrays.asList(typePlaceholder); + } + + @Override + public List visit(ExtendsWildcardType extendsWildcardType) { + return extendsWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(GenericRefType genericRefType) { + return new ArrayList<>(); + } + } + +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index e5e60882..57aef860 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.sat.asp.writer; +import java.util.Collection; import java.util.HashSet; +import java.util.List; public class ASPWriter { @@ -17,4 +19,8 @@ public class ASPWriter { } return ret; } + + public void addAll(Collection aspStatements) { + content.addAll(aspStatements); + } }