2017 © Pedro Peláez
 

library state_machine

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

image

mothership/state_machine

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  • Tuesday, September 12, 2017
  • by Mothership
  • Repository
  • 3 Watchers
  • 10 Stars
  • 2,404 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 5 Open issues
  • 19 Versions
  • 1 % Grown

The README.md

PHP State Machine / FSM / Acceptance Automata

Summary Latest Stable Version Total Downloads Latest Unstable Version License

A PHP based state machine implementation with a graph generator., (*1)

For more informations visit the website, (*2)

Design goals

There are a lot of FSM(Finite State Machines) implementation. But most implementations do not have a proper documentation or are too overengineered in a lot of terms. This library tries to be a starting point for your own implementation by reading the code. In fact you can also use this for your own projects., (*3)

Features

  • Create a FSM-compatible state machine
  • Define your states, transitions and conditions completely in a configuration file. Currently only supports YAML
  • NO additional logic is needed to process the transitions. The state machine will automatically try to detect valid transitions
  • Render a graphic image that shows the behaviour of the state machine.

Tests

You can run the unit tests with the following command:, (*4)

$ cd path/to/Mothership/Component/StateMachine/ #check your path
$ composer install
$ phpunit

Installation

You can use composer to directly install in in your project. Do not use the master branch but only tagged versions., (*5)

"require": {
    "mothership/state_machine": "v1.3.*"
}

Then just run composer install, (*6)

$ composer install

Requirement for rendering the graph

The StateMachine render graph functionality depends on the graphviz library. On os with apt just install it with the package managment, (*7)

sudo apt-get install graphviz

Usage

The state machine needs three files to be able to run:, (*8)

  1. A concrete state machine
  2. The yaml configuration file
  3. A workflow definition

Quickstart

In case you have everything or just want to skip the more detailed parts, take a look at this directory within this repository:, (*9)

/src/Examples/Simple/SimpleStateMachine.php
$stateMachine = new \Mothership\StateMachine\Examples\Simple('workflow.yml');
$stateMachine->run();

1. The concrecte state machine

There is no concrete StateMachine implementation in this library, except the examples. However the implementation is very easy and in fact just an inheritance of the abstract class., (*10)

// set your own namespace
namespace Mothership\StateMachine\Examples\Simple;

class SimpleStateMachine extends \Mothership\StateMachine\StateMachineAbstract
{
// that's all!
}

2. The yaml configuration file

The yaml configuration file is the most important file as it describes various business cases. You will find more advanced use cases later. For the beginning take a look at the following workflow., (*11)

, (*12)

By the way: the graph has been rendered with graphviz. You can also easily render it by your own with this command:, (*13)

$stateMachine->renderGraph('/your/path/file.png');

If you are familiar with the concepts of a state machine, you will know that a state machine contains, (*14)

  • a defined start
  • a defined end
  • a number of transitions
  • a set of various states

Let's see, how the configuration file could look like, by inspecting the workflow.yml:, (*15)

class:
  name: Mothership\StateMachine\Examples\Simple\SimpleWorkflow
  args: []

states:
  start:
    type: initial
  second_state:
    type: normal
    transitions_from:   [start]
    transitions_to:     [second_state]
  third_state:
    type: normal
    transitions_from:   [second_state]
    transitions_to:     [third_state]
  finish:
    type: final
    transitions_from:  [third_state]
    transitions_to:    [finish]

Good to know

  • Every state, except start needs to be implemented as a method. For example the state with the name second_state expects a method named second_state() in the workflow class.
  • The name of the transitions is always equals to the name of the transitions_to field. If there is an condition, the transition name will also be changed.
  • You do not need to implement any programming logic for state transitions.
  • The possible types of the state can be initial, normal or final.

3. The workflow class

While the configuration file defines the possible transitions of the automata, the workflow class contains the possible transitions. Let's check this code:, (*16)

namespace Mothership\StateMachine\Examples\Simple;

use Mothership\StateMachine\WorkflowAbstract;

class SimpleWorkflow extends WorkflowAbstract
{

    function second_state()
    {

    }

    function third_state()
    {

    }

    function finish()
    {
    }
}
  • There is no method start() as this state will never be executed but is just a starting point
  • The basic methods do not return any value. More advanced methods can for example return a boolean value or any string. This important for more advanced use cases.
  • The name of the class MUST match the name of the class in the configuration file
  • You can use a preDispatch() or postDispatch() method after each state. It might be better to use a callback handler for that kind of stuff but to keep this library small, easy to understand and suitable for basic cases, this decision was made.

Render the graph

It is recommended to render your workflow as a graph, so that you will get visual support., (*17)

$state_machine = new StateMachine();
$state_machine = $state_machine->renderGraph($path, false);

$paht: path where the state machine will save the image, (*18)

true/false: if you want that after the render the state machine exits (default is true), (*19)

Advanced Use Cases

Let's assume a more advanced workflow like that one: Advanced Workflow. The rendered graph will look like the following:, (*20)

, (*21)

We will go through smaller examples to discuss the different transition types., (*22)

Conditionals

Check the transition product_has_media_gallery and create_media_gallery|get_images, (*23)

Start Condition Target
product_has_media_gallery true create_media_gallery
product_has_media_gallery false get_images

