This is a tool that can be used to create code from a description of a state machine. The state machine is described using a simple JSON structure.
{"states":
[
{"name":"idle", "on":"InIdle",
"transitions":
[
{"event":"start","to":"running","action":"Foo"}
]},
{"name":"running", "on":"InRunning",
"transitions":
[
{"event":"stop","to":"idle","action":"Foo"}
]}
],
"init":"idle",
"name":"StateMachine",
"iface":""}
An array of states defining the states of the state machine.
Name of the state.
Name of the function to be executed whenever a transition into this state occurse.
The type of the function must be func(e Event, s State) error
.
An array of transitions describing how and when to switch states.
The name of the event.
Name of the target state. If the specified event occurs the state machine will transition into the target state. This may be left empty if no transition shall occur.
Name of the function to be executed whenever this transition occurs. The function is called
before the state is updated. The type of the function must be func(e Event, s State) error
. This
may be left empty if no function should be executed.
Name of a function to be executed to check whether the transition should happen or not. The type
of the function must be func(e Event, s State) (bool, error)
. If this function returns false
then action
doesn't happen and no state transition happens.
The initial state.
The name of the state machine. This will also be used as the name of the type of the state machine in the generated code.
When iface
is not empty the state machine will have an internal state of this type. All callbacks
will be invoked on the internal state.
To use the state machine in the generated code use the New<name you provided>
method. Then you can trigger transitions
be invoking the Event
method on it:
sm := NewStateMachine()
err := sm.Event(EventStart)
if err != nil {
panic(err.Error())
}
The Event
function has the type func(e Event) error
. It may return an error if there's no transition registered
for this event in the current state. If there's an error in a callback (such as action
, condition
or on
) then Event
will
abort and return that error.
The tool will also generate constants Event*
and State*
for each event and state. To access the current state of the
state machine use sm.State()
which has type func() State
. It also generates a SetState(state State, event Event, invokeOn bool) error
method and a SetIface(iface Iface)
method (if iface
is used). The special constant NoEvent
may be used for invalid events.
For a working example see the example/
, example2/
and example3/
directories.