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 else 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;
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) reduce(r, r.pos-1, phase(), only);
75 Input.Region region = phase().getLocation().createRegion(phase().getLocation());
76 new Result(r.rewrite(region, phase().parser().cache()), this, r, phase());
80 private void reduce(Position r, int pos, GSS.Phase target, Result only) {
81 Forest[] holder = r.holder;
82 Forest old = holder[pos];
83 if (results==null) return; // FIXME: this should not happen
84 for(Result res : results)
85 if (only == null || res == only) {
86 Node child = res.parent();
87 holder[pos] = res.getForest();
88 if (pos>0) child.reduce(r, pos-1, target, null);
90 Input.Region region = child.phase().getLocation().createRegion(target.getLocation());
91 new Reduction(child, r, r.rewrite(region, phase().parser().cache()), target);
97 Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
100 this.fromEmptyReduction = fromEmptyReduction;
101 if (phase.hash.get(state, result.phase()) != null) throw new Error("severe problem!");
102 phase.hash.put(state, result.phase(), this);
104 state.invokeEpsilonReductions(phase().token, this);
107 // Add/Remove Children/Results //////////////////////////////////////////////////////////////////////////////
109 public void removeChild(Result child) {
110 if (children==null) return;
111 children.remove(child);
114 public void removeResult(Result result) {
115 if (results==null) return;
116 results.remove(result);
119 public void addChild(Result child) {
120 if (children==null) return; // FIXME: this should not happen
123 public void addResult(Result r) {
124 if (results.contains(r)) return;
127 if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
130 // GraphViz //////////////////////////////////////////////////////////////////////////////
132 public GraphViz.Node toGraphViz(GraphViz gv) {
133 if (results.size()==0) return null;
134 if (gv.hasNode(this)) return gv.createNode(this);
135 GraphViz.Node n = gv.createNode(this);
136 n.label = "state["+state.toInt()+"]";
137 n.shape = "rectangle";
138 boolean hasparents = false;
139 for(Result r : results) n.edge(r, "");
140 n.color = state.doomed ? "red" : "green";
141 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
144 public boolean isTransparent() { return false; }
145 public boolean isHidden() { return false; }