(Still needs lots of work to change it from an overview to a walkthrough)

To use, simply derive a class from MiniMachine and for each desired state add a method named after that state with the following signature:

MiniState MyState(MiniEvent e)

The MiniEvent parameter's Input property is the input to the state, which will either be one of the predefined values (Enter, Exit, DefaultTo(MiniState) or GetParent) or a custom value defined your derived machine. The state returns null if it handled the input or returns its parent state if it didn't handle it (top-level states return Root(MiniEvent) as their parent).

States can call TransitionTo(MiniState) to transition to another state in response to a custom input. States that have substates must call DefaultTo(MiniState) to transition to a substate in response to a DefaultTo(MiniState) input.

State methods typically use the same skeleton code consisting of a switch on the Input property with a case for each input the state handles. These case blocks end with "return null" (instead of "break") to indicate the input was handled. Following the switch is a "return Parent" which is only reached if the input was not handled. States are not required to handle Enter, Exit or DefaultTo(MiniState) but they must handle GetParent. That said, the typical implementation does NOT explicitly handle GetParent since the "return Parent" does exactly what GetParent should do.

Derived machines can define any input values as long as they don't start with '!', which is reserved for use by built-in inputs.

The derived class must call DefaultTo(MiniState) first in order to set the default (initial) state. This may result in calls to various states. The derived class can then start passing events to Process(MiniEvent) which may again result in calls to various states. All of these methods are protected so only the derived class can call them directly. It's the derived class' business to decide whether or how the machine, its states or its inputs are publicly exposed.

It's worth noting that when an event is passed to Process(MiniEvent), a whole series of events may be generated and passed to various states. For example, if an event passed to Process(MiniEvent) causes a state transition then the following sequence might result: (1) Exit passed to the current state, (2) Enter passed to the new state, (3) DefaultTo(MiniState) passed to the new state. If substates are involved, there will be even more events passed to more states.

Last edited Feb 26, 2013 at 5:32 AM by MikeRiedel, version 1

Comments

No comments yet.