JavaCompilerCore/test/asp/UnifyWithoutWildcards.java

104 lines
4.1 KiB
Java
Raw Normal View History

2018-02-27 18:10:16 +00:00
package asp;
import de.dhbwstuttgart.parser.NullToken;
2018-03-01 11:31:56 +00:00
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.Clingo;
import de.dhbwstuttgart.sat.asp.parser.ASPParser;
2018-03-07 21:41:00 +00:00
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
2018-02-27 18:10:16 +00:00
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
2018-03-01 11:31:56 +00:00
import de.dhbwstuttgart.syntaxtree.type.*;
2018-03-11 23:38:02 +00:00
import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter;
2018-02-27 18:10:16 +00:00
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
2018-03-01 11:31:56 +00:00
import de.dhbwstuttgart.typeinference.result.ResultSet;
2018-02-27 18:10:16 +00:00
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
2018-03-01 11:31:56 +00:00
import org.junit.Test;
2018-02-27 18:10:16 +00:00
2018-03-01 11:31:56 +00:00
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Ref;
import java.util.*;
2018-02-27 18:10:16 +00:00
public class UnifyWithoutWildcards {
2018-03-01 11:31:56 +00:00
public static final String tempDirectory = "/tmp/";
@Test
public void adapt() 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()));
2018-03-07 23:03:26 +00:00
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken());
RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken());
2018-03-01 11:31:56 +00:00
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet);
2018-03-11 23:38:02 +00:00
System.out.println(ResultSetPrinter.print(resultSet));
2018-03-07 21:41:00 +00:00
assert resultSet.results.size() > 0;
2018-03-01 11:31:56 +00:00
}
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
2018-03-07 21:41:00 +00:00
content = ASPFactory.generateASP(toTest, this.getFC());
2018-03-01 11:31:56 +00:00
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content);
writer.close();
Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp")));
String result = clingo.runClingo();
ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest));
return resultSet;
}
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<>();
}
}
protected Collection<TypePlaceholder> getInvolvedTPHS(ConstraintSet<Pair> toTest) {
2018-03-01 11:31:56 +00:00
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;
});
2018-02-27 18:10:16 +00:00
return ret;
}
2018-03-01 11:31:56 +00:00
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));
2018-02-27 18:10:16 +00:00
return ret;
}
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
2018-02-27 18:10:16 +00:00
}