public class AbortHandler extends Object
Instances of this class may be attached to and retrieved from a graph and may receive requests for stopping and canceling an algorithm that is currently executed on the given graph.
An instance of this class can be attached to a graph using method createForGraph(Graph)
. Using the handler's stop()
and cancel()
methods, it is possible to schedule requests for early termination. Algorithms can check for these
requests and handle them appropriately.
stop()
– The algorithm should terminate gracefully, delivering a consistent result. Although the termination will be early, it
will usually not be immediate.
cancel()
– The algorithm should terminate immediately and all work done so far will be discarded.
If a graph with an attached handler is processed by multiple algorithms (or multiple times by the same algorithm), the
attached handler has to be reset()
between algorithm runs. Otherwise, previous requests for early termination
may lead to an undesired early termination of the next algorithm run.
Methods stop()
and cancel()
are primarily meant for multi-threaded scenarios where the algorithm runs
in a background thread and stop()
/cancel()
are called from the main or UI thread. For pure
single-threaded use cases, StopDuration
and CancelDuration
may be used for automatically stopping or cancelling the algorithm after a specified period of time has elapsed.
Algorithms have to retrieve an instance of this class from the graph that is processed using method
getFromGraph(Graph)
. Then, the algorithm needs to query the retrieved instance of this class for stop or
cancel requests using method check()
.
Alternatively, convenience method check(Graph)
for one-time checks is available. For performance critical code
that checks repeatedly, it is recommended to follow the first approach, though.
When handling a stop request, algorithms should ensure that the resulting graph is still in a consistent state.
Modifier and Type | Field and Description |
---|---|
static GraphDpKey<AbortHandler> |
ABORT_HANDLER_DPKEY
A
DataProvider key for attaching an AbortHandler instance to a graph
Only instances of AbortHandler should be assigned to this IDataProvider , otherwise a ClassCastException
will occur. |
Constructor and Description |
---|
AbortHandler()
Creates a new
AbortHandler instance. |
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Schedules a cancel request.
|
boolean |
check()
Determines whether or not an algorithm should terminate immediately.
|
static boolean |
check(Graph graph)
Determines whether or not an algorithm should terminate immediately.
|
static void |
copyHandler(Graph source,
Graph target)
Attaches the
AbortHandler instance of the given source graph to the target graph as well. |
static AbortHandler |
createForGraph(Graph graph)
Creates an
handler instance and attaches it to the given graph. |
Duration |
getCancelDuration()
Gets the duration an algorithm may run before being cancelled automatically.
|
static AbortHandler |
getFromGraph(Graph graph)
Returns an
AbortHandler instance for the given graph. |
Duration |
getStopDuration()
Gets the duration an algorithm may run before being stopped automatically.
|
static boolean |
hasHandler(Graph graph)
Determines whether or not an
AbortHandler instance is attached to the given graph. |
boolean |
isCancelRequested()
Returns whether or not a cancel request was scheduled explicitly with the
cancel() method. |
boolean |
isCheckFailed()
Gets whether or not methods
check() or check(Graph) were called after a stop or cancel event. |
boolean |
isStopRequested()
Returns whether or not a stop request was scheduled explicitly with the
stop() method. |
static void |
removeFromGraph(Graph graph)
Removes any attached
AbortHandler instance from the given graph. |
void |
reset()
Resets the state of the
handler . |
void |
setCancelDuration(Duration value)
Sets the duration an algorithm may run before being cancelled automatically.
|
void |
setStopDuration(Duration value)
Sets the duration an algorithm may run before being stopped automatically.
|
void |
stop()
Schedules a stop request.
|
Duration |
timeToCancel()
Determines the remaining time (in milliseconds) until an algorithm that
checks this handler is
cancelled automatically. |
Duration |
timeToStop()
Determines the remaining time until an algorithm that
checks this handler is stopped automatically. |
public static final GraphDpKey<AbortHandler> ABORT_HANDLER_DPKEY
DataProvider
key for attaching an AbortHandler instance to a graph
Only instances of AbortHandler
should be assigned to this IDataProvider
, otherwise a ClassCastException
will occur. Layout algorithms will use the attached handler to check for requests to cancel or stop the layout process.
public AbortHandler()
AbortHandler
instance.public void cancel()
check()
after a cancel request has been scheduled will trigger an
AlgorithmAbortedException
. This usually results in an immediate termination of the algorithm. Thus, a
consistent state of the processed graph cannot be guaranteed. For this reason, it is strongly recommended to cancel only
algorithms that work on copies of the real graph structure such as layout algorithms running in buffered mode.check()
,
stop()
public boolean check()
This method returns true
if the algorithm should terminate gracefully and ensures that the processed graph
remains in a consistent state.
AlgorithmAbortedException
- if the algorithm should terminate immediatelytrue
, if the algorithm should terminate immediately, false
otherwisecancel()
,
stop()
public static final boolean check(Graph graph)
This method returns true
if the algorithm should terminate gracefully and ensures that the processed graph
remains in a consistent state.
This convenience method is meant for one-time checks only. For performance critical code that needs to check repeatedly,
it is recommended to retrieve the given graph's attached handler once and only call the handler's check()
method repeatedly.
AlgorithmAbortedException
- if the algorithm should terminate immediatelyNullPointerException
- if the given graph is null
true
, if the algorithm should stop immediately while still providing some valid result, false
otherwisecheck()
,
cancel()
,
stop()
public static final void copyHandler(Graph source, Graph target)
AbortHandler
instance of the given source graph to the target graph as well.NullPointerException
- if the given source is null
AbortHandler
attached to the given target graph, this method will silently replace that instance
with the one attached to the source graph.source
- the graph whose handler is attached to the target graphtarget
- the graph to which the handler of the source graph is attachedpublic static final AbortHandler createForGraph(Graph graph)
handler
instance and attaches it to the given graph.
If the given graph has already an attached handler instance, this is the instance that will be returned and this method
will not create a new AbortHandler
.
This method should be called by client code prior to starting a graph algorithm that may be terminated early.
NullPointerException
- if the given graph is null
.graph
- the graph to which the handler will be attachedAbortHandler
instance for the given graphhasHandler(Graph)
public Duration getCancelDuration()
An algorithm is terminated immediately, if the time in between creating
or resetting
this handler and calling check()
exceeds the cancel duration.
IllegalArgumentException
- if the duration is negativeDuration.ZERO
. No automatic termination will occur.reset()
,
cancel()
,
setCancelDuration(Duration)
public static final AbortHandler getFromGraph(Graph graph)
AbortHandler
instance for the given graph.
If createForGraph(Graph)
has been used for attaching a new handler to the given graph, this is the instance
that will be returned. Otherwise, a non-functional instance is returned whose methods do nothing. Use hasHandler(Graph)
to check whether or not a handler has been already attached to the given graph.
NullPointerException
- if the given graph is null
graph
- the given graphAbortHandler
for the given graph or a non-functional instance if no handler has been previously createdcreateForGraph(Graph)
,
hasHandler(Graph)
public Duration getStopDuration()
An algorithm is terminated gracefully, if the time in between creating
or resetting
this handler and calling check()
exceeds the stop duration.
IllegalArgumentException
- if the duration is negativeDuration.ZERO
. No automatic termination will occur.timeToStop()
,
reset()
,
stop()
,
setStopDuration(Duration)
public static final boolean hasHandler(Graph graph)
AbortHandler
instance is attached to the given graph.NullPointerException
- if the given graph is null
graph
- the given graphtrue
if a handler is attached to the given graph, false
otherwisepublic boolean isCancelRequested()
cancel()
method.public boolean isCheckFailed()
check()
or check(Graph)
were called after a stop or cancel event.
More precisely, it returns true
if one of the check methods either threw an AlgorithmAbortedException
or
returned true
to indicate that the calling algorithm should terminate gracefully. Otherwise, this method returns
false
.
true
if the check methods were called after a stop or cancel event, false
otherwisecheck()
,
check(Graph)
public boolean isStopRequested()
stop()
method.public static final void removeFromGraph(Graph graph)
AbortHandler
instance from the given graph.NullPointerException
- if the given graph is null
.graph
- the given graphpublic void reset()
handler
.
Resetting the handler
discards any previous stop or cancel requests. Moreover, the handler's
internal timestamp that is used for determining whether or not an algorithm should be stopped or cancelled automatically
is reset as well.
This method should be called whenever a graph with an attached handler is processed an additional time to prevent previous requests for early termination to result in an undesired early termination of the next algorithm run.
public void setCancelDuration(Duration value)
An algorithm is terminated immediately, if the time in between creating
or resetting
this handler and calling check()
exceeds the cancel duration.
IllegalArgumentException
- if the duration is negativeDuration.ZERO
. No automatic termination will occur.value
- the duration (in milliseconds)reset()
,
cancel()
,
getCancelDuration()
public void setStopDuration(Duration value)
An algorithm is terminated gracefully, if the time in between creating
or resetting
this handler and calling check()
exceeds the stop duration.
IllegalArgumentException
- if the duration is negativeDuration.ZERO
. No automatic termination will occur.value
- the durationtimeToStop()
,
reset()
,
stop()
,
getStopDuration()
public void stop()
Algorithms that detect stop requests should terminate gracefully and ensure that the processed graph remains in a consistent state.
If a cancel()
request has already been scheduled for this handler, the stop request is ignored. If a cancel()
request is scheduled later on, the stop request is overridden.
public Duration timeToCancel()
checks
this handler is
cancelled automatically.setCancelDuration(Duration)
,
reset()
,
cancel()
public Duration timeToStop()
checks
this handler is stopped automatically.
If the stop duration
is less than or equal to zero, -1
will be returned which
means that the algorithm may run unrestricted.
-1
if the algorithm may run unrestrictedsetStopDuration(Duration)
,
reset()
,
stop()