Tests anpassen. Beginnen mit Oder Constraints

This commit is contained in:
JanUlrich 2018-04-11 14:13:15 +02:00
parent e7e96d5943
commit 277dac20e7
4 changed files with 64 additions and 12 deletions

View File

@ -49,7 +49,6 @@ public class Clingo {
InputStream output = clingo.getInputStream(); InputStream output = clingo.getInputStream();
clingo.waitFor(); clingo.waitFor();
String result = IOUtils.toString(output, StandardCharsets.UTF_8); String result = IOUtils.toString(output, StandardCharsets.UTF_8);
System.out.println(result);
return result; return result;
} }
} }

View File

@ -12,6 +12,7 @@ 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 javax.management.Notification;
import java.util.*; import java.util.*;
public class ASPFactory implements TypeVisitor<String>{ public class ASPFactory implements TypeVisitor<String>{
@ -19,16 +20,23 @@ public class ASPFactory implements TypeVisitor<String>{
public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{ public static String generateASP(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException{
ASPFactory factory = new ASPFactory(); ASPFactory factory = new ASPFactory();
factory.convertFC(fcClasses); factory.convertFC(fcClasses);
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next(); //List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
for(Constraint<Pair> constraint : constraints1){ for(Pair p : constraints.getUndConstraints()){
for(Pair p : constraint){
factory.convertPair(p); factory.convertPair(p);
} }
for(Set<Constraint<Pair>> constraint : constraints.getOderConstraints()){
for(Constraint<Pair> oder : constraint){
factory.convertOderConstraint(oder);
}
} }
return factory.writer.getASPFile(); return factory.writer.getASPFile();
} }
private void convertOderConstraint(Constraint<Pair> oder) {
throw new NotImplementedException();
}
ASPWriter writer = new ASPWriter(); ASPWriter writer = new ASPWriter();
boolean isFCType = false; boolean isFCType = false;

View File

@ -52,4 +52,12 @@ public class ConstraintSet<A> {
ret.oderConstraints = newOder; ret.oderConstraints = newOder;
return ret; return ret;
} }
public Constraint<A> getUndConstraints() {
return undConstraints;
}
public List<Set<Constraint<A>>> getOderConstraints() {
return oderConstraints;
}
} }

View File

@ -8,7 +8,7 @@ import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResolvedType; import de.dhbwstuttgart.typeinference.result.ResolvedType;
@ -19,7 +19,6 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.Ref;
import java.util.*; import java.util.*;
public class UnifyWithoutWildcards { public class UnifyWithoutWildcards {
@ -34,7 +33,7 @@ public class UnifyWithoutWildcards {
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, 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()); RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet); ResultSet resultSet = run(testSet, getFC());
//System.out.println(ResultSetPrinter.print(resultSet)); //System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() > 0; assert resultSet.results.size() > 0;
ResolvedType rsType = resultSet.resolveType(list2.get(1)); ResolvedType rsType = resultSet.resolveType(list2.get(1));
@ -48,7 +47,7 @@ public class UnifyWithoutWildcards {
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, 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()); RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet); ResultSet resultSet = run(testSet, getFC());
//System.out.println(ResultSetPrinter.print(resultSet)); //System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() == 0; //Hier gibt es keine Lösung. assert resultSet.results.size() == 0; //Hier gibt es keine Lösung.
//TODO: Kann in zukünfigen Fällen eine Lösung geben //TODO: Kann in zukünfigen Fällen eine Lösung geben
@ -60,19 +59,55 @@ public class UnifyWithoutWildcards {
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken()); TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken()); RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet); ResultSet resultSet = run(testSet, getFC());
System.out.println(resultSet.results.size()); System.out.println(resultSet.results.size());
} }
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException { @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 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());
System.out.println(resultSet.results.size());
}
public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException {
String content = ""; String content = "";
content = ASPFactory.generateASP(toTest, this.getFC()); content = ASPFactory.generateASP(toTest, fc);
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content); writer.println(content);
writer.close(); writer.close();
Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp")));
String result = clingo.runClingo(); String result = clingo.runClingo();
System.out.println(result);
ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest)); ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest));
return resultSet; return resultSet;
} }
@ -129,4 +164,6 @@ public class UnifyWithoutWildcards {
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{} private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
private class Test1<A> extends Object{} private class Test1<A> extends Object{}
private class Test2 extends Object{}
} }