diff --git a/pom.xml b/pom.xml index 6dcac215..9f184c4f 100644 --- a/pom.xml +++ b/pom.xml @@ -76,7 +76,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/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 1714570e..c4fc5b58 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -43,16 +43,25 @@ 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<>(); //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())); } @@ -78,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()){ @@ -88,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; @@ -150,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()); } 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..9f529bbe 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -36,11 +36,13 @@ 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()); } 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/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java new file mode 100644 index 00000000..e16592cc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -0,0 +1,26 @@ +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"), + ASP_PARAMLIST_ORDER("paramOrder"); + + 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/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"), diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index b3534f47..80ebfc38 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -1,17 +1,20 @@ 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.antlr.UnifyResultBaseListener; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser; 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.*; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTreeWalker; import javax.json.Json; import javax.json.JsonArray; @@ -20,6 +23,7 @@ import java.io.StringReader; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Ablauf: @@ -29,12 +33,14 @@ 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 { - private final Collection originalTPHs; +public class ASPParser extends UnifyResultBaseListener { + 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 @@ -45,61 +51,137 @@ public class ASPParser { return new ASPParser(toParse, oldPlaceholders).resultSet; } + @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 = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Equals-Regel"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + 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){ + 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)); + } + + /* + 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){ + //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()); @@ -129,53 +212,14 @@ public class ASPParser { this.nextNode = next; } } - private void parseParameter(String statement){ - 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(2); - 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){ - 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)); - } - } 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 ParameterListNode param = parameterLists.get(pointer); pointer = param.nextNode; params.add(param.type); 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/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; } 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..1777eabd --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -0,0 +1,127 @@ +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.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.util.*; + +public class ASPFactory implements TypeVisitor{ + + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPFactory factory = new ASPFactory(); + factory.convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + + 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){ + 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; + if(!it.hasNext()){ + return ASPRule.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(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()); + String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, 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); + 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/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java new file mode 100644 index 00000000..11408c25 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -0,0 +1,264 @@ +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.util.*; + + +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! + * 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); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + factory.writer.addAll(generateVar(constraints)); + + 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 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); + 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 static 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; + } + + //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); + 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. + * 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 + */ + private 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 = null; + if(isFCType){ + stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), + currentFCTypePointer, name); + } else { + 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(); + } + + 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/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java deleted file mode 100644 index 676aa33f..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ /dev/null @@ -1,105 +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(); - private final String asp; - - 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 asp; - } - - 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 - ASPPairSmaller fcEntry = (ASPPairSmaller) convert(fcp); - writer.add(new ASPStatement(fcEntry.toASP())); - } - for(Pair cons : constraintSet){ - writer.add(new ASPStatement(convert(cons).toASP())); - } - - 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())); - } - - @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..57aef860 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,8 +1,8 @@ package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; - +import java.util.Collection; import java.util.HashSet; +import java.util.List; public class ASPWriter { @@ -19,4 +19,8 @@ public class ASPWriter { } return ret; } + + public void addAll(Collection aspStatements) { + content.addAll(aspStatements); + } } 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 8e732d64..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; - -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 + ")"; - } - - @Override - public String toASP() { - return toString(); - } - - @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 19730639..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java +++ /dev/null @@ -1,25 +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(ls.toASP())); - writer.add(new ASPStatement(rs.toASP())); - } - - public 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 178bb8fa..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 + "," + 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 5eab65be..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java +++ /dev/null @@ -1,31 +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 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 deleted file mode 100644 index 6e715dba..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 24b33eaf..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPTypeVar implements ASPType{ - private final String name; - - public ASPTypeVar(String name){ - this.name = name; - } - - @Override - public String toString() { - return ASPRule.ASP_TYPE_VAR+"("+ name +")"; - } - - @Override - public String toASP() { - return toString(); - } - - @Override - public String getPointer() { - return name; - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 67e1b14c..52921c5d 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/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/ClingoTest.java b/test/asp/ClingoTest.java index f47b644c..90635bde 100644 --- a/test/asp/ClingoTest.java +++ b/test/asp/ClingoTest.java @@ -2,7 +2,7 @@ package asp; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.sat.asp.parser.ASPParser; -import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.sat.asp.Clingo; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -25,7 +25,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 db697312..6f69d639 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -4,10 +4,11 @@ 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.ASPGenerator; +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; @@ -27,16 +28,19 @@ 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("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()),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)); - run(testSet); + ResultSet resultSet = run(testSet); + System.out.println(ResultSetPrinter.print(resultSet)); + 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); @@ -77,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..2c9fc46f --- /dev/null +++ b/test/asp/gencay/GeneratorTest.java @@ -0,0 +1,63 @@ +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 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())); + 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 diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index cea141f9..632d288e 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -1,7 +1,7 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -55,8 +55,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)