Tests an neue ASPFactory anpassen

This commit is contained in:
JanUlrich 2018-03-07 22:41:00 +01:00
parent c4aec8379e
commit 095f9a7956
5 changed files with 22 additions and 13 deletions

View File

@ -22,26 +22,29 @@ import java.util.*;
public class ASPFactory implements TypeVisitor<String>{ public class ASPFactory implements TypeVisitor<String>{
public 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();
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(Constraint<Pair> constraint : constraints1){
for(Pair p : constraint){ for(Pair p : constraint){
convertPair(p); factory.convertPair(p);
} }
} }
return writer.getASPFile(); return factory.writer.getASPFile();
} }
ASPWriter writer = new ASPWriter(); ASPWriter writer = new ASPWriter();
boolean isFCType = false;
private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException { private void convertFC(Collection<ClassOrInterface> classes) throws ClassNotFoundException {
Set<Pair> fc = FCGenerator.toFC(classes); Set<Pair> fc = FCGenerator.toFC(classes);
isFCType = true;
for(Pair fcp : fc){ for(Pair fcp : fc){
convertPair(fcp); convertPair(fcp);
} }
isFCType = false;
} }
private void convertPair(Pair p){ private void convertPair(Pair p){
@ -96,7 +99,8 @@ public class ASPFactory implements TypeVisitor<String>{
params.add(param.acceptTV(this)); params.add(param.acceptTV(this));
} }
String typeName = ASPStringConverter.toConstant(refType.getName()); String typeName = ASPStringConverter.toConstant(refType.getName());
ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE.toString(), pointer, typeName, convertParameterlist(params)); String ruleName = isFCType?ASPRule.ASP_TYPE.toString():ASPRule.ASP_FCTYPE.toString();
ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params));
writer.add(stmt); writer.add(stmt);
return pointer; return pointer;
} }
@ -110,6 +114,7 @@ public class ASPFactory implements TypeVisitor<String>{
public String visit(TypePlaceholder typePlaceholder) { public String visit(TypePlaceholder typePlaceholder) {
String name = ASPStringConverter.toConstant(typePlaceholder.getName()); String name = ASPStringConverter.toConstant(typePlaceholder.getName());
ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE_VAR.toString(), name); ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE_VAR.toString(), name);
writer.add(stmt);
return name; return name;
} }

View File

@ -17,8 +17,7 @@ import java.util.List;
public class ASPGenerator { public class ASPGenerator {
ASPWriter writer = new ASPWriter(); ASPWriter writer = new ASPWriter();
private final String asp; /*
public ASPGenerator(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException { public ASPGenerator(ConstraintSet<Pair> constraints, Collection<ClassOrInterface> fcClasses) throws ClassNotFoundException {
List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next(); List<Constraint<Pair>> constraints1 = constraints.cartesianProduct().iterator().next();
List<Pair> constraintPairs = new ArrayList<>(); List<Pair> constraintPairs = new ArrayList<>();
@ -27,9 +26,10 @@ public class ASPGenerator {
} }
asp = toASP(constraintPairs, FCGenerator.toFC(fcClasses)); asp = toASP(constraintPairs, FCGenerator.toFC(fcClasses));
} }
*/
public String getASP(){ public String getASP(){
return asp; return "";
} }
private String toASP(List<Pair> constraintSet, Collection<Pair> fc){ private String toASP(List<Pair> constraintSet, Collection<Pair> fc){

View File

@ -2,6 +2,7 @@ package asp;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.sat.asp.parser.ASPParser; import de.dhbwstuttgart.sat.asp.parser.ASPParser;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.sat.asp.writer.ASPGenerator;
import de.dhbwstuttgart.sat.asp.Clingo; import de.dhbwstuttgart.sat.asp.Clingo;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
@ -25,7 +26,7 @@ public class ClingoTest {
@Test @Test
public void test() throws IOException, InterruptedException, ClassNotFoundException { public void test() throws IOException, InterruptedException, ClassNotFoundException {
String content = ""; String content = "";
content = new ASPGenerator(this.getPairs(), this.getFC()).getASP(); content = ASPFactory.generateASP(this.getPairs(), this.getFC());
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content); writer.println(content);

View File

@ -4,6 +4,7 @@ import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.Clingo; import de.dhbwstuttgart.sat.asp.Clingo;
import de.dhbwstuttgart.sat.asp.parser.ASPParser; import de.dhbwstuttgart.sat.asp.parser.ASPParser;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.sat.asp.writer.ASPGenerator;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
@ -32,12 +33,13 @@ public class UnifyWithoutWildcards {
RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken()); RefType t2 = new RefType(new JavaClassName("Vector"), list, 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);
System.out.println(resultSet); System.out.println(resultSet.results);
assert resultSet.results.size() > 0;
} }
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException { public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException {
String content = ""; String content = "";
content = new ASPGenerator(toTest, this.getFC()).getASP(); content = ASPFactory.generateASP(toTest, this.getFC());
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content); writer.println(content);

View File

@ -1,6 +1,7 @@
package asp.typeinference; package asp.typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.sat.asp.writer.ASPFactory;
import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.sat.asp.writer.ASPGenerator;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
@ -55,8 +56,8 @@ public class ASPTest {
} }
final ConstraintSet<Pair> cons = compiler.getConstraints(); final ConstraintSet<Pair> cons = compiler.getConstraints();
ASPGenerator generator = new ASPGenerator(cons, allClasses); String asp = ASPFactory.generateASP(cons, allClasses);
System.out.println(generator.getASP()); System.out.println(asp);
} }
static String readFile(String path, Charset encoding) static String readFile(String path, Charset encoding)