2017 © Pedro Peláez
 

library techne

Finite State Machine implementation

image

thetwelvelabs/techne

Finite State Machine implementation

  • Tuesday, January 29, 2013
  • by chriswoodford
  • Repository
  • 1 Watchers
  • 38 Stars
  • 1,533 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

# Finite-State Machine Library

A simple PHP implementation of a Finite-State Machine, (*1)

## Installation

Use Composer to install this library in your project, (*2)

Create your composer.json file

  {
      "require": {
          "thetwelvelabs/techne": "0.2.*@dev"
      }
  }

Download composer into your application root

  $ curl -s http://getcomposer.org/installer | php

Install your dependencies

  $ php composer.phar install

## Usage

Let's use a light switch as a simple example.
A light switch as two states: on and off. The state of a light switch is transitioned from one to the other by flipping the switch. We'll assume that the initial state of the light switch is 'off', (*3)

Define your FSM

  $machine = new StateMachine\FiniteStateMachine();
  $machine->setInitialState('off');

Define the transitions

  $turnOff = new StateMachine\Transition('on', 'off');
  $turnOn = new StateMachine\Transition('off', 'on');

Add a guard to the turnOn transition

  // flipping the switch on requires electricity
  $hasElectricity = true;
  $turnOn->before(function() use ($hasElectricity) {
      return $hasElectricity ? true : false;
  });

Define the events

  $machine->addEvent('flip', array($turnOn, $turnOff));

Transition from off to on

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'on'  

Transition back to off

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'off'  

The Versions

29/01 2013

0.2.x-dev

0.2.9999999.9999999-dev http://thetwelvelabs.com

Finite State Machine implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

fsm finite-state machine

29/01 2013

dev-master

9999999-dev http://thetwelvelabs.com

Finite State Machine implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

fsm finite-state machine

18/11 2012

0.1.x-dev

0.1.9999999.9999999-dev http://thetwelvelabs.com

Finite State Machine implementation

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

fsm finite-state machine