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;
|
2018-05-28 13:51:40 +00:00
|
|
|
import de.dhbwstuttgart.sat.asp.ASPUnifyWithoutWildcards;
|
2018-03-01 11:31:56 +00:00
|
|
|
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-04-11 12:13:15 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
2018-02-27 18:10:16 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
|
|
|
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
2018-04-09 11:00:24 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.result.ResolvedType;
|
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.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<>();
|
2018-03-12 23:24:40 +00:00
|
|
|
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());
|
2018-03-08 00:41:38 +00:00
|
|
|
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));
|
2018-04-11 12:13:15 +00:00
|
|
|
ResultSet resultSet = run(testSet, getFC());
|
2018-04-09 11:00:24 +00:00
|
|
|
//System.out.println(ResultSetPrinter.print(resultSet));
|
2018-03-07 21:41:00 +00:00
|
|
|
assert resultSet.results.size() > 0;
|
2018-04-09 11:00:24 +00:00
|
|
|
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));
|
2018-04-11 12:13:15 +00:00
|
|
|
ResultSet resultSet = run(testSet, getFC());
|
2018-04-09 11:00:24 +00:00
|
|
|
//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));
|
2018-04-11 12:13:15 +00:00
|
|
|
ResultSet resultSet = run(testSet, getFC());
|
2018-04-09 11:00:24 +00:00
|
|
|
System.out.println(resultSet.results.size());
|
2018-03-01 11:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 12:13:15 +00:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:16:45 +00:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:13:15 +00:00
|
|
|
@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());
|
2018-04-11 15:16:45 +00:00
|
|
|
assert resultSet.results.size() == 1;
|
2018-04-11 12:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 14:04:12 +00:00
|
|
|
@Test
|
|
|
|
public void martinTest() throws InterruptedException, IOException, ClassNotFoundException {
|
|
|
|
ConstraintSet<Pair> testSet = new ConstraintSet<>();
|
|
|
|
List<RefTypeOrTPHOrWildcardOrGeneric> list1 =
|
|
|
|
Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken()));
|
|
|
|
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
|
|
|
|
RefType t2 = new RefType(new JavaClassName("java.util.Map"), list1, new NullToken());
|
|
|
|
RefType t3 = new RefType(new JavaClassName("java.util.Map"), list1, new NullToken());
|
|
|
|
Set<Constraint<Pair>> oderCons = new HashSet<>();
|
|
|
|
Constraint<Pair> cons1 = new Constraint<>();
|
|
|
|
cons1.add(new Pair(t2, t3, PairOperator.EQUALSDOT));
|
|
|
|
oderCons.add(cons1);
|
|
|
|
testSet.addOderConstraint(oderCons);
|
|
|
|
ResultSet resultSet = run(testSet, getFC());
|
|
|
|
assert resultSet.results.size() > 0;
|
|
|
|
}
|
|
|
|
|
2018-06-13 21:54:35 +00:00
|
|
|
@Test
|
|
|
|
public void fc() throws ClassNotFoundException {
|
|
|
|
Collection<ClassOrInterface> fc = new ArrayList<>();
|
|
|
|
fc.add(ASTFactory.createClass(MatrixTest.class));
|
|
|
|
fc.add(ASTFactory.createClass(Vector.class));
|
|
|
|
String content = "";
|
|
|
|
content = ASPFactory.generateASP(new ConstraintSet<>(), fc);
|
|
|
|
System.out.println(content);
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:13:15 +00:00
|
|
|
public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException {
|
2018-03-01 11:31:56 +00:00
|
|
|
String content = "";
|
2018-04-11 12:13:15 +00:00
|
|
|
content = ASPFactory.generateASP(toTest, fc);
|
2018-06-28 14:04:12 +00:00
|
|
|
System.out.println(content);
|
2018-03-01 11:31:56 +00:00
|
|
|
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
|
|
|
|
writer.println(content);
|
|
|
|
writer.close();
|
2018-05-28 13:51:40 +00:00
|
|
|
ASPUnifyWithoutWildcards clingo = new ASPUnifyWithoutWildcards(Arrays.asList(new File(tempDirectory + "test.lp")));
|
2018-03-01 11:31:56 +00:00
|
|
|
String result = clingo.runClingo();
|
2018-04-11 12:13:15 +00:00
|
|
|
System.out.println(result);
|
2018-03-01 11:31:56 +00:00
|
|
|
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<>();
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 15:54:32 +00:00
|
|
|
|
|
|
|
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));
|
2018-04-09 11:00:24 +00:00
|
|
|
ret.add(ASTFactory.createClass(Test1.class));
|
2018-06-28 14:04:12 +00:00
|
|
|
ret.add(ASTFactory.createClass(Map.class));
|
2018-03-01 11:31:56 +00:00
|
|
|
//ret.add(ASTFactory.createObjectClass());
|
|
|
|
//ret.add(ASTFactory.createClass(java.util.List.class));
|
2018-02-27 18:10:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-03-01 12:25:03 +00:00
|
|
|
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
|
2018-04-09 11:00:24 +00:00
|
|
|
|
|
|
|
private class Test1<A> extends Object{}
|
2018-04-11 12:13:15 +00:00
|
|
|
|
|
|
|
private class Test2 extends Object{}
|
2018-06-13 21:54:35 +00:00
|
|
|
|
|
|
|
private class MatrixTest extends Vector<Vector<Integer>>{}
|
2018-02-27 18:10:16 +00:00
|
|
|
}
|