JavaCompilerCore/test/asp/UnifyWithoutWildcards.java

188 lines
8.8 KiB
Java

package asp;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.ASPUnifyWithoutWildcards;
import de.dhbwstuttgart.sat.asp.parser.ASPParser;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
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.result.ResolvedType;
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.*;
public class UnifyWithoutWildcards {
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()));
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));
ResultSet resultSet = run(testSet, getFC());
//System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() > 0;
ResolvedType rsType = resultSet.resolveType(list2.get(1));
assert ((RefType)rsType.resolvedType).getName().equals("java.util.Map");
}
@Test
public void adaptNewParaListTest1() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet, getFC());
//System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() == 0; //Hier gibt es keine Lösung.
//TODO: Kann in zukünfigen Fällen eine Lösung geben
}
@Test
public void step1() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet, getFC());
System.out.println(resultSet.results.size());
}
@Test
public void failTest() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test2"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
testSet.addUndConstraint(new Pair(t1, t3, PairOperator.SMALLERDOT));
Set<ClassOrInterface> fc = new HashSet<>();
fc.add(ASTFactory.createClass(Test2.class));
ResultSet resultSet = run(testSet, fc);
assert resultSet.results.size()==1;
}
@Test
public void oderConstraintTest() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test2"), new ArrayList<>(), new NullToken());
Set<Constraint<Pair>> oderCons = new HashSet<>();
Constraint<Pair> cons1 = new Constraint<>();
cons1.add(new Pair(t1, t2, PairOperator.EQUALSDOT));
oderCons.add(cons1);
Constraint<Pair> cons2 = new Constraint<>();
cons2.add(new Pair(t1, t3, PairOperator.EQUALSDOT));
oderCons.add(cons2);
testSet.addOderConstraint(oderCons);
ResultSet resultSet = run(testSet, getFC());
assert resultSet.results.size() == 1;
}
@Test
public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
for(int i = 0; i<1; i++){
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken());
Set<Constraint<Pair>> oderCons = new HashSet<>();
Constraint<Pair> cons1 = new Constraint<>();
cons1.add(new Pair(t1, t2, PairOperator.SMALLERDOT));
oderCons.add(cons1);
Constraint<Pair> cons2 = new Constraint<>();
cons2.add(new Pair(t1, t3, PairOperator.SMALLERDOT));
oderCons.add(cons2);
testSet.addOderConstraint(oderCons);
}
ResultSet resultSet = run(testSet, getFC());
assert resultSet.results.size() == 1;
}
public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
content = ASPFactory.generateASP(toTest, fc);
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content);
writer.close();
ASPUnifyWithoutWildcards clingo = new ASPUnifyWithoutWildcards(Arrays.asList(new File(tempDirectory + "test.lp")));
String result = clingo.runClingo();
System.out.println(result);
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) {
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;
}
private Collection<ClassOrInterface> getFC() {
Set<ClassOrInterface> ret = new HashSet<>();
ret.add(ASTFactory.createClass(Matrix.class));
ret.add(ASTFactory.createClass(Test1.class));
//ret.add(ASTFactory.createObjectClass());
//ret.add(ASTFactory.createClass(java.util.List.class));
return ret;
}
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
private class Test1<A> extends Object{}
private class Test2 extends Object{}
}