Package org.cytoscape.work
Interface Task
-
- All Known Subinterfaces:
CyNetworkReader,CyPropertyReader,CySessionReader,CyTableReader,CyWriter,ObservableTask,VizmapReader
- All Known Implementing Classes:
AbstractCyNetworkReader,AbstractEdgeViewTask,AbstractLayoutTask,AbstractNetworkCollectionTask,AbstractNetworkTask,AbstractNetworkViewCollectionTask,AbstractNetworkViewTask,AbstractNodeViewTask,AbstractParallelPartitionLayoutTask,AbstractPartitionLayoutTask,AbstractRootNetworkCollectionTask,AbstractRowTask,AbstractTableCellTask,AbstractTableColumnTask,AbstractTableTask,AbstractTask
public interface TaskThis interface specifies a unit of work to be executed asynchronously in its ownThreadalong with a user interface to display its progress, provide a means for the user to cancel theTask, and show information about anyExceptions thrown during its execution.Tasks are executed as part of aTaskIteratorwhich will be passed into aTaskManager'sexecutemethod.Some hints for writing a
Task:- Exceptions:
- When an exception is thrown, the
Taskshould not catch it and set a status message or the progress, even to provide explanatory messages for the user. ATaskManagermay disregard status message and progress updates once an exception is thrown. Any helpful user messages regarding the exception should be contained solely in the exception. - If a
Taskthrows a low level exception, it should catch it and throw an exception with a high level description. For example:
Any helpful messages for the user should be contained in an exception.try { ... } catch (IOException exception) // Low level exception { // Throw a high level exception that gives a high level explanation // that makes sense for a non-technical user. throw new Exception("Oops. Looks like you specified an invalid file.", exception) } - When a
Taskencounters an error that is not in the form of an exception, like an invalid variable or incorrectly formatted parameter, theTaskshould not set the status message giving an explanation of the error and then exit. Instead, it should throw an exception.The wrong way:
public void run(TaskMonitor taskMonitor) { if (myParameter == null) { taskMonitor.setStatusMessage("Whoa, looks like you didn't specified the parameter."); return; } }The right way:
This is done because it is possible for thepublic void run(TaskMonitor taskMonitor) throws Exception { if (myParameter == null) throw new Exception("Whoa, looks like you didn't specified the parameter."); }TaskManagerto close theTask's user interface when theTaskreturns before the user can read the message. Throwing an exception ensures the user will see the message.
- When an exception is thrown, the
- Status Messages:
-
The
Task, when specifying its status message, should describe what it will do, not what it has done. Specifically, if theTaskhas several constituent parts, it should set its status message at the beginning of a part, not at the end. For example, assume aTaskhas two parts, A and B:
Setting the status message after part A is unnecessary because the status message is immediately changed when part B starts. Setting the status message after part B is unnecessary because thepublic void run(TaskMonitor taskMonitor) { taskMonitor.setStatusMessage("Starting part A..."); ... // do part A taskMonitor.setStatusMessage("Part A is done."); taskMonitor.setStatusMessage("Starting part B..."); ... // do part B taskMonitor.setStatusMessage("Part B is done."); }Taskends immediately after part B finishes. Therefore,Tasks should set the status message at the beginning of a part. -
Information regarding the result of the
Task's should not be specified in the status message. For example:
This is because the purpose of the status message is to inform the user what thepublic void run(TaskMonitor taskMonitor) { int result = ... // some complicated computation taskMonitor.setStatusMessage("The result of the computation is " + result); // Give the user a chance to read the message: try { Thread.wait(1000); } catch (InterruptedException exception) { } }Taskis currently doing, not what it has done. If theTaskwishes to provide any information regarding what it has done, it must do so through alternate means. - The
Taskshould not set the status message or progress immediately before theTaskfinishes. This is because theTaskManagermay close theTask's user interface before the user has a chance to read it. For example:public void run(TaskMonitor taskMonitor) throws Exception { ... // Some complicated calculation // This is unnecessary: taskMonitor.setStatusMessage("We're all done."); taskMonitor.setProgress(1.0); } -
To specify an indefinite state in the progress bar, the
Taskshould set its progress by using thesetProgressmethod ofTaskMonitorto a value less than0.
-
The
Module:
work-apiTo use this in your app, include the following dependency in your POM:
<dependency> <groupId>org.cytoscape</groupId> <artifactId>work-api</artifactId> </dependency>
Cytoscape Backwards Compatibility (SPI Interface): We expect that this interface will be implemented. Therefore to maintain backwards compatibility this interface will only be modified for major version updates.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidcancel()This method is called by theTaskManagerwhen the user chooses to cancel theTask.voidrun(TaskMonitor taskMonitor)This method is called when theTaskbegins execution.
-
-
-
Method Detail
-
run
void run(TaskMonitor taskMonitor) throws Exception
This method is called when theTaskbegins execution. This method should not be called by the programmer, as it will be called by theTaskManager.- Parameters:
taskMonitor- TheTaskMonitorprovided byTaskManager. to allow theTaskto modify its user interface.- Throws:
Exception- TheTaskis at liberty to throw an exception inrun. The exception is caught byTaskManagerand is displayed in the interface. If aTaskdoes not throw an exception, theTaskimplementation does not need to specify thethrows Exceptionclause for therunmethod. Moreover, exceptions should be the way theTaskcommunicates the occurrence of a fatal error, like a low-level exception or an invalid parameter, to theTaskManager.
-
cancel
void cancel()
This method is called by theTaskManagerwhen the user chooses to cancel theTask.This method should not be called by the programmer, as it might be called by the
TaskManager.This method should inform the
Taskthat it must terminate execution cleanly and do any necessary cleanup work required.WARNING: this method is called by a different thread than the thread executing
run. The programmer must be aware of concurrency issues!
-
-