X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FResult.java;h=95baeba55974e7cd9d53baffabd4e3b44fc4e296;hp=8eccf96eb4d9cfbee7e8b374267cf840ac5818e0;hb=2afdfe14e78fa0597186614937c679a09d74ecdf;hpb=e84029a8b861075d6d0ed5040f919b2e4da4c98f diff --git a/src/edu/berkeley/sbp/Result.java b/src/edu/berkeley/sbp/Result.java index 8eccf96..95baeba 100644 --- a/src/edu/berkeley/sbp/Result.java +++ b/src/edu/berkeley/sbp/Result.java @@ -1,31 +1,77 @@ // Copyright 2006 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 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 HashSet children = new HashSet(); + private boolean destroyed = false; + private int usedByNonDoomedNode = 0; - public Position reduction() { return reduction; } - public GSS.Phase phase() { return phase; } + public GSS.Phase phase() { return parent==null ? null : parent.phase(); } public Forest getForest() { return f; } public Node parent() { return parent; } + public void addChild(Node child) { + children.add(child); + usedByNonDoomedNode += child.state().doomed ? 0 : 1; + } + public void removeChild(Node child) { + if (!children.contains(child)) return; + children.remove(child); + usedByNonDoomedNode -= child.state().doomed ? 0 : 1; + check(); + } + + public boolean usedByAnyNode() { return children.size() > 0; } + public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; } + + public String toString() { return super.toString()+"->"+parent(); } + + public void check() { if (children.size()==0) destroy(); } + public void destroy() { + if (destroyed) return; + if (parent==null) return; // never destroy the "primordeal" result + destroyed = true; + parent.removeChild(this); + while(children.size() > 0) + for(Node n : children) { + removeChild(n); + n.removeResult(this); + break; + } + } public Result(Forest f, Node parent, Position reduction) { + this(f, parent, reduction, null); + } + public Result(Forest f, Node parent, Position reduction, GSS.Phase target) { this.f = f; - this.reduction = reduction; this.parent = parent; - if (parent != null) phase = parent.phase(); + if (parent != null) parent.addChild(this); + if (reduction == null) return; + Parser.Table.State state0 = (Parser.Table.State)parent.state().gotoSetNonTerminals.get(reduction.owner()); + 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 (parent()!=null) n.edge(parent, ""); + 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