MOA 12.03
Real Time Analytics for Data Streams
MainTask.java
Go to the documentation of this file.
00001 /*
00002  *    MainTask.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.File;
00023 import java.io.IOException;
00024 import java.io.Serializable;
00025 
00026 import moa.core.ObjectRepository;
00027 import moa.core.SerializeUtils;
00028 import moa.options.FileOption;
00029 
00037 public abstract class MainTask extends AbstractTask {
00038 
00039     private static final long serialVersionUID = 1L;
00040 
00042     protected static final int INSTANCES_BETWEEN_MONITOR_UPDATES = 10;
00043 
00045     public FileOption outputFileOption = new FileOption("taskResultFile", 'O',
00046             "File to save the final result of the task to.", null, "moa", true);
00047 
00048     @Override
00049     protected Object doTaskImpl(TaskMonitor monitor, ObjectRepository repository) {
00050         Object result = doMainTask(monitor, repository);
00051         if (monitor.taskShouldAbort()) {
00052             return null;
00053         }
00054         File outputFile = this.outputFileOption.getFile();
00055         if (outputFile != null) {
00056             if (result instanceof Serializable) {
00057                 monitor.setCurrentActivity("Saving result of task "
00058                         + getTaskName() + " to file " + outputFile + "...",
00059                         -1.0);
00060                 try {
00061                     SerializeUtils.writeToFile(outputFile,
00062                             (Serializable) result);
00063                 } catch (IOException ioe) {
00064                     throw new RuntimeException("Failed writing result of task "
00065                             + getTaskName() + " to file " + outputFile, ioe);
00066                 }
00067             } else {
00068                 throw new RuntimeException("Result of task " + getTaskName()
00069                         + " is not serializable, so cannot be written to file "
00070                         + outputFile);
00071             }
00072         }
00073         return result;
00074     }
00075 
00088     protected abstract Object doMainTask(TaskMonitor monitor,
00089             ObjectRepository repository);
00090 }
 All Classes Namespaces Files Functions Variables Enumerations