1 // Copyright 2006 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.Position;
10 import java.lang.reflect.*;
12 /** a node in the GSS */
14 implements Invokable<Position, Result>,
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; }
24 public int toInt() { return idx; }
26 boolean destroyed = false;
29 if (destroyed) return;
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;
42 while(children.size()>0)
43 for(Result r : children) {
48 while(results.size()>0)
49 for(Result r : results) {
58 //////////////////////////////////////////////////////////////////////
60 private static int node_idx = 0;
61 private final int idx = node_idx++;
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>();
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);
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);
91 Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
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);
98 state.invokeEpsilonReductions(phase().token, this);
101 // Add/Remove Children/Results //////////////////////////////////////////////////////////////////////////////
103 public void removeChild(Result child) {
104 if (children==null) return;
105 children.remove(child);
108 public void removeResult(Result result) {
109 if (results==null) return;
110 results.remove(result);
113 public void addChild(Result child) {
114 if (children==null) return; // FIXME: this should not happen
117 public void addResult(Result r) {
118 if (results.contains(r)) return;
121 if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
124 // GraphViz //////////////////////////////////////////////////////////////////////////////
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);
138 public boolean isTransparent() { return false; }
139 public boolean isHidden() { return false; }