use Sequence.Pos rather than Sequence.Position wherever possible
[sbp.git] / src / edu / berkeley / sbp / Node.java
1 // Copyright 2006-2007 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.Pos;
8 import edu.berkeley.sbp.Sequence.Pos;
9 import java.io.*;
10 import java.util.*;
11 import java.lang.reflect.*;
12
13 /** a node in the GSS */
14 final class Node
15     implements Invokable<Pos, Result>,
16                IntegerMappable,
17                GraphViz.ToGraphViz,
18                Iterable<Result> {
19
20     /** which GSS.Phase this Node belongs to */
21     public GSS.Phase phase() { return phase; }
22     public Iterator<Result> iterator() { return results.iterator(); }
23     public Parser.Table.State state() { return state; }
24
25     public int toInt() { return idx; }
26
27     boolean destroyed = false;
28
29     public void check() {
30         if (destroyed) return;
31         boolean dead = true;
32         // - all nodes must have a parent
33         // - non-doomed nodes must either:
34         //      - be on the frontier or
35         //      - have a non-doomed node closer to the frontier than themself
36         if (phase.isFrontier()) dead = false;
37         else for(Result r : children)
38                  if (state.doomed) { if (r.usedByAnyNode()) { dead = false; break; } }
39                  else              { if (r.usedByNonDoomedNode()) { dead = false; break; } }
40         dead |= results.size()==0;
41         if (!dead) return;
42         destroyed = true;
43         while(children.size()>0)
44             for(Result r : children) {
45                 children.remove(r);
46                 r.destroy();
47                 break;
48             }
49         while(results.size()>0)
50             for(Result r : results) {
51                 results.remove(r);
52                 r.removeChild(this);
53                 break;
54             }
55         results = null;
56         children = null;
57     }
58
59     //////////////////////////////////////////////////////////////////////
60
61     private static int node_idx = 0;
62     private final int idx = node_idx++;
63
64     private final GSS.Phase phase;
65     private final Parser.Table.State state;
66     private final boolean fromEmptyReduction;
67     //private       FastSet<Result> results = new FastSet<Result>();
68     private       HashSet<Result> results = new HashSet<Result>();
69     private       HashSet<Result> children = new HashSet<Result>();
70
71     public final void invoke(Pos r, Result only) {
72         boolean emptyProductions = only==null;
73         if (emptyProductions != (r.numPops()==0)) return;
74         if (r.numPops()!=0)  reduce(r, r.numPops()-1, phase(), only);
75         else {
76             Input.Region region = phase().getLocation().createRegion(phase().getLocation());
77             new Result(r.rewrite(region, phase().parser().cache()), this, r, phase());
78         }
79     }
80
81     private void reduce(Pos r, int pos, GSS.Phase target, Result only) {
82         Forest[] holder = r.holder;
83         Forest old = holder[pos];
84         if (results==null) return;   // FIXME: this should not happen
85         for(Result res : results)
86             if (only == null || res == only) {
87                 Node child = res.parent();
88                 holder[pos] = res.getForest();
89                 if (pos>0)  child.reduce(r, pos-1, target, null);
90                 else {
91                     Input.Region region = child.phase().getLocation().createRegion(target.getLocation());
92                     new Reduction(child, r, r.rewrite(region, phase().parser().cache()), target);
93                 }
94             }
95         holder[pos] = old;
96     }
97
98     Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
99         this.phase = phase;
100         this.state = state;
101         this.fromEmptyReduction = fromEmptyReduction;
102         if (phase.hash.get(state, result.phase()) != null) throw new Error("severe problem!");
103         phase.hash.put(state, result.phase(), this);
104         addResult(result);
105         state.invokeEpsilonReductions(phase().token, this);
106     }
107
108     // Add/Remove Children/Results //////////////////////////////////////////////////////////////////////////////
109
110     public void removeChild(Result child)   {
111         if (children==null) return;
112         children.remove(child);
113         check();
114     }
115     public void removeResult(Result result) {
116         if (results==null) return;
117         results.remove(result);
118         check();
119     }
120     public void addChild(Result child)      {
121         if (children==null) return;   // FIXME: this should not happen
122         children.add(child);
123     }
124     public void addResult(Result r) {
125         if (results.contains(r)) return;
126         results.add(r);
127         r.addChild(this);
128         if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
129     }
130
131     // GraphViz //////////////////////////////////////////////////////////////////////////////
132
133     public GraphViz.Node toGraphViz(GraphViz gv) {
134         if (results.size()==0) return null;
135         if (gv.hasNode(this)) return gv.createNode(this);
136         GraphViz.Node n = gv.createNode(this);
137         n.label = "state["+state.toInt()+"]";
138         n.shape = "rectangle";
139         boolean hasparents = false;
140         for(Result r : results) n.edge(r, "");
141         n.color = state.doomed ? "red" : "green";
142         ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
143         return n;
144     }
145     public boolean isTransparent() { return false; }
146     public boolean isHidden() { return false; }
147
148 }