add ctx cache to merge op. now i get optimal graph it seems. doesn't seem longer for java.* with JavaLR. still landmine in Pattern.java, Foo.java. Made toDOTString sort predctx nodes by id for repeatable tests. updated unit tests.

This commit is contained in:
Terence Parr 2012-07-24 16:26:01 -07:00
parent ac4f00524e
commit 2ae4d4eebd
8 changed files with 291 additions and 228 deletions

View File

@ -131,7 +131,7 @@ public class ATNConfigSet implements Set<ATNConfig> {
// a previous (s,i,pi,_), merge with it and save result
boolean rootIsWildcard = !fullCtx;
PredictionContext merged =
PredictionContext.merge(existing.context, config.context, rootIsWildcard);
PredictionContext.merge(existing.context, config.context, contextCache, rootIsWildcard);
// no need to check for existing.context, config.context in cache
// since only way to create new graphs is "call rule" and here. We
// cache at both places.

View File

@ -1,5 +1,7 @@
package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
import java.util.Arrays;
import java.util.Iterator;
@ -123,13 +125,17 @@ public class ArrayPredictionContext extends PredictionContext {
* elements from this context and merge into new context.
*/
@Override
public PredictionContext popAll(int invokingState, boolean fullCtx) {
public PredictionContext popAll(int invokingState,
@NotNull PredictionContextCache contextCache,
boolean fullCtx)
{
int index = Arrays.binarySearch(this.invokingStates, invokingState);
if ( index < 0 ) {
return this;
}
PredictionContext newCtx = this.parents[index].popAll(invokingState, fullCtx);
PredictionContext newCtx =
this.parents[index].popAll(invokingState, contextCache, fullCtx);
for (int i = 0; i < this.invokingStates.length; i++) {
if (i == index) continue;
PredictionContext next;
@ -141,7 +147,7 @@ public class ArrayPredictionContext extends PredictionContext {
this.invokingStates[i]);
}
boolean rootIsWildcard = fullCtx;
newCtx = merge(newCtx, next, rootIsWildcard);
newCtx = merge(newCtx, next, contextCache, rootIsWildcard);
}
return newCtx;

View File

@ -241,7 +241,7 @@ import java.util.Set;
*/
public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
public static boolean debug = false;
public static boolean debug_list_atn_decisions = true;
public static boolean debug_list_atn_decisions = false;
public static boolean dfa_debug = false;
public static boolean retry_debug = false;
@ -378,6 +378,7 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
if ( dfa_debug ) System.out.println("ctx sensitive state "+outerContext+" in "+s);
boolean loopsSimulateTailRecursion = true;
boolean fullCtx = false;
contextCache = new PredictionContextCache("predict ctx cache built in execDFA");
ATNConfigSet s0_closure =
computeStartState(dfa.atnStartState, outerContext,
greedy, loopsSimulateTailRecursion,
@ -388,6 +389,7 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
outerContext,
decState.getNumberOfTransitions(),
greedy);
contextCache = null;
return fullCtxSet.uniqueAlt;
}
if ( s.isAcceptState ) {
@ -419,7 +421,9 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
" at DFA state "+s.stateNumber);
}
contextCache = new PredictionContextCache("predict ctx cache built in execDFA");
alt = execATN(dfa, s, input, startIndex, outerContext);
contextCache = null;
// this adds edge even if next state is accept for
// same alt; e.g., s0-A->:s1=>2-B->:s2=>2
// TODO: This next stuff kills edge, but extra states remain. :(
@ -942,7 +946,7 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
boolean collectPredicates,
boolean greedy, boolean loopsSimulateTailRecursion)
{
// System.out.println(PredictionContext.toDotString(config.context));
// System.out.println(PredictionContext.toDOTString(config.context));
final int initialDepth = 0;
closureCheckingStopStateAndLoopRecursion(config, configs, closureBusy, collectPredicates, greedy,
@ -1017,7 +1021,9 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
if ( debug ) System.out.print("Loop end; pop, stack=" + config.context);
LoopEndState end = (LoopEndState)config.state;
// pop all the way back until we don't see the loopback state anymore
config.context = config.context.popAll(end.loopBackStateNumber, configs.fullCtx);
config.context = config.context.popAll(end.loopBackStateNumber,
contextCache,
configs.fullCtx);
if ( debug ) System.out.println(" becomes "+config.context);
}
}

