Code Monkey home page Code Monkey logo

declarative-fsm's Introduction

Introduction

The purpose of this project is to use the expressive power of the Python language to declaratively implement finite state machines.

Concepts

A few concepts are important to understand in order to properly use this project. As an aid, an image has been provided below to help convey the ideas covered in this section.

Finite State Machine

Actions
An action is executed upon entering or exiting a state. Actions are the mechanism by which a finite state machine (FSM) gets work done.

Guards
A guard constrains transitions by allowing transitions to occur only under specific circumstances. A good example of how guards are useful can be found in the security domain. In this domain a guard can be implemented that only allows a transition to occur if the user has previously authenticated with the system.

States
A state is considered to be the value of all the variables in an FSM at some point in time. All the states of interest as well as the initial state must be declared while initializing the FSM in order for it to build it's internal transition table.

Transitions
A transition is the action of moving an FSM from an initial state to a desired state. All the possible transitions must be declared while initializing the FSM in order to constrain how states are traversed.

Code Example

class LightSwitch(FiniteStateMachine):
  # Initial state.
  initial_state = 'off'

  # Possible transitions.
  transitions = [
    ('off', 'on'),
    ('on', 'off'),
    ('off', 'broken'),
    ('on', 'broken')
  ]

  # Initialize the FSM.
  def __init__(self, *args, **kwargs):
    super(LightSwitch, self).__init__(*args, **kwargs)
    self.electricity = True

  # Handle incoming events.
  def on_message(self, message):
    if message == 'turn on':
      self.transition(to = 'on', event = message)
    elif message == 'turn off':
      self.transition(to = 'off', event = message)
    elif message == 'break':
      self.transition(to = 'broken', event = message)

  # Attach a guard to the FSM that protects transitions into
  # the on state. The guard is a predicate that returns True
  # or False based on the availability of electricity to the
  # socket.
  @Guard(state = 'on')
  def check_electricity(self):
    return self.electricity

  # Attach an action to the FSM that will execute anytime
  # the off state is entered.
  @Action(state = 'off')
  def turn_off(self, message):
    self.indicator = 'dim'

  # Attach an action to the FSM that will execute anytime
  # the on state is entered.
  @Action(state = 'on')
  def turn_on(self, message):
    self.indicator = 'lit'

  # Attach an action to the FSM that will execute anytime
  # the broken state is entered.
  @Action(state = 'broken')
  def smash(self, message):
    self.indicator = 'broken'

# Instantiate an FSM and send it events to process.
light_switch = LightSwitch()
light_switch.on_message('turn on')
light_switch.on_message('turn off')
light_switch.on_message('break')

declarative-fsm's People

Contributors

thomasquintana avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.