change naming: use pred/succ rather than parent/child
[sbp.git] / src / edu / berkeley / sbp / Result.java
index 8eccf96..efaad4c 100644 (file)
@@ -1,31 +1,78 @@
-// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp;
-import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
-import edu.berkeley.sbp.Parser.Table.*;
-import edu.berkeley.sbp.Sequence.Position;
-import java.io.*;
+import edu.berkeley.sbp.Sequence.Pos;
+import edu.berkeley.sbp.Sequence.Pos;
 import java.util.*;
-import java.lang.reflect.*;
 
-class Result {
+final class Result implements GraphViz.ToGraphViz {
 
     private Forest f;
-    private Node parent;
-    private GSS.Phase phase;
-    private Position reduction;
+    private Node pred;
+    private HashSet<Node> successors = new HashSet<Node>();
+    private boolean destroyed = false;
+    private int usedByNonDoomedNode = 0;
 
-    public Position reduction() { return reduction; }
-    public GSS.Phase phase() { return phase; }
+    public GSS.Phase phase() { return pred==null ? null : pred.phase(); }
     public Forest getForest() { return f; }
-    public Node parent() { return parent; }
+    public Node pred() { return pred; }
+    public void addSucc(Node succ) {
+        successors.add(succ);
+        usedByNonDoomedNode += succ.state().doomed ? 0 : 1;
+    }
+    public void removeSucc(Node succ) {
+        if (!successors.contains(succ)) return;
+        successors.remove(succ);
+        usedByNonDoomedNode -= succ.state().doomed ? 0 : 1;
+        check();
+    }
+
+    public boolean usedByAnyNode() { return successors.size() > 0; }
+    public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
+
+    public String toString() { return super.toString()+"->"+pred(); }
+
+    public void check() { if (successors.size()==0) destroy(); }
+    public void destroy() {
+        if (destroyed) return;
+        if (pred==null) return;  // never destroy the "primordeal" result
+        destroyed = true;
+        pred.removeSucc(this);
+        while(successors.size() > 0)
+            for(Node n : successors) {
+                removeSucc(n);
+                n.removeResult(this);
+                break;
+            }
+    }
 
-    public Result(Forest f, Node parent, Position reduction) {
+    public Result(Forest f, Node pred, Pos reduction) {
+        this(f, pred, reduction, null);
+    }
+    public Result(Forest f, Node pred, Pos reduction, GSS.Phase target) {
         this.f = f;
-        this.reduction = reduction;
-        this.parent = parent;
-        if (parent != null) phase = parent.phase();
+        this.pred = pred;
+        if (pred != null) pred.addSucc(this);
+        if (reduction == null) return;
+        Parser.Table.State state0 = (Parser.Table.State)pred.state().gotoSetNonTerminals.get(reduction);
+        target.newNodeFromReduction(this, state0, reduction);
+    }
+
+    // GraphViz //////////////////////////////////////////////////////////////////////////////
+
+    public GraphViz.Node toGraphViz(GraphViz gv) {
+        if (gv.hasNode(this)) return gv.createNode(this);
+        GraphViz.Node n = gv.createNode(this);
+        n.label = ""+f;
+        n.shape = "rectangle";
+        if (pred()!=null) n.edge(pred, "");
+        n.color = "blue";
+        if (phase() != null)
+            ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
+        return n;
     }
+    public boolean isTransparent() { return false; }
+    public boolean isHidden() { return false; }
 
 }
\ No newline at end of file