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