public class FSMBuilder extends FSMBehavior
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) { // 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) { // 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
}
}
}
The DSL may also be used in statically compiled (@CompileStatic) Groovy code.
The delay passed to after() is a number of seconds.
This class is implemented in Java (rather than Groovy) intentionally: the Groovy compiler
adds synthetic propertyMissing/methodMissing bridge methods to nested
Groovy classes, and the static type checker then resolves any unqualified property in a
delegating closure against those bridges (as Object), breaking access to fields
of the enclosing class in statically compiled FSM definitions.
| Modifier and Type | Class and Description |
|---|---|
static class |
FSMBuilder.FSMState |
static class |
FSMBuilder.StateBuilder |
FSMBehavior.State| Constructor and Description |
|---|
FSMBuilder() |
| Modifier and Type | Method and Description |
|---|---|
static FSMBehavior |
build(groovy.lang.Closure<?> c) |
FSMBehavior.State |
state(java.lang.Object name,
groovy.lang.Closure<?> c) |
action, add, clear, done, getCurrentState, reenterState, reset, setInitialState, setNextState, terminate, trigger, triggeragent, agentForService, agentForService, agentsForService, agentsForService, block, block, compareTo, getPriority, isBlocked, onEnd, onStart, println, restartpublic static FSMBehavior build(@DelegatesTo(value=FSMBuilder.class) groovy.lang.Closure<?> c)
public FSMBehavior.State state(java.lang.Object name, @DelegatesTo(value=FSMBuilder.StateBuilder.class,strategy=1) groovy.lang.Closure<?> c)