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