clean up, add comments, fix graph unit tests, updated toDotString().

This commit is contained in:
Terence Parr 2012-07-21 15:10:15 -07:00
parent f78bf4d097
commit 991014d3f2
6 changed files with 178 additions and 170 deletions

View File

@ -33,13 +33,12 @@ import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
/** An ATN state, predicted alt, and syntactic/semantic context.
* The syntactic context is a pointer into the rule invocation
/** A tuple: (ATN state, predicted alt, syntactic, semantic context).
* The syntactic context is a graph-structured stack node whose
* path(s) to the root is the rule invocation(s)
* chain used to arrive at the state. The semantic context is
* the unordered set semantic predicates encountered before reaching
* the tree of semantic predicates encountered before reaching
* an ATN state.
*
* (state, alt, rule context, semantic context)
*/
public class ATNConfig {
/** The ATN state associated with this configuration */

View File

@ -4,12 +4,15 @@ import java.util.Arrays;
import java.util.Iterator;
public class ArrayPredictionContext extends PredictionContext {
// parent can be null only if full ctx mode and we make an array
// from EMPTY and non-empty. We merge EMPTY by null parent and
// invokingState = EMPTY_FULL_INVOKING_STATE
/** Parent can be null only if full ctx mode and we make an array
* from EMPTY and non-empty. We merge EMPTY by using null parent and
* invokingState == EMPTY_FULL_INVOKING_STATE
*/
public final PredictionContext[] parents;
// sorted for merge sort, no duplicates; if present,
// EMPTY_FULL_INVOKING_STATE is always first
/** Sorted for merge, no duplicates; if present,
* EMPTY_FULL_INVOKING_STATE is always first
*/
public final int[] invokingStates;
public ArrayPredictionContext(SingletonPredictionContext a) {
@ -136,9 +139,10 @@ public class ArrayPredictionContext extends PredictionContext {
StringBuilder buf = new StringBuilder();
buf.append("[");
for (int i=0; i<invokingStates.length; i++) {
if ( i>0 ) buf.append(",");
if ( i>0 ) buf.append(", ");
buf.append(invokingStates[i]);
if ( parents[i]!=null ) {
buf.append(' ');
buf.append(parents[i].toString());
}
else {

View File

@ -35,6 +35,6 @@ public class EmptyPredictionContext extends SingletonPredictionContext {
@Override
public String toString() {
return "";
return "$";
}
}

View File

@ -509,7 +509,7 @@ public class LexerATNSimulator extends ATNSimulator {
protected ATNConfigSet computeStartState(@NotNull IntStream input,
@NotNull ATNState p)
{
PredictionContext initialContext = EmptyPredictionContext.EMPTY;
PredictionContext initialContext = PredictionContext.EMPTY;
ATNConfigSet configs = new ATNConfigSet();
for (int i=0; i<p.getNumberOfTransitions(); i++) {
ATNState target = p.transition(i).target;

View File

@ -14,7 +14,12 @@ import java.util.List;
import java.util.Map;
public abstract class PredictionContext implements Iterable<SingletonPredictionContext> {
/** Represents $ in local ctx prediction, which means wildcard. *+x = *. */
public static final EmptyPredictionContext EMPTY = new EmptyPredictionContext();
/** Represents $ in an array in full ctx mode, when $ doesn't mean wildcard:
* $ + x = [$,x]. Here, $ = EMPTY_FULL_CTX_INVOKING_STATE.
*/
public static final int EMPTY_FULL_CTX_INVOKING_STATE = Integer.MAX_VALUE;
public static int globalNodeCount = 0;
@ -276,8 +281,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
boolean rootIsWildcard)
{
if ( rootIsWildcard ) {
if ( a == EMPTY ) return a;
if ( b == EMPTY ) return b;
if ( a == EMPTY ) return a; // * + b = *
if ( b == EMPTY ) return b; // a + * = *
}
else {
if ( a == EMPTY && b == EMPTY ) return EMPTY; // $ + $ = $
@ -396,12 +401,14 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
if ( current instanceof SingletonPredictionContext ) {
String s = String.valueOf(current.id);
buf.append(" s").append(s);
buf.append(" [label=\"").append(current.getInvokingState(0)).append("\"];\n");
String invokingState = String.valueOf(current.getInvokingState(0));
if ( current instanceof EmptyPredictionContext ) invokingState = "$";
buf.append(" [label=\"").append(invokingState).append("\"];\n");
continue;
}
ArrayPredictionContext arr = (ArrayPredictionContext)current;
buf.append(" s").append(arr.id);
buf.append(" [label=\"");
buf.append(" [shape=box, label=\"");
buf.append(Arrays.toString(arr.invokingStates));
buf.append("\"];\n");
}
@ -409,13 +416,12 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
for (PredictionContext current : nodes) {
if ( current==EMPTY ) continue;
for (int i = 0; i < current.size(); i++) {
// String s = String.valueOf(System.identityHashCode(current));
String s = String.valueOf(current.id);
buf.append(" s").append(s);
buf.append("->");
buf.append("s");
buf.append(current.getParent(i).id);
buf.append(";\n");
buf.append(" [label=\"parent["+i+"]\"];\n");
}
}

View File

@ -21,179 +21,179 @@ public class TestGraphNodes extends TestCase {
PredictionContext b = PredictionContext.EMPTY;
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$:0]", nodes.toString());
assertEquals("[$]", nodes.toString());
}
@Test public void testLeftEmpty() {
PredictionContext a = PredictionContext.EMPTY;
PredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
PredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$:0]", nodes.toString());
assertEquals("[$]", nodes.toString());
}
@Test public void testRightEmpty() {
PredictionContext a = PredictionContext.EMPTY;
PredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
PredictionContext b = createSingleton(PredictionContext.EMPTY, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[$:0]", nodes.toString());
assertEquals("[$]", nodes.toString());
}
@Test public void testSameSingleTops() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, "a");
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, "a");
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("[a:1, $:0]", nodes.toString());
assertEquals("[1 $, $]", nodes.toString());
}
@Test public void test_a$_ax$() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, "a");
PredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext a2 = createSingleton(x, "a");
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a2 = createSingleton(x, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[a:1, $:0]", nodes.toString());
assertEquals("[1 $, $]", nodes.toString());
}
@Test public void test_ax$_a$() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext a1 = createSingleton(x, "a");
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, "a");
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[a:3, $:0]", nodes.toString());
assertEquals("[1 $, $]", nodes.toString());
}
@Test public void testDiffSingleTops() {
PredictionContext a1 = createSingleton(PredictionContext.EMPTY, "a");
PredictionContext a2 = createSingleton(PredictionContext.EMPTY, "b");
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("[[a, b]:3, $:0]", nodes.toString());
assertEquals("[[1 $, 2 $], $]", nodes.toString());
}
@Test public void test_ax_ax_id() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext a1 = createSingleton(x, "a");
PredictionContext a2 = createSingleton(x, "a");
@Test public void test_ax_ax() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a1 = createSingleton(x, 1);
PredictionContext a2 = createSingleton(x, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
List<PredictionContext> nodes = PredictionContext.getAllNodes(r);
assertEquals("[a:2, x:1, $:0]", nodes.toString());
assertEquals("[1 9 $, 9 $, $]", nodes.toString());
}
@Test public void test_ax_ax_eq() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext a1 = createSingleton(x1, "a");
PredictionContext a2 = createSingleton(x2, "a");
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a1 = createSingleton(x1, 1);
PredictionContext a2 = createSingleton(x2, 1);
PredictionContext r = PredictionContext.merge(a1, a2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s3 [label=\"a\"];\n" +
" s1 [label=\"x\"];\n" +
" s3 [label=\"1\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s3->s1;\n" +
" s1->s0;\n" +
" s3->s1 [label=\"parent[0]\"];\n" +
" s1->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_abx_abx_id() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext b1 = createSingleton(x1, "b");
PredictionContext b2 = createSingleton(x2, "b");
PredictionContext a1 = createSingleton(b1, "a");
PredictionContext a2 = createSingleton(b2, "a");
@Test public void test_abx_abx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext b1 = createSingleton(x1, 2);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [label=\"a\"];\n" +
" s3 [label=\"b\"];\n" +
" s1 [label=\"x\"];\n" +
" s5 [label=\"1\"];\n" +
" s3 [label=\"2\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s5->s3;\n" +
" s3->s1;\n" +
" s1->s0;\n" +
" s5->s3 [label=\"parent[0]\"];\n" +
" s3->s1 [label=\"parent[0]\"];\n" +
" s1->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_abx_acx_id() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext b = createSingleton(x1, "b");
PredictionContext c = createSingleton(x2, "c");
PredictionContext a1 = createSingleton(b, "a");
PredictionContext a2 = createSingleton(c, "a");
@Test public void test_abx_acx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext b = createSingleton(x1, 2);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s8 [label=\"a\"];\n" +
" s7 [label=\"[b, c]\"];\n" +
" s2 [label=\"x\"];\n" +
" s8 [label=\"1\"];\n" +
" s7 [shape=box, label=\"[2, 3]\"];\n" +
" s2 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"x\"];\n" +
" s8->s7;\n" +
" s7->s1;\n" +
" s7->s2;\n" +
" s2->s0;\n" +
" s1->s0;\n" +
" s1 [label=\"9\"];\n" +
" s8->s7 [label=\"parent[0]\"];\n" +
" s7->s1 [label=\"parent[0]\"];\n" +
" s7->s2 [label=\"parent[1]\"];\n" +
" s2->s0 [label=\"parent[0]\"];\n" +
" s1->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_ax_bx_id() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext a = createSingleton(x, "a");
PredictionContext b = createSingleton(x, "b");
@Test public void test_ax_bx() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a = createSingleton(x, 1);
PredictionContext b = createSingleton(x, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [label=\"[a, b]\"];\n" +
" s1 [label=\"x\"];\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s4->s1;\n" +
" s4->s1;\n" +
" s1->s0;\n" +
" s4->s1 [label=\"parent[0]\"];\n" +
" s4->s1 [label=\"parent[1]\"];\n" +
" s1->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_aex_bfx_id() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, "x");
PredictionContext e = createSingleton(x1, "e");
PredictionContext f = createSingleton(x2, "f");
PredictionContext a = createSingleton(e, "a");
PredictionContext b = createSingleton(f, "b");
@Test public void test_aex_bfx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext e = createSingleton(x1, 5);
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));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s7 [label=\"[a, b]\"];\n" +
" s4 [label=\"f\"];\n" +
" s2 [label=\"x\"];\n" +
" s7 [shape=box, label=\"[1, 2]\"];\n" +
" s4 [label=\"6\"];\n" +
" s2 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s3 [label=\"e\"];\n" +
" s1 [label=\"x\"];\n" +
" s7->s3;\n" +
" s7->s4;\n" +
" s4->s2;\n" +
" s2->s0;\n" +
" s3->s1;\n" +
" s1->s0;\n" +
" s3 [label=\"5\"];\n" +
" s1 [label=\"9\"];\n" +
" s7->s3 [label=\"parent[0]\"];\n" +
" s7->s4 [label=\"parent[1]\"];\n" +
" s4->s2 [label=\"parent[0]\"];\n" +
" s2->s0 [label=\"parent[0]\"];\n" +
" s3->s1 [label=\"parent[0]\"];\n" +
" s1->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@ -201,9 +201,9 @@ public class TestGraphNodes extends TestCase {
// Array merges
@Test public void test_Aab_Ac() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, "c");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -211,18 +211,18 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [label=\"[a, b, c]\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6->s0;\n" +
" s6->s0;\n" +
" s6->s0;\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));
}
@Test public void test_Aa_Aa() {
SingletonPredictionContext a1 = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext a2 = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext a1 = createSingleton(PredictionContext.EMPTY, 1);
SingletonPredictionContext a2 = createSingleton(PredictionContext.EMPTY, 1);
ArrayPredictionContext A1 = create(a1);
ArrayPredictionContext A2 = create(a2);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -230,17 +230,17 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s8 [label=\"[a]\"];\n" +
" s7 [shape=box, label=\"[1]\"];\n" +
" s0 [label=\"$\"];\n" +
" s8->s0;\n" +
" s7->s0 [label=\"parent[0]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aa_Abc() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, "c");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -248,19 +248,19 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [label=\"[a, b, c]\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6->s0;\n" +
" s6->s0;\n" +
" s6->s0;\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));
}
@Test public void test_Aac_Ab() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, "c");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -268,19 +268,19 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s6 [label=\"[a, b, c]\"];\n" +
" s6 [shape=box, label=\"[1, 2, 3]\"];\n" +
" s0 [label=\"$\"];\n" +
" s6->s0;\n" +
" s6->s0;\n" +
" s6->s0;\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));
}
@Test public void test_Aab_Aa() {
SingletonPredictionContext a1 = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext b = createSingleton(PredictionContext.EMPTY, "b");
SingletonPredictionContext a2 = createSingleton(PredictionContext.EMPTY, "a");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -288,18 +288,18 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s9 [label=\"[a, b]\"];\n" +
" s8 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s9->s0;\n" +
" s9->s0;\n" +
" s8->s0 [label=\"parent[0]\"];\n" +
" s8->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aab_Ab() {
SingletonPredictionContext a = createSingleton(PredictionContext.EMPTY, "a");
SingletonPredictionContext b1 = createSingleton(PredictionContext.EMPTY, "b");
SingletonPredictionContext b2 = createSingleton(PredictionContext.EMPTY, "b");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -307,19 +307,19 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s9 [label=\"[a, b]\"];\n" +
" s8 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s9->s0;\n" +
" s9->s0;\n" +
" s8->s0 [label=\"parent[0]\"];\n" +
" s8->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aax_Aay() { // ax + ay -> merged a, array parent
SingletonPredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
SingletonPredictionContext a1 = createSingleton(x, "a");
SingletonPredictionContext y = createSingleton(PredictionContext.EMPTY, "y");
SingletonPredictionContext a2 = createSingleton(y, "a");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -327,23 +327,23 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s12 [label=\"[a]\"];\n" +
" s10 [label=\"[x, y]\"];\n" +
" s11 [shape=box, label=\"[1]\"];\n" +
" s9 [shape=box, label=\"[9, 10]\"];\n" +
" s0 [label=\"$\"];\n" +
" s12->s10;\n" +
" s10->s0;\n" +
" s10->s0;\n" +
" s11->s9 [label=\"parent[0]\"];\n" +
" s9->s0 [label=\"parent[0]\"];\n" +
" s9->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aaxc_Aayd() { // ax,c + ay,d -> merged a, array parent
SingletonPredictionContext x = createSingleton(PredictionContext.EMPTY, "x");
SingletonPredictionContext a1 = createSingleton(x, "a");
SingletonPredictionContext c = createSingleton(PredictionContext.EMPTY, "c");
SingletonPredictionContext y = createSingleton(PredictionContext.EMPTY, "y");
SingletonPredictionContext a2 = createSingleton(y, "a");
SingletonPredictionContext d = createSingleton(PredictionContext.EMPTY, "d");
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);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
@ -351,22 +351,21 @@ public class TestGraphNodes extends TestCase {
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s14 [label=\"[a, c, d]\"];\n" +
" s13 [shape=box, label=\"[1, 3, 4]\"];\n" +
" s0 [label=\"$\"];\n" +
" s12 [label=\"[x, y]\"];\n" +
" s14->s12;\n" +
" s14->s0;\n" +
" s14->s0;\n" +
" s12->s0;\n" +
" s12->s0;\n" +
" s11 [shape=box, label=\"[9, 10]\"];\n" +
" s13->s11 [label=\"parent[0]\"];\n" +
" s13->s0 [label=\"parent[1]\"];\n" +
" s13->s0 [label=\"parent[2]\"];\n" +
" s11->s0 [label=\"parent[0]\"];\n" +
" s11->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
public SingletonPredictionContext createSingleton(PredictionContext parent, String payload) {
// SingletonPredictionContext a = new SingletonPredictionContext(parent, payload);
// return a;
return null;
public SingletonPredictionContext createSingleton(PredictionContext parent, int payload) {
SingletonPredictionContext a = new SingletonPredictionContext(parent, payload);
return a;
}
public ArrayPredictionContext create(SingletonPredictionContext... nodes) {