    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    import org.maltparser.parser.history.kbest.ScoredKBestList;
    013
    014    /**
    015    *
    016    * @author Johan Hall
    017    * @since 1.1
    018    **/
    019    public class ComplexDecisionAction implements GuideUserAction, MultipleDecision {
    020            private History history;
    021            private ArrayList<SimpleDecisionAction> decisions;
    022
    023            public ComplexDecisionAction(History history) throws MaltChainedException {
    024                    this.history = history;
    025                    decisions = new ArrayList<SimpleDecisionAction>(history.getDecisionTables().size());
    026                    for (int i=0, n = history.getDecisionTables().size(); i < n; i++) {
    027                            decisions.add(new SimpleDecisionAction(history, history.getDecisionTables().get(i)));
    028                    }
    029            }
    030
    031            public ComplexDecisionAction(GuideHistory history) throws MaltChainedException {
    032                    this((History)history);
    033            }
    034
    035            /* GuideUserAction interface */
    036            public void addAction(ArrayList<ActionContainer> actionContainers) throws MaltChainedException {
    037                    if (actionContainers == null || actionContainers.size() != history.getActionTables().size()) {
    038                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
    039                    }
    040                    int j = 0;
    041                    for (int i = 0, n = history.getDecisionTables().size(); i < n; i++) {
    042                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
    043                                    CombinedTableContainer tableContainer = (CombinedTableContainer)history.getDecisionTables().get(i);
    044                                    int nContainers = tableContainer.getNumberContainers();
    045                                    decisions.get(i).addDecision(tableContainer.getCombinedCode(actionContainers.subList(j, j + nContainers)));
    046                                    j = j + nContainers;
    047                            } else {
    048                                    decisions.get(i).addDecision(actionContainers.get(j).getActionCode());
    049                                    j++;
    050                            }
    051                    }
    052            }
    053
    054            public void getAction(ArrayList<ActionContainer> actionContainers) throws MaltChainedException {
    055                    if (actionContainers == null || actionContainers.size() != history.getActionTables().size()) {
    056                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
    057                    }
    058                    int j = 0;
    059                    for (int i = 0, n=history.getDecisionTables().size(); i < n; i++) {
    060                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
    061                                    CombinedTableContainer tableContainer = (CombinedTableContainer)history.getDecisionTables().get(i);
    062                                    int nContainers = tableContainer.getNumberContainers();
    063                                    tableContainer.setActionContainer(actionContainers.subList(j, j + nContainers), decisions.get(i).getDecisionCode());
    064                                    j = j + nContainers;
    065                            } else {
    066                                    actionContainers.get(j).setAction(decisions.get(i).getDecisionCode());
    067                                    j++;
    068                            }
    069                    }
    070            }
    071
    072            public void addAction(ActionContainer[] actionContainers) throws MaltChainedException {
    073                    if (actionContainers == null || actionContainers.length != history.getActionTables().size()) {
    074                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
    075                    }
    076                    int j = 0;
    077                    for (int i = 0, n = history.getDecisionTables().size(); i < n; i++) {
    078                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
    079                                    CombinedTableContainer tableContainer = (CombinedTableContainer)history.getDecisionTables().get(i);
    080                                    int nContainers = tableContainer.getNumberContainers();
    081                                    decisions.get(i).addDecision(tableContainer.getCombinedCode(actionContainers, j));
    082                                    j = j + nContainers;
    083                            } else {
    084                                    decisions.get(i).addDecision(actionContainers[j].getActionCode());
    085                                    j++;
    086                            }
    087                    }
    088            }
    089
    090            public void getAction(ActionContainer[] actionContainers) throws MaltChainedException {
    091                    if (actionContainers == null || actionContainers.length != history.getActionTables().size()) {
    092                            throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
    093                    }
    094                    int j = 0;
    095                    for (int i = 0, n=history.getDecisionTables().size(); i < n; i++) {
    096                            if (history.getDecisionTables().get(i) instanceof CombinedTableContainer) {
    097                                    CombinedTableContainer tableContainer = (CombinedTableContainer)history.getDecisionTables().get(i);
    098                                    int nContainers = tableContainer.getNumberContainers();
    099                                    tableContainer.setActionContainer(actionContainers, j, decisions.get(i).getDecisionCode());
    100                                    j = j + nContainers;
    101                            } else {
    102                                    actionContainers[j].setAction(decisions.get(i).getDecisionCode());
    103                                    j++;
    104                            }
    105                    }
    106            }
    107
    108
    109            public void getKBestLists(ArrayList<ScoredKBestList> kbestListContainers) throws MaltChainedException {
    110    //              if (kbestListContainers == null || kbestListContainers.size() != history.getActionTables().size()) {
    111    //                      throw new HistoryException("The action containers does not exist or is not of the same size as the action table. ");
    112    //              }
    113                    kbestListContainers.clear();
    114                    for (int i = 0, n=decisions.size(); i < n; i++) {
    115                            kbestListContainers.add((ScoredKBestList)decisions.get(i).getKBestList());
    116                    }
    117            }
    118
    119            public void getKBestLists(ScoredKBestList[] kbestListContainers) throws MaltChainedException {
    120                    for (int i = 0, n=decisions.size(); i < n; i++) {
    121                            kbestListContainers[0] = (ScoredKBestList)decisions.get(i).getKBestList();
    122                    }
    123            }
    124
    125            public int numberOfActions() {
    126                    return history.getActionTables().size();
    127            }
    128
    129            public GuideUserHistory getGuideUserHistory() {
    130                    return (GuideUserHistory)history;
    131            }
    132
    133            public void clear() {
    134                    for (int i=0, n = decisions.size(); i < n;i++) {
    135                            decisions.get(i).clear();
    136                    }
    137            }
    138
    139            /* MultipleDecision */
    140            public SingleDecision getSingleDecision(int decisionIndex) throws MaltChainedException {
    141                    return decisions.get(decisionIndex);
    142            }
    143
    144            /* GuideDecision */
    145            public int numberOfDecisions() {
    146                    return history.getDecisionTables().size();
    147            }
    148
    149            public GuideHistory getGuideHistory() {
    150                    return (GuideHistory)history;
    151            }
    152
    153            public boolean equals(Object obj) {
    154                    if (this == obj)
    155                            return true;
    156                    if (obj == null)
    157                            return false;
    158                    if (getClass() != obj.getClass())
    159                            return false;
    160                    ComplexDecisionAction other = (ComplexDecisionAction) obj;
    161                    if (decisions == null) {
    162                            if (other.decisions != null)
    163                                    return false;
    164                    } else if (decisions.size() != other.decisions.size()) {
    165                            return false;
    166                    } else {
    167                            for (int i = 0; i < decisions.size(); i++) {
    168                                    try {
    169                                            if (decisions.get(i).getDecisionCode() != other.decisions.get(i).getDecisionCode()) {
    170                                                    return false;
    171                                            }
    172                                    } catch (MaltChainedException e) {
    173                                            System.err.println("Error in equals. ");
    174                                    }
    175                            }
    176                    }
    177
    178                    return true;
    179            }
    180
    181            public String toString() {
    182                    StringBuilder sb = new StringBuilder();
    183                    for (int i = 0, n = decisions.size(); i < n; i++) {
    184                            sb.append(decisions.get(i));
    185                            sb.append(';');
    186                    }
    187                    if (sb.length() > 0) {
    188                            sb.setLength(sb.length()-1);
    189                    }
    190                    return sb.toString();
    191            }
    192    }