PHP Classes

PHP State Machine One: Process transitions in a state machine

Recommend this page to a friend!
  Info   View files Example   View files View files (45)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 138 All time: 9,196 This week: 339Up
Version License PHP version Categories
statemachineone 1.0.1GNU Lesser Genera...5Algorithms, PHP 5, Emulators
Description 

Author

This package can process transitions in a state machine.

It can compose a state machine by defining a list of states and conditions applied to state variables that must be true so the transitions between them happen.

The package can also define jobs with initial values of variables, as well run those jobs and performing the transitions between states when the conditions defined before are true.

Innovation Award
PHP Programming Innovation award nominee
December 2018
Number 6
State machines are components that can keep track of the states of a system and evaluate conditions that trigger the transitions between different states of a component.

This package can animate a state machine defined by states, transitions and conditions so it can be used on any applications that can make use of state machines.

Manuel Lemos
Picture of Jorge Castro
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 30 packages by
Country: Chile Chile
Age: 47
All time rank: 12893 in Chile Chile
Week rank: 30 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Example

<?php

use eftec\statemachineone\Job;
use
eftec\statemachineone\StateMachineOne;
use
eftec\statemachineone\Transition;

include
"../vendor/autoload.php";

define("STATE_PICK",1);
define("STATE_CANCEL",2);
define("STATE_TRANSPORT",3);
define("STATE_ABORTTRANSPORT",4);
define("STATE_TODELIVER",5);
define("STATE_HELP",6);
define("STATE_DELIVERED",7);
define("STATE_ABORTED",8);

$smachine=new StateMachineOne(null);
$smachine->setDebug(true);
$smachine->tableJobs="deliver_product";
$smachine->tableJobLogs="deliver_product_log";
$smachine->setDefaultInitState(STATE_PICK);
$smachine->fieldDefault=[
   
'customerpresent'=>null
   
,'addressnotfound'=>null
   
,'signeddeliver'=>null
   
,'abort'=>null
   
,'instock'=>null
   
,'picked'=>null];
$smachine->setdb('mysql','localhost',"root","abc.123","statemachinedb");
$smachine->createDbTable(true); // you don't need to create this table every time.

$smachine->setStopTrigger(function($smo,$job) {echo "Trigger: job is stopping<br>"; return true;});
$smachine->setPauseTrigger(function($smo,$job) {echo "Trigger: job is paused<br>"; return true;},'after');
//$smachine->loadDBActiveJobs();


$smachine->setStates(
        [
STATE_PICK=>'STATE_PICK'
       
,STATE_CANCEL=>'STATE_CANCEL'
       
,STATE_TRANSPORT=>'STATE_TRANSPORT'
       
,STATE_ABORTTRANSPORT=>'STATE_ABORTTRANSPORT'
       
,STATE_TODELIVER=>'STATE_TODELIVER'
       
,STATE_HELP=>'STATE_HELP'
       
,STATE_DELIVERED=>'STATE_DELIVERED'
       
,STATE_ABORTED=>'STATE_ABORTED']);

// if instock = 0 and picked = 1 then change and set instock = 1 , instock = 2
// if _timeout then change and set instock = 1 , instock = 2
$dummy='hello';

function
dummy($job) {
    return
'hello';
}

$smachine->addTransition(STATE_PICK,STATE_CANCEL,'when instock = 0',"stop");
$smachine->addTransition(STATE_PICK,STATE_TRANSPORT,'when picked = 1',"change");
$smachine->addTransition(STATE_TRANSPORT,STATE_TODELIVER,'when addressnotfound = 0',"change");
$smachine->addTransition(STATE_TRANSPORT,STATE_HELP,'when addressnotfound = 1',"change");
//$smachine->addTransition(STATE_HELP,STATE_ABORTED,'when addressnotfound = 9999 timeout 1',"stop"); // we wait 2 seconds, then we give it up

$smachine->addTransition(STATE_HELP,STATE_ABORTED,'when addressnotfound = 9999',"stop"); // we wait 2 seconds, then we give it up
$smachine->addTransition(STATE_HELP,STATE_TODELIVER,'when addressnotfound = 0',"change");
//$smachine->addTransition(STATE_TODELIVER,STATE_DELIVERED
// ,'when signeddeliver = 1 set addressnotfound = 0 , customerpresent = 1 timeout 3600',"stop");

