|
Search this API | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object y.algo.AbortHandler
public class AbortHandler
This class provides a means for early termination of graph algorithms.
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(y.base.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()
cancel()
IMPORTANT: It is not guaranteed that the processed graph will be in a consistent state after cancellation. 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. Furthermore, the state of the used
Layouter
instance may become corrupted. Hence, a new instance has to be created after each
cancellation.
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, setStopDuration(long)
and
setCancelDuration(long)
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(y.base.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(y.base.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.
Field Summary | |
---|---|
static java.lang.Object |
ABORT_HANDLER_DPKEY
A DataProvider key for attaching an AbortHandler instance to a graph
Only instances of AbortHandler should be assigned to this DataProvider , otherwise a
ClassCastException will occur. |
Constructor Summary | |
---|---|
AbortHandler()
Creates a new AbortHandler instance. |
Method Summary | |
---|---|
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. |
long |
getCancelDuration()
Returns the duration (in milliseconds) an algorithm may run before being cancelled automatically. |
static AbortHandler |
getFromGraph(Graph graph)
Returns an AbortHandler instance for the given graph. |
long |
getStopDuration()
Returns the duration (in milliseconds) an algorithm may run before being stopped automatically. |
boolean |
hasCheckFailed()
Returns whether or not methods check() or check(y.base.Graph) were called after a stop or cancel
event. |
static boolean |
hasHandler(Graph graph)
Determines whether or not an AbortHandler instance is attached to the given graph. |
long |
millisToCancel()
Determines the remaining time (in milliseconds) until an algorithm that checks this handler is
cancelled automatically. |
long |
millisToStop()
Determines the remaining time (in milliseconds) until an algorithm that checks this handler is
stopped automatically. |
static void |
removeFromGraph(Graph graph)
Removes any attached AbortHandler instance from the given graph. |
void |
reset()
Resets the state of the handler . |
void |
setCancelDuration(long ms)
Specifies the duration (in milliseconds) an algorithm may run before being cancelled automatically. |
void |
setStopDuration(long ms)
Specifies the duration (in milliseconds) an algorithm may run before being stopped automatically. |
void |
stop()
Schedules a stop request. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.Object ABORT_HANDLER_DPKEY
DataProvider
key for attaching an AbortHandler instance to a graph
Only instances of AbortHandler
should be assigned to this DataProvider
, otherwise a
ClassCastException
will occur. Layout algorithms will use the attached handler to check for requests to
cancel or stop the layout process.
Constructor Detail |
---|
public AbortHandler()
AbortHandler
instance.
The current time
will be used as timestamp for determining whether or not an
algorithm should be stopped or cancelled automatically.
setStopDuration(long)
,
setCancelDuration(long)
,
reset()
Method Detail |
---|
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.
check()
,
cancel()
public long millisToStop()
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(long)
,
reset()
,
stop()
public long getStopDuration()
An algorithm is terminated gracefully, if the time in between creating
or
resetting
this handler and calling check()
exceeds the stop duration.
setStopDuration(long)
,
millisToStop()
,
reset()
,
stop()
public void setStopDuration(long ms)
An algorithm is terminated gracefully, if the time in between creating
or
resetting
this handler and calling check()
exceeds the stop duration.
ms
- the duration (in milliseconds)
java.lang.IllegalArgumentException
- if the duration is negativesetStopDuration(long)
,
millisToStop()
,
reset()
,
stop()
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 long millisToCancel()
checks
this handler is
cancelled automatically.
setCancelDuration(long)
,
reset()
,
cancel()
public long getCancelDuration()
An algorithm is terminated immediately, if the time in between creating
or
resetting
this handler and calling check()
exceeds the cancel duration.
setCancelDuration(long)
,
getCancelDuration()
,
millisToCancel()
,
reset()
,
cancel()
public void setCancelDuration(long ms)
An algorithm is terminated immediately, if the time in between creating
or
resetting
this handler and calling check()
exceeds the cancel duration.
ms
- the duration (in milliseconds)
java.lang.IllegalArgumentException
- if the duration is negativegetCancelDuration()
,
millisToCancel()
,
reset()
,
cancel()
public boolean hasCheckFailed()
check()
or check(y.base.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(y.base.Graph)
public boolean check()
This method returns true
if the algorithm should terminate gracefully and ensures that the
processed graph remains in a consistent state.
true
, if the algorithm should terminate immediately, false
otherwise
AlgorithmAbortedException
- if the algorithm should terminate immediatelycancel()
,
stop()
public 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 updated to the current time
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.
setCancelDuration(long)
,
setStopDuration(long)
public static 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.
graph
- the graph to which the handler will be attached
AbortHandler
instance for the given graph
java.lang.NullPointerException
- if the given graph is null
.hasHandler(y.base.Graph)
public static void removeFromGraph(Graph graph)
AbortHandler
instance from the given graph.
graph
- the given graph
java.lang.NullPointerException
- if the given graph is null
.public static AbortHandler getFromGraph(Graph graph)
AbortHandler
instance for the given graph.
If createForGraph(y.base.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(y.base.Graph)
to check whether or not a handler has been already attached to the given graph.
graph
- the given graph
AbortHandler
for the given graph or a non-functional instance if no handler has been
previously created
java.lang.NullPointerException
- if the given graph is null
createForGraph(y.base.Graph)
,
hasHandler(y.base.Graph)
public static void copyHandler(Graph source, Graph target)
AbortHandler
instance of the given source graph to the target
graph as well.
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 attached
java.lang.NullPointerException
- if the given source is null
public static boolean hasHandler(Graph graph)
AbortHandler
instance is attached to the given graph.
graph
- the given graph
true
if a handler is attached to the given graph, false
otherwise
java.lang.NullPointerException
- if the given graph is null
public static 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.
true
, if the algorithm should stop immediately while still providing some valid result,
false
otherwise
AlgorithmAbortedException
- if the algorithm should terminate immediately
java.lang.NullPointerException
- if the given graph is null
check()
,
cancel()
,
stop()
|
© Copyright 2000-2022, yWorks GmbH. All rights reserved. |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |