APS GeneratorTest.simple anfügen

This commit is contained in:
JanUlrich 2018-03-27 14:25:54 +02:00
parent 4886976752
commit 87a2ed8117
2 changed files with 56 additions and 12 deletions

View File

@ -12,11 +12,18 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import java.sql.Ref;
import java.util.*; import java.util.*;
public class ASPGencayFactory implements TypeVisitor<String> {
public class ASPGencayFactory 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)
* 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!
*
*/
ASPWriter writer = new ASPWriter(); ASPWriter writer = new ASPWriter();
boolean isFCType = false; boolean isFCType = false;
@ -45,15 +52,14 @@ public class ASPGencayFactory implements TypeVisitor<String> {
} }
private void generateTheta(RefType t){ private void generateTheta(RefType t){
String statement = "theta"+t.getParaList().size() + "("; String rule = "theta"+t.getParaList().size() ;
statement += ASPStringConverter.toConstant(t.getName()); String name = ASPStringConverter.toConstant(t.getName());
statement+=")"; writer.add(makeStatement(rule, name));
writer.add(new ASPStatement(statement));
} }
private void convertPair(Pair p){ private void convertPair(Pair p){
String ls = p.TA1.acceptTV(this); String ls = generateFCType((RefType) p.TA1);
String rs = p.TA2.acceptTV(this); String rs = generateFCType((RefType) p.TA2);
ASPStatement pairStmt = null; ASPStatement pairStmt = null;
if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ if(p.GetOperator().equals(PairOperator.SMALLERDOT)){
pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs);
@ -65,6 +71,24 @@ public class ASPGencayFactory implements TypeVisitor<String> {
writer.add(pairStmt); 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 ASPStatement makeStatement(String rule, String... params){
String stmt = rule + "("; String stmt = rule + "(";
for(String param : params){ for(String param : params){
@ -76,7 +100,7 @@ public class ASPGencayFactory implements TypeVisitor<String> {
} }
private String convertParameterlist(List<String> pointers){ private String convertParameterlist(List<String> pointers){
if(pointers.size()==1)return pointers.get(0); //if(pointers.size()==1)return pointers.get(0);
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
Iterator<String> it = pointers.iterator(); Iterator<String> it = pointers.iterator();
String p = pointer; String p = pointer;
@ -104,7 +128,7 @@ public class ASPGencayFactory implements TypeVisitor<String> {
@Override @Override
public String visit(RefType refType) { public String visit(RefType refType) {
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
List<String> params = new ArrayList<>(); List<String> params = null;
params = generateParameter(refType); params = generateParameter(refType);
params.remove(0); //Das erste ist der eigentliche Typ kein parameter params.remove(0); //Das erste ist der eigentliche Typ kein parameter
String typeName = ASPStringConverter.toConstant(refType.getName()); String typeName = ASPStringConverter.toConstant(refType.getName());
@ -138,7 +162,14 @@ public class ASPGencayFactory implements TypeVisitor<String> {
@Override @Override
public String visit(TypePlaceholder typePlaceholder) { public String visit(TypePlaceholder typePlaceholder) {
String name = ASPStringConverter.toConstant(typePlaceholder.getName()); String name = ASPStringConverter.toConstant(typePlaceholder.getName());
ASPStatement stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name);
ASPStatement stmt = null;
if(isFCType){
stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), name);
} else {
stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name);
}
writer.add(stmt); writer.add(stmt);
return name; return name;
} }

View File

@ -27,7 +27,20 @@ import java.util.concurrent.ArrayBlockingQueue;
public class GeneratorTest extends UnifyWithoutWildcards{ public class GeneratorTest extends UnifyWithoutWildcards{
@Test @Test
public void matrix() throws InterruptedException, IOException, ClassNotFoundException { public void simple() throws ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
List<RefTypeOrTPHOrWildcardOrGeneric> list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
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,
new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class))));
System.out.println(resultSet);
}
@Test
public void matrix() throws ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>(); ConstraintSet<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
List<RefTypeOrTPHOrWildcardOrGeneric> list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); List<RefTypeOrTPHOrWildcardOrGeneric> list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken()));