Union: many cleanups
[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     private int usedByNonDoomedNode = 0;
21
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) {
27         children.add(child);
28         usedByNonDoomedNode += child.state().doomed ? 0 : 1;
29     }
30     public void removeChild(Node child) {
31         children.remove(child);
32         usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
33     }
34
35     public boolean usedByAnyNode() { return children.size() > 0; }
36     public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
37
38     public String toString() { return super.toString()+"->"+parent(); }
39
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
44         destroyed = true;
45         if (parent() != null) {
46             parent().removeChild(this);
47             parent().check();
48         }
49         OUTER: while(true) {
50             for(Node n : children) {
51                 children.remove(n);
52                 n.removeResult(this);
53                 n.check();
54                 continue OUTER;
55             }
56             break;
57         }
58     }
59
60     public Result(Forest f, Node parent, Position reduction) {
61         this.f = f;
62         this.reduction = reduction;
63         this.parent = parent;
64         if (parent != null) parent.addChild(this);
65         if (parent != null) phase = parent.phase();
66     }
67
68     // GraphViz //////////////////////////////////////////////////////////////////////////////
69
70     public GraphViz.Node toGraphViz(GraphViz gv) {
71         if (gv.hasNode(this)) return gv.createNode(this);
72         GraphViz.Node n = gv.createNode(this);
73         n.label = ""+f;
74         n.shape = "rectangle";
75         if (parent()!=null) n.edge(parent, "");
76         n.color = "blue";
77         if (phase() != null)
78             ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
79         return n;
80     }
81     public boolean isTransparent() { return false; }
82     public boolean isHidden() { return false; }
83
84 }