    001    package org.maltparser.parser.history.action;
    002
    003
    004    import java.lang.reflect.Constructor;
    005    import java.lang.reflect.InvocationTargetException;
    006
    007    import org.maltparser.core.exception.MaltChainedException;
    008    import org.maltparser.parser.history.GuideHistory;
    009    import org.maltparser.parser.history.HistoryException;
    010    import org.maltparser.parser.history.History;
    011    import org.maltparser.parser.history.container.TableContainer;
    012    import org.maltparser.parser.history.container.TableContainer.RelationToNextDecision;
    013    import org.maltparser.parser.history.kbest.KBestList;
    014    /**
    015    *
    016    * @author Johan Hall
    017    * @since 1.1
    018    **/
    019    public class SimpleDecisionAction implements  SingleDecision {
    020            private History history;
    021            private TableContainer tableContainer;
    022            private int decision;
    023            private KBestList kBestList;
    024
    025
    026            public SimpleDecisionAction(History history, TableContainer tableContainer) throws MaltChainedException {
    027                    this.history = history;
    028                    this.tableContainer = tableContainer;
    029                    createKBestList();
    030                    clear();
    031            }
    032
    033            /* Action interface */
    034            public void clear() {
    035                    decision = -1;
    036                    if (kBestList != null) {
    037                            kBestList.reset();
    038                    }
    039            }
    040
    041            public int numberOfDecisions() {
    042                    return 1;
    043            }
    044
    045            /* SingleDecision interface */
    046            public void addDecision(int code) throws MaltChainedException {
    047                    if (code == -1 || !tableContainer.containCode(code)) {
    048                            decision = -1;
    049                    }
    050                    decision = code;
    051            }
    052
    053            public void addDecision(String symbol) throws MaltChainedException {
    054                    decision = tableContainer.getCode(symbol);
    055            }
    056
    057            public int getDecisionCode() throws MaltChainedException {
    058                    return decision;
    059            }
    060
    061            public int getDecisionCode(String symbol) throws MaltChainedException {
    062                    return tableContainer.getCode(symbol);
    063            }
    064
    065            public String getDecisionSymbol() throws MaltChainedException {
    066                    return tableContainer.getSymbol(decision);
    067            }
    068
    069            public boolean updateFromKBestList() throws MaltChainedException {
    070                    if (kBestList == null) {
    071                            return false;
    072                    }
    073                    return kBestList.updateActionWithNextKBest();
    074            }
    075
    076            public boolean continueWithNextDecision() throws MaltChainedException {
    077                    return tableContainer.continueWithNextDecision(decision);
    078            }
    079
    080            public GuideHistory getGuideHistory() {
    081                    return (GuideHistory)history;
    082            }
    083
    084            /* Getters and Setters */
    085            public History getActionHistory() {
    086                    return history;
    087            }
    088
    089            public TableContainer getTableContainer() {
    090                    return tableContainer;
    091            }
    092
    093            public KBestList getKBestList() throws MaltChainedException {
    094                    return kBestList;
    095            }
    096
    097            public RelationToNextDecision getRelationToNextDecision() {
    098                    return tableContainer.getRelationToNextDecision();
    099            }
    100
    101            private void createKBestList() throws MaltChainedException {
    102                    final Class<?> kBestListClass = history.getKBestListClass();
    103                    if (kBestListClass == null) {
    104                            return;
    105                    }
    106                    final Class<?>[] argTypes = { java.lang.Integer.class, org.maltparser.parser.history.action.SingleDecision.class };
    107
    108                    final Object[] arguments = new Object[2];
    109                    arguments[0] = history.getKBestSize();
    110                    arguments[1] = this;
    111                    try {
    112                            final Constructor<?> constructor = kBestListClass.getConstructor(argTypes);
    113                            kBestList = (KBestList)constructor.newInstance(arguments);
    114                    } catch (NoSuchMethodException e) {
    115                            throw new HistoryException("The kBestlist '"+kBestListClass.getName()+"' cannot be initialized. ", e);
    116                    } catch (InstantiationException e) {
    117                            throw new HistoryException("The kBestlist '"+kBestListClass.getName()+"' cannot be initialized. ", e);
    118                    } catch (IllegalAccessException e) {
    119                            throw new HistoryException("The kBestlist '"+kBestListClass.getName()+"' cannot be initialized. ", e);
    120                    } catch (InvocationTargetException e) {
    121                            throw new HistoryException("The kBestlist '"+kBestListClass.getName()+"' cannot be initialized. ", e);
    122                    }
    123            }
    124
    125            public String toString() {
    126                    final StringBuilder sb = new StringBuilder();
    127                    sb.append(decision);
    128                    return sb.toString();
    129            }
    130    }