001    package org.maltparser.parser.history.action;
002    
003    import java.util.ArrayList;
004    
005    import org.maltparser.core.exception.MaltChainedException;
006    import org.maltparser.parser.history.GuideHistory;
007    import org.maltparser.parser.history.GuideUserHistory;
008    import org.maltparser.parser.history.HistoryException;
009    import org.maltparser.parser.history.History;
010    import org.maltparser.parser.history.container.ActionContainer;
011    import org.maltparser.parser.history.container.CombinedTableContainer;
012    
013    /**
014    *
015    * @author Johan Hall
016    * @since 1.1
017    **/
018    public class ComplexDecisionAction implements GuideUserAction, MultipleDecision {
019            protected History history;
020            protected ArrayList<SimpleDecisionAction> decisions;
021            
022            public ComplexDecisionAction(History history) throws MaltChainedException {
023                    setHistory(history);
024                    initDecisions();
025            }
026            
027            public ComplexDecisionAction(GuideHistory history) throws MaltChainedException {
028                    setHistory((History)history);
029                    initDecisions();
030            }
031            
032            /* GuideUserAction interface */
033            public void addAction(ArrayList<ActionContainer> actionContainers) throws MaltChainedException {
034                    if (actionContainers == null || actionContainers.size() != history.getActionTables().size()) {
035                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
036                    }
037                    int j = 0;
038                    for (int i = 0, n = history.getDecisionTables().size(); i < n; i++) {
039                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
040                                    int nContainers = ((CombinedTableContainer)history.getDecisionTables().get(i)).getNumberContainers();
041                                    decisions.get(i).addDecision(((CombinedTableContainer)history.getDecisionTables().get(i)).getCombinedCode(actionContainers.subList(j, j + nContainers)));
042                                    j = j + nContainers;
043                            } else {
044                                    decisions.get(i).addDecision(actionContainers.get(j).getActionCode());
045                                    j++;
046                            }
047                    }
048            }
049            
050            public void getAction(ArrayList<ActionContainer> actionContainers) throws MaltChainedException {
051                    if (actionContainers == null || actionContainers.size() != history.getActionTables().size()) {
052                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
053                    }
054                    int j = 0;
055                    for (int i = 0, n=history.getDecisionTables().size(); i < n; i++) {
056                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
057                                    int nContainers = ((CombinedTableContainer)history.getDecisionTables().get(i)).getNumberContainers();
058                                    ((CombinedTableContainer)history.getDecisionTables().get(i)).setActionContainer(actionContainers.subList(j, j + nContainers), decisions.get(i).getDecisionCode());
059                                    j = j + nContainers;
060                            } else {
061                                    actionContainers.get(j).setAction(decisions.get(i).getDecisionCode());
062                                    j++;
063                            }
064                    }
065            }
066    
067            public int numberOfActions() {
068                    return history.getActionTables().size();
069            }
070            
071            public GuideUserHistory getGuideUserHistory() {
072                    return (GuideUserHistory)history;
073            }
074            
075            public void clear() {
076                    for (int i=0, n = decisions.size(); i < n;i++) {
077                            decisions.get(i).clear();
078                    }
079            }
080            
081            /* MultipleDecision */
082            public SingleDecision getSingleDecision(int decisionIndex) throws MaltChainedException {
083                    return decisions.get(decisionIndex);
084            }
085    
086            /* GuideDecision */
087            public int numberOfDecisions() {
088                    return history.getDecisionTables().size();
089            }
090    
091            public GuideHistory getGuideHistory() {
092                    return (GuideHistory)history;
093            }
094            
095            /* Initializer */
096            protected void initDecisions() throws MaltChainedException {
097                    decisions = new ArrayList<SimpleDecisionAction>(history.getDecisionTables().size());
098                    for (int i=0, n = history.getDecisionTables().size(); i < n; i++) {
099                            decisions.add(new SimpleDecisionAction(history, history.getDecisionTables().get(i)));
100                    }
101            }
102            
103            /* Getters and Setters */
104            protected void setHistory(History history) {
105                    this.history = history;
106            }
107    
108            public String toString() {
109                    StringBuilder sb = new StringBuilder();
110                    for (int i = 0, n = decisions.size(); i < n; i++) {
111                            sb.append(decisions.get(i));
112                            sb.append(';');
113                    }
114                    if (sb.length() > 0) {
115                            sb.setLength(sb.length()-1);
116                    }
117                    return sb.toString();
118            }
119    }