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