1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
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;
11 import java.lang.reflect.*;
13 /** a node in the GSS */
15 implements Invokable<Pos, Result>,
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; }
25 public int toInt() { return idx; }
27 boolean destroyed = false;
30 if (destroyed) return;
32 // - all nodes must have a predecessor
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 : successors)
38 if (state.doomed) { if (r.usedByAnyNode()) { dead = false; break; } }
39 else { if (r.usedByNonDoomedNode()) { dead = false; break; } }
40 dead |= results.size()==0;
43 if (phase() != null && phase().hash != null)
44 phase().hash.remove(state, predPhase);
45 while(successors.size()>0)
46 for(Result r : successors) {
51 while(results.size()>0)
52 for(Result r : results) {
61 //////////////////////////////////////////////////////////////////////
63 private static int node_idx = 0;
64 private final int idx = node_idx++;
66 private final GSS.Phase phase;
67 private final Parser.Table.State state;
68 private final boolean fromEmptyReduction;
69 //private FastSet<Result> results = new FastSet<Result>();
70 private HashSet<Result> results = new HashSet<Result>();
71 private HashSet<Result> successors = new HashSet<Result>();
73 public final void invoke(Pos r, Result only) {
74 boolean emptyProductions = only==null;
75 if (emptyProductions != (r.numPops()==0)) return;
76 if (r.numPops()!=0) reduce(r, r.numPops()-1, phase(), only);
78 Input.Region region = phase().getLocation().createRegion(phase().getLocation());
79 Result.newResult(r.rewrite(region), this, r, phase());
83 private void reduce(Pos r, int pos, GSS.Phase target, Result only) {
84 Forest[] holder = r.holder;
85 Forest old = holder[pos];
86 if (results==null) return; // FIXME: this should not happen
87 for(Result res : results)
88 if (only == null || res == only) {
89 Node pred = res.pred();
90 holder[pos] = res.getForest();
91 if (pos>0) pred.reduce(r, pos-1, target, null);
93 Input.Region region = pred.phase().getLocation().createRegion(target.getLocation());
94 new Reduction(pred, r, r.rewrite(region), target);
100 Node(GSS.Phase phase, Result pred, State state, boolean fromEmptyReduction) {
103 this.fromEmptyReduction = fromEmptyReduction;
104 if (phase.hash.get(state, pred.phase()) != null) throw new Error("severe problem!");
105 phase.hash.put(state, pred.phase(), this);
107 state.invokeEpsilonReductions(phase().token, this);
110 // Add/Remove Successors/Results //////////////////////////////////////////////////////////////////////////////
112 public void removeSucc(Result succ) {
113 if (successors==null) return;
114 successors.remove(succ);
117 public void removeResult(Result result) {
118 if (results==null) return;
119 results.remove(result);
122 public void addSucc(Result succ) {
123 if (successors==null) return; // FIXME: this should not happen
124 successors.add(succ);
126 public void addResult(Result r) {
127 if (results.contains(r)) return;
130 if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
133 // GraphViz //////////////////////////////////////////////////////////////////////////////
135 public GraphViz.Node toGraphViz(GraphViz gv) {
136 if (results.size()==0) return null;
137 if (gv.hasNode(this)) return gv.createNode(this);
138 GraphViz.Node n = gv.createNode(this);
139 n.label = "state["+state.toInt()+"]";
140 n.shape = "rectangle";
141 boolean haspreds = false;
142 for(Result r : results) n.edge(r, "");
143 n.color = state.doomed ? "red" : "green";
144 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
147 public boolean isTransparent() { return false; }
148 public boolean isHidden() { return false; }