Tests erweitern und ANTLR Grammatik parsed nur noch unifier Regeln

This commit is contained in:
JanUlrich 2018-04-09 13:00:24 +02:00
parent 22ff521d08
commit e7e96d5943
10 changed files with 249 additions and 134 deletions

View File

@ -21,6 +21,7 @@ public class Clingo {
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/result.lp"));
programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp"));
} }
@ -37,7 +38,8 @@ public class Clingo {
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
commands.add(pathToClingo); commands.add(pathToClingo);
//commands.add("--outf=2"); //use JSON-Output //commands.add("--outf=2"); //use JSON-Output
commands.add("--outf=1"); //use JSON-Output commands.add("--outf=1"); //use Text-Output
commands.add("-n 0"); //Compute all models
for(File file : input){ for(File file : input){
commands.add(file.getPath()); commands.add(file.getPath());
} }
@ -47,6 +49,7 @@ public class Clingo {
InputStream output = clingo.getInputStream(); InputStream output = clingo.getInputStream();
clingo.waitFor(); clingo.waitFor();
String result = IOUtils.toString(output, StandardCharsets.UTF_8); String result = IOUtils.toString(output, StandardCharsets.UTF_8);
System.out.println(result);
return result; return result;
} }
} }

View File

@ -16,13 +16,7 @@ import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.antlr.v4.runtime.tree.ParseTreeWalker;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import java.io.StringReader;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -39,7 +33,7 @@ public class ASPParser extends UnifyResultBaseListener {
private Map<String, ParsedType> types = new HashMap<>(); private Map<String, ParsedType> types = new HashMap<>();
private Set<String> tphs = new HashSet<>(); private Set<String> tphs = new HashSet<>();
private Map<String, ParameterListNode> parameterLists = new HashMap<>(); private Map<String, ParameterListNode> parameterLists = new HashMap<>();
private Set<Relation> equalsRelations = new HashSet<>(); private Set<Relation> unifierRelations = new HashSet<>();
private Set<Relation> smallerRelations = new HashSet<>(); private Set<Relation> smallerRelations = new HashSet<>();
/** /**
@ -88,12 +82,12 @@ public class ASPParser extends UnifyResultBaseListener {
} }
@Override @Override
public void enterEquals(UnifyResultParser.EqualsContext ctx) { public void enterUnifier(UnifyResultParser.UnifierContext ctx) {
List<String> parameterList = parseParameterList(ctx.parameterList()); List<String> parameterList = parseParameterList(ctx.parameterList());
if(parameterList.size()<2)throw new DebugException("Fehler in Equals-Regel"); if(parameterList.size()<2)throw new DebugException("Fehler in Unifier-Regel");
String ls = parameterList.get(0); String ls = parameterList.get(0);
String rs = parameterList.get(1); String rs = parameterList.get(1);
equalsRelations.add(new Relation(ls, rs)); unifierRelations.add(new Relation(ls, rs));
} }
@ -172,7 +166,7 @@ public class ASPParser extends UnifyResultBaseListener {
//for(String paramPointer : types.values().stream().map(parsedType -> parsedType.params).collect(Collectors.toList())){ } //for(String paramPointer : types.values().stream().map(parsedType -> parsedType.params).collect(Collectors.toList())){ }
//Dann die Equalsdot Statements //Dann die Equalsdot Statements
for(Relation eq : equalsRelations){ for(Relation eq : unifierRelations){
RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(eq.left); RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(eq.left);
RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(eq.right); RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(eq.right);
if(lsType instanceof TypePlaceholder && rsType instanceof RefType) if(lsType instanceof TypePlaceholder && rsType instanceof RefType)

View File

@ -8,6 +8,7 @@ resultSetRule :
| smaller | smaller
| typeVar | typeVar
| type | type
| unifier
| otherRule | otherRule
; ;
@ -17,6 +18,7 @@ value : NAME
parameter : PARAMLIST_NAME parameterList; parameter : PARAMLIST_NAME parameterList;
equals : EQUALS_NAME parameterList; equals : EQUALS_NAME parameterList;
unifier : UNIFIER_NAME parameterList;
smaller : SMALLER_NAME parameterList; smaller : SMALLER_NAME parameterList;
typeVar : TYPEVAR_NAME parameterList; typeVar : TYPEVAR_NAME parameterList;
type : TYPE_NAME parameterList; type : TYPE_NAME parameterList;
@ -24,6 +26,7 @@ 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 //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'; PARAMLIST_NAME : 'param';
UNIFIER_NAME : 'unifier';
EQUALS_NAME : 'equals'; EQUALS_NAME : 'equals';
SMALLER_NAME : 'smaller'; SMALLER_NAME : 'smaller';
TYPEVAR_NAME : 'typeVar'; TYPEVAR_NAME : 'typeVar';

View File

@ -4,20 +4,22 @@ T__2=3
T__3=4 T__3=4
T__4=5 T__4=5
PARAMLIST_NAME=6 PARAMLIST_NAME=6
EQUALS_NAME=7 UNIFIER_NAME=7
SMALLER_NAME=8 EQUALS_NAME=8
TYPEVAR_NAME=9 SMALLER_NAME=9
TYPE_NAME=10 TYPEVAR_NAME=10
NAME=11 TYPE_NAME=11
WS=12 NAME=12
LINE_COMMENT=13 WS=13
LINE_COMMENT=14
'ANSWER'=1 'ANSWER'=1
'.'=2 '.'=2
'('=3 '('=3
','=4 ','=4
')'=5 ')'=5
'param'=6 'param'=6
'equals'=7 'unifier'=7
'smaller'=8 'equals'=8
'typeVar'=9 'smaller'=9
'type'=10 'typeVar'=10
'type'=11

View File

@ -83,6 +83,18 @@ public class UnifyResultBaseListener implements UnifyResultListener {
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitEquals(UnifyResultParser.EqualsContext ctx) { } @Override public void exitEquals(UnifyResultParser.EqualsContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterUnifier(UnifyResultParser.UnifierContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitUnifier(UnifyResultParser.UnifierContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *

View File

@ -17,8 +17,9 @@ public class UnifyResultLexer extends Lexer {
protected static final PredictionContextCache _sharedContextCache = protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache(); new PredictionContextCache();
public static final int 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, T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, UNIFIER_NAME=7,
SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; EQUALS_NAME=8, SMALLER_NAME=9, TYPEVAR_NAME=10, TYPE_NAME=11, NAME=12,
WS=13, LINE_COMMENT=14;
public static String[] channelNames = { public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN" "DEFAULT_TOKEN_CHANNEL", "HIDDEN"
}; };
@ -28,17 +29,19 @@ public class UnifyResultLexer extends Lexer {
}; };
public static final String[] ruleNames = { public static final String[] ruleNames = {
"T__0", "T__1", "T__2", "T__3", "T__4", "PARAMLIST_NAME", "EQUALS_NAME", "T__0", "T__1", "T__2", "T__3", "T__4", "PARAMLIST_NAME", "UNIFIER_NAME",
"SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" "EQUALS_NAME", "SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS",
"LINE_COMMENT"
}; };
private static final String[] _LITERAL_NAMES = { private static final String[] _LITERAL_NAMES = {
null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'unifier'",
"'typeVar'", "'type'" "'equals'", "'smaller'", "'typeVar'", "'type'"
}; };
private static final String[] _SYMBOLIC_NAMES = { private static final String[] _SYMBOLIC_NAMES = {
null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", null, null, null, null, null, null, "PARAMLIST_NAME", "UNIFIER_NAME",
"TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" "EQUALS_NAME", "SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS",
"LINE_COMMENT"
}; };
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
@ -98,31 +101,33 @@ public class UnifyResultLexer extends Lexer {
public ATN getATN() { return _ATN; } public ATN getATN() { return _ATN; }
public static final String _serializedATN = public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786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\7v\2\2TU\7{\2\2UV\7r\2\2VW\7g\2\2W\30\3\2\2\2XZ\t\2\2\2YX\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"+ "\2Z[\3\2\2\2[Y\3\2\2\2[\\\3\2\2\2\\\32\3\2\2\2]_\t\3\2\2^]\3\2\2\2_`\3"+
"\2\2\2\6\2QV^\3\b\2\2"; "\2\2\2`^\3\2\2\2`a\3\2\2\2ab\3\2\2\2bc\b\16\2\2c\34\3\2\2\2dh\7\'\2\2"+
"eg\n\4\2\2fe\3\2\2\2gj\3\2\2\2hf\3\2\2\2hi\3\2\2\2ik\3\2\2\2jh\3\2\2\2"+
"kl\b\17\2\2l\36\3\2\2\2\6\2[`h\3\b\2\2";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@ -4,20 +4,22 @@ T__2=3
T__3=4 T__3=4
T__4=5 T__4=5
PARAMLIST_NAME=6 PARAMLIST_NAME=6
EQUALS_NAME=7 UNIFIER_NAME=7
SMALLER_NAME=8 EQUALS_NAME=8
TYPEVAR_NAME=9 SMALLER_NAME=9
TYPE_NAME=10 TYPEVAR_NAME=10
NAME=11 TYPE_NAME=11
WS=12 NAME=12
LINE_COMMENT=13 WS=13
LINE_COMMENT=14
'ANSWER'=1 'ANSWER'=1
'.'=2 '.'=2
'('=3 '('=3
','=4 ','=4
')'=5 ')'=5
'param'=6 'param'=6
'equals'=7 'unifier'=7
'smaller'=8 'equals'=8
'typeVar'=9 'smaller'=9
'type'=10 'typeVar'=10
'type'=11

View File

@ -67,6 +67,16 @@ public interface UnifyResultListener extends ParseTreeListener {
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitEquals(UnifyResultParser.EqualsContext ctx); void exitEquals(UnifyResultParser.EqualsContext ctx);
/**
* Enter a parse tree produced by {@link UnifyResultParser#unifier}.
* @param ctx the parse tree
*/
void enterUnifier(UnifyResultParser.UnifierContext ctx);
/**
* Exit a parse tree produced by {@link UnifyResultParser#unifier}.
* @param ctx the parse tree
*/
void exitUnifier(UnifyResultParser.UnifierContext ctx);
/** /**
* Enter a parse tree produced by {@link UnifyResultParser#smaller}. * Enter a parse tree produced by {@link UnifyResultParser#smaller}.
* @param ctx the parse tree * @param ctx the parse tree

View File

@ -17,24 +17,26 @@ public class UnifyResultParser extends Parser {
protected static final PredictionContextCache _sharedContextCache = protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache(); new PredictionContextCache();
public static final int 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, T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, UNIFIER_NAME=7,
SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; EQUALS_NAME=8, SMALLER_NAME=9, TYPEVAR_NAME=10, TYPE_NAME=11, NAME=12,
WS=13, LINE_COMMENT=14;
public static final int public static final int
RULE_answer = 0, RULE_resultSetRule = 1, RULE_parameterList = 2, RULE_value = 3, 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_parameter = 4, RULE_equals = 5, RULE_unifier = 6, RULE_smaller = 7,
RULE_type = 8, RULE_otherRule = 9; RULE_typeVar = 8, RULE_type = 9, RULE_otherRule = 10;
public static final String[] ruleNames = { public static final String[] ruleNames = {
"answer", "resultSetRule", "parameterList", "value", "parameter", "equals", "answer", "resultSetRule", "parameterList", "value", "parameter", "equals",
"smaller", "typeVar", "type", "otherRule" "unifier", "smaller", "typeVar", "type", "otherRule"
}; };
private static final String[] _LITERAL_NAMES = { private static final String[] _LITERAL_NAMES = {
null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'unifier'",
"'typeVar'", "'type'" "'equals'", "'smaller'", "'typeVar'", "'type'"
}; };
private static final String[] _SYMBOLIC_NAMES = { private static final String[] _SYMBOLIC_NAMES = {
null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", null, null, null, null, null, null, "PARAMLIST_NAME", "UNIFIER_NAME",
"TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" "EQUALS_NAME", "SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS",
"LINE_COMMENT"
}; };
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
@ -113,21 +115,21 @@ public class UnifyResultParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(20); setState(22);
match(T__0); match(T__0);
setState(26); setState(28);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _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)) { while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << PARAMLIST_NAME) | (1L << UNIFIER_NAME) | (1L << EQUALS_NAME) | (1L << SMALLER_NAME) | (1L << TYPEVAR_NAME) | (1L << TYPE_NAME) | (1L << NAME))) != 0)) {
{ {
{ {
setState(21); setState(23);
resultSetRule(); resultSetRule();
setState(22); setState(24);
match(T__1); match(T__1);
} }
} }
setState(28); setState(30);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
} }
@ -160,6 +162,9 @@ public class UnifyResultParser extends Parser {
public TypeContext type() { public TypeContext type() {
return getRuleContext(TypeContext.class,0); return getRuleContext(TypeContext.class,0);
} }
public UnifierContext unifier() {
return getRuleContext(UnifierContext.class,0);
}
public OtherRuleContext otherRule() { public OtherRuleContext otherRule() {
return getRuleContext(OtherRuleContext.class,0); return getRuleContext(OtherRuleContext.class,0);
} }
@ -181,48 +186,55 @@ public class UnifyResultParser extends Parser {
ResultSetRuleContext _localctx = new ResultSetRuleContext(_ctx, getState()); ResultSetRuleContext _localctx = new ResultSetRuleContext(_ctx, getState());
enterRule(_localctx, 2, RULE_resultSetRule); enterRule(_localctx, 2, RULE_resultSetRule);
try { try {
setState(35); setState(38);
_errHandler.sync(this); _errHandler.sync(this);
switch (_input.LA(1)) { switch (_input.LA(1)) {
case PARAMLIST_NAME: case PARAMLIST_NAME:
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(29); setState(31);
parameter(); parameter();
} }
break; break;
case EQUALS_NAME: case EQUALS_NAME:
enterOuterAlt(_localctx, 2); enterOuterAlt(_localctx, 2);
{ {
setState(30); setState(32);
equals(); equals();
} }
break; break;
case SMALLER_NAME: case SMALLER_NAME:
enterOuterAlt(_localctx, 3); enterOuterAlt(_localctx, 3);
{ {
setState(31); setState(33);
smaller(); smaller();
} }
break; break;
case TYPEVAR_NAME: case TYPEVAR_NAME:
enterOuterAlt(_localctx, 4); enterOuterAlt(_localctx, 4);
{ {
setState(32); setState(34);
typeVar(); typeVar();
} }
break; break;
case TYPE_NAME: case TYPE_NAME:
enterOuterAlt(_localctx, 5); enterOuterAlt(_localctx, 5);
{ {
setState(33); setState(35);
type(); type();
} }
break; break;
case NAME: case UNIFIER_NAME:
enterOuterAlt(_localctx, 6); enterOuterAlt(_localctx, 6);
{ {
setState(34); setState(36);
unifier();
}
break;
case NAME:
enterOuterAlt(_localctx, 7);
{
setState(37);
otherRule(); otherRule();
} }
break; break;
@ -269,27 +281,27 @@ public class UnifyResultParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(37); setState(40);
match(T__2); match(T__2);
setState(38); setState(41);
value(); value();
setState(43); setState(46);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
while (_la==T__3) { while (_la==T__3) {
{ {
{ {
setState(39); setState(42);
match(T__3); match(T__3);
setState(40); setState(43);
value(); value();
} }
} }
setState(45); setState(48);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
} }
setState(46); setState(49);
match(T__4); match(T__4);
} }
} }
@ -327,20 +339,20 @@ public class UnifyResultParser extends Parser {
ValueContext _localctx = new ValueContext(_ctx, getState()); ValueContext _localctx = new ValueContext(_ctx, getState());
enterRule(_localctx, 6, RULE_value); enterRule(_localctx, 6, RULE_value);
try { try {
setState(50); setState(53);
_errHandler.sync(this); _errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) {
case 1: case 1:
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(48); setState(51);
match(NAME); match(NAME);
} }
break; break;
case 2: case 2:
enterOuterAlt(_localctx, 2); enterOuterAlt(_localctx, 2);
{ {
setState(49); setState(52);
resultSetRule(); resultSetRule();
} }
break; break;
@ -382,9 +394,9 @@ public class UnifyResultParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(52); setState(55);
match(PARAMLIST_NAME); match(PARAMLIST_NAME);
setState(53); setState(56);
parameterList(); parameterList();
} }
} }
@ -424,9 +436,51 @@ public class UnifyResultParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(55); setState(58);
match(EQUALS_NAME); match(EQUALS_NAME);
setState(56); setState(59);
parameterList();
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class UnifierContext extends ParserRuleContext {
public TerminalNode UNIFIER_NAME() { return getToken(UnifyResultParser.UNIFIER_NAME, 0); }
public ParameterListContext parameterList() {
return getRuleContext(ParameterListContext.class,0);
}
public UnifierContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_unifier; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterUnifier(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitUnifier(this);
}
}
public final UnifierContext unifier() throws RecognitionException {
UnifierContext _localctx = new UnifierContext(_ctx, getState());
enterRule(_localctx, 12, RULE_unifier);
try {
enterOuterAlt(_localctx, 1);
{
setState(61);
match(UNIFIER_NAME);
setState(62);
parameterList(); parameterList();
} }
} }
@ -462,13 +516,13 @@ public class UnifyResultParser extends Parser {
public final SmallerContext smaller() throws RecognitionException { public final SmallerContext smaller() throws RecognitionException {
SmallerContext _localctx = new SmallerContext(_ctx, getState()); SmallerContext _localctx = new SmallerContext(_ctx, getState());
enterRule(_localctx, 12, RULE_smaller); enterRule(_localctx, 14, RULE_smaller);
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(58); setState(64);
match(SMALLER_NAME); match(SMALLER_NAME);
setState(59); setState(65);
parameterList(); parameterList();
} }
} }
@ -504,13 +558,13 @@ public class UnifyResultParser extends Parser {
public final TypeVarContext typeVar() throws RecognitionException { public final TypeVarContext typeVar() throws RecognitionException {
TypeVarContext _localctx = new TypeVarContext(_ctx, getState()); TypeVarContext _localctx = new TypeVarContext(_ctx, getState());
enterRule(_localctx, 14, RULE_typeVar); enterRule(_localctx, 16, RULE_typeVar);
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(61); setState(67);
match(TYPEVAR_NAME); match(TYPEVAR_NAME);
setState(62); setState(68);
parameterList(); parameterList();
} }
} }
@ -546,13 +600,13 @@ public class UnifyResultParser extends Parser {
public final TypeContext type() throws RecognitionException { public final TypeContext type() throws RecognitionException {
TypeContext _localctx = new TypeContext(_ctx, getState()); TypeContext _localctx = new TypeContext(_ctx, getState());
enterRule(_localctx, 16, RULE_type); enterRule(_localctx, 18, RULE_type);
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(64); setState(70);
match(TYPE_NAME); match(TYPE_NAME);
setState(65); setState(71);
parameterList(); parameterList();
} }
} }
@ -588,13 +642,13 @@ public class UnifyResultParser extends Parser {
public final OtherRuleContext otherRule() throws RecognitionException { public final OtherRuleContext otherRule() throws RecognitionException {
OtherRuleContext _localctx = new OtherRuleContext(_ctx, getState()); OtherRuleContext _localctx = new OtherRuleContext(_ctx, getState());
enterRule(_localctx, 18, RULE_otherRule); enterRule(_localctx, 20, RULE_otherRule);
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(67); setState(73);
match(NAME); match(NAME);
setState(68); setState(74);
parameterList(); parameterList();
} }
} }
@ -610,24 +664,25 @@ public class UnifyResultParser extends Parser {
} }
public static final String _serializedATN = public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17I\4\2\t\2\4\3\t"+ "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\20O\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"+ "\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"+
"\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"+ "\f\t\f\3\2\3\2\3\2\3\2\7\2\35\n\2\f\2\16\2 \13\2\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\5\6\4\2M\27\3\2\2\2\6\36(\60\67";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@ -11,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResolvedType;
import de.dhbwstuttgart.typeinference.result.ResultSet; import de.dhbwstuttgart.typeinference.result.ResultSet;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.junit.Test; import org.junit.Test;
@ -34,8 +35,33 @@ public class UnifyWithoutWildcards {
RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet); ResultSet resultSet = run(testSet);
System.out.println(ResultSetPrinter.print(resultSet)); //System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() > 0; assert resultSet.results.size() > 0;
ResolvedType rsType = resultSet.resolveType(list2.get(1));
assert ((RefType)rsType.resolvedType).getName().equals("java.util.Map");
}
@Test
public void adaptNewParaListTest1() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Test1"), list1, new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet);
//System.out.println(ResultSetPrinter.print(resultSet));
assert resultSet.results.size() == 0; //Hier gibt es keine Lösung.
//TODO: Kann in zukünfigen Fällen eine Lösung geben
}
@Test
public void step1() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
TypePlaceholder t1 = TypePlaceholder.fresh(new NullToken());
RefType t2 = new RefType(new JavaClassName("java.lang.Object"), new ArrayList<>(), new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
ResultSet resultSet = run(testSet);
System.out.println(resultSet.results.size());
} }
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException { public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException {
@ -95,9 +121,12 @@ public class UnifyWithoutWildcards {
private Collection<ClassOrInterface> getFC() { private Collection<ClassOrInterface> getFC() {
Set<ClassOrInterface> ret = new HashSet<>(); Set<ClassOrInterface> ret = new HashSet<>();
ret.add(ASTFactory.createClass(Matrix.class)); ret.add(ASTFactory.createClass(Matrix.class));
ret.add(ASTFactory.createClass(Test1.class));
//ret.add(ASTFactory.createObjectClass()); //ret.add(ASTFactory.createObjectClass());
//ret.add(ASTFactory.createClass(java.util.List.class)); //ret.add(ASTFactory.createClass(java.util.List.class));
return ret; return ret;
} }
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{} private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
private class Test1<A> extends Object{}
} }