Oder-Constraints generieren

This commit is contained in:
JanUlrich 2018-04-11 17:16:45 +02:00
parent 277dac20e7
commit 10be0b17e9
5 changed files with 134 additions and 14 deletions

View File

@ -23,6 +23,7 @@ public class Clingo {
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/result.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/result.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/cartesian.lp"));
} }
public Clingo(List<File> inputFiles){ public Clingo(List<File> inputFiles){

View File

@ -8,7 +8,10 @@ public enum ASPRule {
ASP_PARAMLIST_END_POINTER("null"), ASP_PARAMLIST_END_POINTER("null"),
ASP_TYPE("type"), ASP_TYPE("type"),
ASP_FCTYPE("typeFC"), ASP_FCTYPE("typeFC"),
ASP_TYPE_VAR("typeVar"); ASP_TYPE_VAR("typeVar"), ASP_ODER("oder"),
ASP_CONSTRAINT("constraint"),
ASP_LIST_NAME("list"),
ASP_LIST_ENDPOINTER("null");
private final String text; private final String text;

View File

@ -24,23 +24,71 @@ public class ASPFactory implements TypeVisitor<String>{
for(Pair p : constraints.getUndConstraints()){ for(Pair p : constraints.getUndConstraints()){
factory.convertPair(p); factory.convertPair(p);
} }
for(Set<Constraint<Pair>> constraint : constraints.getOderConstraints()){ for(Set<Constraint<Pair>> oder : constraints.getOderConstraints()){
for(Constraint<Pair> oder : constraint){
factory.convertOderConstraint(oder); factory.convertOderConstraint(oder);
} }
}
return factory.writer.getASPFile(); return factory.writer.getASPFile();
} }
private void convertOderConstraint(Constraint<Pair> oder) { /**
throw new NotImplementedException(); * Wandelt eine Reihe von Constraints zu einem Und-Constraint - also einer Liste - um.
* Dieser kann dann in Oder-Constraints verwendet werden.
* @param undCons - Eine Liste von Constraints. Bsp: equals(..), smallerDot(..)
* @return - list(equals, list(smallerDot(..., null)
*/
protected ASPStatement convertListToUndConstraint(List<ASPStatement> undCons) {
if(undCons.size() == 0)throw new NullPointerException();
if(undCons.size() == 1){
return undCons.get(0);
}
ASPStatement list = makeStatement(ASPRule.ASP_LIST_ENDPOINTER.toString());
for(ASPStatement con : undCons){
list = makeStatement(ASPRule.ASP_LIST_NAME.toString(), con.getASP(), list.getASP());
}
return list;
} }
ASPWriter writer = new ASPWriter(); protected ASPStatement convertListToUndConstraint(Constraint<Pair> undCons) {
List<ASPStatement> convert = new ArrayList<>();
for(Pair p : undCons){
convert.add(generatePairStmt(p));
}
return convertListToUndConstraint(convert);
}
protected void convertOderConstraint(Set<Constraint<Pair>> oder) {
if(oder.size() < 2)throw new NotImplementedException();
List<ASPStatement> ret = new ArrayList<>();
Iterator<Constraint<Pair>> it = oder.iterator();
String pointer1 = ASPStringConverter.toConstant(NameGenerator.makeNewName());
ASPStatement stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), pointer1,
convertListToUndConstraint(it.next()).getASP());
ret.add(stmt);
while(it.hasNext()){
String pointer2 = ASPStringConverter.toConstant(NameGenerator.makeNewName());
Constraint<Pair> cons = it.next();
stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), pointer2,
convertListToUndConstraint(cons).getASP());
ret.add(stmt);
ASPStatement oderStmt = makeStatement(ASPRule.ASP_ODER.toString(), pointer1, pointer2);
if(it.hasNext()){
String oderPointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
stmt = makeStatement(ASPRule.ASP_CONSTRAINT.toString(), oderPointer, oderStmt.getASP());
ret.add(stmt);
pointer1 = oderPointer;
}else{
ret.add(oderStmt);
}
}
//Alle erstellten Constraints schreiben:
writer.addAll(ret);
}
protected ASPWriter writer = new ASPWriter();
boolean isFCType = false; boolean isFCType = false;
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException { protected void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes); Set<Pair> fc = FCGenerator.toFC(classes);
isFCType = true; isFCType = true;
for(Pair fcp : fc){ for(Pair fcp : fc){
@ -49,7 +97,12 @@ public class ASPFactory implements TypeVisitor<String>{
isFCType = false; isFCType = false;
} }
private void convertPair(Pair p){ protected void convertPair(Pair p){
ASPStatement pairStmt = generatePairStmt(p);
writer.add(pairStmt);
}
protected ASPStatement generatePairStmt(Pair p) {
String ls = p.TA1.acceptTV(this); String ls = p.TA1.acceptTV(this);
String rs = p.TA2.acceptTV(this); String rs = p.TA2.acceptTV(this);
ASPStatement pairStmt = null; ASPStatement pairStmt = null;
@ -60,10 +113,10 @@ public class ASPFactory implements TypeVisitor<String>{
}else if(p.GetOperator().equals(PairOperator.SMALLER)){ }else if(p.GetOperator().equals(PairOperator.SMALLER)){
pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs);
}else throw new NotImplementedException(); }else throw new NotImplementedException();
writer.add(pairStmt); return pairStmt;
} }
private ASPStatement makeStatement(String rule, String... params){ protected ASPStatement makeStatement(String rule, String... params){
String stmt = rule + "("; String stmt = rule + "(";
for(String param : params){ for(String param : params){
stmt += param + ","; stmt += param + ",";
@ -73,7 +126,7 @@ public class ASPFactory implements TypeVisitor<String>{
return new ASPStatement(stmt); return new ASPStatement(stmt);
} }
private String convertParameterlist(List<String> pointers){ protected String convertParameterlist(List<String> pointers){
String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName());
Iterator<String> it = pointers.iterator(); Iterator<String> it = pointers.iterator();
String p = pointer; String p = pointer;

View File

@ -77,6 +77,24 @@ public class UnifyWithoutWildcards {
assert resultSet.results.size()==1; 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 @Test
public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException { public void oderConstraints() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>(); ConstraintSet<Pair> testSet = new ConstraintSet<>();
@ -95,7 +113,7 @@ public class UnifyWithoutWildcards {
testSet.addOderConstraint(oderCons); testSet.addOderConstraint(oderCons);
} }
ResultSet resultSet = run(testSet, getFC()); ResultSet resultSet = run(testSet, getFC());
System.out.println(resultSet.results.size()); assert resultSet.results.size() == 1;
} }
public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException { public ResultSet run(ConstraintSet<Pair> toTest, Collection<ClassOrInterface> fc) throws IOException, InterruptedException, ClassNotFoundException {

View File

@ -0,0 +1,45 @@
package asp.unifywithoutwildcards;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.sat.asp.writer.ASPStatement;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ASPFactoryTest extends ASPFactory {
@Test
public void undConstraintTest(){
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken());
Constraint<Pair> toTest = new Constraint();
for(int i = 0; i<5;i++){
toTest.add(new Pair(tph, tph, PairOperator.EQUALSDOT));
}
List<ASPStatement> undCons = new ArrayList<>();
for(Pair p : toTest){
undCons.add(generatePairStmt(p));
}
ASPStatement ret = convertListToUndConstraint(undCons);
System.out.println(ret.getASP());
}
@Test
public void oderConstraintTest(){
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken());
Set<Constraint<Pair>> oderTest = new HashSet<>();
for(int i = 0; i<5;i++){
Constraint<Pair> toTest = new Constraint();
toTest.add(new Pair(tph, tph, PairOperator.EQUALSDOT));
oderTest.add(toTest);
}
this.convertOderConstraint(oderTest);
System.out.println(this.writer.getASPFile());
}
}