if merged array is 1 node, return singleton. more unit tests

This commit is contained in:
Terence Parr 2012-07-22 15:05:12 -07:00
parent 3edb35d95e
commit 44ae1dad0b
3 changed files with 433 additions and 115 deletions

View File

@ -23,6 +23,7 @@ public class ArrayPredictionContext extends PredictionContext {
public ArrayPredictionContext(PredictionContext[] parents, int[] invokingStates) {
super(calculateHashCode(parents, invokingStates));
assert parents!=null && parents.length>0;
assert invokingStates!=null && invokingStates.length>0;
// System.err.println("CREATE ARRAY: "+Arrays.toString(parents)+", "+Arrays.toString(invokingStates));
this.parents = parents;
this.invokingStates = invokingStates;

View File

@ -239,9 +239,9 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
if ( a.invokingState==b.invokingState ) { // a == b
PredictionContext parent = merge(a.parent, b.parent, rootIsWildcard);
// if parent is same as existing a or b parent, return it
if ( parent == a.parent ) return a; // ax + ax = ax
if ( parent == b.parent ) return b; // not sure can happen since merge(a,a) returns left a
// 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
// else: ax + ay = a'[x,y]
// merge parents x and y, giving array node with x,y then remainders
// of those graphs. dup a, a' points at merged array
@ -364,6 +364,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
k++;
}
// copy over any payloads remaining in either array
if (i < a.invokingStates.length) {
for (int p = i; p < a.invokingStates.length; p++) {
@ -380,6 +381,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
}
// trim merged
if ( k < mergedParents.length ) { // write index < last position; trim
int p = mergedParents.length-1;
@ -387,6 +389,10 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// p is now last non-null index
if ( p < mergedParents.length-1 ) {
int n = p+1;
if ( n == 1 ) { // for just one merged element, return singleton top
return new SingletonPredictionContext(mergedParents[0],
mergedInvokingStates[0]);
}
mergedParents = Arrays.copyOf(mergedParents, n);
mergedInvokingStates = Arrays.copyOf(mergedInvokingStates, n);
}
@ -422,13 +428,22 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
ArrayPredictionContext arr = (ArrayPredictionContext)current;
buf.append(" s").append(arr.id);
buf.append(" [shape=box, label=\"");
buf.append(Arrays.toString(arr.invokingStates));
buf.append("[");
boolean first = true;
for (int inv : arr.invokingStates) {
if ( !first ) buf.append(", ");
if ( inv == EMPTY_FULL_CTX_INVOKING_STATE ) buf.append("$");
else buf.append(inv);
first = false;
}
buf.append("]");
buf.append("\"];\n");
}
for (PredictionContext current : nodes) {
if ( current==EMPTY ) continue;
for (int i = 0; i < current.size(); i++) {
if ( current.getParent(i)==null ) continue;
String s = String.valueOf(current.id);
buf.append(" s").append(s);
buf.append("->");

View File

@ -6,8 +6,6 @@ import org.antlr.v4.runtime.atn.PredictionContext;
import org.antlr.v4.runtime.atn.SingletonPredictionContext;
import org.junit.Test;
import java.util.List;
public class TestGraphNodes extends TestCase {
@Override
protected void setUp() throws Exception {
@ -15,77 +13,198 @@ public class TestGraphNodes extends TestCase {
}
public boolean rootIsWildcard() { return true; }
public boolean fullCtx() { return false; }
@Test public void testBothEmpty() {
PredictionContext a = PredictionContext.EMPTY;
PredictionContext b = PredictionContext.EMPTY;
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$]", nodes.toString());
@Test public void test_$_$() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY,
PredictionContext.EMPTY,
rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void testLeftEmpty() {
PredictionContext a = PredictionContext.EMPTY;
PredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$]", nodes.toString());
@Test public void test_$_$_fullctx() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY,
PredictionContext.EMPTY,
fullCtx());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void testRightEmpty() {
PredictionContext a = PredictionContext.EMPTY;
PredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$]", nodes.toString());
@Test public void test_x_$() {
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void testSameSingleTops() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[1 $, $]", nodes.toString());
@Test public void test_x_$_fullctx() {
PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, 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->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_a$_ax$() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
@Test public void test_$_x() {
PredictionContext r = PredictionContext.merge(PredictionContext.EMPTY, x(), rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s0 [label=\"$\"];\n" +
"}\n";
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s2 [shape=box, label=\"[$, 9]\"];\n" +
" s0 [label=\"$\"];\n" +
" s2->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_a_a() {
PredictionContext r = PredictionContext.merge(a(), a(), rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s1->s0;\n" +
"}\n";
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());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[1 $, $]", nodes.toString());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s1->s0;\n" +
"}\n";
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));
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->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_ax$_a$() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x = x();
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext a2 = a();
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[1 $, $]", nodes.toString());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [label=\"1\"];\n" +
" s0 [label=\"$\"];\n" +
" s3->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void testDiffSingleTops() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, 2);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[[1 $, 2 $], $]", nodes.toString());
@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));
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->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_ax_ax() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
@Test public void test_a_b() {
PredictionContext r = PredictionContext.merge(a(), b(), 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->s0 [label=\"parent[0]\"];\n" +
" s3->s0 [label=\"parent[1]\"];\n" +
"}\n";
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());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[1 9 $, 9 $, $]", nodes.toString());
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->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_ax_ax_eq() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
@Test public void test_ax_ax() {
PredictionContext x1 = x();
PredictionContext x2 = x();
PredictionContext a1 = createSingleton(x1, 1);
PredictionContext a2 = createSingleton(x2, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
@ -103,8 +222,8 @@ public class TestGraphNodes extends TestCase {
}
@Test public void test_abx_abx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x1 = x();
PredictionContext x2 = x();
PredictionContext b1 = createSingleton(x1, 2);
PredictionContext b2 = createSingleton(x2, 2);
PredictionContext a1 = createSingleton(b1, 1);
@ -126,8 +245,8 @@ public class TestGraphNodes extends TestCase {
}
@Test public void test_abx_acx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x1 = x();
PredictionContext x2 = x();
PredictionContext b = createSingleton(x1, 2);
PredictionContext c = createSingleton(x2, 3);
PredictionContext a1 = createSingleton(b, 1);
@ -149,8 +268,8 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_ax_bx_same_x() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
@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());
@ -169,8 +288,8 @@ public class TestGraphNodes extends TestCase {
}
@Test public void test_ax_bx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x1 = x();
PredictionContext x2 = x();
PredictionContext a = createSingleton(x1, 1);
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
@ -188,9 +307,29 @@ public class TestGraphNodes extends TestCase {
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));
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" +
" s5->s1 [label=\"parent[0]\"];\n" +
" s5->s3 [label=\"parent[1]\"];\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_a$_bx() {
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext x2 = x();
PredictionContext a = a();
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
@ -205,9 +344,28 @@ public class TestGraphNodes extends TestCase {
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s4->s0 [label=\"parent[0]\"];\n" +
" s4->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_aex_bfx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x1 = x();
PredictionContext x2 = x();
PredictionContext e = createSingleton(x1, 5);
PredictionContext f = createSingleton(x2, 6);
PredictionContext a = createSingleton(e, 1);
@ -235,12 +393,12 @@ public class TestGraphNodes extends TestCase {
// Array merges
@Test public void test_Aab_Ac() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, 3);
ArrayPredictionContext A1 = create(a,b);
ArrayPredictionContext A2 = create(c);
@Test public void test_Aab_Ac() { // a,b + c
SingletonPredictionContext a = a();
SingletonPredictionContext b = b();
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));
String expecting =
@ -256,10 +414,10 @@ public class TestGraphNodes extends TestCase {
}
@Test public void test_Aa_Aa() {
SingletonPredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
ArrayPredictionContext A1 = create(a1);
ArrayPredictionContext A2 = create(a2);
SingletonPredictionContext a1 = a();
SingletonPredictionContext a2 = a();
ArrayPredictionContext A1 = array(a1);
ArrayPredictionContext A2 = array(a2);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
@ -272,12 +430,12 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aa_Abc() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, 3);
ArrayPredictionContext A1 = create(a);
ArrayPredictionContext A2 = create(b,c);
@Test public void test_Aa_Abc() { // a + b,c
SingletonPredictionContext a = a();
SingletonPredictionContext b = b();
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));
String expecting =
@ -292,12 +450,12 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aac_Ab() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, 3);
ArrayPredictionContext A1 = create(a,c);
ArrayPredictionContext A2 = create(b);
@Test public void test_Aac_Ab() { // a,c + b
SingletonPredictionContext a = a();
SingletonPredictionContext b = b();
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));
String expecting =
@ -312,12 +470,9 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aab_Aa() {
SingletonPredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
SingletonPredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
ArrayPredictionContext A1 = create(a1,b);
ArrayPredictionContext A2 = create(a2);
@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));
String expecting =
@ -331,12 +486,9 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aab_Ab() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext b1 = createSingleton(PredictionContext.EMPTY, 2);
SingletonPredictionContext b2 = createSingleton(PredictionContext.EMPTY, 2);
ArrayPredictionContext A1 = create(a,b1);
ArrayPredictionContext A2 = create(b2);
@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));
String expecting =
@ -350,19 +502,39 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aax_Aay() { // ax + ay -> merged a, array parent
SingletonPredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
SingletonPredictionContext a1 = createSingleton(x, 1);
SingletonPredictionContext y = createSingleton(PredictionContext.EMPTY, 10);
SingletonPredictionContext a2 = createSingleton(y, 1);
ArrayPredictionContext A1 = create(a1);
ArrayPredictionContext A2 = create(a2);
@Test public void test_Aax_Aby() { // ax + by but in arrays
SingletonPredictionContext a = createSingleton(x(), 1);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1]\"];\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s3 [label=\"10\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"9\"];\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));
}
@Test public void test_Aax_Aay() { // ax + ay -> merged singleton a, array parent
SingletonPredictionContext a1 = createSingleton(x(), 1);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [label=\"1\"];\n" +
" s9 [shape=box, label=\"[9, 10]\"];\n" +
" s0 [label=\"$\"];\n" +
" s11->s9;\n" +
@ -373,14 +545,10 @@ public class TestGraphNodes extends TestCase {
}
@Test public void test_Aaxc_Aayd() { // ax,c + ay,d -> merged a, array parent
SingletonPredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
SingletonPredictionContext a1 = createSingleton(x, 1);
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, 3);
SingletonPredictionContext y = createSingleton(PredictionContext.EMPTY, 10);
SingletonPredictionContext a2 = createSingleton(y, 1);
SingletonPredictionContext d = createSingleton(PredictionContext.EMPTY, 4);
ArrayPredictionContext A1 = create(a1,c);
ArrayPredictionContext A2 = create(a2,d);
SingletonPredictionContext a1 = createSingleton(x(), 1);
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));
String expecting =
@ -398,12 +566,146 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aaubv_Acwdx() { // au,bv + cw,dx -> [a,b,c,d]->[u,v,w,x]
SingletonPredictionContext a = createSingleton(u(), 1);
SingletonPredictionContext b = createSingleton(v(), 2);
SingletonPredictionContext c = createSingleton(w(), 3);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s13 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s5 [label=\"8\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"7\"];\n" +
" s1 [label=\"6\"];\n" +
" s13->s1 [label=\"parent[0]\"];\n" +
" s13->s3 [label=\"parent[1]\"];\n" +
" s13->s5 [label=\"parent[2]\"];\n" +
" s5->s0;\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aaubv_Abvdx() { // au,bv + bv,dx -> [a,b,d]->[u,v,x]
SingletonPredictionContext a = createSingleton(u(), 1);
SingletonPredictionContext b1 = createSingleton(v(), 2);
SingletonPredictionContext b2 = createSingleton(v(), 2);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s13 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s5 [label=\"8\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"7\"];\n" +
" s1 [label=\"6\"];\n" +
" s13->s1 [label=\"parent[0]\"];\n" +
" s13->s3 [label=\"parent[1]\"];\n" +
" s13->s5 [label=\"parent[2]\"];\n" +
" s5->s0;\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aaubv_Abwdx() { // au,bv + bw,dx -> [a,b,d]->[u,[v,w],x]
SingletonPredictionContext a = createSingleton(u(), 1);
SingletonPredictionContext b1 = createSingleton(v(), 2);
SingletonPredictionContext b2 = createSingleton(w(), 2);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s13 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s5 [label=\"8\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"7\"];\n" +
" s1 [label=\"6\"];\n" +
" s13->s1 [label=\"parent[0]\"];\n" +
" s13->s3 [label=\"parent[1]\"];\n" +
" s13->s5 [label=\"parent[2]\"];\n" +
" s5->s0;\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aaubv_Abvdu() { // au,bv + bv,du -> [a,b,d]->[u,v,u]; u,v shared
SingletonPredictionContext a = createSingleton(u(), 1);
SingletonPredictionContext b1 = createSingleton(v(), 2);
SingletonPredictionContext b2 = createSingleton(v(), 2);
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));
// TODO: doesn't merge parents
String expecting =
"\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
// ------------ SUPPORT -------------------------
protected SingletonPredictionContext a() {
return createSingleton(PredictionContext.EMPTY, 1);
}
private SingletonPredictionContext b() {
return createSingleton(PredictionContext.EMPTY, 2);
}
private SingletonPredictionContext c() {
return createSingleton(PredictionContext.EMPTY, 3);
}
private SingletonPredictionContext d() {
return createSingleton(PredictionContext.EMPTY, 4);
}
private SingletonPredictionContext u() {
return createSingleton(PredictionContext.EMPTY, 6);
}
private SingletonPredictionContext v() {
return createSingleton(PredictionContext.EMPTY, 7);
}
private SingletonPredictionContext w() {
return createSingleton(PredictionContext.EMPTY, 8);
}
private SingletonPredictionContext x() {
return createSingleton(PredictionContext.EMPTY, 9);
}
private SingletonPredictionContext y() {
return createSingleton(PredictionContext.EMPTY, 10);
}
public SingletonPredictionContext createSingleton(PredictionContext parent, int payload) {
SingletonPredictionContext a = new SingletonPredictionContext(parent, payload);
return a;
}
public ArrayPredictionContext create(SingletonPredictionContext... nodes) {
public ArrayPredictionContext array(SingletonPredictionContext... nodes) {
PredictionContext[] parents = new PredictionContext[nodes.length];
int[] invokingStates = new int[nodes.length];
for (int i=0; i<nodes.length; i++) {