Gencoy ASP Generator implementieren
This commit is contained in:
parent
87a2ed8117
commit
000c16b907
@ -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;
|
||||
|
||||
|
@ -22,11 +22,39 @@ public class ASPGencayFactory implements TypeVisitor<String> {
|
||||
* 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<ASPStatement> generateVar(ConstraintSet<Pair> constraints){
|
||||
List<ASPStatement> 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<TypePlaceholder> getInvolvedTPHS(ConstraintSet<Pair> toTest) {
|
||||
List<TypePlaceholder> 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<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
|
||||
ASPGencayFactory factory = new ASPGencayFactory();
|
||||
factory.convertFC(fcClasses);
|
||||
@ -36,6 +64,7 @@ public class ASPGencayFactory implements TypeVisitor<String> {
|
||||
factory.convertPair(p);
|
||||
}
|
||||
}
|
||||
factory.writer.addAll(generateVar(constraints));
|
||||
|
||||
return factory.writer.getASPFile();
|
||||
}
|
||||
@ -58,8 +87,8 @@ public class ASPGencayFactory implements TypeVisitor<String> {
|
||||
}
|
||||
|
||||
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<String> {
|
||||
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<String> {
|
||||
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<String> 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<String> params = null;
|
||||
params = generateParameter(refType);
|
||||
@ -136,12 +177,16 @@ public class ASPGencayFactory implements TypeVisitor<String> {
|
||||
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<String> generateParameter(RefType type){
|
||||
private List<String> generateParameter(RefType type){
|
||||
List<String> ret = new ArrayList<>();
|
||||
ret.add(ASPStringConverter.toConstant(type.getName()));
|
||||
for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){
|
||||
@ -165,7 +210,8 @@ public class ASPGencayFactory implements TypeVisitor<String> {
|
||||
|
||||
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<String> {
|
||||
public String visit(GenericRefType genericRefType) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private static class TPHExtractor implements TypeVisitor<List<TypePlaceholder>>{
|
||||
@Override
|
||||
public List<TypePlaceholder> visit(RefType refType) {
|
||||
ArrayList<TypePlaceholder> ret = new ArrayList<>();
|
||||
for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){
|
||||
ret.addAll(param.acceptTV(this));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TypePlaceholder> visit(SuperWildcardType superWildcardType) {
|
||||
return superWildcardType.getInnerType().acceptTV(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TypePlaceholder> visit(TypePlaceholder typePlaceholder) {
|
||||
return Arrays.asList(typePlaceholder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TypePlaceholder> visit(ExtendsWildcardType extendsWildcardType) {
|
||||
return extendsWildcardType.getInnerType().acceptTV(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TypePlaceholder> visit(GenericRefType genericRefType) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<ASPStatement> aspStatements) {
|
||||
content.addAll(aspStatements);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user