Finite State Machine in Python

20 February, 2016
Python Algorithms

A "Finite State Machine" (abbreviated FSM), also called "State Machine" or "Finite State Automaton" is an abstract machine which consists of a set of states (including the initial state and one or more end states), a set of input events, a set of output events, and a state transition function. A transition function takes the current state and an input event as an input and returns the new set of output events and the next (new) state. Some of the states are used as "terminal states".


The following is a python implementation of state machine which captures state transition between states of water. It's a work in progress.

class State:
    name = None

    def __init__(self, name):
        self.name = name


class Transition:
    trigger = None
    source = []
    destination = None

    def __init__(self, trigger, source, dest):
        self.source = source
        self.destination = dest
        self.trigger = trigger


class WaterStateMachine:
    states = ['solid', 'liquid', 'gas', 'plasma']
    state_transitions = []
    transition_table = [
        {'trigger': 'melt', 'source': 'solid', 'dest': 'liquid'},
        {'trigger': 'evaporate', 'source': 'liquid', 'dest': 'gas'},
        {'trigger': 'condense', 'source': 'gas', 'dest': 'liquid'},
        {'trigger': 'sublimate', 'source': 'solid', 'dest': 'gas'},
        {'trigger': 'ionize', 'source': 'gas', 'dest': 'plasma'}
    ]

    def __init__(self, state="liquid"):
        self.state = state
        for transition in self.transition_table:
            trigger = transition.get('trigger')
            source = transition.get('source')
            dest = transition.get('liquid')
            transition = Transition(trigger, source, dest)
            self.state_transitions.append(transition)

    def apply_trigger(self, trigger):
        for transition in self.transition_table:
            transition_trigger = transition.get('trigger')
            transition_source = transition.get('source')

            if trigger == transition_trigger and transition_source == self.state:
                self.set_state(transition.get('dest'))

    def set_state(self, state):
        self.state = state
        return self.state

    def get_state(self):
        return self.state


"""
Finite State Machines. What does it supports.
1. Can have multiple states any operations
2. Triggers
3. Callbacks
"""

water_state_machine = WaterStateMachine("gas")
print water_state_machine.get_state()
water_state_machine.apply_trigger('condense')
print water_state_machine.get_state()
water_state_machine.apply_trigger('evaporate')
print water_state_machine.get_state()
water_state_machine.apply_trigger('ionize')
print water_state_machine.get_state()
water_state_machine.apply_trigger('evaporate')
print water_state_machine.get_state()
water_state_machine.apply_trigger('evaporate')
print water_state_machine.get_state()
Thank you for taking the time to read this post. If you're considering using Digital Ocean, the hosting provider this blog is hosted on, please consider using this link to sign up.