E - Event enumpublic class SimpleFSM<E extends Enum<E>> extends Object
Classically, State S and Event E yield some action, then a transition to State S'.
In this impl, the Events are defined in terms of an enum, and the States are
created by name via the state(String) call; they are managed singletons.
For each state you define transition handling for any interesting event types via
the State.transition(). You do not have to define every transition; invalid transitions
will through SimpleFSM.TransitionException.
Instead of defining the outcome state when you define the transition, the transition callback is responsible for returning the next state, or null for no change.
begin(State) sets the intitial state and primes the SM for execution.
Then fire(Enum, Object...) is called as events are received. Beware, you cannot
call fire() from inside a callback; the lock is not reentrant.
| Modifier and Type | Class and Description |
|---|---|
static class |
SimpleFSM.State<EE extends Enum<EE>>
State class.
|
static interface |
SimpleFSM.Transition<EE extends Enum<EE>>
Callback interface for consumption of a event in a state, with payload.
|
static class |
SimpleFSM.TransitionException
Any exception which occurs in transition processing.
|
| Constructor and Description |
|---|
SimpleFSM(String name,
Class<E> eventClass)
Create simple finite state machine driver.
|
| Modifier and Type | Method and Description |
|---|---|
void |
begin(SimpleFSM.State<E> s)
Prime this FSM for processing; after this, no more transitions can be specified.
|
String |
dumpFSM()
Useful dump of FSM definitions.
|
SimpleFSM.State<E> |
fire(E event,
Object... args)
Fire an event.
|
void |
fireAsynchronously(E event,
Object... args)
This is useful in that you can queue an event for later delivery from inside the
state machine, as this will deliver the event/args from a seperate thread.
|
SimpleFSM.State<E> |
getCurrentState()
Get the current state.
|
Class<E> |
getEventClass()
Event class.
|
String |
getName()
Name of this FSM
|
long |
getTransitionAttemptCount()
Number of times a transition was attempted.
|
long |
getTransitionFailureCount()
Number of times a transition resulted in an exception
|
long |
getTransitionNoopCount()
Number of times a transition occurred without changing state.
|
boolean |
isFrozen()
Is this FSM frozen and accepting events?
|
SimpleFSM<E> |
onException(E failEvent,
SimpleFSM.Transition<E> onException)
Event/transition to call on transition exception.
|
void |
shutdown()
Shutdown this FSM.
|
SimpleFSM.State<E> |
state(String name)
Define a new
SimpleFSM.State by name, or retrieve the state if it exists. |
public String getName()
public void fireAsynchronously(E event, Object... args)
event - eventargs - payloadpublic SimpleFSM.State<E> fire(E event, Object... args) throws SimpleFSM.TransitionException
event - eventargs - payloadSimpleFSM.TransitionException - on any errorpublic SimpleFSM<E> onException(E failEvent, SimpleFSM.Transition<E> onException)
failEvent - event to deliver on transition exceptiononException - transition to callpublic void begin(SimpleFSM.State<E> s)
s - start statepublic SimpleFSM.State<E> getCurrentState()
public SimpleFSM.State<E> state(String name)
SimpleFSM.State by name, or retrieve the state if it exists.name - namepublic boolean isFrozen()
public String dumpFSM()
public void shutdown()
public long getTransitionNoopCount()
public long getTransitionAttemptCount()
public long getTransitionFailureCount()
Copyright © 2020. All rights reserved.