1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
10 import java.lang.reflect.*;
12 final class Result implements GraphViz.ToGraphViz {
16 private GSS.Phase phase;
17 private Position reduction;
18 private HashSet<Node> children = new HashSet<Node>();
19 private boolean destroyed = false;
20 private int usedByNonDoomedNode = 0;
22 public Position reduction() { return reduction; }
23 public GSS.Phase phase() { return phase; }
24 public Forest getForest() { return f; }
25 public Node parent() { return parent; }
26 public void addChild(Node child) {
28 usedByNonDoomedNode += child.state().doomed ? 0 : 1;
30 public void removeChild(Node child) {
31 children.remove(child);
32 usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
35 public boolean usedByAnyNode() { return children.size() > 0; }
36 public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
38 public String toString() { return super.toString()+"->"+parent(); }
40 public void check() { if (children.size()==0) destroy(); }
41 public void destroy() {
42 if (destroyed) return;
43 if (parent==null) return; // never destroy the "primordeal" result
45 if (parent() != null) {
46 parent().removeChild(this);
50 for(Node n : children) {
60 public Result(Forest f, Node parent, Position reduction) {
62 this.reduction = reduction;
64 if (parent != null) parent.addChild(this);
65 if (parent != null) phase = parent.phase();
68 // GraphViz //////////////////////////////////////////////////////////////////////////////
70 public GraphViz.Node toGraphViz(GraphViz gv) {
71 if (gv.hasNode(this)) return gv.createNode(this);
72 GraphViz.Node n = gv.createNode(this);
74 n.shape = "rectangle";
75 if (parent()!=null) n.edge(parent, "");
78 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
81 public boolean isTransparent() { return false; }
82 public boolean isHidden() { return false; }