    001    package org.maltparser.parser.history;
    002
    003    import java.util.ArrayList;
    004
    005    import org.maltparser.core.exception.MaltChainedException;
    006    import org.maltparser.parser.history.action.ActionDecision;
    007    import org.maltparser.parser.history.action.GuideUserAction;
    008    /**
    009     *
    010     * @author Johan Hall
    011    */
    012    public class HistoryTreeNode implements HistoryNode {
    013            private GuideUserAction action;
    014            private HistoryTreeNode parent;
    015            private int depth;
    016            private ArrayList<HistoryTreeNode> children;
    017    //      private double score;
    018
    019            public HistoryTreeNode(HistoryNode previousNode, GuideUserAction action) {
    020                    setPreviousNode(parent);
    021                    setAction(action);
    022                    children = new ArrayList<HistoryTreeNode>();
    023            }
    024
    025    //      public HistoryTreeNode(HistoryNode previousNode, GuideUserAction action, double score) {
    026    //              setPreviousNode(parent);
    027    //              setAction(action);
    028    //              setScore(score);
    029    //              children = new ArrayList<HistoryTreeNode>();
    030    //      }
    031
    032            public GuideUserAction getAction() {
    033                    return action;
    034            }
    035
    036            public void setAction(GuideUserAction action) {
    037                    this.action = action;
    038            }
    039
    040            public HistoryNode getPreviousNode() {
    041                    return parent;
    042            }
    043
    044            public void setPreviousNode(HistoryNode node) {
    045                    if (node instanceof HistoryTreeNode) {
    046                            this.parent = (HistoryTreeNode)node;
    047                            parent.addChild(this);
    048                            setDepth(parent.getDepth()+1);
    049                    }
    050            }
    051
    052            public int getDepth() {
    053                    return depth;
    054            }
    055
    056            public void setDepth(int depth) {
    057                    this.depth = depth;
    058            }
    059
    060            public void addChild(HistoryTreeNode child) {
    061                    children.add(child);
    062            }
    063
    064            public void removeChild(HistoryTreeNode child) {
    065                    children.remove(child);
    066            }
    067
    068            public HistoryTreeNode getChild(ActionDecision childDecision) {
    069                    for (HistoryTreeNode c : children) {
    070                            if (c.getAction().equals(childDecision)) {
    071                                    return c;
    072                            }
    073                    }
    074                    return null;
    075            }
    076
    077    //      public double getScore() {
    078    //              return score;
    079    //      }
    080    //
    081    //      public void setScore(double score) {
    082    //              this.score = score;
    083    //      }
    084
    085            public int getPosition() {
    086                    return depth;
    087            }
    088
    089            public void clear() throws MaltChainedException {
    090                    if (parent != null) {
    091                            parent.removeChild(this);
    092                    }
    093                    setAction(null);
    094                    setPreviousNode(null);
    095                    children.clear();
    096            }
    097
    098            public boolean equals(Object obj) {
    099                    return super.equals(obj);
    100            }
    101
    102            public int hashCode() {
    103                    return super.hashCode();
    104            }
    105
    106            public String toString() {
    107                    final StringBuilder sb = new StringBuilder();
    108                    for (int i = 0; i <= depth; i++) {
    109                            sb.append("  ");
    110                    }
    111                    sb.append(action);
    112                    sb.append('\n');
    113                    for (int i = 0; i < children.size(); i++) {
    114                            sb.append(children.get(i));
    115                    }
    116                    return sb.toString();
    117            }
    118
    119    }