1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.Sequence.Position;
6 import edu.berkeley.sbp.Sequence.Pos;
9 final class Result implements GraphViz.ToGraphViz {
13 private HashSet<Node> children = new HashSet<Node>();
14 private boolean destroyed = false;
15 private int usedByNonDoomedNode = 0;
17 public GSS.Phase phase() { return parent==null ? null : parent.phase(); }
18 public Forest getForest() { return f; }
19 public Node parent() { return parent; }
20 public void addChild(Node child) {
22 usedByNonDoomedNode += child.state().doomed ? 0 : 1;
24 public void removeChild(Node child) {
25 if (!children.contains(child)) return;
26 children.remove(child);
27 usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
31 public boolean usedByAnyNode() { return children.size() > 0; }
32 public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
34 public String toString() { return super.toString()+"->"+parent(); }
36 public void check() { if (children.size()==0) destroy(); }
37 public void destroy() {
38 if (destroyed) return;
39 if (parent==null) return; // never destroy the "primordeal" result
41 parent.removeChild(this);
42 while(children.size() > 0)
43 for(Node n : children) {
50 public Result(Forest f, Node parent, Pos reduction) {
51 this(f, parent, reduction, null);
53 public Result(Forest f, Node parent, Pos reduction, GSS.Phase target) {
56 if (parent != null) parent.addChild(this);
57 if (reduction == null) return;
58 Parser.Table.State state0 = (Parser.Table.State)parent.state().gotoSetNonTerminals.get(reduction);
59 target.newNodeFromReduction(this, state0, reduction);
62 // GraphViz //////////////////////////////////////////////////////////////////////////////
64 public GraphViz.Node toGraphViz(GraphViz gv) {
65 if (gv.hasNode(this)) return gv.createNode(this);
66 GraphViz.Node n = gv.createNode(this);
68 n.shape = "rectangle";
69 if (parent()!=null) n.edge(parent, "");
72 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
75 public boolean isTransparent() { return false; }
76 public boolean isHidden() { return false; }