001package org.maltparser.core.syntaxgraph.writer;
002
003import org.maltparser.core.config.ConfigurationDir;
004import org.maltparser.core.exception.MaltChainedException;
005import org.maltparser.core.flow.FlowChartInstance;
006import org.maltparser.core.flow.item.ChartItem;
007import org.maltparser.core.flow.spec.ChartItemSpecification;
008import org.maltparser.core.io.dataformat.DataFormatException;
009import org.maltparser.core.io.dataformat.DataFormatInstance;
010import org.maltparser.core.io.dataformat.DataFormatManager;
011import org.maltparser.core.options.OptionManager;
012import org.maltparser.core.symbol.SymbolTableHandler;
013import org.maltparser.core.syntaxgraph.TokenStructure;
014
015/**
016*
017*
018* @author Johan Hall
019*/
020public class WriteChartItem extends ChartItem {
021        private String idName;
022        private String outputFormatName;
023        private String outputFileName;
024        private String outputCharSet;
025        private String writerOptions;
026        private Class<? extends SyntaxGraphWriter> graphWriterClass;
027        
028        private String nullValueStrategy;
029        
030        private SyntaxGraphWriter writer;
031        private String sourceName;
032        private String optiongroupName;
033        private DataFormatInstance outputDataFormatInstance;
034        private TokenStructure cachedGraph = null;
035        
036        public WriteChartItem() { super(); }
037        
038        public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
039                super.initialize(flowChartinstance, chartItemSpecification);
040                
041                for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
042                        if (key.equals("id")) {
043                                idName = chartItemSpecification.getChartItemAttributes().get(key);
044                        } else if (key.equals("source")) {
045                                sourceName = chartItemSpecification.getChartItemAttributes().get(key);
046                        } else if (key.equals("optiongroup")) {
047                                optiongroupName = chartItemSpecification.getChartItemAttributes().get(key);
048                        }
049                }
050                
051                if (idName == null) {
052                        idName = getChartElement("write").getAttributes().get("id").getDefaultValue();
053                } else if (sourceName == null) {
054                        sourceName = getChartElement("write").getAttributes().get("source").getDefaultValue();
055                } else if (optiongroupName == null) {
056                        optiongroupName = getChartElement("write").getAttributes().get("optiongroup").getDefaultValue();
057                }
058                
059                setOutputFormatName(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "format").toString());
060                setOutputFileName(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "outfile").toString());
061                setOutputCharSet(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "charset").toString());
062                setWriterOptions(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "writer_options").toString());
063                setSyntaxGraphWriterClass((Class<?>)OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "writer"));
064
065                setNullValueStrategy(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString());
066
067                initOutput(getNullValueStrategy());
068                initWriter(getSyntaxGraphWriterClass(), getOutputFileName(), getOutputCharSet(), getWriterOptions());
069        }
070        
071        public int preprocess(int signal) throws MaltChainedException {
072                return signal;
073        }
074        
075        public int process(int signal) throws MaltChainedException {
076                if (cachedGraph == null) {
077                        cachedGraph = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
078                        writer.writeProlog();
079                }
080                writer.writeSentence(cachedGraph);
081                if (signal == ChartItem.TERMINATE) {
082                        writer.writeEpilog();
083                }
084                return signal;
085        }
086        
087        public int postprocess(int signal) throws MaltChainedException {
088                return signal;
089        }
090        
091        public void terminate() throws MaltChainedException {
092                if (writer != null) {
093                        writer.close();
094                        writer = null;
095                }
096                outputDataFormatInstance = null;
097                cachedGraph = null;
098        }
099        
100        public String getOutputFormatName() {
101                if (outputFormatName == null) {
102                        return "/appdata/dataformat/conllx.xml";
103                }
104                return outputFormatName;
105        }
106
107        public void setOutputFormatName(String outputFormatName) {
108                this.outputFormatName = outputFormatName;
109        }
110
111        public String getOutputFileName() {
112                if (outputFileName == null) {
113                        return "/dev/stdout";
114                }
115                return outputFileName;
116        }
117
118        public void setOutputFileName(String outputFileName) {
119                this.outputFileName = outputFileName;
120        }
121
122        public String getOutputCharSet() {
123                if (outputCharSet == null) {
124                        return "UTF-8";
125                }
126                return outputCharSet;
127        }
128
129        public void setOutputCharSet(String outputCharSet) {
130                this.outputCharSet = outputCharSet;
131        }
132
133        public String getWriterOptions() {
134                if (writerOptions == null) {
135                        return "";
136                }
137                return writerOptions;
138        }
139
140        public void setWriterOptions(String writerOptions) {
141                this.writerOptions = writerOptions;
142        }
143
144        public Class<? extends SyntaxGraphWriter> getSyntaxGraphWriterClass() {
145                return graphWriterClass;
146        }
147
148        public void setSyntaxGraphWriterClass(Class<?> graphWriterClass) throws MaltChainedException {
149                try {
150                        if (graphWriterClass != null) {
151                                this.graphWriterClass = graphWriterClass.asSubclass(org.maltparser.core.syntaxgraph.writer.SyntaxGraphWriter.class);
152                        }
153                } catch (ClassCastException e) {
154                        throw new DataFormatException("The class '"+graphWriterClass.getName()+"' is not a subclass of '"+org.maltparser.core.syntaxgraph.writer.SyntaxGraphWriter.class.getName()+"'. ", e);
155                }
156        }
157
158        public String getNullValueStrategy() {
159                if (nullValueStrategy == null) {
160                        return "one";
161                }
162                return nullValueStrategy;
163        }
164
165        public void setNullValueStrategy(String nullValueStrategy) {
166                this.nullValueStrategy = nullValueStrategy;
167        }
168        
169        public void initOutput(String nullValueStategy) throws MaltChainedException {
170                ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
171                DataFormatManager dataFormatManager = configDir.getDataFormatManager();
172                SymbolTableHandler symbolTables = configDir.getSymbolTables();
173                
174                if (configDir.sizeDataFormatInstance() == 0 || dataFormatManager.getInputDataFormatSpec() != dataFormatManager.getOutputDataFormatSpec()) {
175                        outputDataFormatInstance = dataFormatManager.getOutputDataFormatSpec().createDataFormatInstance(symbolTables, nullValueStategy);
176                        configDir.addDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName(), outputDataFormatInstance);
177                } else {
178                        outputDataFormatInstance = configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName()); //dataFormatInstances.get(dataFormatManager.getInputDataFormatSpec().getDataFormatName());
179                }
180        }
181        
182        public void initWriter(Class<? extends SyntaxGraphWriter> syntaxGraphWriterClass, String outputFile, String outputCharSet, 
183                        String writerOption) throws MaltChainedException {
184                try {   
185                        writer = syntaxGraphWriterClass.newInstance();
186                        if (outputFile == null || outputFile.length() == 0 || outputFile.equals("/dev/stdout")) {
187                                writer.open(System.out, outputCharSet);
188                        } else {
189                                writer.open(outputFile, outputCharSet);
190                        }
191                        writer.setDataFormatInstance(outputDataFormatInstance);
192                        writer.setOptions(writerOption);
193                } catch (InstantiationException e) {
194                        throw new DataFormatException("The data writer '"+syntaxGraphWriterClass.getName()+"' cannot be initialized. ", e);
195                } catch (IllegalAccessException e) {
196                        throw new DataFormatException("The data writer '"+syntaxGraphWriterClass.getName()+"' cannot be initialized. ", e);
197                }
198        }
199
200        public Class<? extends SyntaxGraphWriter> getGraphWriterClass() {
201                return graphWriterClass;
202        }
203
204        public SyntaxGraphWriter getWriter() {
205                return writer;
206        }
207
208        public String getSourceName() {
209                return sourceName;
210        }
211
212        public DataFormatInstance getOutputDataFormatInstance() {
213                return outputDataFormatInstance;
214        }
215        
216        public boolean equals(Object obj) {
217                if (this == obj)
218                        return true;
219                if (obj == null)
220                        return false;
221                if (getClass() != obj.getClass())
222                        return false;
223                return obj.toString().equals(this.toString());
224        }
225        
226        public int hashCode() {
227                return 217 + (null == toString() ? 0 : toString().hashCode());
228        }
229        
230        public String toString() {
231                StringBuilder sb = new StringBuilder();
232                sb.append("    write ");
233                sb.append("id:");sb.append(idName);
234                sb.append(' ');
235                sb.append("source:");
236                sb.append(sourceName);
237                sb.append(' ');
238                sb.append("optiongroup:");
239                sb.append(optiongroupName);
240                return sb.toString();
241        }
242}