A simple implementation of the finite state machine.
The purpose of this repo is to understand how finite state machines work.
source: https://www.python-course.eu/finite_state_machine.php
How it works:
Setting up the machine
-
define a number of states -- (optionally) with a transition function.
The transition function consumes 1 element from the input. Returns -- a next state based on the element (this next state is passed to the machine again.) -- the rest of the input elements.
if a state has a transition function, it'll perform the function on the input element. if not, it is just a state (likely an end state)
-
add all the desired states to the machine.
-
add all the end states to the machine.
-
set the start state.
Running the machine
- define a array of input elements. (e.g. sequence of strings or a sequence of characters i.e. a string)
- input the input elements to the machine.
- the start state of the machine performs its start state transition function to the FIRST element of the input sequence.
- the transition function returns the next state and the rest of the input elements.
- the next element in the input sequence is passed to the transition function of the next state.
- this continues until an end state is reached. (e.g. positive state, negative state or error state)