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