$darkmode
DENOPTIM
ProgramTask.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no> and
4 * Marco Foscato <marco.foscato@uib.no>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20package denoptim.task;
21
22import java.io.File;
23import java.io.PrintWriter;
24import java.io.StringWriter;
25import java.util.logging.Handler;
26import java.util.logging.Level;
27import java.util.logging.Logger;
28
29import denoptim.exception.DENOPTIMException;
30import denoptim.files.FileFormat;
31import denoptim.files.FileUtils;
32import denoptim.io.DenoptimIO;
33import denoptim.logging.StaticLogger;
34import denoptim.utils.TaskUtils;
35
43public abstract class ProgramTask extends Task
44{
48 protected File configFilePathName;
49
53 protected String loggerIdentifier = "none";
54
55//------------------------------------------------------------------------------
56
62 public ProgramTask(File configFile, File workDir)
63 {
65 this.configFilePathName = configFile;
66 this.workDir = workDir;
67 loggerIdentifier = this.getClass().getSimpleName() + "-" + id;
68 }
69
70//------------------------------------------------------------------------------
71
77 @Override
78 public Object call()
79 {
80 StaticLogger.appLogger.log(Level.INFO, "Starting " + loggerIdentifier
81 + " (input="
82 + configFilePathName + ", workSpace=" + workDir + ")");
83 try
84 {
85 runProgram();
86 StaticLogger.appLogger.log(Level.INFO, "Completed "
88 } catch (Throwable t)
89 {
90 thrownExc = t;
92 }
93
95 {
97 }
98
99 return null;
100 }
101
102//------------------------------------------------------------------------------
103
109 protected void handleThrowable()
110 {
111 thrownExc.printStackTrace();
113 stopLogger();
114 }
115
116//------------------------------------------------------------------------------
117
122 protected void stopLogger()
123 {
124 // The logger is garbage-collected once we leave this thread, but we
125 // must stop the file handler gracefully.
126 Logger logger = Logger.getLogger(loggerIdentifier);
127 for (Handler h : logger.getHandlers())
128 h.close();
129 }
130
131//------------------------------------------------------------------------------
132
139 public void printErrorToFile()
140 {
141 StringWriter sw = new StringWriter();
142 PrintWriter pw = new PrintWriter(sw);
143 thrownExc.printStackTrace(pw);
144 String errFile = workDir + SEP + "ERROR";
145
146 try {
147 DenoptimIO.writeData(errFile, sw.toString(), false);
148 StaticLogger.appLogger.log(Level.SEVERE,
149 "ERROR occurred in " + loggerIdentifier + ". "
150 + "Details in "+errFile);
151 } catch (DENOPTIMException e) {
152 StaticLogger.appLogger.log(Level.SEVERE,
153 "ERROR occurred in " + loggerIdentifier + ". Details are "
154 + "reported here since we could not write to file '"
155 + errFile + "'. ");
156 thrownExc.printStackTrace();
157 }
159 }
160
161//------------------------------------------------------------------------------
162
163 protected abstract void runProgram() throws Throwable;
164
165//------------------------------------------------------------------------------
166
167}
static void addToRecentFiles(String fileName, FileFormat ff)
Appends an entry to the list of recent files.
Definition: FileUtils.java:67
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Logger class for DENOPTIM.
static final Logger appLogger
Task structure for any of the main programs in the denoptim project, such as genetic algorithm and co...
void printErrorToFile()
Method that can be called to create a text file with the error triggered by any Throwable that can be...
Object call()
This method redirects the callable functionality to an abstract method (namely ProgramTask#runProgram...
String loggerIdentifier
Identifier of this program's logger.
ProgramTask(File configFile, File workDir)
Creates and configures the program task.
void handleThrowable()
Method to handle any Throwable originated from the runProgram() method.
abstract void runProgram()
void stopLogger()
Stops the program-specific logger and releases the lock file on the logfile.
File configFilePathName
File containing configuration parameters for the program task.
Manager for tasks submitted by the GUI.
A task that can throw exceptions.
Definition: Task.java:30
boolean notifyGlobalTaskManager
Flag controlling whether this task is expected to notify the static task manager.
Definition: Task.java:35
File workDir
The file system location where we want to be placed when doing the work.
Definition: Task.java:78
int id
A user-assigned id for this task.
Definition: Task.java:68
final String SEP
System-dependent file separator.
Definition: Task.java:88
Throwable thrownExc
Exception thrown.
Definition: Task.java:61
Utilities for tasks.
Definition: TaskUtils.java:31
static synchronized int getUniqueTaskIndex()
Unique counter for tasks.
Definition: TaskUtils.java:41
File formats identified by DENOPTIM.
Definition: FileFormat.java:32