Finite state machine (FSM) builder. FSMs are commonly needed for protocol implementation. While fjåge provides a FSMBehavior class to help with making FSMs, this class adds some Groovy functionality to make it simpler to describe FSMs in Groovy.
The usage of this class is best described through an example FSM:
enum State { TICK, TOCK } enum Event { RESET } FSMBehavior fsm = FSMBuilder.build { long count = 0 state(State.TICK) { onEnter { println 'TICK' } after(1.seconds) { // every time we enter the TICK state, setNextState(State.TOCK) // switch to TOCK after 1 second } action { println "CLOCK says $count seconds" block() } onEvent(Event.RESET) { // reset count if RESET trigger received count = 0 } } state(State.TOCK) { onEnter { println 'TOCK' after(1.second) { // in this form, the delay is recomputed every setNextState(State.TICK) // time the TOCK state is entered } } onEvent(Event.RESET) { // reset count if RESET trigger received count = 0 // and also go back to TICK state setNextState(State.TICK) } onExit { // increment count every time we leave count++ // the TICK state } } }
Type Params | Return Type | Name and description |
---|---|---|
|
static FSMBehavior |
build(Closure c) |
Methods inherited from class | Name |
---|---|
class FSMBehavior |
add, trigger, trigger, clear, reset, done, action, terminate, getCurrentState, reenterState, setNextState, setInitialState, println, onStart, block, block, restart, onEnd, agent, isBlocked, agentForService, agentForService, agentsForService, agentsForService, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
class Behavior |
println, reset, onStart, done, block, block, action, restart, onEnd, agent, isBlocked, agentForService, agentForService, agentsForService, agentsForService, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |