make Result final
[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.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
8 import java.io.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 final class Result implements GraphViz.ToGraphViz {
13
14     private Forest f;
15     private Node parent;
16     private GSS.Phase phase;
17     private Position reduction;
18     private HashSet<Node> children = new HashSet<Node>();
19     private boolean destroyed = false;
20
21     public Position reduction() { return reduction; }
22     public GSS.Phase phase() { return phase; }
23     public Forest getForest() { return f; }
24     public Node parent() { return parent; }
25     public void addChild(Node child) { children.add(child); }
26     public void removeChild(Node child) { children.remove(child); }
27
28     public boolean usedByAnyNode() { return children.size() > 0; }
29     public boolean usedByNonDoomedNode() {
30         for(Node n : children)
31             if (!n.state().doomed)
32                 return true;
33         return false;
34     }
35
36     public String toString() { return super.toString()+"->"+parent(); }
37
38     public void check() { if (children.size()==0) destroy(); }
39     public void destroy() {
40         if (destroyed) return;
41         if (parent==null) return;  // never destroy the "primordeal" result
42         destroyed = true;
43         if (parent() != null) {
44             parent().removeChild(this);
45             parent().check();
46         }
47         OUTER: while(true) {
48             for(Node n : children) {
49                 children.remove(n);
50                 n.removeResult(this);
51                 n.check();
52                 continue OUTER;
53             }
54             break;
55         }
56     }
57
58     public Result(Forest f, Node parent, Position reduction) {
59         this.f = f;
60         this.reduction = reduction;
61         this.parent = parent;
62         if (parent != null) parent.addChild(this);
63         if (parent != null) phase = parent.phase();
64     }
65
66     // GraphViz //////////////////////////////////////////////////////////////////////////////
67
68     public GraphViz.Node toGraphViz(GraphViz gv) {
69         if (gv.hasNode(this)) return gv.createNode(this);
70         GraphViz.Node n = gv.createNode(this);
71         n.label = ""+f;
72         n.shape = "rectangle";
73         if (parent()!=null) n.edge(parent, "");
74         n.color = "blue";
75         if (phase() != null)
76             ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
77         return n;
78     }
79     public boolean isTransparent() { return false; }
80     public boolean isHidden() { return false; }
81
82 }