factor Pos out of Position in preparation for serialiable parse tables
[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 edu.berkeley.sbp.Sequence.Pos;
7 import java.util.*;
8
9 final class Result implements GraphViz.ToGraphViz {
10
11     private Forest f;
12     private Node parent;
13     private HashSet<Node> children = new HashSet<Node>();
14     private boolean destroyed = false;
15     private int usedByNonDoomedNode = 0;
16
17     public GSS.Phase phase() { return parent==null ? null : parent.phase(); }
18     public Forest getForest() { return f; }
19     public Node parent() { return parent; }
20     public void addChild(Node child) {
21         children.add(child);
22         usedByNonDoomedNode += child.state().doomed ? 0 : 1;
23     }
24     public void removeChild(Node child) {
25         if (!children.contains(child)) return;
26         children.remove(child);
27         usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
28         check();
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         parent.removeChild(this);
42         while(children.size() > 0)
43             for(Node n : children) {
44                 removeChild(n);
45                 n.removeResult(this);
46                 break;
47             }
48     }
49
50     public Result(Forest f, Node parent, Pos reduction) {
51         this(f, parent, reduction, null);
52     }
53     public Result(Forest f, Node parent, Pos reduction, GSS.Phase target) {
54         this.f = f;
55         this.parent = parent;
56         if (parent != null) parent.addChild(this);
57         if (reduction == null) return;
58         Parser.Table.State state0 = (Parser.Table.State)parent.state().gotoSetNonTerminals.get(reduction);
59         target.newNodeFromReduction(this, state0, reduction);
60     }
61
62     // GraphViz //////////////////////////////////////////////////////////////////////////////
63
64     public GraphViz.Node toGraphViz(GraphViz gv) {
65         if (gv.hasNode(this)) return gv.createNode(this);
66         GraphViz.Node n = gv.createNode(this);
67         n.label = ""+f;
68         n.shape = "rectangle";
69         if (parent()!=null) n.edge(parent, "");
70         n.color = "blue";
71         if (phase() != null)
72             ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
73         return n;
74     }
75     public boolean isTransparent() { return false; }
76     public boolean isHidden() { return false; }
77
78 }