View File

@ -7,13 +7,16 @@ import org.antlr.v4.runtime.misc.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class PredictionContext implements Iterable<SingletonPredictionContext> {
public abstract class PredictionContext implements Iterable<SingletonPredictionContext>,
Comparable<PredictionContext> // to sort node lists by id
{
/** Represents $ in local ctx prediction, which means wildcard. *+x = *. */
public static final EmptyPredictionContext EMPTY = new EmptyPredictionContext();
@ -68,7 +71,14 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
return this == EMPTY;
}
public abstract PredictionContext popAll(int invokingState, boolean fullCtx);
public abstract PredictionContext popAll(int invokingState,
@NotNull PredictionContextCache contextCache,
boolean fullCtx);
@Override
public int compareTo(PredictionContext o) { // used for toDotString to print nodes in order
return id - o.id;
}
@Override
public int hashCode() {
@ -109,6 +119,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// dispatch
public static PredictionContext merge(PredictionContext a, PredictionContext b,
@NotNull PredictionContextCache contextCache,
boolean rootIsWildcard)
{
if ( (a==null&&b==null) || a.equals(b) ) return a; // share same graph if both same
@ -116,6 +127,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
if ( a instanceof SingletonPredictionContext && b instanceof SingletonPredictionContext) {
return mergeSingletons((SingletonPredictionContext)a,
(SingletonPredictionContext)b,
contextCache,
rootIsWildcard);
}
@ -134,19 +146,21 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
b = new ArrayPredictionContext((SingletonPredictionContext)b);
}
return mergeArrays((ArrayPredictionContext) a, (ArrayPredictionContext) b,
contextCache,
rootIsWildcard);
}
// http://www.antlr.org/wiki/download/attachments/32014352/singleton-merge.png
public static PredictionContext mergeSingletons(SingletonPredictionContext a,
SingletonPredictionContext b,
@NotNull PredictionContextCache contextCache,
boolean rootIsWildcard)
{
PredictionContext rootMerge = mergeRoot(a, b, rootIsWildcard);
PredictionContext rootMerge = mergeRoot(a, b, contextCache, rootIsWildcard);
if ( rootMerge!=null ) return rootMerge;
if ( a.invokingState==b.invokingState ) { // a == b
PredictionContext parent = merge(a.parent, b.parent, rootIsWildcard);
PredictionContext parent = merge(a.parent, b.parent, contextCache, rootIsWildcard);
// if parent is same as existing a or b parent or reduced to a parent, return it
if ( parent == a.parent ) return a; // ax + bx = ax, if a=b
if ( parent == b.parent ) return b; // ax + bx = bx, if a=b
@ -154,7 +168,9 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// merge parents x and y, giving array node with x,y then remainders
// of those graphs. dup a, a' points at merged array
// new joined parent so create new singleton pointing to it, a'
return new SingletonPredictionContext(parent, a.invokingState);
PredictionContext a_ = new SingletonPredictionContext(parent, a.invokingState);
a_ = contextCache.add(a_);
return a_;
}
else { // a != b payloads differ
// see if we can collapse parents due to $+x parents if local ctx
@ -174,7 +190,9 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
payloads[1] = a.invokingState;
}
PredictionContext[] parents = {singleParent, singleParent};
return new ArrayPredictionContext(parents, payloads);
PredictionContext a_ = new ArrayPredictionContext(parents, payloads);
a_ = contextCache.add(a_);
return a_;
}
// parents differ and can't merge them. Just pack together
// into array; can't merge.
@ -186,7 +204,9 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
payloads[1] = a.invokingState;
parents = new PredictionContext[] {b.parent, a.parent};
}
return new ArrayPredictionContext(parents, payloads);
PredictionContext a_ = new ArrayPredictionContext(parents, payloads);
a_ = contextCache.add(a_);
return a_;
}
}
@ -195,6 +215,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
/** Handle case where at least one of a or b is $ (EMPTY) */
public static PredictionContext mergeRoot(SingletonPredictionContext a,
SingletonPredictionContext b,
@NotNull PredictionContextCache contextCache,
boolean rootIsWildcard)
{
if ( rootIsWildcard ) {
@ -206,15 +227,17 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
if ( a == EMPTY ) { // $ + x = [$,x]
int[] payloads = {EMPTY_FULL_CTX_INVOKING_STATE, b.invokingState};
PredictionContext[] parents = {null, b.parent};
ArrayPredictionContext joined =
PredictionContext joined =
new ArrayPredictionContext(parents, payloads);
joined = contextCache.add(joined);
return joined;
}
if ( b == EMPTY ) { // x + $ = [$,x] ($ is always first if present)
int[] payloads = {EMPTY_FULL_CTX_INVOKING_STATE, a.invokingState};
PredictionContext[] parents = {null, a.parent};
ArrayPredictionContext joined =
PredictionContext joined =
new ArrayPredictionContext(parents, payloads);
joined = contextCache.add(joined);
return joined;
}
}
@ -224,6 +247,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// http://www.antlr.org/wiki/download/attachments/32014352/array-merge.png
public static PredictionContext mergeArrays(ArrayPredictionContext a,
ArrayPredictionContext b,
@NotNull PredictionContextCache contextCache,
boolean rootIsWildcard)
{
// merge sorted payloads a + b => M
@ -252,7 +276,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
mergedInvokingStates[k] = payload;
}
else { // ax+ay -> a'[x,y]
PredictionContext mergedParent = merge(a_parent, b_parent, rootIsWildcard);
PredictionContext mergedParent =
merge(a_parent, b_parent, contextCache, rootIsWildcard);
mergedParents[k] = mergedParent;
mergedInvokingStates[k] = payload;
}
@ -298,16 +323,19 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
if ( p < lastSlot ) {
int n = p+1; // how many slots we really used in merge
if ( n == 1 ) { // for just one merged element, return singleton top
return new SingletonPredictionContext(mergedParents[0],
mergedInvokingStates[0]);
PredictionContext a_ = new SingletonPredictionContext(mergedParents[0],
mergedInvokingStates[0]);
a_ = contextCache.add(a_);
return a_;
}
mergedParents = Arrays.copyOf(mergedParents, n);
mergedInvokingStates = Arrays.copyOf(mergedInvokingStates, n);
}
}
ArrayPredictionContext M =
PredictionContext M =
new ArrayPredictionContext(mergedParents, mergedInvokingStates);
M = contextCache.add(M);
// if we created same array as a or b, return that instead
// TODO: track whether this is possible above during merge sort for speed
@ -336,13 +364,14 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
}
public static String toDotString(PredictionContext context) {
public static String toDOTString(PredictionContext context) {
if ( context==null ) return "";
StringBuilder buf = new StringBuilder();
buf.append("digraph G {\n");
buf.append("rankdir=LR;\n");
List<PredictionContext> nodes = getAllContextNodes(context);
Collections.sort(nodes);
for (PredictionContext current : nodes) {
if ( current instanceof SingletonPredictionContext ) {
@ -476,7 +505,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
public static List<PredictionContext> getAllContextNodes(PredictionContext context) {
List<PredictionContext> nodes = new ArrayList<PredictionContext>();
Map<PredictionContext, PredictionContext> visited =
new IdentityHashMap<PredictionContext, PredictionContext>();
new IdentityHashMap<PredictionContext, PredictionContext>();
getAllContextNodes_(context, nodes, visited);
return nodes;
}

View File

@ -24,7 +24,7 @@ public class PredictionContextCache {
if ( ctx==PredictionContext.EMPTY ) return PredictionContext.EMPTY;
PredictionContext existing = cache.get(ctx);
if ( existing!=null ) {
System.out.println(name+" reuses "+existing);
// System.out.println(name+" reuses "+existing);
return existing;
}
cache.put(ctx, ctx);

View File

@ -1,5 +1,7 @@
package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.NotNull;
import java.util.Iterator;
public class SingletonPredictionContext extends PredictionContext {
@ -49,9 +51,12 @@ public class SingletonPredictionContext extends PredictionContext {
}
@Override
public PredictionContext popAll(int invokingState, boolean fullCtx) {
public PredictionContext popAll(int invokingState,
@NotNull PredictionContextCache contextCache,
boolean fullCtx)
{
if ( invokingState == this.invokingState ) {
return parent.popAll(invokingState, fullCtx);
return parent.popAll(invokingState, contextCache, fullCtx);
}
return this;
}

11
tool/playground/Bar.java Normal file
View File

@ -0,0 +1,11 @@
class Bar {
private int bitsOrSingle(int bits, int ch) {
boolean d =
!(3==4 &&
(ch == 0xff || ch == 0xb5 ||
// ch == 0x53 || ch == 0x73 ||
// ch == 0x4b || ch == 0x6b ||
ch == 0xc5 || ch == 0xe5));
return 9;
}
}

View File

@ -3,13 +3,17 @@ package org.antlr.v4.test;
import junit.framework.TestCase;
import org.antlr.v4.runtime.atn.ArrayPredictionContext;
import org.antlr.v4.runtime.atn.PredictionContext;
import org.antlr.v4.runtime.atn.PredictionContextCache;
import org.antlr.v4.runtime.atn.SingletonPredictionContext;
import org.junit.Test;
public class TestGraphNodes extends TestCase {
PredictionContextCache contextCache;
@Override
protected void setUp() throws Exception {
PredictionContext.globalNodeCount = 1;
contextCache = new PredictionContextCache("testing");
}
public boolean rootIsWildcard() { return true; }
@ -18,188 +22,190 @@ public class TestGraphNodes extends TestCase {
@Test public void test_$_$() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY,
PredictionContext.EMPTY,
contextCache,
rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_$_$_fullctx() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY,
PredictionContext.EMPTY,
contextCache,
fullCtx());
System.out.println(PredictionContext.toDotString(r));
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_x_$() {
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_x_$_fullctx() {
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s2 [shape=box, label=\"[$, 9]\"];\n" +
" s0 [label=\"$\"];\n" +
" s2 [shape=box, label=\"[$, 9]\"];\n" +
" s2->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_$_x() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY, x(), rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY, x(), contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_$_x_fullctx() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY, x(), fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY, x(), contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s2 [shape=box, label=\"[$, 9]\"];\n" +
" s0 [label=\"$\"];\n" +
" s2 [shape=box, label=\"[$, 9]\"];\n" +
" s2->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a_a() {
PredictionContext r = PredictionContext.merge(a(), a(), rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a(), a(), contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"1\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a$_ax() {
PredictionContext a1 = a();
PredictionContext x = x();
PredictionContext a2 = createSingleton(x, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"1\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a$_ax_fullctx() {
PredictionContext a1 = a();
PredictionContext x = x();
PredictionContext a2 = createSingleton(x, 1);
PredictionContext r = PredictionContext.merge(a1, a2, fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [label=\"1\"];\n" +
" s4 [shape=box, label=\"[$, 9]\"];\n" +
" s0 [label=\"$\"];\n" +
" s5->s4;\n" +
" s4 [shape=box, label=\"[$, 9]\"];\n" +
" s5 [label=\"1\"];\n" +
" s4->s0 [label=\"parent[1]\"];\n" +
" s5->s4;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax$_a$() {
PredictionContext x = x();
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = a();
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"1\"];\n" +
" s3->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax$_a$_fullctx() {
PredictionContext x = x();
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = a();
PredictionContext r = PredictionContext.merge(a1, a2, fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [label=\"1\"];\n" +
" s4 [shape=box, label=\"[$, 9]\"];\n" +
" s0 [label=\"$\"];\n" +
" s5->s4;\n" +
" s4 [shape=box, label=\"[$, 9]\"];\n" +
" s5 [label=\"1\"];\n" +
" s4->s0 [label=\"parent[1]\"];\n" +
" s5->s4;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a_b() {
PredictionContext r = PredictionContext.merge(a(), b(), rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a(), b(), contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s3->s0 [label=\"parent[0]\"];\n" +
" s3->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax_ax_same() {
PredictionContext x = x();
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = createSingleton(x, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s2 [label=\"1\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s2->s1;\n" +
" s1 [label=\"9\"];\n" +
" s2 [label=\"1\"];\n" +
" s1->s0;\n" +
" s2->s1;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax_ax() {
@ -207,18 +213,18 @@ public class TestGraphNodes extends TestCase {
PredictionContext x2 = x();
PredictionContext a1 = createSingleton(x1, 1);
PredictionContext a2 = createSingleton(x2, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [label=\"1\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s3->s1;\n" +
" s1 [label=\"9\"];\n" +
" s3 [label=\"1\"];\n" +
" s1->s0;\n" +
" s3->s1;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_abx_abx() {
@ -228,20 +234,20 @@ public class TestGraphNodes extends TestCase {
PredictionContext b2 = createSingleton(x2, 2);
PredictionContext a1 = createSingleton(b1, 1);
PredictionContext a2 = createSingleton(b2, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [label=\"1\"];\n" +
" s3 [label=\"2\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s5->s3;\n" +
" s3->s1;\n" +
" s1 [label=\"9\"];\n" +
" s3 [label=\"2\"];\n" +
" s5 [label=\"1\"];\n" +
" s1->s0;\n" +
" s3->s1;\n" +
" s5->s3;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_abx_acx() {
@ -251,40 +257,40 @@ public class TestGraphNodes extends TestCase {
PredictionContext c = createSingleton(x2, 3);
PredictionContext a1 = createSingleton(b, 1);
PredictionContext a2 = createSingleton(c, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a1, a2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s8 [label=\"1\"];\n" +
" s7 [shape=box, label=\"[2, 3]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s8->s7;\n" +
" s1 [label=\"9\"];\n" +
" s7 [shape=box, label=\"[2, 3]\"];\n" +
" s8 [label=\"1\"];\n" +
" s1->s0;\n" +
" s7->s1 [label=\"parent[0]\"];\n" +
" s7->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
" s8->s7;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax_bx_same() {
PredictionContext x = x();
PredictionContext a = createSingleton(x, 1);
PredictionContext b = createSingleton(x, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s4->s1 [label=\"parent[0]\"];\n" +
" s4->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax_bx() {
@ -292,75 +298,75 @@ public class TestGraphNodes extends TestCase {
PredictionContext x2 = x();
PredictionContext a = createSingleton(x1, 1);
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\n" +
" s5 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s5->s1 [label=\"parent[0]\"];\n" +
" s5->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_ax_by() {
PredictionContext a = createSingleton(x(), 1);
PredictionContext b = createSingleton(y(), 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [shape=box, label=\"[1, 2]\"];\n" +
" s3 [label=\"10\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\n" +
" s3 [label=\"10\"];\n" +
" s5 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s3->s0;\n" +
" s5->s1 [label=\"parent[0]\"];\n" +
" s5->s3 [label=\"parent[1]\"];\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a$_bx() {
PredictionContext x2 = x();
PredictionContext a = a();
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s4->s0 [label=\"parent[0]\"];\n" +
" s4->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_a$_bx_fullctx() {
PredictionContext x2 = x();
PredictionContext a = a();
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s4->s0 [label=\"parent[0]\"];\n" +
" s4->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_aex_bfx() {
@ -370,25 +376,25 @@ public class TestGraphNodes extends TestCase {
PredictionContext f = createSingleton(x2, 6);
PredictionContext a = createSingleton(e, 1);
PredictionContext b = createSingleton(f, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(a, b, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s4 [label=\"6\"];\n" +
" s2 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"5\"];\n" +
" s1 [label=\"9\"];\n" +
" s7->s3 [label=\"parent[0]\"];\n" +
" s7->s4 [label=\"parent[1]\"];\n" +
" s4->s2;\n" +
" s2 [label=\"9\"];\n" +
" s3 [label=\"5\"];\n" +
" s4 [label=\"6\"];\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s2->s0;\n" +
" s3->s1;\n" +
" s1->s0;\n" +
" s4->s2;\n" +
" s7->s3 [label=\"parent[0]\"];\n" +
" s7->s4 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
// Array merges
@ -396,14 +402,14 @@ public class TestGraphNodes extends TestCase {
@Test public void test_A$_A$_fullctx() {
ArrayPredictionContext A1 = array(PredictionContext.EMPTY);
ArrayPredictionContext A2 = array(PredictionContext.EMPTY);
PredictionContext r = PredictionContext.merge(A1, A2, fullCtx());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, fullCtx());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [shape=box, label=\"[-2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aab_Ac() { // a,b + c
@ -412,18 +418,18 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext c = c();
ArrayPredictionContext A1 = array(a, b);
ArrayPredictionContext A2 = array(c);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s6->s0 [label=\"parent[0]\"];\n" +
" s6->s0 [label=\"parent[1]\"];\n" +
" s6->s0 [label=\"parent[2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aa_Aa() {
@ -431,16 +437,16 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext a2 = a();
ArrayPredictionContext A1 = array(a1);
ArrayPredictionContext A2 = array(a2);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [shape=box, label=\"[1]\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [shape=box, label=\"[1]\"];\n" +
" s3->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aa_Abc() { // a + b,c
@ -449,18 +455,18 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext c = c();
ArrayPredictionContext A1 = array(a);
ArrayPredictionContext A2 = array(b, c);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s6->s0 [label=\"parent[0]\"];\n" +
" s6->s0 [label=\"parent[1]\"];\n" +
" s6->s0 [label=\"parent[2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aac_Ab() { // a,c + b
@ -469,50 +475,50 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext c = c();
ArrayPredictionContext A1 = array(a, c);
ArrayPredictionContext A2 = array(b);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s6->s0 [label=\"parent[0]\"];\n" +
" s6->s0 [label=\"parent[1]\"];\n" +
" s6->s0 [label=\"parent[2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aab_Aa() { // a,b + a
ArrayPredictionContext A1 = array(a(), b());
ArrayPredictionContext A2 = array(a());
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s3->s0 [label=\"parent[0]\"];\n" +
" s3->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aab_Ab() { // a,b + b
ArrayPredictionContext A1 = array(a(), b());
ArrayPredictionContext A2 = array(b());
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [shape=box, label=\"[1, 2]\"];\n" +
" s3->s0 [label=\"parent[0]\"];\n" +
" s3->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aax_Aby() { // ax + by but in arrays
@ -520,21 +526,21 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext b = createSingleton(y(), 2);
ArrayPredictionContext A1 = array(a);
ArrayPredictionContext A2 = array(b);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s3 [label=\"10\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\n" +
" s3 [label=\"10\"];\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s1->s0;\n" +
" s3->s0;\n" +
" s7->s1 [label=\"parent[0]\"];\n" +
" s7->s3 [label=\"parent[1]\"];\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aax_Aay() { // ax + ay -> merged singleton a, array parent
@ -542,19 +548,19 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext a2 = createSingleton(y(), 1);
ArrayPredictionContext A1 = array(a1);
ArrayPredictionContext A2 = array(a2);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s8 [label=\"1\"];\n" +
" s7 [shape=box, label=\"[9, 10]\"];\n" +
" s0 [label=\"$\"];\n" +
" s8->s7;\n" +
" s7 [shape=box, label=\"[9, 10]\"];\n" +
" s8 [label=\"1\"];\n" +
" s7->s0 [label=\"parent[0]\"];\n" +
" s7->s0 [label=\"parent[1]\"];\n" +
" s8->s7;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaxc_Aayd() { // ax,c + ay,d -> merged a, array parent
@ -562,21 +568,21 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext a2 = createSingleton(y(), 1);
ArrayPredictionContext A1 = array(a1, c());
ArrayPredictionContext A2 = array(a2, d());
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s10 [shape=box, label=\"[1, 3, 4]\"];\n" +
" s0 [label=\"$\"];\n" +
" s9 [shape=box, label=\"[9, 10]\"];\n" +
" s10 [shape=box, label=\"[1, 3, 4]\"];\n" +
" s9->s0 [label=\"parent[0]\"];\n" +
" s9->s0 [label=\"parent[1]\"];\n" +
" s10->s9 [label=\"parent[0]\"];\n" +
" s10->s0 [label=\"parent[1]\"];\n" +
" s10->s0 [label=\"parent[2]\"];\n" +
" s9->s0 [label=\"parent[0]\"];\n" +
" s9->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaubv_Acwdx() { // au,bv + cw,dx -> [a,b,c,d]->[u,v,w,x]
@ -586,27 +592,27 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext d = createSingleton(x(), 4);
ArrayPredictionContext A1 = array(a, b);
ArrayPredictionContext A2 = array(c, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1, 2, 3, 4]\"];\n" +
" s7 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s5 [label=\"8\"];\n" +
" s3 [label=\"7\"];\n" +
" s1 [label=\"6\"];\n" +
" s3 [label=\"7\"];\n" +
" s5 [label=\"8\"];\n" +
" s7 [label=\"9\"];\n" +
" s11 [shape=box, label=\"[1, 2, 3, 4]\"];\n" +
" s1->s0;\n" +
" s3->s0;\n" +
" s5->s0;\n" +
" s7->s0;\n" +
" s11->s1 [label=\"parent[0]\"];\n" +
" s11->s3 [label=\"parent[1]\"];\n" +
" s11->s5 [label=\"parent[2]\"];\n" +
" s11->s7 [label=\"parent[3]\"];\n" +
" s7->s0;\n" +
" s5->s0;\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaubv_Abvdx() { // au,bv + bv,dx -> [a,b,d]->[u,v,x]
@ -616,24 +622,24 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext d = createSingleton(x(), 4);
ArrayPredictionContext A1 = array(a, b1);
ArrayPredictionContext A2 = array(b2, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s7 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"7\"];\n" +
" s1 [label=\"6\"];\n" +
" s3 [label=\"7\"];\n" +
" s7 [label=\"9\"];\n" +
" s11 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s1->s0;\n" +
" s3->s0;\n" +
" s7->s0;\n" +
" s11->s1 [label=\"parent[0]\"];\n" +
" s11->s3 [label=\"parent[1]\"];\n" +
" s11->s7 [label=\"parent[2]\"];\n" +
" s7->s0;\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaubv_Abwdx() { // au,bv + bw,dx -> [a,b,d]->[u,[v,w],x]
@ -643,25 +649,25 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext d = createSingleton(x(), 4);
ArrayPredictionContext A1 = array(a, b1);
ArrayPredictionContext A2 = array(b2, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s12 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s7 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s11 [shape=box, label=\"[7, 8]\"];\n" +
" s1 [label=\"6\"];\n" +
" s12->s1 [label=\"parent[0]\"];\n" +
" s12->s11 [label=\"parent[1]\"];\n" +
" s12->s7 [label=\"parent[2]\"];\n" +
" s7 [label=\"9\"];\n" +
" s11 [shape=box, label=\"[7, 8]\"];\n" +
" s12 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s1->s0;\n" +
" s7->s0;\n" +
" s11->s0 [label=\"parent[0]\"];\n" +
" s11->s0 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
" s12->s1 [label=\"parent[0]\"];\n" +
" s12->s11 [label=\"parent[1]\"];\n" +
" s12->s7 [label=\"parent[2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaubv_Abvdu() { // au,bv + bv,du -> [a,b,d]->[u,v,u]; u,v shared
@ -671,22 +677,22 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext d = createSingleton(u(), 4);
ArrayPredictionContext A1 = array(a, b1);
ArrayPredictionContext A2 = array(b2, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s3 [label=\"7\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"6\"];\n" +
" s3 [label=\"7\"];\n" +
" s11 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s1->s0;\n" +
" s3->s0;\n" +
" s11->s1 [label=\"parent[0]\"];\n" +
" s11->s3 [label=\"parent[1]\"];\n" +
" s11->s1 [label=\"parent[2]\"];\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}
@Test public void test_Aaubu_Acudu() { // au,bu + cu,du -> [a,b,c,d]->[u,u,u,u]
@ -696,21 +702,21 @@ public class TestGraphNodes extends TestCase {
SingletonPredictionContext d = createSingleton(u(), 4);
ArrayPredictionContext A1 = array(a, b);
ArrayPredictionContext A2 = array(c, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
PredictionContext r = PredictionContext.merge(A1, A2, contextCache, rootIsWildcard());
System.out.println(PredictionContext.toDOTString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1, 2, 3, 4]\"];\n" +
" s1 [label=\"6\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"6\"];\n" +
" s11 [shape=box, label=\"[1, 2, 3, 4]\"];\n" +
" s1->s0;\n" +
" s11->s1 [label=\"parent[0]\"];\n" +
" s11->s1 [label=\"parent[1]\"];\n" +
" s11->s1 [label=\"parent[2]\"];\n" +
" s11->s1 [label=\"parent[3]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
assertEquals(expecting, PredictionContext.toDOTString(r));
}