d333f414f8d9fb6316bb83d30f017199b7bab0cf
[sbp.git] / src / edu / berkeley / sbp / Result.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.Sequence.Position;
6 import java.util.*;
7
8 final class Result implements GraphViz.ToGraphViz {
9
10     private Forest f;
11     private Node parent;
12     private HashSet<Node> children = new HashSet<Node>();
13     private boolean destroyed = false;
14     private int usedByNonDoomedNode = 0;
15
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) {
20         children.add(child);
21         usedByNonDoomedNode += child.state().doomed ? 0 : 1;
22     }
23     public void removeChild(Node child) {
24         if (!children.contains(child)) return;
25         children.remove(child);
26         usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
27         check();
28     }
29
30     public boolean usedByAnyNode() { return children.size() > 0; }
31     public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
32
33     public String toString() { return super.toString()+"->"+parent(); }
34
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
39         destroyed = true;
40         parent.removeChild(this);
41         while(children.size() > 0)
42             for(Node n : children) {
43                 children.remove(n);
44                 n.removeResult(this);
45                 break;
46             }
47     }
48
49     public Result(Forest f, Node parent, Position reduction) {
50         this(f, parent, reduction, null);
51     }
52     public Result(Forest f, Node parent, Position reduction, GSS.Phase target) {
53         this.f = f;
54         this.parent = parent;
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);
59     }
60
61     // GraphViz //////////////////////////////////////////////////////////////////////////////
62
63     public GraphViz.Node toGraphViz(GraphViz gv) {
64         if (gv.hasNode(this)) return gv.createNode(this);
65         GraphViz.Node n = gv.createNode(this);
66         n.label = ""+f;
67         n.shape = "rectangle";
68         if (parent()!=null) n.edge(parent, "");
69         n.color = "blue";
70         if (phase() != null)
71             ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
72         return n;
73     }
74     public boolean isTransparent() { return false; }
75     public boolean isHidden() { return false; }
76
77 }