MOA 12.03
Real Time Analytics for Data Streams
WriteStreamToARFFFile.java
Go to the documentation of this file.
00001 /*
00002  *    WriteStreamToARFFFile.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.tasks;
00021 
00022 import java.io.BufferedWriter;
00023 import java.io.File;
00024 import java.io.FileWriter;
00025 import java.io.Writer;
00026 
00027 import moa.core.ObjectRepository;
00028 import moa.options.ClassOption;
00029 import moa.options.FileOption;
00030 import moa.options.FlagOption;
00031 import moa.options.IntOption;
00032 import moa.streams.InstanceStream;
00033 
00040 public class WriteStreamToARFFFile extends MainTask {
00041 
00042     @Override
00043     public String getPurposeString() {
00044         return "Outputs a stream to an ARFF file.";
00045     }
00046 
00047     private static final long serialVersionUID = 1L;
00048 
00049     public ClassOption streamOption = new ClassOption("stream", 's',
00050             "Stream to write.", InstanceStream.class,
00051             "generators.RandomTreeGenerator");
00052 
00053     public FileOption arffFileOption = new FileOption("arffFile", 'f',
00054             "Destination ARFF file.", null, "arff", true);
00055 
00056     public IntOption maxInstancesOption = new IntOption("maxInstances", 'm',
00057             "Maximum number of instances to write to file.", 10000000, 0,
00058             Integer.MAX_VALUE);
00059 
00060     public FlagOption suppressHeaderOption = new FlagOption("suppressHeader",
00061             'h', "Suppress header from output.");
00062 
00063     @Override
00064     protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) {
00065         InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
00066         File destFile = this.arffFileOption.getFile();
00067         if (destFile != null) {
00068             try {
00069                 Writer w = new BufferedWriter(new FileWriter(destFile));
00070                 monitor.setCurrentActivityDescription("Writing stream to ARFF");
00071                 if (!this.suppressHeaderOption.isSet()) {
00072                     w.write(stream.getHeader().toString());
00073                     w.write("\n");
00074                 }
00075                 int numWritten = 0;
00076                 while ((numWritten < this.maxInstancesOption.getValue())
00077                         && stream.hasMoreInstances()) {
00078                     w.write(stream.nextInstance().toString());
00079                     w.write("\n");
00080                     numWritten++;
00081                 }
00082                 w.close();
00083             } catch (Exception ex) {
00084                 throw new RuntimeException(
00085                         "Failed writing to file " + destFile, ex);
00086             }
00087             return "Stream written to ARFF file " + destFile;
00088         }
00089         throw new IllegalArgumentException("No destination file to write to.");
00090     }
00091 
00092     @Override
00093     public Class<?> getTaskResultType() {
00094         return String.class;
00095     }
00096 }
 All Classes Namespaces Files Functions Variables Enumerations