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 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;
43 while(children.size()>0)
44 for(Result r : children) {
49 while(results.size()>0)
50 for(Result r : results) {
59 //////////////////////////////////////////////////////////////////////
61 private static int node_idx = 0;
62 private final int idx = node_idx++;
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>();
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);
76 Input.Region region = phase().getLocation().createRegion(phase().getLocation());
77 new Result(r.rewrite(region), this, r, phase());
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);
91 Input.Region region = child.phase().getLocation().createRegion(target.getLocation());
92 new Reduction(child, r, r.rewrite(region), target);
98 Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
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);
105 state.invokeEpsilonReductions(phase().token, this);
108 // Add/Remove Children/Results //////////////////////////////////////////////////////////////////////////////
110 public void removeChild(Result child) {
111 if (children==null) return;
112 children.remove(child);
115 public void removeResult(Result result) {
116 if (results==null) return;
117 results.remove(result);
120 public void addChild(Result child) {
121 if (children==null) return; // FIXME: this should not happen
124 public void addResult(Result r) {
125 if (results.contains(r)) return;
128 if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
131 // GraphViz //////////////////////////////////////////////////////////////////////////////
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);
145 public boolean isTransparent() { return false; }
146 public boolean isHidden() { return false; }