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