1 // Copyright 2006 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;
8 final class Result implements GraphViz.ToGraphViz {
12 private HashSet<Node> children = new HashSet<Node>();
13 private boolean destroyed = false;
14 private int usedByNonDoomedNode = 0;
16 public GSS.Phase phase() { return parent==null ? null : parent.phase(); }
17 public Forest getForest() { return f; }
18 public Node parent() { return parent; }
19 public void addChild(Node child) {
21 usedByNonDoomedNode += child.state().doomed ? 0 : 1;
23 public void removeChild(Node child) {
24 if (!children.contains(child)) return;
25 children.remove(child);
26 usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
30 public boolean usedByAnyNode() { return children.size() > 0; }
31 public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
33 public String toString() { return super.toString()+"->"+parent(); }
35 public void check() { if (children.size()==0) destroy(); }
36 public void destroy() {
37 if (destroyed) return;
38 if (parent==null) return; // never destroy the "primordeal" result
40 parent.removeChild(this);
41 while(children.size() > 0)
42 for(Node n : children) {
49 public Result(Forest f, Node parent, Position reduction) {
50 this(f, parent, reduction, null);
52 public Result(Forest f, Node parent, Position reduction, GSS.Phase target) {
55 if (parent != null) parent.addChild(this);
56 if (reduction == null) return;
57 Parser.Table.State state0 = (Parser.Table.State)parent.state().gotoSetNonTerminals.get(reduction.owner());
58 target.newNodeFromReduction(this, state0, reduction);
61 // GraphViz //////////////////////////////////////////////////////////////////////////////
63 public GraphViz.Node toGraphViz(GraphViz gv) {
64 if (gv.hasNode(this)) return gv.createNode(this);
65 GraphViz.Node n = gv.createNode(this);
67 n.shape = "rectangle";
68 if (parent()!=null) n.edge(parent, "");
71 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
74 public boolean isTransparent() { return false; }
75 public boolean isHidden() { return false; }