b6f45851746dc32bd7add3942fb91aedee0a4a7b
[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         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;
40         if (!dead) 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)  reduce(r, r.pos-1, phase(), only);
74         else {
75             Input.Region region = phase().getLocation().createRegion(phase().getLocation());
76             new Result(r.rewrite(region, phase().parser().cache()), this, r, phase());
77         }
78     }
79
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);
89                 else {
90                     Input.Region region = child.phase().getLocation().createRegion(target.getLocation());
91                     new Reduction(child, r, r.rewrite(region, phase().parser().cache()), target);
92                 }
93             }
94         holder[pos] = old;
95     }
96
97     Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
98         this.phase = phase;
99         this.state = state;
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);
103         addResult(result);
104         state.invokeEpsilonReductions(phase().token, this);
105     }
106
107     // Add/Remove Children/Results //////////////////////////////////////////////////////////////////////////////
108
109     public void removeChild(Result child)   {
110         if (children==null) return;
111         children.remove(child);
112         check();
113     }
114     public void removeResult(Result result) {
115         if (results==null) return;
116         results.remove(result);
117         check();
118     }
119     public void addChild(Result child)      {
120         if (children==null) return;   // FIXME: this should not happen
121         children.add(child);
122     }
123     public void addResult(Result r) {
124         if (results.contains(r)) return;
125         results.add(r);
126         r.addChild(this);
127         if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
128     }
129
130     // GraphViz //////////////////////////////////////////////////////////////////////////////
131
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);
142         return n;
143     }
144     public boolean isTransparent() { return false; }
145     public boolean isHidden() { return false; }
146
147 }