toDOTString static method

String toDOTString(
  1. PredictionContext? context
)

Implementation

static String toDOTString(PredictionContext? context) {
  if (context == null) return '';
  final buf = StringBuffer();
  buf.write('digraph G {\n');
  buf.write('rankdir=LR;\n');

  final nodes = getAllContextNodes(context);
  nodes.sort((PredictionContext o1, PredictionContext o2) {
    return o1.id - o2.id;
  });

  for (var current in nodes) {
    if (current is SingletonPredictionContext) {
      final s = current.id.toString();
      buf.write('  s');
      buf.write(s);
      var returnState = current.getReturnState(0).toString();
      if (current is EmptyPredictionContext) returnState = r'$';
      buf.write(' [label=\"');
      buf.write(returnState);
      buf.write('\"];\n');
      continue;
    }
    final arr = current as ArrayPredictionContext;
    buf.write('  s');
    buf.write(arr.id);
    buf.write(' [shape=box, label=\"');
    buf.write('[');
    var first = true;
    for (var inv in arr.returnStates) {
      if (!first) buf.write(', ');
      if (inv == EMPTY_RETURN_STATE) {
        buf.write(r'$');
      } else {
        buf.write(inv);
      }
      first = false;
    }
    buf.write(']');
    buf.write('\"];\n');
  }

  for (var current in nodes) {
    if (current == EmptyPredictionContext.Instance) continue;
    for (var i = 0; i < current.length; i++) {
      if (current.getParent(i) == null) continue;
      final s = current.id.toString();
      buf.write('  s');
      buf.write(s);
      buf.write('->');
      buf.write('s');
      buf.write(current.getParent(i)?.id);
      if (current.length > 1) {
        buf.write(' [label=\"parent[$i]\"];\n');
      } else {
        buf.write(';\n');
      }
    }
  }

  buf.write('}\n');
  return buf.toString();
}