Package: pystate
FiniteStateMachine - A finite state machine class using coroutinesFiniteStateMachine is a class representing a finite state machine. Each state is represented by an instance of theState class. Each state also has a state handler function defined for it. The handler function is a co-routine thatexcepts an event and performs an action based on it.To define the state machine:1) define the states (e.g. STATE_A = State('STATE_A'))2) define the state handler functions. See below for the structure of a state handling function.3) create an instance of the state machine (e.g. fsm = Fsm())4) add states to the state machine, including exactly one state marked as the initial state. Each state also takesa sequence of states that it can be transitioned from (from_states).5) call the start function on the FSM (e.g. fsm.start())For each event you will need to call the dispatch_event function (e.g. fsm.dispatch_event()) to route the eventto the co-routine. An event can be anything you want (e.g. a tuple with event_id and arguments). The main loopsgenerally looks like:try:while True:event = get_next_event()fsm.dispatch_event(event)except ExpectedExit as e:passThe basic structure of a state handler is:def state_handler_<state name>(fsm):# Enter the main loop for the co-routinewhile True:event = yieldif event == 'EVENT_1':# Transition to another statefsm.transition_to(STATE_X)elif event == 'EVENT_2':# Do some processing but stay in this stateprint('Got EVENT_2')elif event == 'TERMINATING_EVENT':raise FsmExitelse:print('Unrecognized event (%s)' % event)A simple example of this is shown in the turnstile_test.py test case.For convenience this can be wrapped with a @state_handler decorator. The decorator takes care of the co-routineboiler plate and hands the handler function an fsm and event. This would look like:@pystate.state_handlerdef state_locked_handler(event, fsm):if event == 'EVENT_1':# Transition to another statefsm.transition_to(STATE_X)elif event == 'EVENT_2':# Do some processing but stay in this stateprint('Got EVENT_2')elif event == 'TERMINATING_EVENT':raise FsmExitelse:print('Unrecognized event (%s)' % event)There are two ways to handle a state that needs to keep persistant data. You can create a callable clas (i.e. definethe __call__ dunder method to call as the state handler.) This allows you to use the state_handler decorator aroundthe __call__ method. Alternatively, you can set the state data above the while loop if you define the co-routine byhand, however, this precludes using the decorator. See the callable_test.py test case for an example.Author: Len WangerLast Updated: 7/7/2016Copyright (c) 2016 Len WangerPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.Note: Substantial code was adapted from Christian Maugg's pystatemachine codeCopyright (c) 2015 Christian Maugg(https://raw.githubusercontent.com/cmaugg/pystatemachine/master/pystatemachine.py)