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 Task
This interface specifies a unit of work to be executed
asynchronously in its own
Thread
along with a user interface to
display its progress, provide a means for the user to
cancel the Task
, and show information
about any Exception
s thrown during its
execution.
Task
s are executed as part of a TaskIterator
which will be passed into a TaskManager
's execute
method.
Some hints for writing a Task
:
- Exceptions:
- When an exception is thrown, the
Task
should not catch it and set a status message or the progress, even to provide explanatory messages for the user. ATaskManager
may 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
Task
throws 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
Task
encounters an error that is not in the form of an exception, like an invalid variable or incorrectly formatted parameter, theTask
should 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."); }
TaskManager
to close theTask
's user interface when theTask
returns 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 theTask
has several constituent parts, it should set its status message at the beginning of a part, not at the end. For example, assume aTask
has 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."); }
Task
ends immediately after part B finishes. Therefore,Task
s 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) { } }
Task
is currently doing, not what it has done. If theTask
wishes to provide any information regarding what it has done, it must do so through alternate means. - The
Task
should not set the status message or progress immediately before theTask
finishes. This is because theTaskManager
may 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
Task
should set its progress by using thesetProgress
method ofTaskMonitor
to a value less than0
.
-
The
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.
Module: work-api
To use this in your app, include the following dependency in your POM:
<dependency> <groupId>org.cytoscape</groupId> <artifactId>work-api</artifactId> </dependency>
-
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
This method is called by theTaskManager
when the user chooses to cancel theTask
.void
run(TaskMonitor taskMonitor)
This method is called when theTask
begins execution.
-
Method Details
-
run
This method is called when theTask
begins execution. This method should not be called by the programmer, as it will be called by theTaskManager
.- Parameters:
taskMonitor
- TheTaskMonitor
provided byTaskManager
. to allow theTask
to modify its user interface.- Throws:
Exception
- TheTask
is at liberty to throw an exception inrun
. The exception is caught byTaskManager
and is displayed in the interface. If aTask
does not throw an exception, theTask
implementation does not need to specify thethrows Exception
clause for therun
method. Moreover, exceptions should be the way theTask
communicates 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 theTaskManager
when 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
Task
that 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!
-