Package: favink
FAVinkFAVinkGetting StartedTransition Table and Initial StateEventsEvent Handler DefinitionsCar ExampleAPI ReferencePredefinedFiniteAutomataMethodsFiniteAutomata.get_state(self)FiniteAutomata.get_allowed_transitions(self)FiniteAutomata.is_allowed(self, transition)Transition MethodsOver-simple python finite automata (finite-state machine) implementationGetting StartedTo install favink use the package from the PyPI repository:pipinstallfavinkTo add finite automata feature to the class in your code you have to inherit
FiniteAutomata class and define the following members:transitions tabletransitionsinitial stateinit_stateevent handlers methods.For every transition the constructor creates a dynamic method with argument mask(self, *args, **kwargs). Each method is named after the transition.To make the transition you should call the transition method. After the method
has been called it changes the instance state and invoke related event handlers.
If the called transition isn't allowed for the current stateInvalidTransitionextension will be raised.Transition Table and Initial StateThetransitionsis a dictionary where keys are transition names,
values define the allowed and target states:transitions={"transition_1":["allowed_state_1","target_state_1"],"transition_2":[["allowed_state_2","allowed_state_3"],"target_state_2"]}Initial state is defined byinit_statemember.EventsMaking of transition triggers the following events and invokes the related handlers
(if they have been implemented in the class):afterbeforeonEvent Handler DefinitionsFor every state (for examplestate_name) the following methods can be defined:defbefore_state_name(self,name,*args,**kwargs):...defon_state_name(self,name,origin,*args,**kwargs):...defafter_state_name(self,name,*args,**kwargs):...The following arguments are passed to the handlers:nameis a invoked transition name,origin(passed only toon_...handler) is a previous state name,*args, **kwargsare positional and keyword arguments passed to the transition method.Ifafter_...orbefore...handlers raise the exception transition is aborted.Car ExampleclassCar(FiniteAutomata):init_state="stopped"transitions={"start_engine":["stopped","idle"],"stop_engine":["idle","stopped"],"forward":["idle","moving_forward"],"backward":["idle","moving_backward"],"stop":[["moving_forward","moving_backward"],"idle"],}defon_stopped(self,transition,origin,*args,**kwargs):print("Engine has been stopped")defon_idle(self,transition,origin,*args,**kwargs):print("I'm not moving, but engine is on")defon_moving_forward(self,transition,origin,*args,**kwargs):print("Let's go!")defon_moving_backward(self,transition,origin,*args,**kwargs):print("Why are we retreating?")car=Car()car.start_engine()car.forward()car.stop()car.backward()car.stop()car.stop_engine()Output:I'm not moving, but engine is on
Let's go!
I'm not moving, but engine is on
Why are we retreating?
I'm not moving, but engine is on
Engine has been stoppedAPI ReferencePredefinedFiniteAutomataMethodsFiniteAutomata.get_state(self)Returns the current instance state name as a string.FiniteAutomata.get_allowed_transitions(self)Return the list contains all transactions which are allowed for the current instance state.FiniteAutomata.is_allowed(self, transition)Transition MethodsDynamically defined methods for every transition (key) in thetransitionsdictionary.