MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * DoTask.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; 00021 00022 import moa.core.Globals; 00023 import moa.core.Measurement; 00024 import moa.core.StringUtils; 00025 import moa.core.TimingUtils; 00026 import moa.options.ClassOption; 00027 import moa.options.FlagOption; 00028 import moa.options.IntOption; 00029 import moa.options.Option; 00030 import moa.tasks.FailedTaskReport; 00031 import moa.tasks.Task; 00032 import moa.tasks.TaskThread; 00033 import weka.core.Version; 00034 00041 public class DoTask { 00042 00044 public static final char[] progressAnimSequence = new char[]{'-', '\\', 00045 '|', '/'}; 00046 00048 public static final int MAX_STATUS_STRING_LENGTH = 79; 00049 00055 public static boolean isJavaVersionOK() { 00056 boolean isJavaVersionOK = true; 00057 String version = System.getProperty("java.version"); 00058 char minor = version.charAt(2); 00059 char point = version.charAt(4); 00060 if (minor < '6' || point < '0') { 00061 isJavaVersionOK = false; 00062 System.err.println(); 00063 System.err.println(Globals.getWorkbenchInfoString()); 00064 System.err.println(); 00065 System.err.print("JDK 1.6.0 or higher is required to run MOA. "); 00066 System.err.println("JDK version " + version + " found"); 00067 } 00068 return isJavaVersionOK; 00069 } 00070 00079 public static boolean isWekaVersionOK() { 00080 Version version = new Version(); 00081 if (version.isOlder("3.7.1")) { 00082 System.err.println(); 00083 System.err.println(Globals.getWorkbenchInfoString()); 00084 System.err.println(); 00085 System.err.print("Weka 3.7.1 or higher is required to run MOA. "); 00086 System.err.println("Weka version " + Version.VERSION + " found"); 00087 return false; 00088 } else { 00089 return true; 00090 } 00091 } 00092 00098 public static void main(String[] args) { 00099 try { 00100 if (args.length < 1) { 00101 System.err.println(); 00102 System.err.println(Globals.getWorkbenchInfoString()); 00103 System.err.println(); 00104 System.err.println("No task specified."); 00105 } else { 00106 if (isJavaVersionOK() == false || isWekaVersionOK() == false) { 00107 return; 00108 } 00109 // create standard options 00110 FlagOption suppressStatusOutputOption = new FlagOption( 00111 "suppressStatusOutput", 'S', 00112 "Suppress the task status output that is normally send to stderr."); 00113 FlagOption suppressResultOutputOption = new FlagOption( 00114 "suppressResultOutput", 'R', 00115 "Suppress the task result output that is normally send to stdout."); 00116 IntOption statusUpdateFrequencyOption = new IntOption( 00117 "statusUpdateFrequency", 00118 'F', 00119 "How many milliseconds to wait between status updates.", 00120 1000, 0, Integer.MAX_VALUE); 00121 Option[] extraOptions = new Option[]{ 00122 suppressStatusOutputOption, suppressResultOutputOption, 00123 statusUpdateFrequencyOption}; 00124 // build a single string by concatenating cli options 00125 StringBuilder cliString = new StringBuilder(); 00126 for (int i = 0; i < args.length; i++) { 00127 cliString.append(" ").append(args[i]); 00128 } 00129 // parse options 00130 Task task = (Task) ClassOption.cliStringToObject(cliString.toString(), Task.class, extraOptions); 00131 Object result = null; 00132 if (suppressStatusOutputOption.isSet()) { 00133 result = task.doTask(); 00134 } else { 00135 System.err.println(); 00136 System.err.println(Globals.getWorkbenchInfoString()); 00137 System.err.println(); 00138 boolean preciseTiming = TimingUtils.enablePreciseTiming(); 00139 // start the task thread 00140 TaskThread taskThread = new TaskThread(task); 00141 taskThread.start(); 00142 int progressAnimIndex = 0; 00143 // inform user of progress 00144 while (!taskThread.isComplete()) { 00145 StringBuilder progressLine = new StringBuilder(); 00146 progressLine.append(progressAnimSequence[progressAnimIndex]); 00147 progressLine.append(' '); 00148 progressLine.append(StringUtils.secondsToDHMSString(taskThread.getCPUSecondsElapsed())); 00149 progressLine.append(" ["); 00150 progressLine.append(taskThread.getCurrentStatusString()); 00151 progressLine.append("] "); 00152 double fracComplete = taskThread.getCurrentActivityFracComplete(); 00153 if (fracComplete >= 0.0) { 00154 progressLine.append(StringUtils.doubleToString( 00155 fracComplete * 100.0, 2, 2)); 00156 progressLine.append("% "); 00157 } 00158 progressLine.append(taskThread.getCurrentActivityString()); 00159 while (progressLine.length() < MAX_STATUS_STRING_LENGTH) { 00160 progressLine.append(" "); 00161 } 00162 if (progressLine.length() > MAX_STATUS_STRING_LENGTH) { 00163 progressLine.setLength(MAX_STATUS_STRING_LENGTH); 00164 progressLine.setCharAt( 00165 MAX_STATUS_STRING_LENGTH - 1, '~'); 00166 } 00167 System.err.print(progressLine.toString()); 00168 System.err.print('\r'); 00169 if (++progressAnimIndex >= progressAnimSequence.length) { 00170 progressAnimIndex = 0; 00171 } 00172 try { 00173 Thread.sleep(statusUpdateFrequencyOption.getValue()); 00174 } catch (InterruptedException ignored) { 00175 // wake up 00176 } 00177 } 00178 StringBuilder cleanupString = new StringBuilder(); 00179 for (int i = 0; i < MAX_STATUS_STRING_LENGTH; i++) { 00180 cleanupString.append(' '); 00181 } 00182 System.err.println(cleanupString); 00183 result = taskThread.getFinalResult(); 00184 if (!(result instanceof FailedTaskReport)) { 00185 System.err.print("Task completed in " 00186 + StringUtils.secondsToDHMSString(taskThread.getCPUSecondsElapsed())); 00187 if (preciseTiming) { 00188 System.err.print(" (CPU time)"); 00189 } 00190 System.err.println(); 00191 System.err.println(); 00192 } 00193 } 00194 if (result instanceof FailedTaskReport) { 00195 System.err.println("Task failed. Reason: "); 00196 ((FailedTaskReport) result).getFailureReason().printStackTrace(); 00197 } else { 00198 if (!suppressResultOutputOption.isSet()) { 00199 if (result instanceof Measurement[]) { 00200 StringBuilder sb = new StringBuilder(); 00201 Measurement.getMeasurementsDescription( 00202 (Measurement[]) result, sb, 0); 00203 System.out.println(sb.toString()); 00204 } else { 00205 System.out.println(result); 00206 } 00207 System.out.flush(); 00208 } 00209 } 00210 } 00211 } catch (Exception ex) { 00212 ex.printStackTrace(); 00213 } 00214 } 00215 }