001    package org.maltparser;
002    
003    import java.util.SortedMap;
004    import java.util.TreeMap;
005    
006    import org.maltparser.core.exception.MaltChainedException;
007    import org.maltparser.core.flow.FlowChartInstance;
008    import org.maltparser.core.flow.FlowChartManager;
009    import org.maltparser.core.helper.SystemLogger;
010    import org.maltparser.core.helper.Util;
011    import org.maltparser.core.options.OptionManager;
012    import org.maltparser.core.plugin.PluginLoader;
013    
014    
015    public class Engine  {
016            private final long startTime;
017            private final FlowChartManager flowChartManager;
018            private final SortedMap<Integer,FlowChartInstance> flowChartInstances;
019            
020            public Engine() throws MaltChainedException {
021                    startTime = System.currentTimeMillis();
022                    flowChartManager = new FlowChartManager();
023                    flowChartManager.getFlowChartSystem().load(getClass().getResource("/appdata/flow/flowchartsystem.xml"));
024                    flowChartManager.getFlowChartSystem().load(PluginLoader.instance());
025                    flowChartManager.load(getClass().getResource("/appdata/flow/flowcharts.xml"));
026                    flowChartManager.load(PluginLoader.instance());
027                    flowChartInstances = new TreeMap<Integer,FlowChartInstance>();
028            }
029    
030            public FlowChartInstance initialize(int optionContainerIndex) throws MaltChainedException {
031                    String flowChartName = null;
032                    if (OptionManager.instance().getOptionValueNoDefault(optionContainerIndex, "config", "flowchart") != null) {
033                            flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "config", "flowchart").toString();
034                    }
035                    if (flowChartName == null) {
036                            if (OptionManager.instance().getOptionValueNoDefault(optionContainerIndex, "singlemalt", "mode") != null) {
037                                    // This fix maps --singlemalt-mode option to --config-flowchart option because of historical reasons (version 1.0-1.1)
038                                    flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "singlemalt", "mode").toString();
039                                    OptionManager.instance().overloadOptionValue(optionContainerIndex, "config", "flowchart", flowChartName);
040                            } else {
041                                    flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "config", "flowchart").toString();
042                            }
043                    }
044                    FlowChartInstance flowChartInstance = flowChartManager.initialize(optionContainerIndex, flowChartName);
045                    flowChartInstances.put(optionContainerIndex, flowChartInstance);
046                    return flowChartInstance;
047            }
048            
049            public void process(int optionContainerIndex) throws MaltChainedException {
050                    FlowChartInstance flowChartInstance = flowChartInstances.get(optionContainerIndex);
051                    if (flowChartInstance.hasPreProcessChartItems()) {
052                            flowChartInstance.preprocess();
053                    }
054                    if (flowChartInstance.hasProcessChartItems()) {
055                            boolean moreInput = true;
056                            int tic = 0;
057                            int sentenceCounter = 0;
058                            while (moreInput) {
059                                    moreInput = flowChartInstance.process();
060                                    if (moreInput) {
061                                            sentenceCounter++;
062                                    }
063    //                              System.out.println(sentenceCounter);
064                                    if (sentenceCounter < 101 && sentenceCounter == 1 || sentenceCounter == 10 || sentenceCounter == 100) {
065                                            Util.startTicer(SystemLogger.logger(), startTime, 10, sentenceCounter);
066                                    } 
067                                    if (sentenceCounter%100 == 0) {
068                                            tic = Util.simpleTicer(SystemLogger.logger(), startTime, 10, tic, sentenceCounter);
069                                    }
070                            }
071                            Util.endTicer(SystemLogger.logger(), startTime, 10, tic, sentenceCounter);
072                    }
073                    if (flowChartInstance.hasPostProcessChartItems()) {
074                            flowChartInstance.postprocess();
075                    }
076            }
077            
078            public void terminate(int optionContainerIndex) throws MaltChainedException {
079                    flowChartInstances.get(optionContainerIndex).terminate();
080            }
081    }