package asp; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.sat.asp.Clingo; 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 testSet = new ConstraintSet<>(); List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); List 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 testSet = new ConstraintSet<>(); List 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 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 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 fc = new HashSet<>(); fc.add(ASTFactory.createClass(Test2.class)); ResultSet resultSet = run(testSet, fc); assert resultSet.results.size()==1; } @Test public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet 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 list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); RefType t3 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken()); Set> oderCons = new HashSet<>(); Constraint cons1 = new Constraint<>(); cons1.add(new Pair(t1, t2, PairOperator.SMALLERDOT)); oderCons.add(cons1); Constraint cons2 = new Constraint<>(); cons2.add(new Pair(t1, t3, PairOperator.SMALLERDOT)); oderCons.add(cons2); testSet.addOderConstraint(oderCons); } ResultSet resultSet = run(testSet, getFC()); System.out.println(resultSet.results.size()); } public ResultSet run(ConstraintSet toTest, Collection 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(); Clingo clingo = new Clingo(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>{ @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<>(); } } protected 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; } private Collection getFC() { Set 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 extends HashMap>{} private class Test1 extends Object{} private class Test2 extends Object{} }