As you can see, you need to have a condition AFTER the start state. This means that the state product_has_media_gallery needs to return a boolean value true OR false. To be able to do this, you need to update your yaml configuration like this:, (*24)

  product_has_media_gallery:
    type: normal
    transitions_from: [download_directory_exist]
    transitions_to:   [product_has_media_gallery]

  create_media_gallery:
    type: normal
    transitions_from: [{status: product_has_media_gallery, result:  false}]
    transitions_to:   [create_media_gallery]

  get_images:
    type: normal
    transitions_from: [{status: product_has_media_gallery, result:  true}, create_media_gallery]
    transitions_to:   [get_images]

  • The conditions are AlWAYS set in the target transition
  • Even if we use BOOLEAN values here, you can actually also return a STRING! This is valid as well:, (*25)

    transitions_from: [{status: product_has_media_gallery, result:  'yes, okay'}]
    
  • The state get_images can be processed with two different transitions.

Loops

Loops are very useful if you need to process a large set of data. The pattern is pretty simple. Take a look at the states process_images, has_more and finish., (*26)

If there is more to process, then continue to process_images, otherwise finish. As a matrix this would be:, (*27)

Start Condition Target
has_more true process_images
has_more false finish

Just do it like that:, (*28)

  process_images:
    type: normal
    transitions_from: [get_images, {status: has_more, result:  true}]
    transitions_to:   [process_images]

  has_more:
    type: normal
    transitions_from: [assign_image]
    transitions_to:   [has_more]

  finish:
    type: final
    transitions_from:  [{status: has_more, result:  false}]
    transitions_to:  [finish]

Exception handling

You can use exception handling in different cases., (*29)

Define an exception state

This is not a typical FSA. But defining an external exception can help you to build more tolerant state machines. However you should be very careful with this one. Defining an exception state can lead to some bad design decisions as you leave the exception handling into the responsibility of the appropriate exception state., (*30)

Consider the usage of the exception state very carefully:, (*31)

  • Do i need a general exception handling. If you need a centralized exception handling this could be useful but you can also build try-catch blocks in every state
  • Do i need to re-run a failed transition? You can use the exception state to re-run the state machine from a defined transition. Just set the transition in the yaml file.

Catch the run method

Wrap a try-catch around the run method and build a custom exception handler. Useful to have it in a centralized way. However it is difficult to return to a working state again., (*32)

  public function run(array $args = [])
  {
    try {
        parent::run($args);
    } catch (WorkflowException $e) {
        $this->handleException('Workflow', $e);
    } catch (StatusException $e) {
        $this->handleException('Workflow', $e);
    } catch (\Exception $e) {
        $this->handleException('General', $e);
    }
  }

Catch in the states

Catch any exception which you want to control. It is recommended to be as specific as possible., (*33)

test_state()
{
    try {
    } catch (\Highest\Exception $e) {
        echo $e->getMessage();
    }
}

More examples

Check the directory ./src/Examples to explore more advanced examples by your own. You can also add new state machines into this directory, to automatically run unit tests., (*34)

Simple

Advanced Workflow, (*35)

SimpleLoop

Advanced Workflow, (*36)

IfConditions

Advanced Workflow, (*37)

Boolean Conditions

Advanced Workflow, (*38)

Advanced Workflow

Advanced Workflow, (*39)

Exception Workflow

This workflow is pretty advanced and IS NOT A STANDARD automata in terms of a DFA which only has ONE defined start. It is more useful if you need to build more complex state machines where you might have exceptions in different part of the code and do not know where to handle it. It is possible to model an exception handling as a transition for every single node but the side effect would be that you will have a lot of transitions in bigger graphs., (*40)

It is not recommended to have a general exception handling like that one, especially if you don't know what your workflow is doing., (*41)

Exception Workflow, (*42)

Tests

  • Run test from root directory: phpunit --coverage-text
  • You can add your State machine in the ./src/Examples folder and it will be automatically tested with the command about. Just use the same conventions name space

Notes

The Versions

12/09 2017

dev-develop

dev-develop https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

12/09 2017

dev-master

9999999-dev https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

12/09 2017

v1.4.5

1.4.5.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi

12/09 2017

v1.4.4

1.4.4.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

12/09 2017

v1.4.0

1.4.0.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

20/09 2016

v1.3.1

1.3.1.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

11/07 2016

1.3.0

1.3.0.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

10/03 2016

v1.2.1

1.2.1.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

09/03 2016

v1.2.0

1.2.0.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

03/03 2016

v1.1.2

1.1.2.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

02/03 2016

v1.1.1

1.1.1.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

29/02 2016

v1.1.0

1.1.0.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

19/02 2016

v1.0.4

1.0.4.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

17/02 2016

v1.0.3

1.0.3.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

02/02 2016

v1.0.2

1.0.2.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

02/02 2016

v1.0.1

1.0.1.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

01/02 2016

v1.0.0

1.0.0.0 https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Don Bosco van Hoi
by Maurizio Brioschi

09/11 2015

dev-#1_add_logging_system

dev-#1_add_logging_system https://www.mothership.de

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maurizio Brioschi

27/10 2015

v0.1

0.1.0.0

php implementation of state machine, completely configured by an external .yml file with the render of a graph's description

  Sources   Download

The Requires

 

The Development Requires

by Maurizio Brioschi