$smachine->addTransition(STATE_TODELIVER,STATE_DELIVERED
   
,'when signeddeliver = 1 set addressnotfound = 0 , customerpresent = 1 timeout 3600',"stop");
$smachine->addEvent('CUSTOMERPRESENT','set addressnotfound = 0 , customerpresent = 1');

$smachine->checkConsistency();

$object=['customerpresent'=>1
   
,'addressnotfound'=>1
   
,'signeddeliver'=>1
   
,'abort'=>-1
   
,'fieldnotstored'=>'hi' // this field is not store or it's part of the state machine
   
,'instock'=>1
   
,'picked'=>1];

$job=$smachine->createJob($object);

$smachine->checkAllJobs();
sleep(2);
$smachine->checkAllJobs();

echo
"<hr>";
$object=['customerpresent'=>-1 // undefined
   
,'addressnotfound'=>-1 // undefined.
   
,'signeddeliver'=>1
   
,'abort'=>-1
   
,'fieldnotstored'=>'hi' // this field is not store
   
,'instock'=>1
   
,'picked'=>1];
$job=$smachine->createJob($object);
$smachine->checkAllJobs();
echo
"calling event CUSTOMERPRESENT<br>";
$smachine->callEvent('CUSTOMERPRESENT');
$smachine->checkAllJobs();



  Files folder image Files  
File Role Description
Files folder imageDocs (5 files)
Files folder imageexample (12 files, 1 directory)
Files folder imagelib (9 files)
Files folder imagetests (4 files, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Job.md Data Auxiliary data
Accessible without login Plain text file Job.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file StateMachineOne.md Data Auxiliary data
Accessible without login Plain text file StateMachineOne.md Data Auxiliary data
Accessible without login Plain text file Transition.md Data Auxiliary data
Accessible without login Plain text file Transition.md Data Auxiliary data

  Files folder image Files  /  Docs  
File Role Description
  Accessible without login Image file ChopSuey.jpg Data Auxiliary data
  Accessible without login Image file cooker.jpg Data Auxiliary data
  Accessible without login Image file deliveryboy.jpg Data Auxiliary data
  Accessible without login Image file food.jpg Data Auxiliary data
  Accessible without login Image file milk1.jpg Data Auxiliary data

  Files folder image Files  /  example  
File Role Description
Files folder imageexampledb (1 directory)
  Accessible without login Plain text file BuyMilk.php Example Example script
  Accessible without login Plain text file Car.php Example Example script
  Accessible without login Plain text file Car.php Example Example script
  Accessible without login Plain text file ChopSuey.php Example Example script
  Accessible without login Plain text file ChopSueyDoc.php Example Example script
  Accessible without login Plain text file ChopSueyDoc.php Example Example script
  Accessible without login Plain text file example1.php Aux. Auxiliary script
  Accessible without login Plain text file example2.php Example Example script
  Accessible without login Plain text file example2withoutdatabase.php Example Example script
  Accessible without login Plain text file example2withoutdatabase.php Example Example script
  Accessible without login Plain text file hiring.php Example Example script
  Accessible without login Plain text file hiring.php Example Example script

  Files folder image Files  /  example  /  exampledb  
File Role Description
Files folder imagechopsuey (2 files)

  Files folder image Files  /  example  /  exampledb  /  chopsuey  
File Role Description
  Accessible without login Plain text file genseq_seq_chopsuey_jobs.dson.seq Data Auxiliary data
  Accessible without login Plain text file genseq_seq_chopsuey_jobs.dson.seq Data Auxiliary data

  Files folder image Files  /  lib  
File Role Description
  Plain text file Flags.php Class Class source
  Plain text file Flags.php Class Class source
  Plain text file Job.php Class Class source
  Plain text file Pending.php Class Class source
  Plain text file Pending.php Class Class source
  Plain text file StateMachineOne.php Class Class source
  Plain text file StateSerializable.php Class Class source
  Plain text file StateSerializable.php Class Class source
  Plain text file Transition.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagetmpdoc (2 files)
  Plain text file AbstractStateMachineOneTestCase.php Class Class source
  Plain text file AbstractStateMachineOneTestCase.php Class Class source
  Plain text file StateMachineTest.php Class Class source
  Plain text file StateMachineTest.php Class Class source

  Files folder image Files  /  tests  /  tmpdoc  
File Role Description
  Accessible without login Plain text file dummy.txt Doc. Documentation
  Accessible without login Plain text file dummy.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:138
This week:0
All time:9,196
This week:339Up