001 package org.maltparser.core.flow; 002 003 import java.util.HashMap; 004 import java.util.LinkedHashSet; 005 006 import org.maltparser.core.exception.MaltChainedException; 007 import org.maltparser.core.flow.item.ChartItem; 008 import org.maltparser.core.flow.spec.ChartItemSpecification; 009 import org.maltparser.core.flow.spec.ChartSpecification; 010 import org.maltparser.core.helper.Malt04; 011 import org.maltparser.core.helper.SystemLogger; 012 import org.maltparser.core.io.dataformat.DataFormatInstance; 013 import org.maltparser.core.io.dataformat.DataFormatManager; 014 import org.maltparser.core.options.OptionManager; 015 import org.maltparser.core.symbol.SymbolTableHandler; 016 import org.maltparser.core.symbol.trie.TrieSymbolTableHandler; 017 /** 018 * 019 * 020 * @author Johan Hall 021 */ 022 public class FlowChartInstance { 023 private FlowChartManager flowChartManager; 024 private int optionContainerIndex; 025 private String name; 026 private ChartSpecification chartSpecification; 027 private final LinkedHashSet<ChartItem> preProcessChartItems; 028 private final LinkedHashSet<ChartItem> processChartItems; 029 private final LinkedHashSet<ChartItem> postProcessChartItems; 030 031 private SymbolTableHandler symbolTables; 032 private DataFormatManager dataFormatManager; 033 private final HashMap<String,DataFormatInstance> dataFormatInstances; 034 private final HashMap<String,Object> flowChartRegistry; 035 private final StringBuilder flowChartRegistryKey; 036 037 public FlowChartInstance(int optionContainerIndex, ChartSpecification chartSpecification, FlowChartManager flowChartManager) throws MaltChainedException { 038 setFlowChartManager(flowChartManager); 039 setOptionContainerIndex(optionContainerIndex); 040 setChartSpecification(chartSpecification); 041 042 flowChartRegistry = new HashMap<String,Object>(); 043 flowChartRegistryKey = new StringBuilder(); 044 dataFormatInstances = new HashMap<String, DataFormatInstance>(3); 045 046 dataFormatManager = new DataFormatManager(OptionManager.instance().getOptionValue(0, "input", "format").toString(), OptionManager.instance().getOptionValue(0, "output", "format").toString()); 047 symbolTables = new TrieSymbolTableHandler(); 048 Malt04.loadAllMalt04Tagset(OptionManager.instance(),getOptionContainerIndex(),getSymbolTables(),SystemLogger.logger()); 049 050 preProcessChartItems = new LinkedHashSet<ChartItem>(); 051 for (ChartItemSpecification chartItemSpecification : chartSpecification.getPreProcessChartItemSpecifications()) { 052 preProcessChartItems.add(initChartItem(chartItemSpecification)); 053 } 054 processChartItems = new LinkedHashSet<ChartItem>(); 055 for (ChartItemSpecification chartItemSpecification : chartSpecification.getProcessChartItemSpecifications()) { 056 processChartItems.add(initChartItem(chartItemSpecification)); 057 } 058 059 postProcessChartItems = new LinkedHashSet<ChartItem>(); 060 for (ChartItemSpecification chartItemSpecification : chartSpecification.getPostProcessChartItemSpecifications()) { 061 postProcessChartItems.add(initChartItem(chartItemSpecification)); 062 } 063 } 064 065 protected ChartItem initChartItem(ChartItemSpecification chartItemSpecification) throws MaltChainedException { 066 ChartItem chartItem = null; 067 try { 068 chartItem = chartItemSpecification.getChartItemClass().newInstance(); 069 chartItem.initialize(this, chartItemSpecification); 070 } catch (InstantiationException e) { 071 throw new FlowException("The chart item '"+chartItemSpecification.getChartItemName()+"' could not be created. ", e); 072 } catch (IllegalAccessException e) { 073 throw new FlowException("The chart item '"+chartItemSpecification.getChartItemName()+"' could not be created. ", e); 074 } 075 return chartItem; 076 } 077 078 public SymbolTableHandler getSymbolTables() { 079 return symbolTables; 080 } 081 082 public void setSymbolTables(SymbolTableHandler symbolTables) { 083 this.symbolTables = symbolTables; 084 } 085 086 public DataFormatManager getDataFormatManager() { 087 return dataFormatManager; 088 } 089 090 public void setDataFormatManager(DataFormatManager dataFormatManager) { 091 this.dataFormatManager = dataFormatManager; 092 } 093 094 private void setFlowChartRegistryKey(Class<?> entryClass, String identifier) { 095 flowChartRegistryKey.setLength(0); 096 flowChartRegistryKey.append(identifier.toString()); 097 flowChartRegistryKey.append(entryClass.toString()); 098 } 099 100 public void addFlowChartRegistry(Class<?> entryClass, String identifier, Object entry) { 101 setFlowChartRegistryKey(entryClass, identifier); 102 flowChartRegistry.put(flowChartRegistryKey.toString(), entry); 103 } 104 105 public void removeFlowChartRegistry(Class<?> entryClass, String identifier) { 106 setFlowChartRegistryKey(entryClass, identifier); 107 flowChartRegistry.remove(flowChartRegistryKey.toString()); 108 } 109 110 public Object getFlowChartRegistry(Class<?> entryClass, String identifier) { 111 setFlowChartRegistryKey(entryClass, identifier); 112 return flowChartRegistry.get(flowChartRegistryKey.toString()); 113 } 114 115 public HashMap<String, DataFormatInstance> getDataFormatInstances() { 116 return dataFormatInstances; 117 } 118 119 public FlowChartManager getFlowChartManager() { 120 return flowChartManager; 121 } 122 123 protected void setFlowChartManager(FlowChartManager flowChartManager) { 124 this.flowChartManager = flowChartManager; 125 } 126 127 public int getOptionContainerIndex() { 128 return optionContainerIndex; 129 } 130 131 protected void setOptionContainerIndex(int optionContainerIndex) { 132 this.optionContainerIndex = optionContainerIndex; 133 } 134 135 public ChartSpecification getChartSpecification() { 136 return chartSpecification; 137 } 138 139 protected void setChartSpecification(ChartSpecification chartSpecification) { 140 this.chartSpecification = chartSpecification; 141 } 142 143 public LinkedHashSet<ChartItem> getPreProcessChartItems() { 144 return preProcessChartItems; 145 } 146 147 public LinkedHashSet<ChartItem> getProcessChartItems() { 148 return processChartItems; 149 } 150 151 public LinkedHashSet<ChartItem> getPostProcessChartItems() { 152 return postProcessChartItems; 153 } 154 155 public boolean hasPreProcessChartItems() { 156 return !(preProcessChartItems.size() == 0); 157 } 158 159 public boolean hasProcessChartItems() { 160 return !(processChartItems.size() == 0); 161 } 162 163 public boolean hasPostProcessChartItems() { 164 return !(postProcessChartItems.size() == 0); 165 } 166 167 public boolean preprocess() throws MaltChainedException { 168 LinkedHashSet<ChartItem> chartItems = getPreProcessChartItems(); 169 if (chartItems.size() == 0) { 170 return false; 171 } 172 boolean more = true; 173 for (ChartItem chartItem : chartItems) { 174 more = chartItem.preprocess(); 175 if (!more) { 176 return false; 177 } 178 } 179 return more; 180 } 181 182 public boolean process() throws MaltChainedException { 183 LinkedHashSet<ChartItem> chartItems = getProcessChartItems(); 184 if (chartItems.size() == 0) { 185 return false; 186 } 187 boolean more = true; 188 for (ChartItem chartItem : chartItems) { 189 more = chartItem.process(more); 190 // if (!more) { 191 // return false; 192 // } 193 } 194 return more; 195 } 196 197 public boolean postprocess() throws MaltChainedException { 198 LinkedHashSet<ChartItem> chartItems = getPostProcessChartItems(); 199 if (chartItems.size() == 0) { 200 return false; 201 } 202 boolean more = true; 203 for (ChartItem chartItem : chartItems) { 204 more = chartItem.postprocess(); 205 if (!more) { 206 return false; 207 } 208 } 209 return more; 210 } 211 212 public void terminate() throws MaltChainedException { 213 LinkedHashSet<ChartItem> chartItems = getPreProcessChartItems(); 214 for (ChartItem chartItem : chartItems) { 215 chartItem.terminate(); 216 } 217 chartItems = getProcessChartItems(); 218 for (ChartItem chartItem : chartItems) { 219 chartItem.terminate(); 220 } 221 chartItems = getPostProcessChartItems(); 222 for (ChartItem chartItem : chartItems) { 223 chartItem.terminate(); 224 } 225 flowChartRegistry.clear(); 226 flowChartRegistryKey.setLength(0); 227 symbolTables = null; 228 229 } 230 231 public String getName() { 232 return name; 233 } 234 235 public void setName(String name) { 236 this.name = name; 237 } 238 239 public int hashCode() { 240 final int prime = 31; 241 int result = 1; 242 result = prime * result + optionContainerIndex; 243 result = prime * result + ((name == null) ? 0 : name.hashCode()); 244 result = prime * result + ((chartSpecification == null) ? 0 : chartSpecification.hashCode()); 245 result = prime * result + ((dataFormatInstances == null) ? 0 : dataFormatInstances.hashCode()); 246 result = prime * result + ((dataFormatManager == null) ? 0 : dataFormatManager.hashCode()); 247 result = prime * result + ((flowChartRegistry == null) ? 0 : flowChartRegistry.hashCode()); 248 result = prime * result + ((postProcessChartItems == null) ? 0 : postProcessChartItems.hashCode()); 249 result = prime * result + ((preProcessChartItems == null) ? 0 : preProcessChartItems.hashCode()); 250 result = prime * result + ((processChartItems == null) ? 0 : processChartItems.hashCode()); 251 result = prime * result + ((symbolTables == null) ? 0 : symbolTables.hashCode()); 252 return result; 253 } 254 255 public boolean equals(Object obj) { 256 if (this == obj) 257 return true; 258 if (obj == null) 259 return false; 260 if (getClass() != obj.getClass()) 261 return false; 262 FlowChartInstance other = (FlowChartInstance) obj; 263 if (optionContainerIndex != other.optionContainerIndex) 264 return false; 265 if (name == null) { 266 if (other.name != null) 267 return false; 268 } else if (!name.equals(other.name)) 269 return false; 270 if (chartSpecification == null) { 271 if (other.chartSpecification != null) 272 return false; 273 } else if (!chartSpecification.equals(other.chartSpecification)) 274 return false; 275 if (dataFormatInstances == null) { 276 if (other.dataFormatInstances != null) 277 return false; 278 } else if (!dataFormatInstances.equals(other.dataFormatInstances)) 279 return false; 280 if (dataFormatManager == null) { 281 if (other.dataFormatManager != null) 282 return false; 283 } else if (!dataFormatManager.equals(other.dataFormatManager)) 284 return false; 285 if (flowChartRegistry == null) { 286 if (other.flowChartRegistry != null) 287 return false; 288 } else if (!flowChartRegistry.equals(other.flowChartRegistry)) 289 return false; 290 if (postProcessChartItems == null) { 291 if (other.postProcessChartItems != null) 292 return false; 293 } else if (!postProcessChartItems.equals(other.postProcessChartItems)) 294 return false; 295 if (preProcessChartItems == null) { 296 if (other.preProcessChartItems != null) 297 return false; 298 } else if (!preProcessChartItems.equals(other.preProcessChartItems)) 299 return false; 300 if (processChartItems == null) { 301 if (other.processChartItems != null) 302 return false; 303 } else if (!processChartItems.equals(other.processChartItems)) 304 return false; 305 if (symbolTables == null) { 306 if (other.symbolTables != null) 307 return false; 308 } else if (!symbolTables.equals(other.symbolTables)) 309 return false; 310 return true; 311 } 312 313 public String toString() { 314 final StringBuilder sb = new StringBuilder(); 315 sb.append(name);sb.append('\n'); 316 if (preProcessChartItems.size() > 0) { 317 sb.append(" preprocess:");sb.append('\n'); 318 for (ChartItem key : preProcessChartItems) { 319 sb.append(key); 320 sb.append('\n'); 321 } 322 } 323 if (processChartItems.size() > 0) { 324 sb.append(" process:");sb.append('\n'); 325 for (ChartItem key : processChartItems) { 326 sb.append(key); 327 sb.append('\n'); 328 } 329 } 330 if (postProcessChartItems.size() > 0) { 331 sb.append(" postprocess:");sb.append('\n'); 332 for (ChartItem key : postProcessChartItems) { 333 sb.append(key); 334 sb.append('\n'); 335 } 336 } 337 338 return sb.toString(); 339 } 340 }