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