From 284af1246ba50f0690a9d94a89a16cdaa8bf9c19 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 07:41:46 +0100 Subject: [PATCH 01/18] =?UTF-8?q?Nicht=20lauff=C3=A4hig.=20ASPFactory=20ne?= =?UTF-8?q?u=20implementieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sat/asp/ASPStringConverter.java | 29 +++++++++++++++++-- src/de/dhbwstuttgart/sat/asp/Clingo.java | 1 + .../sat/asp/writer/ASPFactory.java | 16 ++++++++++ .../sat/asp/writer/ASPGenerator.java | 7 ++--- .../sat/asp/writer/model/ASPGenericType.java | 6 ++-- .../sat/asp/writer/model/ASPPair.java | 5 ++-- .../asp/writer/model/ASPParameterList.java | 2 +- .../sat/asp/writer/model/ASPRefType.java | 5 ---- .../sat/asp/writer/model/ASPType.java | 2 -- .../sat/asp/writer/model/ASPTypeVar.java | 11 +++---- .../syntaxtree/factory/ASTFactory.java | 25 +++++++++------- test/asp/UnifyWithoutWildcards.java | 5 ++-- test/javFiles/LambdaRunnable.jav | 3 ++ 13 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java diff --git a/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java index 0497b09a..5235ab1a 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java +++ b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java @@ -2,16 +2,39 @@ package de.dhbwstuttgart.sat.asp; import de.dhbwstuttgart.parser.scope.JavaClassName; +import java.util.HashMap; +import java.util.Map; + public class ASPStringConverter { + private static final Map replacements = new HashMap<>(); + static{ + replacements.put(".", "_DOT_"); + replacements.put("$", "_DOLLAR_"); + } + public static String toConstant(JavaClassName name){ - return toConstant(name.toString().replace(".", "_")); + return toConstant(name.toString()); } public static String toConstant(String name){ - return "c" + name.toString().replace(".", "_"); + return "c" + replace(name); } public static String fromConstant(String s){ - return s.replace("_", ".").substring(1); + return unReplace(s).substring(1); + } + + private static String replace(String input){ + for(String toReplace : replacements.keySet()){ + input = input.replace(toReplace, replacements.get(toReplace)); + } + return input; + } + + private static String unReplace(String input){ + for(String toReplace : replacements.keySet()){ + input = input.replace(replacements.get(toReplace), toReplace); + } + return input; } } diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index ed1e9f40..e0f29cb1 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -41,6 +41,7 @@ public class Clingo { commands.add(file.getPath()); } commands.addAll(programFiles.stream().map(f->f.getPath()).collect(Collectors.toList())); + Process clingo = new ProcessBuilder( commands.toArray(new String[0])).start(); InputStream output = clingo.getInputStream(); clingo.waitFor(); diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java new file mode 100644 index 00000000..46c047d5 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.sat.asp.writer; + +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.type.RefType; + +import java.util.Collection; + +public class ASPFactory { + public static String convertFC(Collection classes){ + return null; //TODO + } + + public static String convert(RefType t){ + return null; //TODO + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index 676aa33f..3f999a41 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -36,11 +36,10 @@ public class ASPGenerator { TypeConverter converter = new TypeConverter(); for(Pair fcp : fc){ //Wenn dieser Cast fehlschlägt stimmt etwas nicht. Alle Paare in der FC müssen smaller Operatoren haen - ASPPairSmaller fcEntry = (ASPPairSmaller) convert(fcp); - writer.add(new ASPStatement(fcEntry.toASP())); + convert(fcp); } for(Pair cons : constraintSet){ - writer.add(new ASPStatement(convert(cons).toASP())); + convert(cons); } return writer.getASPFile(); @@ -88,7 +87,7 @@ public class ASPGenerator { @Override public ASPType visit(TypePlaceholder typePlaceholder) { - return new ASPTypeVar(ASPStringConverter.toConstant(typePlaceholder.getName())); + return new ASPTypeVar(ASPStringConverter.toConstant(typePlaceholder.getName()),writer); } @Override diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java index 8e732d64..327ce04a 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.sat.asp.writer.model; import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.ASPWriter; public class ASPGenericType implements ASPType{ private final String name; @@ -13,9 +14,8 @@ public class ASPGenericType implements ASPType{ return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; } - @Override - public String toASP() { - return toString(); + private String toASP() { + return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; } @Override diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java index 19730639..e881f346 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java @@ -9,11 +9,10 @@ public abstract class ASPPair { public ASPPair(ASPType ls, ASPType rs, ASPWriter writer){ this.leftSide = ls; this.rightSide = rs; - writer.add(new ASPStatement(ls.toASP())); - writer.add(new ASPStatement(rs.toASP())); + writer.add(new ASPStatement(toASP())); } - public String toASP(){ + private String toASP(){ return this.getRuleName() + "(" + leftSide.getPointer() + ","+ rightSide.getPointer() + ")"; } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java index 178bb8fa..fafa50f5 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java @@ -29,7 +29,7 @@ public class ASPParameterList { if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); param += nextPointer; writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); - writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," + t.getPointer() + "," + paramNum + ")")); paramNum++; } } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java index 5eab65be..46a4d1c7 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java @@ -19,11 +19,6 @@ public class ASPRefType implements ASPType { return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; } - @Override - public String toASP() { - return toString(); - } - @Override public String getPointer() { return name; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java index 6e715dba..201e36ad 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java @@ -1,7 +1,5 @@ package de.dhbwstuttgart.sat.asp.writer.model; public interface ASPType { - String toASP(); - String getPointer(); } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java index 24b33eaf..e10a88bf 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java @@ -1,22 +1,23 @@ package de.dhbwstuttgart.sat.asp.writer.model; import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.ASPWriter; public class ASPTypeVar implements ASPType{ private final String name; - public ASPTypeVar(String name){ + public ASPTypeVar(String name, ASPWriter writer){ this.name = name; + writer.add(new ASPStatement(this.toASP())); } @Override public String toString() { - return ASPRule.ASP_TYPE_VAR+"("+ name +")"; + return toASP(); } - @Override - public String toASP() { - return toString(); + private String toASP() { + return ASPRule.ASP_TYPE_VAR+"("+ name +")"; } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index fa930f58..de1bec99 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -49,12 +49,10 @@ public class ASTFactory { parameterSuperClass = (ParameterizedType) tempSuperClass; java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; - if(superjreClass != null){ - List params = new ArrayList<>(); - for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new GenericRefType(tv.getName(), new NullToken())); - } - superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); + if(parameterSuperClass != null){ + superClass = (RefType) convertType(parameterSuperClass); + }else if(superjreClass != null){ + superClass = (RefType) convertType(superjreClass); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) superClass = (RefType) createType(java.lang.Object.class, name, ""); } @@ -70,19 +68,24 @@ public class ASTFactory { } private static RefTypeOrTPHOrWildcardOrGeneric convertType(Type type){ - JavaClassName name = null; - List params = new ArrayList<>(); if(type instanceof ParameterizedType){ + List params = new ArrayList<>(); for(Type paramType : ((ParameterizedType)type).getActualTypeArguments()){ params.add(convertType(paramType)); } + JavaClassName name = new JavaClassName(((ParameterizedType) type).getRawType().getTypeName()); + return new RefType(name, params, new NullToken()); }else if(type instanceof TypeVariable){ - + return new GenericRefType(((TypeVariable) type).getName(), new NullToken()); }else if(type instanceof Class){ + List params = new ArrayList<>(); Class paramClass = (Class) type; - //params.add(new RefType(paramClass.getName()) + for(TypeVariable tv : paramClass.getTypeParameters()){ + params.add(new GenericRefType(tv.getName(), new NullToken())); + } + JavaClassName name = new JavaClassName(paramClass.getName()); + return new RefType(name, params, new NullToken()); }else throw new NotImplementedException(); - return new RefType(name, params, new NullToken()); } private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index db697312..998ec56c 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -28,10 +28,11 @@ public class UnifyWithoutWildcards { public void adapt() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet testSet = new ConstraintSet<>(); List list = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - RefType t1 = new RefType(new JavaClassName("Matrix"), new NullToken()); + RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), new NullToken()); RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); - run(testSet); + ResultSet resultSet = run(testSet); + System.out.println(resultSet); } public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { diff --git a/test/javFiles/LambdaRunnable.jav b/test/javFiles/LambdaRunnable.jav index 982680c3..37924ea2 100644 --- a/test/javFiles/LambdaRunnable.jav +++ b/test/javFiles/LambdaRunnable.jav @@ -1,3 +1,6 @@ +import java.lang.Runnable; +import java.lang.String; +import java.lang.System; public class LamRunnable{ From c4aec8379e952d791a11b05cdd2cee8933e354d6 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 21:52:46 +0100 Subject: [PATCH 02/18] Erste Version von ASPFactory implementieren --- .../sat/asp/writer/ASPFactory.java | 123 +++++++++++++++++- 1 file changed, 116 insertions(+), 7 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 46c047d5..30c06169 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -1,16 +1,125 @@ package de.dhbwstuttgart.sat.asp.writer; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +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.PairTPHEqualTPH; +import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType; +import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH; +import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import java.util.Collection; +import java.util.*; -public class ASPFactory { - public static String convertFC(Collection classes){ - return null; //TODO +public class ASPFactory implements TypeVisitor{ + + public String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPFactory factory = new ASPFactory(); + convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + convertPair(p); + } + } + + return writer.getASPFile(); } - public static String convert(RefType t){ - return null; //TODO + ASPWriter writer = new ASPWriter(); + + private void convertFC(Collection classes) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes); + for(Pair fcp : fc){ + convertPair(fcp); + } + } + + private void convertPair(Pair p){ + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); + ASPStatement pairStmt = null; + if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_EQUALS_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); + }else throw new NotImplementedException(); + writer.add(pairStmt); + } + + private ASPStatement makeStatement(String rule, String... params){ + String stmt = rule + "("; + for(String param : params){ + stmt += param + ","; + } + stmt = stmt.substring(0,stmt.length()-1); + stmt += ")"; + return new ASPStatement(stmt); + } + + private String convertParameterlist(List pointers){ + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Iterator it = pointers.iterator(); + String p = pointer; + while (it.hasNext()){ + ASPStatement stmt; + String type = it.next(); + String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + if(it.hasNext()){ + stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type, nextP); + }else{ + stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type, + ASPRule.ASP_PARAMLIST_END_POINTER.toString()); + } + p = nextP; + writer.add(stmt); + } + return pointer; + } + + @Override + public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + List params = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + params.add(param.acceptTV(this)); + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE.toString(), pointer, typeName, convertParameterlist(params)); + writer.add(stmt); + return pointer; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + String name = ASPStringConverter.toConstant(typePlaceholder.getName()); + ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE_VAR.toString(), name); + return name; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + throw new NotImplementedException(); } } From 095f9a79561603d71c54b1a82aee94659dc02630 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 22:41:00 +0100 Subject: [PATCH 03/18] Tests an neue ASPFactory anpassen --- .../dhbwstuttgart/sat/asp/writer/ASPFactory.java | 15 ++++++++++----- .../sat/asp/writer/ASPGenerator.java | 6 +++--- test/asp/ClingoTest.java | 3 ++- test/asp/UnifyWithoutWildcards.java | 6 ++++-- test/asp/typeinference/ASPTest.java | 5 +++-- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 30c06169..ab6ef51b 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -22,26 +22,29 @@ import java.util.*; public class ASPFactory implements TypeVisitor{ - public String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ ASPFactory factory = new ASPFactory(); - convertFC(fcClasses); + factory.convertFC(fcClasses); List> constraints1 = constraints.cartesianProduct().iterator().next(); for(Constraint constraint : constraints1){ for(Pair p : constraint){ - convertPair(p); + factory.convertPair(p); } } - return writer.getASPFile(); + return factory.writer.getASPFile(); } ASPWriter writer = new ASPWriter(); + boolean isFCType = false; private void convertFC(Collection classes) throws ClassNotFoundException { Set fc = FCGenerator.toFC(classes); + isFCType = true; for(Pair fcp : fc){ convertPair(fcp); } + isFCType = false; } private void convertPair(Pair p){ @@ -96,7 +99,8 @@ public class ASPFactory implements TypeVisitor{ params.add(param.acceptTV(this)); } 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); return pointer; } @@ -110,6 +114,7 @@ public class ASPFactory implements TypeVisitor{ public String visit(TypePlaceholder typePlaceholder) { String name = ASPStringConverter.toConstant(typePlaceholder.getName()); ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE_VAR.toString(), name); + writer.add(stmt); return name; } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index 3f999a41..a3752fcc 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -17,8 +17,7 @@ import java.util.List; public class ASPGenerator { ASPWriter writer = new ASPWriter(); - private final String asp; - +/* public ASPGenerator(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException { List> constraints1 = constraints.cartesianProduct().iterator().next(); List constraintPairs = new ArrayList<>(); @@ -27,9 +26,10 @@ public class ASPGenerator { } asp = toASP(constraintPairs, FCGenerator.toFC(fcClasses)); } + */ public String getASP(){ - return asp; + return ""; } private String toASP(List constraintSet, Collection fc){ diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java index f47b644c..248cf5a2 100644 --- a/test/asp/ClingoTest.java +++ b/test/asp/ClingoTest.java @@ -2,6 +2,7 @@ package asp; import de.dhbwstuttgart.parser.NullToken; 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.Clingo; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; @@ -25,7 +26,7 @@ public class ClingoTest { @Test public void test() throws IOException, InterruptedException, ClassNotFoundException { 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"); writer.println(content); diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 998ec56c..cb414f6c 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -4,6 +4,7 @@ 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.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -32,12 +33,13 @@ public class UnifyWithoutWildcards { RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); ResultSet resultSet = run(testSet); - System.out.println(resultSet); + System.out.println(resultSet.results); + assert resultSet.results.size() > 0; } public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { String content = ""; - content = new ASPGenerator(toTest, this.getFC()).getASP(); + content = ASPFactory.generateASP(toTest, this.getFC()); PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); writer.println(content); diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index cea141f9..112d2023 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -1,6 +1,7 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; @@ -55,8 +56,8 @@ public class ASPTest { } final ConstraintSet cons = compiler.getConstraints(); - ASPGenerator generator = new ASPGenerator(cons, allClasses); - System.out.println(generator.getASP()); + String asp = ASPFactory.generateASP(cons, allClasses); + System.out.println(asp); } static String readFile(String path, Charset encoding) From e8388e2748a74ae66ace9805fe6329cc0bef3166 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 22:42:05 +0100 Subject: [PATCH 04/18] =?UTF-8?q?Aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sat/asp/writer/ASPFactory.java | 6 - .../sat/asp/writer/ASPGenerator.java | 104 ------------------ .../asp/writer/{model => }/ASPStatement.java | 2 +- .../sat/asp/writer/ASPWriter.java | 2 - .../sat/asp/writer/model/ASPFCType.java | 14 --- .../sat/asp/writer/model/ASPGenericType.java | 25 ----- .../sat/asp/writer/model/ASPPair.java | 24 ---- .../sat/asp/writer/model/ASPPairEquals.java | 15 --- .../sat/asp/writer/model/ASPPairSmaller.java | 17 --- .../asp/writer/model/ASPPairSmallerDot.java | 15 --- .../asp/writer/model/ASPParameterList.java | 45 -------- .../sat/asp/writer/model/ASPRefType.java | 26 ----- .../sat/asp/writer/model/ASPType.java | 5 - .../sat/asp/writer/model/ASPTypeVar.java | 27 ----- 14 files changed, 1 insertion(+), 326 deletions(-) delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java rename src/de/dhbwstuttgart/sat/asp/writer/{model => }/ASPStatement.java (91%) delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPFCType.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index ab6ef51b..8afce74c 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -4,18 +4,12 @@ import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; 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.PairTPHEqualTPH; -import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType; -import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH; -import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import java.util.*; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java deleted file mode 100644 index a3752fcc..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ /dev/null @@ -1,104 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer; - -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; -import de.dhbwstuttgart.sat.asp.ASPStringConverter; -import de.dhbwstuttgart.sat.asp.writer.model.*; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public class ASPGenerator { - ASPWriter writer = new ASPWriter(); -/* - public ASPGenerator(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException { - List> constraints1 = constraints.cartesianProduct().iterator().next(); - List constraintPairs = new ArrayList<>(); - for(Constraint constraint : constraints1){ - constraintPairs.addAll(constraint); - } - asp = toASP(constraintPairs, FCGenerator.toFC(fcClasses)); - } - */ - - public String getASP(){ - return ""; - } - - private String toASP(List constraintSet, Collection fc){ - TypeConverter converter = new TypeConverter(); - for(Pair fcp : fc){ - //Wenn dieser Cast fehlschlägt stimmt etwas nicht. Alle Paare in der FC müssen smaller Operatoren haen - convert(fcp); - } - for(Pair cons : constraintSet){ - convert(cons); - } - - return writer.getASPFile(); - } - - private ASPPair convert(Pair pair){ - TypeConverter converter = new TypeConverter(); - ASPType ls = pair.TA1.acceptTV(converter); - ASPType rs = pair.TA2.acceptTV(converter); - if(pair.OperatorEqual()){ - return new ASPPairEquals(ls, rs,writer); - }else if(pair.OperatorSmallerDot()){ - return new ASPPairSmallerDot(ls, rs, writer); - }else if(pair.OperatorSmaller()){ - //Diese Cast müssen auch immer funktionieren, da in smaller Constraints nur RefTypes vorkommen - return new ASPPairSmaller(new ASPFCType((ASPRefType) ls), new ASPFCType((ASPRefType) rs), writer); - }else throw new NotImplementedException(); - } - - private ASPRefType convert(ClassOrInterface cl){ - List paramList = new ArrayList<>(); - for(GenericTypeVar gtv : cl.getGenerics()){ - paramList.add(new ASPGenericType(ASPStringConverter.toConstant(gtv.getName()))); - } - ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(ASPStringConverter.toConstant(cl.getClassName()), params); - } - - private class TypeConverter implements TypeVisitor{ - - @Override - public ASPType visit(RefType type) { - List paramList = new ArrayList<>(); - for(RefTypeOrTPHOrWildcardOrGeneric gtv : type.getParaList()){ - paramList.add(gtv.acceptTV(this)); - } - ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(ASPStringConverter.toConstant(type.getName()), params); - } - - @Override - public ASPType visit(SuperWildcardType superWildcardType) { - throw new NotImplementedException(); - } - - @Override - public ASPType visit(TypePlaceholder typePlaceholder) { - return new ASPTypeVar(ASPStringConverter.toConstant(typePlaceholder.getName()),writer); - } - - @Override - public ASPType visit(ExtendsWildcardType extendsWildcardType) { - throw new NotImplementedException(); - } - - @Override - public ASPType visit(GenericRefType genericRefType) { - return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getParsedName()), - new ASPParameterList(new ArrayList<>(), writer)); - } - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java similarity index 91% rename from src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java index 76bb63e8..8d342464 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.writer.model; +package de.dhbwstuttgart.sat.asp.writer; public class ASPStatement { private final String stmt; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index 450950d9..e5e60882 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,7 +1,5 @@ package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; - import java.util.HashSet; public class ASPWriter { diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPFCType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPFCType.java deleted file mode 100644 index 790fd5af..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPFCType.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public class ASPFCType extends ASPRefType { - public ASPFCType(ASPRefType refType){ - super(refType.name, refType.params); - } - - public String toString(){ - return ASPRule.ASP_FCTYPE + "(" + name +"," + params.name + ")"; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java deleted file mode 100644 index 327ce04a..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public class ASPGenericType implements ASPType{ - private final String name; - - public ASPGenericType(String name){ - this.name = name; - } - - public String toString(){ - return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; - } - - private String toASP() { - return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; - } - - @Override - public String getPointer() { - return name; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java deleted file mode 100644 index e881f346..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java +++ /dev/null @@ -1,24 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public abstract class ASPPair { - public final ASPType leftSide; - public final ASPType rightSide; - - public ASPPair(ASPType ls, ASPType rs, ASPWriter writer){ - this.leftSide = ls; - this.rightSide = rs; - writer.add(new ASPStatement(toASP())); - } - - private String toASP(){ - return this.getRuleName() + "(" + leftSide.getPointer() + ","+ rightSide.getPointer() + ")"; - } - - public String toString(){ - return toASP(); - } - - protected abstract String getRuleName(); -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java deleted file mode 100644 index bf6e2485..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public class ASPPairEquals extends ASPPair{ - public ASPPairEquals(ASPType ls, ASPType rs, ASPWriter writer){ - super(ls, rs, writer); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_EQUALS_NAME.toString(); - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java deleted file mode 100644 index 610e5c56..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -import java.util.Map; - -public class ASPPairSmaller extends ASPPair{ - public ASPPairSmaller(ASPFCType ls, ASPFCType rs, ASPWriter writer){ - super(ls, rs, writer); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_SMALLER_NAME.toString(); - } -} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java deleted file mode 100644 index b572ed99..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public class ASPPairSmallerDot extends ASPPair{ - public ASPPairSmallerDot(ASPType ls, ASPType rs, ASPWriter writer){ - super(ls, rs, writer); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(); - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java deleted file mode 100644 index fafa50f5..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.ASPStringConverter; -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; -import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; - -import java.util.Iterator; -import java.util.List; - -public class ASPParameterList { - public final String name; - private final List types; - - public ASPParameterList(List types, ASPWriter writer){ - int paramNum = 0; - this.types = types; - if(types.size() == 0){ - name = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); - }else{ - name = newName(); - String nextPointer = name; - Iterator it = types.iterator(); - while(it.hasNext()){ - ASPType t = it.next(); - String param = nextPointer + "," + t.toString() + ","; - nextPointer = newName(); - if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); - param += nextPointer; - writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); - writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," + t.getPointer() + "," + paramNum + ")")); - paramNum++; - } - } - } - - private String newName() { - return ASPStringConverter.toConstant(NameGenerator.makeNewName()); - } - - public String toString(){ - return name; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java deleted file mode 100644 index 46a4d1c7..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPRefType implements ASPType { - protected final ASPParameterList params; - protected final String name; - - public ASPRefType(String name, ASPParameterList params){ - this.name = name; - this.params = params; - } - - public ASPParameterList getParams() { - return params; - } - - public String toString(){ - return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; - } - - @Override - public String getPointer() { - return name; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java deleted file mode 100644 index 201e36ad..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -public interface ASPType { - String getPointer(); -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java deleted file mode 100644 index e10a88bf..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; -import de.dhbwstuttgart.sat.asp.writer.ASPWriter; - -public class ASPTypeVar implements ASPType{ - private final String name; - - public ASPTypeVar(String name, ASPWriter writer){ - this.name = name; - writer.add(new ASPStatement(this.toASP())); - } - - @Override - public String toString() { - return toASP(); - } - - private String toASP() { - return ASPRule.ASP_TYPE_VAR+"("+ name +")"; - } - - @Override - public String getPointer() { - return name; - } -} From 87cf6743528a6ae64e25a3cc0fb85dddce460b38 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 23:07:53 +0100 Subject: [PATCH 05/18] Fehler in FCGenerator beheben --- .../parser/SyntaxTreeGenerator/FCGenerator.java | 5 +++-- test/asp/UnifyWithoutWildcards.java | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 24abec67..66cfb764 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -51,8 +51,9 @@ public class FCGenerator { //Generics mit gleichem Namen müssen den selben TPH bekommen for(GenericTypeVar gtv : forType.getGenerics()){ if(!gtvs.containsKey(gtv.getName())){ - gtvs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken())); - newGTVs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken())); + TypePlaceholder replacePlaceholder = TypePlaceholder.fresh(new NullToken()); + gtvs.put(gtv.getName(), replacePlaceholder); + newGTVs.put(gtv.getName(), replacePlaceholder); } params.add(gtvs.get(gtv.getName())); } diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index cb414f6c..2bf74d66 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -5,7 +5,6 @@ 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.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.type.*; From 9f2702276f5314fcad04da1073f6fa0fa8428d3d Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 8 Mar 2018 00:03:26 +0100 Subject: [PATCH 06/18] Fehler beheben --- src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java | 5 ++++- test/asp/UnifyWithoutWildcards.java | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java index 8afce74c..1777eabd 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -69,6 +69,9 @@ public class ASPFactory implements TypeVisitor{ String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); Iterator it = pointers.iterator(); String p = pointer; + if(!it.hasNext()){ + return ASPRule.ASP_PARAMLIST_END_POINTER.toString(); + } while (it.hasNext()){ ASPStatement stmt; String type = it.next(); @@ -93,7 +96,7 @@ public class ASPFactory implements TypeVisitor{ params.add(param.acceptTV(this)); } String typeName = ASPStringConverter.toConstant(refType.getName()); - String ruleName = isFCType?ASPRule.ASP_TYPE.toString():ASPRule.ASP_FCTYPE.toString(); + String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString(); ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); writer.add(stmt); return pointer; diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 2bf74d66..e1afc3c8 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -27,9 +27,10 @@ public class UnifyWithoutWildcards { @Test public void adapt() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet testSet = new ConstraintSet<>(); - List list = Arrays.asList(TypePlaceholder.fresh(new NullToken())); - RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), new NullToken()); - RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken()); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); + RefType t2 = new RefType(new JavaClassName("Vector"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); ResultSet resultSet = run(testSet); System.out.println(resultSet.results); From 3deaceec011318c17354d8236ff5e7500c2c6640 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 8 Mar 2018 01:41:38 +0100 Subject: [PATCH 07/18] =?UTF-8?q?UnifyWithoutWildcards=20Test=20l=C3=A4uft?= =?UTF-8?q?=20durch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java | 8 ++++---- test/asp/UnifyWithoutWildcards.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index b3534f47..1de833d6 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -1,15 +1,12 @@ package de.dhbwstuttgart.sat.asp.parser; import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; -import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; -import de.dhbwstuttgart.sat.asp.writer.model.ASPParameterList; -import de.dhbwstuttgart.sat.asp.writer.model.ASPRefType; -import de.dhbwstuttgart.sat.asp.writer.model.ASPType; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; @@ -98,6 +95,9 @@ public class ASPParser { RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(rs); if(lsType instanceof TypePlaceholder && rsType instanceof RefType) return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder) lsType, rsType); + else if(lsType instanceof TypePlaceholder && rsType instanceof TypePlaceholder) + return new PairTPHEqualTPH((TypePlaceholder)lsType, (TypePlaceholder)rsType); + else throw new NotImplementedException(); } return null; } diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index e1afc3c8..1b33cbd5 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -27,10 +27,10 @@ public class UnifyWithoutWildcards { @Test public void adapt() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet testSet = new ConstraintSet<>(); - List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); - RefType t2 = new RefType(new JavaClassName("Vector"), list2, 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); System.out.println(resultSet.results); From 83aaff140c67b1b750885c6a38fa54890ae2d15b Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 8 Mar 2018 01:44:40 +0100 Subject: [PATCH 08/18] =?UTF-8?q?Aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java | 4 ++++ test/asp/ClingoTest.java | 1 - test/asp/typeinference/ASPTest.java | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index 1de833d6..75acfe90 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -158,6 +158,9 @@ public class ASPParser { } private void parseType(String statement){ + //TODO: Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) + throw new NotImplementedException(); + /* Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+)\\)"); Matcher m = p.matcher(statement); boolean b = m.matches(); @@ -168,6 +171,7 @@ public class ASPParser { List params = this.getParams(rs); this.types.put(ls,new ParsedType(ls, params)); } + */ } private List getParams(String pointer) { diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java index 248cf5a2..90635bde 100644 --- a/test/asp/ClingoTest.java +++ b/test/asp/ClingoTest.java @@ -3,7 +3,6 @@ package asp; import de.dhbwstuttgart.parser.NullToken; 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.Clingo; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index 112d2023..632d288e 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -2,7 +2,6 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.sat.asp.writer.ASPFactory; -import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; From dff72b0c97d7a59382cfb046ea00a4a5d972d151 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 12 Mar 2018 00:38:02 +0100 Subject: [PATCH 09/18] ResultSetOutputGenerator --- .../sat/asp/parser/ASPParser.java | 21 ++++++------ .../visual/ResultSetOutputGenerator.java | 34 +++++++++++++++++++ .../syntaxtree/visual/ResultSetPrinter.java | 20 +++++++++++ test/asp/UnifyWithoutWildcards.java | 3 +- 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index 75acfe90..9aa86891 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -130,6 +130,7 @@ public class ASPParser { } } private void parseParameter(String statement){ + //param(pointer, typ, nextPointer Pattern p = Pattern.compile(ASPRule.ASP_PARAMLIST_NAME+"\\(([^,]+),([^,]+),([^,]+)\\)"); Matcher m = p.matcher(statement); boolean b = m.matches(); @@ -137,7 +138,7 @@ public class ASPParser { if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); String pointer = m.group(1); String type = m.group(2); - String next = m.group(2); + String next = m.group(3); if(next.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))next = null; if(this.parameterLists.containsKey(pointer)){ throw new DebugException("Fehler in Ergebnisparsen"); @@ -158,20 +159,18 @@ public class ASPParser { } private void parseType(String statement){ - //TODO: Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) - throw new NotImplementedException(); - /* - Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+)\\)"); + //Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) + Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+),([^,]+)\\)"); Matcher m = p.matcher(statement); boolean b = m.matches(); if(b){ - if(m.groupCount()<2)throw new DebugException("Fehler in Regex"); - String ls = m.group(1); - String rs = m.group(2); - List params = this.getParams(rs); - this.types.put(ls,new ParsedType(ls, params)); + if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); + String pointer = m.group(1); + String name = m.group(2); + String paraList = m.group(3); + List params = this.getParams(paraList); + this.types.put(pointer,new ParsedType(name, params)); } - */ } private List getParams(String pointer) { diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java new file mode 100644 index 00000000..f50b05a6 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java @@ -0,0 +1,34 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.result.*; + +public class ResultSetOutputGenerator extends OutputGenerator implements ResultSetVisitor{ + + public ResultSetOutputGenerator(StringBuilder out) { + super(out); + } + + @Override + public void visit(PairTPHsmallerTPH p) { + print(p, "<"); + } + + @Override + public void visit(PairTPHequalRefTypeOrWildcardType p) { + print(p, "=."); + } + + @Override + public void visit(PairTPHEqualTPH p) { + print(p, "=."); + } + + private void print(ResultPair p , String operator){ + out.append("("); + p.getLeft().accept((ResultSetVisitor) this); + out.append(" "+operator+" "); + p.getRight().accept((ResultSetVisitor) this); + out.append(")"); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java new file mode 100644 index 00000000..fc67a723 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.Set; + +public class ResultSetPrinter { + + public static String print(ResultSet toPrint){ + StringBuilder output = new StringBuilder(); + for(ResultPair p : toPrint.results){ + p.accept(new ResultSetOutputGenerator(output)); + output.append("\n"); + } + return output.toString(); + } + +} diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 1b33cbd5..eadca649 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -8,6 +8,7 @@ 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.syntaxtree.visual.ResultSetPrinter; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -33,7 +34,7 @@ public class UnifyWithoutWildcards { RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); ResultSet resultSet = run(testSet); - System.out.println(resultSet.results); + System.out.println(ResultSetPrinter.print(resultSet)); assert resultSet.results.size() > 0; } From 123f94aea75f80592f8bef947ff6e49912b045c4 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 13 Mar 2018 00:24:40 +0100 Subject: [PATCH 10/18] =?UTF-8?q?ANTLR=20Parser=20f=C3=BCr=20ASP=20Stateme?= =?UTF-8?q?nts=20implementieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 15 + src/de/dhbwstuttgart/sat/asp/Clingo.java | 3 +- .../sat/asp/parser/ASPParser.java | 62 +- .../sat/asp/parser/antlr/UnifyResult.g4 | 37 + .../sat/asp/parser/antlr/UnifyResult.tokens | 23 + .../parser/antlr/UnifyResultBaseListener.java | 159 +++++ .../asp/parser/antlr/UnifyResultLexer.java | 134 ++++ .../asp/parser/antlr/UnifyResultLexer.tokens | 23 + .../asp/parser/antlr/UnifyResultListener.java | 110 +++ .../asp/parser/antlr/UnifyResultParser.java | 639 ++++++++++++++++++ test/asp/UnifyWithoutWildcards.java | 4 +- 11 files changed, 1205 insertions(+), 4 deletions(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java diff --git a/pom.xml b/pom.xml index ea7be6b3..ae0f670a 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,22 @@ de.dhbwstuttgart.parser.antlr + + + aspParser + + antlr4 + + + src/de/dhbwstuttgart/sat/asp/parser/antlr/ + src/de/dhbwstuttgart/sat/asp/parser/antlr/ + + -package + de.dhbwstuttgart.sat.asp.parser.antlr + + + diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index e0f29cb1..9f529bbe 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -36,7 +36,8 @@ public class Clingo { "/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/clingo-5.2.1-linux-x86_64/clingo"; List commands = new ArrayList<>(); commands.add(pathToClingo); - commands.add("--outf=2"); //use JSON-Output + //commands.add("--outf=2"); //use JSON-Output + commands.add("--outf=1"); //use JSON-Output for(File file : input){ commands.add(file.getPath()); } diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index 9aa86891..cfb59933 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -6,9 +6,17 @@ import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultBaseListener; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultListener; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser; import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.tree.ParseTreeWalker; import javax.json.Json; import javax.json.JsonArray; @@ -17,6 +25,7 @@ import java.io.StringReader; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Ablauf: @@ -26,7 +35,7 @@ import java.util.regex.Pattern; * TODO: Überlegen welche Informationen noch nach der Unifizierung gebraucht werden * -> Eigentlich nur die korrekten Namen der Typen und TPHs */ -public class ASPParser { +public class ASPParser extends UnifyResultBaseListener { private final Collection originalTPHs; private ResultSet resultSet; private Map types = new HashMap<>(); @@ -42,7 +51,58 @@ public class ASPParser { return new ASPParser(toParse, oldPlaceholders).resultSet; } + @Override + public void enterParameter(UnifyResultParser.ParameterContext ctx) { + } + @Override + public void enterEquals(UnifyResultParser.EqualsContext ctx) { + List parameterList = ctx.parameterList().value().stream().map(v -> v.getText()).collect(Collectors.toList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Regex"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + /* + RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(ls); + RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(rs); + if(lsType instanceof TypePlaceholder && rsType instanceof RefType) + return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder) lsType, rsType); + else if(lsType instanceof TypePlaceholder && rsType instanceof TypePlaceholder) + return new PairTPHEqualTPH((TypePlaceholder)lsType, (TypePlaceholder)rsType); + else throw new NotImplementedException(); + */ + } + + @Override + public void enterSmaller(UnifyResultParser.SmallerContext ctx) { + } + + @Override + public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { + } + + @Override + public void enterType(UnifyResultParser.TypeContext ctx) { + super.enterType(ctx); + } + + /* + private List parsedRule; + private List parseRule(String rule){ + UnifyResultLexer lexer = new UnifyResultLexer(CharStreams.fromString(rule)); + UnifyResultParser.AspruleContext parseTree = new UnifyResultParser(new CommonTokenStream(lexer)).asprule(); + parsedRule = new ArrayList<>(); + new ParseTreeWalker().walk(this, parseTree); + return parsedRule; + } + + @Override + public void enterAsprule(UnifyResultParser.AspruleContext ctx) { + super.enterAsprule(ctx); + for(int i = 0; i< ctx.getChildCount();i++){ + parsedRule.add(ctx.getChild(i).getText()); + } + } + */ private ASPParser(String toParse, Collection oldPlaceholders){ this.originalTPHs = oldPlaceholders; JsonObject jsonResult = Json.createReader(new StringReader(toParse)).readObject(); diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 new file mode 100644 index 00000000..98b6a5fb --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 @@ -0,0 +1,37 @@ +grammar UnifyResult; + +answer : 'ANSWER' (resultSetRule '.')*; + +resultSetRule : + parameter + | equals + | smaller + | typeVar + | type + | otherRule + ; + +parameterList : '(' value (',' value)* ')'; +value : NAME + | resultSetRule ; + +parameter : PARAMLIST_NAME parameterList; +equals : EQUALS_NAME parameterList; +smaller : SMALLER_NAME parameterList; +typeVar : TYPEVAR_NAME parameterList; +type : TYPE_NAME parameterList; +otherRule : NAME parameterList; + +//TODO: Es sollte Regeln für das Result set geben, welche sich nicht mit den anderen überdecken, dann auch nur diese im Result ausgeben +PARAMLIST_NAME : 'param'; +EQUALS_NAME : 'equals'; +SMALLER_NAME : 'smaller'; +TYPEVAR_NAME : 'typeVar'; +TYPE_NAME : 'type'; +NAME : [a-zA-Z0-9_]+; + +WS : [ \t\r\n\u000C]+ -> skip + ; +LINE_COMMENT + : '%' ~[\r\n]* -> skip + ; diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens new file mode 100644 index 00000000..8f9c0e0a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens @@ -0,0 +1,23 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +PARAMLIST_NAME=6 +EQUALS_NAME=7 +SMALLER_NAME=8 +TYPEVAR_NAME=9 +TYPE_NAME=10 +NAME=11 +WS=12 +LINE_COMMENT=13 +'ANSWER'=1 +'.'=2 +'('=3 +','=4 +')'=5 +'param'=6 +'equals'=7 +'smaller'=8 +'typeVar'=9 +'type'=10 diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java new file mode 100644 index 00000000..f33525d7 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java @@ -0,0 +1,159 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link UnifyResultListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class UnifyResultBaseListener implements UnifyResultListener { + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterAnswer(UnifyResultParser.AnswerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitAnswer(UnifyResultParser.AnswerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterResultSetRule(UnifyResultParser.ResultSetRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitResultSetRule(UnifyResultParser.ResultSetRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParameterList(UnifyResultParser.ParameterListContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParameterList(UnifyResultParser.ParameterListContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterValue(UnifyResultParser.ValueContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitValue(UnifyResultParser.ValueContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParameter(UnifyResultParser.ParameterContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParameter(UnifyResultParser.ParameterContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEquals(UnifyResultParser.EqualsContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEquals(UnifyResultParser.EqualsContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterSmaller(UnifyResultParser.SmallerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitSmaller(UnifyResultParser.SmallerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitTypeVar(UnifyResultParser.TypeVarContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterType(UnifyResultParser.TypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitType(UnifyResultParser.TypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterOtherRule(UnifyResultParser.OtherRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitOtherRule(UnifyResultParser.OtherRuleContext ctx) { } + + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitErrorNode(ErrorNode node) { } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java new file mode 100644 index 00000000..b8660eba --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java @@ -0,0 +1,134 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class UnifyResultLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, EQUALS_NAME=7, + SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + public static final String[] ruleNames = { + "T__0", "T__1", "T__2", "T__3", "T__4", "PARAMLIST_NAME", "EQUALS_NAME", + "SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + + private static final String[] _LITERAL_NAMES = { + null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", + "'typeVar'", "'type'" + }; + private static final String[] _SYMBOLIC_NAMES = { + null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", + "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public UnifyResultLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "UnifyResult.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\17c\b\1\4\2\t\2\4"+ + "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+ + "\13\4\f\t\f\4\r\t\r\4\16\t\16\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\3\3\3\3\4"+ + "\3\4\3\5\3\5\3\6\3\6\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\b\3"+ + "\b\3\t\3\t\3\t\3\t\3\t\3\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\n\3\n\3\n\3\13"+ + "\3\13\3\13\3\13\3\13\3\f\6\fP\n\f\r\f\16\fQ\3\r\6\rU\n\r\r\r\16\rV\3\r"+ + "\3\r\3\16\3\16\7\16]\n\16\f\16\16\16`\13\16\3\16\3\16\2\2\17\3\3\5\4\7"+ + "\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\3\2\5\6\2\62;C\\"+ + "aac|\5\2\13\f\16\17\"\"\4\2\f\f\17\17\2e\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3"+ + "\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2"+ + "\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\3\35"+ + "\3\2\2\2\5$\3\2\2\2\7&\3\2\2\2\t(\3\2\2\2\13*\3\2\2\2\r,\3\2\2\2\17\62"+ + "\3\2\2\2\219\3\2\2\2\23A\3\2\2\2\25I\3\2\2\2\27O\3\2\2\2\31T\3\2\2\2\33"+ + "Z\3\2\2\2\35\36\7C\2\2\36\37\7P\2\2\37 \7U\2\2 !\7Y\2\2!\"\7G\2\2\"#\7"+ + "T\2\2#\4\3\2\2\2$%\7\60\2\2%\6\3\2\2\2&\'\7*\2\2\'\b\3\2\2\2()\7.\2\2"+ + ")\n\3\2\2\2*+\7+\2\2+\f\3\2\2\2,-\7r\2\2-.\7c\2\2./\7t\2\2/\60\7c\2\2"+ + "\60\61\7o\2\2\61\16\3\2\2\2\62\63\7g\2\2\63\64\7s\2\2\64\65\7w\2\2\65"+ + "\66\7c\2\2\66\67\7n\2\2\678\7u\2\28\20\3\2\2\29:\7u\2\2:;\7o\2\2;<\7c"+ + "\2\2<=\7n\2\2=>\7n\2\2>?\7g\2\2?@\7t\2\2@\22\3\2\2\2AB\7v\2\2BC\7{\2\2"+ + "CD\7r\2\2DE\7g\2\2EF\7X\2\2FG\7c\2\2GH\7t\2\2H\24\3\2\2\2IJ\7v\2\2JK\7"+ + "{\2\2KL\7r\2\2LM\7g\2\2M\26\3\2\2\2NP\t\2\2\2ON\3\2\2\2PQ\3\2\2\2QO\3"+ + "\2\2\2QR\3\2\2\2R\30\3\2\2\2SU\t\3\2\2TS\3\2\2\2UV\3\2\2\2VT\3\2\2\2V"+ + "W\3\2\2\2WX\3\2\2\2XY\b\r\2\2Y\32\3\2\2\2Z^\7\'\2\2[]\n\4\2\2\\[\3\2\2"+ + "\2]`\3\2\2\2^\\\3\2\2\2^_\3\2\2\2_a\3\2\2\2`^\3\2\2\2ab\b\16\2\2b\34\3"+ + "\2\2\2\6\2QV^\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens new file mode 100644 index 00000000..8f9c0e0a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens @@ -0,0 +1,23 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +PARAMLIST_NAME=6 +EQUALS_NAME=7 +SMALLER_NAME=8 +TYPEVAR_NAME=9 +TYPE_NAME=10 +NAME=11 +WS=12 +LINE_COMMENT=13 +'ANSWER'=1 +'.'=2 +'('=3 +','=4 +')'=5 +'param'=6 +'equals'=7 +'smaller'=8 +'typeVar'=9 +'type'=10 diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java new file mode 100644 index 00000000..6158b123 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java @@ -0,0 +1,110 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link UnifyResultParser}. + */ +public interface UnifyResultListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link UnifyResultParser#answer}. + * @param ctx the parse tree + */ + void enterAnswer(UnifyResultParser.AnswerContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#answer}. + * @param ctx the parse tree + */ + void exitAnswer(UnifyResultParser.AnswerContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#resultSetRule}. + * @param ctx the parse tree + */ + void enterResultSetRule(UnifyResultParser.ResultSetRuleContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#resultSetRule}. + * @param ctx the parse tree + */ + void exitResultSetRule(UnifyResultParser.ResultSetRuleContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#parameterList}. + * @param ctx the parse tree + */ + void enterParameterList(UnifyResultParser.ParameterListContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#parameterList}. + * @param ctx the parse tree + */ + void exitParameterList(UnifyResultParser.ParameterListContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#value}. + * @param ctx the parse tree + */ + void enterValue(UnifyResultParser.ValueContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#value}. + * @param ctx the parse tree + */ + void exitValue(UnifyResultParser.ValueContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#parameter}. + * @param ctx the parse tree + */ + void enterParameter(UnifyResultParser.ParameterContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#parameter}. + * @param ctx the parse tree + */ + void exitParameter(UnifyResultParser.ParameterContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#equals}. + * @param ctx the parse tree + */ + void enterEquals(UnifyResultParser.EqualsContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#equals}. + * @param ctx the parse tree + */ + void exitEquals(UnifyResultParser.EqualsContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#smaller}. + * @param ctx the parse tree + */ + void enterSmaller(UnifyResultParser.SmallerContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#smaller}. + * @param ctx the parse tree + */ + void exitSmaller(UnifyResultParser.SmallerContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#typeVar}. + * @param ctx the parse tree + */ + void enterTypeVar(UnifyResultParser.TypeVarContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#typeVar}. + * @param ctx the parse tree + */ + void exitTypeVar(UnifyResultParser.TypeVarContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#type}. + * @param ctx the parse tree + */ + void enterType(UnifyResultParser.TypeContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#type}. + * @param ctx the parse tree + */ + void exitType(UnifyResultParser.TypeContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#otherRule}. + * @param ctx the parse tree + */ + void enterOtherRule(UnifyResultParser.OtherRuleContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#otherRule}. + * @param ctx the parse tree + */ + void exitOtherRule(UnifyResultParser.OtherRuleContext ctx); +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java new file mode 100644 index 00000000..bf0789f1 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java @@ -0,0 +1,639 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class UnifyResultParser extends Parser { + static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, EQUALS_NAME=7, + SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; + public static final int + RULE_answer = 0, RULE_resultSetRule = 1, RULE_parameterList = 2, RULE_value = 3, + RULE_parameter = 4, RULE_equals = 5, RULE_smaller = 6, RULE_typeVar = 7, + RULE_type = 8, RULE_otherRule = 9; + public static final String[] ruleNames = { + "answer", "resultSetRule", "parameterList", "value", "parameter", "equals", + "smaller", "typeVar", "type", "otherRule" + }; + + private static final String[] _LITERAL_NAMES = { + null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", + "'typeVar'", "'type'" + }; + private static final String[] _SYMBOLIC_NAMES = { + null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", + "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "UnifyResult.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public UnifyResultParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + public static class AnswerContext extends ParserRuleContext { + public List resultSetRule() { + return getRuleContexts(ResultSetRuleContext.class); + } + public ResultSetRuleContext resultSetRule(int i) { + return getRuleContext(ResultSetRuleContext.class,i); + } + public AnswerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_answer; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterAnswer(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitAnswer(this); + } + } + + public final AnswerContext answer() throws RecognitionException { + AnswerContext _localctx = new AnswerContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_answer); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(20); + match(T__0); + setState(26); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << PARAMLIST_NAME) | (1L << EQUALS_NAME) | (1L << SMALLER_NAME) | (1L << TYPEVAR_NAME) | (1L << TYPE_NAME) | (1L << NAME))) != 0)) { + { + { + setState(21); + resultSetRule(); + setState(22); + match(T__1); + } + } + setState(28); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ResultSetRuleContext extends ParserRuleContext { + public ParameterContext parameter() { + return getRuleContext(ParameterContext.class,0); + } + public EqualsContext equals() { + return getRuleContext(EqualsContext.class,0); + } + public SmallerContext smaller() { + return getRuleContext(SmallerContext.class,0); + } + public TypeVarContext typeVar() { + return getRuleContext(TypeVarContext.class,0); + } + public TypeContext type() { + return getRuleContext(TypeContext.class,0); + } + public OtherRuleContext otherRule() { + return getRuleContext(OtherRuleContext.class,0); + } + public ResultSetRuleContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_resultSetRule; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterResultSetRule(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitResultSetRule(this); + } + } + + public final ResultSetRuleContext resultSetRule() throws RecognitionException { + ResultSetRuleContext _localctx = new ResultSetRuleContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_resultSetRule); + try { + setState(35); + _errHandler.sync(this); + switch (_input.LA(1)) { + case PARAMLIST_NAME: + enterOuterAlt(_localctx, 1); + { + setState(29); + parameter(); + } + break; + case EQUALS_NAME: + enterOuterAlt(_localctx, 2); + { + setState(30); + equals(); + } + break; + case SMALLER_NAME: + enterOuterAlt(_localctx, 3); + { + setState(31); + smaller(); + } + break; + case TYPEVAR_NAME: + enterOuterAlt(_localctx, 4); + { + setState(32); + typeVar(); + } + break; + case TYPE_NAME: + enterOuterAlt(_localctx, 5); + { + setState(33); + type(); + } + break; + case NAME: + enterOuterAlt(_localctx, 6); + { + setState(34); + otherRule(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParameterListContext extends ParserRuleContext { + public List value() { + return getRuleContexts(ValueContext.class); + } + public ValueContext value(int i) { + return getRuleContext(ValueContext.class,i); + } + public ParameterListContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_parameterList; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterParameterList(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitParameterList(this); + } + } + + public final ParameterListContext parameterList() throws RecognitionException { + ParameterListContext _localctx = new ParameterListContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_parameterList); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(37); + match(T__2); + setState(38); + value(); + setState(43); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__3) { + { + { + setState(39); + match(T__3); + setState(40); + value(); + } + } + setState(45); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(46); + match(T__4); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ValueContext extends ParserRuleContext { + public TerminalNode NAME() { return getToken(UnifyResultParser.NAME, 0); } + public ResultSetRuleContext resultSetRule() { + return getRuleContext(ResultSetRuleContext.class,0); + } + public ValueContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_value; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterValue(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitValue(this); + } + } + + public final ValueContext value() throws RecognitionException { + ValueContext _localctx = new ValueContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_value); + try { + setState(50); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(48); + match(NAME); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(49); + resultSetRule(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParameterContext extends ParserRuleContext { + public TerminalNode PARAMLIST_NAME() { return getToken(UnifyResultParser.PARAMLIST_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public ParameterContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_parameter; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterParameter(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitParameter(this); + } + } + + public final ParameterContext parameter() throws RecognitionException { + ParameterContext _localctx = new ParameterContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_parameter); + try { + enterOuterAlt(_localctx, 1); + { + setState(52); + match(PARAMLIST_NAME); + setState(53); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class EqualsContext extends ParserRuleContext { + public TerminalNode EQUALS_NAME() { return getToken(UnifyResultParser.EQUALS_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public EqualsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_equals; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterEquals(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitEquals(this); + } + } + + public final EqualsContext equals() throws RecognitionException { + EqualsContext _localctx = new EqualsContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_equals); + try { + enterOuterAlt(_localctx, 1); + { + setState(55); + match(EQUALS_NAME); + setState(56); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SmallerContext extends ParserRuleContext { + public TerminalNode SMALLER_NAME() { return getToken(UnifyResultParser.SMALLER_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public SmallerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_smaller; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterSmaller(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitSmaller(this); + } + } + + public final SmallerContext smaller() throws RecognitionException { + SmallerContext _localctx = new SmallerContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_smaller); + try { + enterOuterAlt(_localctx, 1); + { + setState(58); + match(SMALLER_NAME); + setState(59); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeVarContext extends ParserRuleContext { + public TerminalNode TYPEVAR_NAME() { return getToken(UnifyResultParser.TYPEVAR_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public TypeVarContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_typeVar; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterTypeVar(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitTypeVar(this); + } + } + + public final TypeVarContext typeVar() throws RecognitionException { + TypeVarContext _localctx = new TypeVarContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_typeVar); + try { + enterOuterAlt(_localctx, 1); + { + setState(61); + match(TYPEVAR_NAME); + setState(62); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeContext extends ParserRuleContext { + public TerminalNode TYPE_NAME() { return getToken(UnifyResultParser.TYPE_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public TypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_type; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterType(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitType(this); + } + } + + public final TypeContext type() throws RecognitionException { + TypeContext _localctx = new TypeContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_type); + try { + enterOuterAlt(_localctx, 1); + { + setState(64); + match(TYPE_NAME); + setState(65); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class OtherRuleContext extends ParserRuleContext { + public TerminalNode NAME() { return getToken(UnifyResultParser.NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public OtherRuleContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_otherRule; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterOtherRule(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitOtherRule(this); + } + } + + public final OtherRuleContext otherRule() throws RecognitionException { + OtherRuleContext _localctx = new OtherRuleContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_otherRule); + try { + enterOuterAlt(_localctx, 1); + { + setState(67); + match(NAME); + setState(68); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17I\4\2\t\2\4\3\t"+ + "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\3"+ + "\2\3\2\3\2\3\2\7\2\33\n\2\f\2\16\2\36\13\2\3\3\3\3\3\3\3\3\3\3\3\3\5\3"+ + "&\n\3\3\4\3\4\3\4\3\4\7\4,\n\4\f\4\16\4/\13\4\3\4\3\4\3\5\3\5\5\5\65\n"+ + "\5\3\6\3\6\3\6\3\7\3\7\3\7\3\b\3\b\3\b\3\t\3\t\3\t\3\n\3\n\3\n\3\13\3"+ + "\13\3\13\3\13\2\2\f\2\4\6\b\n\f\16\20\22\24\2\2\2F\2\26\3\2\2\2\4%\3\2"+ + "\2\2\6\'\3\2\2\2\b\64\3\2\2\2\n\66\3\2\2\2\f9\3\2\2\2\16<\3\2\2\2\20?"+ + "\3\2\2\2\22B\3\2\2\2\24E\3\2\2\2\26\34\7\3\2\2\27\30\5\4\3\2\30\31\7\4"+ + "\2\2\31\33\3\2\2\2\32\27\3\2\2\2\33\36\3\2\2\2\34\32\3\2\2\2\34\35\3\2"+ + "\2\2\35\3\3\2\2\2\36\34\3\2\2\2\37&\5\n\6\2 &\5\f\7\2!&\5\16\b\2\"&\5"+ + "\20\t\2#&\5\22\n\2$&\5\24\13\2%\37\3\2\2\2% \3\2\2\2%!\3\2\2\2%\"\3\2"+ + "\2\2%#\3\2\2\2%$\3\2\2\2&\5\3\2\2\2\'(\7\5\2\2(-\5\b\5\2)*\7\6\2\2*,\5"+ + "\b\5\2+)\3\2\2\2,/\3\2\2\2-+\3\2\2\2-.\3\2\2\2.\60\3\2\2\2/-\3\2\2\2\60"+ + "\61\7\7\2\2\61\7\3\2\2\2\62\65\7\r\2\2\63\65\5\4\3\2\64\62\3\2\2\2\64"+ + "\63\3\2\2\2\65\t\3\2\2\2\66\67\7\b\2\2\678\5\6\4\28\13\3\2\2\29:\7\t\2"+ + "\2:;\5\6\4\2;\r\3\2\2\2<=\7\n\2\2=>\5\6\4\2>\17\3\2\2\2?@\7\13\2\2@A\5"+ + "\6\4\2A\21\3\2\2\2BC\7\f\2\2CD\5\6\4\2D\23\3\2\2\2EF\7\r\2\2FG\5\6\4\2"+ + "G\25\3\2\2\2\6\34%-\64"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index eadca649..f4422ff7 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -28,8 +28,8 @@ public class UnifyWithoutWildcards { @Test public void adapt() throws InterruptedException, IOException, ClassNotFoundException { ConstraintSet testSet = new ConstraintSet<>(); - List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); - List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + 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)); From 4a2b901465e5c73149d958b82a1b8fbb01e5ab06 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 13 Mar 2018 14:53:27 +0100 Subject: [PATCH 11/18] =?UTF-8?q?aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/sat/asp/model/ASPRule.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java index 24ed9269..20d221f9 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -1,12 +1,10 @@ package de.dhbwstuttgart.sat.asp.model; public enum ASPRule { - ASP_GENERIC_TYPE_NAME("genericType"), ASP_PAIR_EQUALS_NAME("equals"), ASP_PAIR_SMALLER_NAME("smaller"), ASP_PAIR_SMALLER_DOT_NAME("smallerDot"), ASP_PARAMLIST_NAME("param"), - ASP_PARAMLISTNUMERATION_NAME("paramNum"), ASP_PARAMLIST_END_POINTER("null"), ASP_TYPE("type"), ASP_FCTYPE("typeFC"), From 6a5ababa519065da29bf37d7adf33fbf9ab49063 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 14 Mar 2018 14:32:36 +0100 Subject: [PATCH 12/18] =?UTF-8?q?ANTLR=20Parsetreewalker=20f=C3=BCr=20ASP?= =?UTF-8?q?=20Result=20implementieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sat/asp/parser/ASPParser.java | 181 ++++++++---------- .../sat/asp/parser/model/ParsedType.java | 4 +- 2 files changed, 83 insertions(+), 102 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index cfb59933..ce43a01f 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -8,14 +8,12 @@ import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultBaseListener; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer; -import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultListener; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser; import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.tree.ParseTreeWalker; import javax.json.Json; @@ -36,11 +34,13 @@ import java.util.stream.Collectors; * -> Eigentlich nur die korrekten Namen der Typen und TPHs */ public class ASPParser extends UnifyResultBaseListener { - private final Collection originalTPHs; + private Collection originalTPHs; private ResultSet resultSet; private Map types = new HashMap<>(); private Set tphs = new HashSet<>(); private Map parameterLists = new HashMap<>(); + private Set equalsRelations = new HashSet<>(); + private Set smallerRelations = new HashSet<>(); /** * Parst clingo output welcher als JSON (option --outf=2) ausgibt @@ -53,36 +53,74 @@ public class ASPParser extends UnifyResultBaseListener { @Override public void enterParameter(UnifyResultParser.ParameterContext ctx) { + //Linked List (pointer, Type, nextPointer) + List params = parseParameterList(ctx.parameterList()); + parameterLists.put(params.get(0), new ParameterListNode(params.get(1), params.get(2))); + } + + private static class Relation { + public final String right; + public final String left; + + Relation(String leftType, String rightType){ + this.left = leftType; + this.right = rightType; + } + + @Override + public int hashCode() { + return (left+right).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Relation) + return (right+left).equals(((Relation) obj).left+((Relation) obj).right); + return super.equals(obj); + } + } + + private List parseParameterList(UnifyResultParser.ParameterListContext ctx){ + return ctx.value().stream().map(v -> + //ASPStringConverter.fromConstant(v.getText()) + v.getText() + ).collect(Collectors.toList()); } @Override public void enterEquals(UnifyResultParser.EqualsContext ctx) { - List parameterList = ctx.parameterList().value().stream().map(v -> v.getText()).collect(Collectors.toList()); - if(parameterList.size()<2)throw new DebugException("Fehler in Regex"); + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Equals-Regel"); String ls = parameterList.get(0); String rs = parameterList.get(1); - /* - RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(ls); - RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(rs); - if(lsType instanceof TypePlaceholder && rsType instanceof RefType) - return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder) lsType, rsType); - else if(lsType instanceof TypePlaceholder && rsType instanceof TypePlaceholder) - return new PairTPHEqualTPH((TypePlaceholder)lsType, (TypePlaceholder)rsType); - else throw new NotImplementedException(); - */ + equalsRelations.add(new Relation(ls, rs)); + } @Override public void enterSmaller(UnifyResultParser.SmallerContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Smaller-Regel"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + smallerRelations.add(new Relation(ls, rs)); } @Override public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<1)throw new DebugException("Fehler in typeVar-Regel"); + tphs.add(parameterList.get(0)); } @Override - public void enterType(UnifyResultParser.TypeContext ctx) { - super.enterType(ctx); + public void enterType(UnifyResultParser.TypeContext ctx){ + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<3)throw new DebugException("Fehler in Type-Regel"); + String name = parameterList.get(0); + String typeName = parameterList.get(1); + String paramPointer = parameterList.get(2); + types.put(name, new ParsedType(typeName, paramPointer)); } /* @@ -104,62 +142,46 @@ public class ASPParser extends UnifyResultBaseListener { } */ private ASPParser(String toParse, Collection oldPlaceholders){ + System.out.println(toParse); this.originalTPHs = oldPlaceholders; + + /* JsonObject jsonResult = Json.createReader(new StringReader(toParse)).readObject(); JsonArray results = jsonResult.getJsonArray("Call").getJsonObject(0). getJsonArray("Witnesses").getJsonObject(0). getJsonArray("Value"); + //Im ersten Schritt werden alle Regeln geparst + String completeResult = ""; + for(int i = 0; i ret = new HashSet<>(); //Zuerst die params und typeVars: - for(int i = 0; i parsedType.params).collect(Collectors.toList())){ } + //Dann die Equalsdot Statements - for(int i = 0; i params = new ArrayList<>(); ParsedType t = types.get(name); - for(String param : t.params){ + + for(String param : getParams(t.params)){ params.add(this.getType(param)); } return new RefType(new JavaClassName(ASPStringConverter.fromConstant(t.name)), params, new NullToken()); @@ -189,49 +212,6 @@ public class ASPParser extends UnifyResultBaseListener { this.nextNode = next; } } - private void parseParameter(String statement){ - //param(pointer, typ, nextPointer - Pattern p = Pattern.compile(ASPRule.ASP_PARAMLIST_NAME+"\\(([^,]+),([^,]+),([^,]+)\\)"); - Matcher m = p.matcher(statement); - boolean b = m.matches(); - if(b){ - if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); - String pointer = m.group(1); - String type = m.group(2); - String next = m.group(3); - if(next.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))next = null; - if(this.parameterLists.containsKey(pointer)){ - throw new DebugException("Fehler in Ergebnisparsen"); - } - this.parameterLists.put(pointer,new ParameterListNode(type, next)); - } - } - - private void parseTypeVar(String statement){ - Pattern p = Pattern.compile(ASPRule.ASP_TYPE_VAR+"\\(([^,]+)\\)"); - Matcher m = p.matcher(statement); - boolean b = m.matches(); - if(b){ - if(m.groupCount()<1)throw new DebugException("Fehler in Regex"); - String name = m.group(1); - this.tphs.add(name); - } - } - - private void parseType(String statement){ - //Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) - Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+),([^,]+)\\)"); - Matcher m = p.matcher(statement); - boolean b = m.matches(); - if(b){ - if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); - String pointer = m.group(1); - String name = m.group(2); - String paraList = m.group(3); - List params = this.getParams(paraList); - this.types.put(pointer,new ParsedType(name, params)); - } - } private List getParams(String pointer) { List params = new ArrayList<>(); @@ -239,6 +219,7 @@ public class ASPParser extends UnifyResultBaseListener { while(pointer != null){ if(!parameterLists.containsKey(pointer)) throw new DebugException("Fehler in Ergebnisparsen"); + //TODO: Fehler in ASP. Die adapt Regel muss erkennen, wenn die Parameterliste auf der linken Seite kürzer ist und diese Rechtzeitig beenden ParameterListNode param = parameterLists.get(pointer); pointer = param.nextNode; params.add(param.type); diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java index c49ce831..d9e06e50 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java @@ -4,8 +4,8 @@ import java.util.List; public class ParsedType { public final String name; - public final List params; - public ParsedType(String name, List params){ + public final String params; + public ParsedType(String name, String params){ this.name = name; this.params = params; } From 7e5aa852d7fb90dd7b8215266df9bc23b4d237df Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 21 Mar 2018 10:35:46 +0100 Subject: [PATCH 13/18] Fehler fix --- src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index ce43a01f..80ebfc38 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -142,7 +142,7 @@ public class ASPParser extends UnifyResultBaseListener { } */ private ASPParser(String toParse, Collection oldPlaceholders){ - System.out.println(toParse); + //System.out.println(toParse); this.originalTPHs = oldPlaceholders; /* @@ -215,8 +215,8 @@ public class ASPParser extends UnifyResultBaseListener { private List getParams(String pointer) { List params = new ArrayList<>(); - if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params; while(pointer != null){ + if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params; if(!parameterLists.containsKey(pointer)) throw new DebugException("Fehler in Ergebnisparsen"); //TODO: Fehler in ASP. Die adapt Regel muss erkennen, wenn die Parameterliste auf der linken Seite kürzer ist und diese Rechtzeitig beenden From daf684deef0a3e07e8d93bf20a16d83a9c15fb89 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 23 Mar 2018 16:54:32 +0100 Subject: [PATCH 14/18] =?UTF-8?q?ASP=20Factory=20f=C3=BCr=20Algorithmus=20?= =?UTF-8?q?von=20Herr=20Gencay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sat/asp/model/ASPGencayRule.java | 25 +++ .../sat/asp/writer/ASPGencayFactory.java | 155 ++++++++++++++++++ test/asp/UnifyWithoutWildcards.java | 3 +- test/asp/gencay/GeneratorTest.java | 50 ++++++ 4 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java create mode 100644 src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java create mode 100644 test/asp/gencay/GeneratorTest.java diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java new file mode 100644 index 00000000..867acd58 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -0,0 +1,25 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPGencayRule { + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("sub"), + ASP_PAIR_SMALLER_DOT_NAME("subEq"), + ASP_PARAMLIST_NAME("paramEq"), + ASP_FC_PARAMLIST_NAME("param"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("typeEq"), + ASP_FCTYPE("type"), + ASP_TYPE_VAR("var"), + ASP_GENERIC_VAR("pph"); + + private final String text; + + private ASPGencayRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java new file mode 100644 index 00000000..5a56c7a7 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -0,0 +1,155 @@ +package de.dhbwstuttgart.sat.asp.writer; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPGencayRule; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; +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.unify.model.PairOperator; + +import java.sql.Ref; +import java.util.*; + +public class ASPGencayFactory implements TypeVisitor { + + ASPWriter writer = new ASPWriter(); + boolean isFCType = false; + + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPGencayFactory factory = new ASPGencayFactory(); + factory.convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + + return factory.writer.getASPFile(); + } + + private void convertFC(Collection classes) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes); + isFCType = true; + for(Pair fcp : fc){ + generateTheta((RefType) fcp.TA1); + generateTheta((RefType) fcp.TA2); + convertPair(fcp); + } + isFCType = false; + } + + private void generateTheta(RefType t){ + String statement = "theta"+t.getParaList().size() + "("; + statement += ASPStringConverter.toConstant(t.getName()); + statement+=")"; + writer.add(new ASPStatement(statement)); + } + + private void convertPair(Pair p){ + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); + ASPStatement pairStmt = null; + if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_EQUALS_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); + }else throw new NotImplementedException(); + writer.add(pairStmt); + } + + private ASPStatement makeStatement(String rule, String... params){ + String stmt = rule + "("; + for(String param : params){ + stmt += param + ","; + } + stmt = stmt.substring(0,stmt.length()-1); + stmt += ")"; + return new ASPStatement(stmt); + } + + private String convertParameterlist(List pointers){ + if(pointers.size()==1)return pointers.get(0); + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Iterator it = pointers.iterator(); + String p = pointer; + String paramname = ASPGencayRule.ASP_PARAMLIST_NAME.toString(); + if(this.isFCType)paramname = ASPGencayRule.ASP_FC_PARAMLIST_NAME.toString(); + if(!it.hasNext()){ + return ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + } + while (it.hasNext()){ + ASPStatement stmt; + String type = it.next(); + String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + if(it.hasNext()){ + stmt = makeStatement(paramname, p, type, nextP); + }else{ + stmt = makeStatement(paramname, p, type, + ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString()); + } + p = nextP; + writer.add(stmt); + } + return pointer; + } + + @Override + public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + List params = new ArrayList<>(); + params = generateParameter(refType); + params.remove(0); //Das erste ist der eigentliche Typ kein parameter + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); + writer.add(stmt); + return pointer; + } + + /** + * Erstellt die Parameterliste, wobei type auch schon als Parameter betrachtet wird. + */ + List generateParameter(RefType type){ + List ret = new ArrayList<>(); + ret.add(ASPStringConverter.toConstant(type.getName())); + for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){ + if(param instanceof RefType){ + ret.addAll(generateParameter((RefType) param)); + }else if(param instanceof TypePlaceholder){ + ret.add(param.acceptTV(this)); + }else throw new NotImplementedException(); + } + return ret; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + String name = ASPStringConverter.toConstant(typePlaceholder.getName()); + ASPStatement stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); + writer.add(stmt); + return name; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + } +} diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index f4422ff7..6f69d639 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -81,7 +81,8 @@ public class UnifyWithoutWildcards { return new ArrayList<>(); } } - private Collection getInvolvedTPHS(ConstraintSet toTest) { + + protected Collection getInvolvedTPHS(ConstraintSet toTest) { List ret = new ArrayList<>(); toTest.map((Pair p)-> { ret.addAll(p.TA1.acceptTV(new TPHExtractor())); diff --git a/test/asp/gencay/GeneratorTest.java b/test/asp/gencay/GeneratorTest.java new file mode 100644 index 00000000..d7e457b2 --- /dev/null +++ b/test/asp/gencay/GeneratorTest.java @@ -0,0 +1,50 @@ +package asp.gencay; + +import asp.UnifyWithoutWildcards; +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.sat.asp.writer.ASPGencayFactory; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +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.*; +import java.util.concurrent.ArrayBlockingQueue; + +public class GeneratorTest extends UnifyWithoutWildcards{ + @Test + public void matrix() 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)); + String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC()); + System.out.println(resultSet); + } + + private Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createClass(Matrix.class)); + //ret.add(ASTFactory.createObjectClass()); + //ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } +} + +class Matrix extends Vector> {} \ No newline at end of file From 4886976752ff34b0b6946562ee45561d0787b5cd Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 23 Mar 2018 17:20:15 +0100 Subject: [PATCH 15/18] Fehler in FCGenerator beheben --- .../SyntaxTreeGenerator/FCGenerator.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 211da847..01908522 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -43,8 +43,16 @@ public class FCGenerator { return getSuperTypes(forType, availableClasses, new HashMap<>()); } - //TODO: implements Interface auch als superklassen beachten - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { + /** + * + * @param forType + * @param availableClasses + * @param gtvs + * @return + * @throws ClassNotFoundException + */ + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, + HashMap gtvs) throws ClassNotFoundException { List params = new ArrayList<>(); //Die GTVs, die in forType hinzukommen: HashMap newGTVs = new HashMap<>(); @@ -79,7 +87,7 @@ public class FCGenerator { Beispie: Matrix extends Vector> Den ersten Parameter mit Vector austauschen und dort alle Generics zu den Typplaceholdern in gtvs austauschen */ - //Hier vermerken, welche Typen im der Superklasse ausgetauscht werden müssen + //Hier vermerken, welche Typen in der Superklasse ausgetauscht werden müssen Iterator itGenParams = superClass.getGenerics().iterator(); Iterator itSetParams = superType.getParaList().iterator(); while(itSetParams.hasNext()){ @@ -89,7 +97,9 @@ public class FCGenerator { newGTVs.put(itGenParams.next().getName(), setSetType); } - RefTypeOrTPHOrWildcardOrGeneric superRefType = superType.acceptTV(new TypeExchanger(newGTVs)); + //Für den superType kann man nun zum Austauschen der Generics wieder die gtvs nehmen: + //Die newGTVs sind nur für den superClass ClassOrInterface welches möglicherweise per reflection geladen wurde abgestimmt + RefTypeOrTPHOrWildcardOrGeneric superRefType = superType.acceptTV(new TypeExchanger(gtvs)); RefTypeOrTPHOrWildcardOrGeneric t1 = new RefType(forType.getClassName(), params, new NullToken()); RefTypeOrTPHOrWildcardOrGeneric t2 = superRefType; From 87a2ed811767df6bc408c0eabad27ad154d8df41 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 27 Mar 2018 14:25:54 +0200 Subject: [PATCH 16/18] =?UTF-8?q?APS=20GeneratorTest.simple=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sat/asp/writer/ASPGencayFactory.java | 53 +++++++++++++++---- test/asp/gencay/GeneratorTest.java | 15 +++++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java index 5a56c7a7..77ea36f3 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -12,11 +12,18 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import java.sql.Ref; import java.util.*; -public class ASPGencayFactory implements TypeVisitor { +public class ASPGencayFactory implements TypeVisitor { + /* TODO: + * Alle TPHs müssen als var(tph) definiert sein + * Wenn es eine Variable ist, dann direkt in die type-Regel schreiben: type(p, type, tph) + * Für die FCTypen eindeutige Namen für die pph-Regeln + * (ergibt sich von selbst, weil man einfach den Namen der TPH in der FC verwenden kann) + * paramOrder wird benötigt! + * + */ ASPWriter writer = new ASPWriter(); boolean isFCType = false; @@ -45,15 +52,14 @@ public class ASPGencayFactory implements TypeVisitor { } private void generateTheta(RefType t){ - String statement = "theta"+t.getParaList().size() + "("; - statement += ASPStringConverter.toConstant(t.getName()); - statement+=")"; - writer.add(new ASPStatement(statement)); + String rule = "theta"+t.getParaList().size() ; + String name = ASPStringConverter.toConstant(t.getName()); + writer.add(makeStatement(rule, name)); } private void convertPair(Pair p){ - String ls = p.TA1.acceptTV(this); - String rs = p.TA2.acceptTV(this); + String ls = generateFCType((RefType) p.TA1); + String rs = generateFCType((RefType) p.TA2); ASPStatement pairStmt = null; if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); @@ -65,6 +71,24 @@ public class ASPGencayFactory implements TypeVisitor { writer.add(pairStmt); } + private String generateFCType(RefType refType){ + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + if(refType.getParaList().size() > 1)throw new NotImplementedException(); + if(refType.getParaList().size() > 0){ + TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); + String pphName = ASPStringConverter.toConstant(typePlaceholder.getName()); + ASPStatement paramStmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), + pointer, pphName); + writer.add(paramStmt); + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); + writer.add(stmt); + return pointer; + } + private ASPStatement makeStatement(String rule, String... params){ String stmt = rule + "("; for(String param : params){ @@ -76,7 +100,7 @@ public class ASPGencayFactory implements TypeVisitor { } private String convertParameterlist(List pointers){ - if(pointers.size()==1)return pointers.get(0); + //if(pointers.size()==1)return pointers.get(0); String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); Iterator it = pointers.iterator(); String p = pointer; @@ -104,7 +128,7 @@ public class ASPGencayFactory implements TypeVisitor { @Override public String visit(RefType refType) { String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); - List params = new ArrayList<>(); + List params = null; params = generateParameter(refType); params.remove(0); //Das erste ist der eigentliche Typ kein parameter String typeName = ASPStringConverter.toConstant(refType.getName()); @@ -138,7 +162,14 @@ public class ASPGencayFactory implements TypeVisitor { @Override public String visit(TypePlaceholder typePlaceholder) { String name = ASPStringConverter.toConstant(typePlaceholder.getName()); - ASPStatement stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); + + ASPStatement stmt = null; + if(isFCType){ + stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), name); + } else { + stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); + } + writer.add(stmt); return name; } diff --git a/test/asp/gencay/GeneratorTest.java b/test/asp/gencay/GeneratorTest.java index d7e457b2..2c9fc46f 100644 --- a/test/asp/gencay/GeneratorTest.java +++ b/test/asp/gencay/GeneratorTest.java @@ -27,7 +27,20 @@ import java.util.concurrent.ArrayBlockingQueue; public class GeneratorTest extends UnifyWithoutWildcards{ @Test - public void matrix() throws InterruptedException, IOException, ClassNotFoundException { + public void simple() throws ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("java.util.List"), list1, new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.util.List"), list2, new NullToken()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + String resultSet = ASPGencayFactory.generateASP(testSet, + new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class)))); + System.out.println(resultSet); + } + + @Test + public void matrix() throws 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())); From 000c16b90779cddf914bc930d1f515aafbe0ee3c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 27 Mar 2018 23:56:12 +0200 Subject: [PATCH 17/18] Gencoy ASP Generator implementieren --- .../sat/asp/model/ASPGencayRule.java | 3 +- .../sat/asp/writer/ASPGencayFactory.java | 128 ++++++++++++++---- .../sat/asp/writer/ASPWriter.java | 6 + 3 files changed, 111 insertions(+), 26 deletions(-) diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java index 867acd58..e16592cc 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -10,7 +10,8 @@ public enum ASPGencayRule { ASP_TYPE("typeEq"), ASP_FCTYPE("type"), ASP_TYPE_VAR("var"), - ASP_GENERIC_VAR("pph"); + ASP_GENERIC_VAR("pph"), + ASP_PARAMLIST_ORDER("paramOrder"); private final String text; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java index 77ea36f3..11408c25 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -22,11 +22,39 @@ public class ASPGencayFactory implements TypeVisitor { * Für die FCTypen eindeutige Namen für die pph-Regeln * (ergibt sich von selbst, weil man einfach den Namen der TPH in der FC verwenden kann) * paramOrder wird benötigt! - * + * Nur bei parameterlisten > 1 + * paramOrder(paralistPointer, parameter, num) + * (ähnlich meiner paramNum) + * Trennung von FC und Eq: + * sub -> type -> param + * ...Eq -> typeEq -> paramEq + * type..(_,_,_p): p kann sein: + * 1. Variable + * 2. ParameterPointer + * 3. null */ ASPWriter writer = new ASPWriter(); boolean isFCType = false; + private static List generateVar(ConstraintSet constraints){ + List ret = new ArrayList<>(); + for(TypePlaceholder tph : getInvolvedTPHS(constraints)){ + ret.add(makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), + ASPStringConverter.toConstant(tph.getName()))); + } + return ret; + } + + protected static 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; + } + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ ASPGencayFactory factory = new ASPGencayFactory(); factory.convertFC(fcClasses); @@ -36,6 +64,7 @@ public class ASPGencayFactory implements TypeVisitor { factory.convertPair(p); } } + factory.writer.addAll(generateVar(constraints)); return factory.writer.getASPFile(); } @@ -58,8 +87,8 @@ public class ASPGencayFactory implements TypeVisitor { } private void convertPair(Pair p){ - String ls = generateFCType((RefType) p.TA1); - String rs = generateFCType((RefType) p.TA2); + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); ASPStatement pairStmt = null; if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); @@ -71,25 +100,7 @@ public class ASPGencayFactory implements TypeVisitor { writer.add(pairStmt); } - private String generateFCType(RefType refType){ - String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); - String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); - if(refType.getParaList().size() > 1)throw new NotImplementedException(); - if(refType.getParaList().size() > 0){ - TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); - String pphName = ASPStringConverter.toConstant(typePlaceholder.getName()); - ASPStatement paramStmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), - pointer, pphName); - writer.add(paramStmt); - } - String typeName = ASPStringConverter.toConstant(refType.getName()); - String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); - ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); - writer.add(stmt); - return pointer; - } - - private ASPStatement makeStatement(String rule, String... params){ + private static ASPStatement makeStatement(String rule, String... params){ String stmt = rule + "("; for(String param : params){ stmt += param + ","; @@ -125,8 +136,38 @@ public class ASPGencayFactory implements TypeVisitor { return pointer; } + //Wird zum erstellen der pph(..) Regeln gebraucht + private String currentFCTypePointer = ""; @Override public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + currentFCTypePointer = pointer; + String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + if(refType.getParaList().size() == 1 ){ + if(refType.getParaList().get(0) instanceof TypePlaceholder){ + TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); + paramPointer = typePlaceholder.acceptTV(this); + } + }else{ + List params = null; + params = generateParameter(refType); + params.remove(0); + paramPointer = convertParameterlist(params); + if(refType.getParaList().size()>1){ + //paramOrder generieren: + for(String param : params) { + ASPStatement pOstmt = makeStatement(ASPGencayRule.ASP_PARAMLIST_ORDER.toString(), + paramPointer, param); + writer.add(pOstmt); + } + } + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); + writer.add(stmt); + return pointer; + /* String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); List params = null; params = generateParameter(refType); @@ -136,12 +177,16 @@ public class ASPGencayFactory implements TypeVisitor { ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); writer.add(stmt); return pointer; + */ } /** * Erstellt die Parameterliste, wobei type auch schon als Parameter betrachtet wird. + * Die RefTypes werden nicht zu extra type-Regeln umgewandelt. Es wird nur ihr Name als Konstante benutzt + * Das funktioniert, weil nacher das ParamOrder zuteilt, welche Typen zusammenhängen. + * Diese funktion nur verwenden, wenn auch ein paramOrder generiert wird */ - List generateParameter(RefType type){ + private List generateParameter(RefType type){ List ret = new ArrayList<>(); ret.add(ASPStringConverter.toConstant(type.getName())); for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){ @@ -165,7 +210,8 @@ public class ASPGencayFactory implements TypeVisitor { ASPStatement stmt = null; if(isFCType){ - stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), name); + stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), + currentFCTypePointer, name); } else { stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); } @@ -183,4 +229,36 @@ public class ASPGencayFactory implements TypeVisitor { public String visit(GenericRefType genericRefType) { throw new NotImplementedException(); } -} + + 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<>(); + } + } + +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index e5e60882..57aef860 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.sat.asp.writer; +import java.util.Collection; import java.util.HashSet; +import java.util.List; public class ASPWriter { @@ -17,4 +19,8 @@ public class ASPWriter { } return ret; } + + public void addAll(Collection aspStatements) { + content.addAll(aspStatements); + } } From 22ff521d08790c7a336d3ca74bcda6955ddb332f Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Wed, 28 Mar 2018 15:35:04 +0200 Subject: [PATCH 18/18] =?UTF-8?q?Aufr=C3=A4umen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 01908522..c4fc5b58 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -161,8 +161,6 @@ public class FCGenerator { public RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType) { if(! gtvs.containsKey(genericRefType.getParsedName())) throw new DebugException("Dieser Fall darf nicht auftreten"); - //TODO: Diesen Dirty-Hack beseitigen. Fehler tritt bei java.lang.invoke.LambdaFormEditor$Transform$Kind auf. - //return UnifyTypeFactory.convert(ASTFactory.createObjectType()); return gtvs.get(genericRefType.getParsedName()); }