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