Download .zip |
Info | Example | View files (45) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-03-28 (19 hours ago) | Not enough user ratings | Total: 118 This week: 5 | All time: 9,133 This week: 69 |
Version | License | PHP version | Categories | |||
statemachineone 1.0.1 | GNU Lesser Genera... | 5 | Algorithms, PHP 5, Emulators |
It is State Machine library written on PHP aimed to business process. This library has only a simple external dependency, it is a minimalist (yet complete) library with only 3 classes.
Since this library is PHP native, then it could run in Laravel, Symfony and any other frameworks.
A State Machine (also called Automata) is a procedural execution of a job based in states. Every job must have a single state at the same time, such as "INITIATED","PENDING","IN PROCESS" and so on, and the job changes of state (transition) according to some logic or condition. Such conditions could be a field, a time or a custom function.
The target of this library is to ease the process to create a state machine for business.
We need to create a process to deliver Chinese food at home. Is it easy?. Well, let's find it out.
Fields are values used for out State Machine. In this example, I am not including other values that it could be useful (such as money, customer name, address and such) because they are not part or used by the state machine.
It must include all the possible situation. The real world is not as easy as: sell and money.
Example/BuyMilk.php (buy milk)
Example/Car.php (car parking)
<?php
use eftec\statemachineone\StateMachineOne;
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();
$smachine->setDebug(true);
$smachine->tableJobs="chopsuey_jobs";
$smachine->tableJobLogs="chopsuey_logs";
$smachine->setDefaultInitState(STATE_PICK);
$smachine->setAutoGarbage(false); // we don't want to delete automatically a stopped job.
$smachine->setStates([STATE_PICK=>'Pick order'
,STATE_CANCEL=>'Cancel order'
,STATE_TRANSPORT=>'Transport order'
,STATE_ABORTTRANSPORT=>'Abort the delivery'
,STATE_TODELIVER=>'Pending to deliver'
,STATE_HELP=>'Request assistance'
,STATE_DELIVERED=>'Delivered'
,STATE_ABORTED=>'Aborted']);
$smachine->fieldDefault=[
'customerpresent'=>-1
,'addressnotfound'=>-1
,'signeddeliver'=>-1
,'abort'=>-1
,'instock'=>-1
,'picked'=>-1
,'message'=>''];
$smachine->setDB('mysql','localhost',"root","abc.123","statemachinedb");
$smachine->createDbTable(false); // you don't need to create this table every time.
$smachine->loadDBAllJob(); // we load all jobs, including finished ones.
// business rules
$smachine->addTransition(STATE_PICK,STATE_PICK
,'when instock = 0 set message="without stock"','stay'); // it stays in the same state
$smachine->addTransition(STATE_PICK,STATE_CANCEL
,'when instock = 0 set abort = 1','stop'); // ends the process
$smachine->addTransition(STATE_PICK,STATE_TRANSPORT
,'when instock = 1','change'); // changes transition
$smachine->addTransition(STATE_TRANSPORT,STATE_ABORTTRANSPORT
,'when abort = 1','stop'); // ends the process
$smachine->addTransition(STATE_TRANSPORT,STATE_DELIVERED
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1 timeout 3600','stop'); // 1 hour max.
$smachine->addTransition(STATE_TRANSPORT,STATE_HELP
,'when addressnotfound = 1 or customerpresent = 0 timeout 3600','change'); // 1 hour max
$smachine->addTransition(STATE_HELP,STATE_ABORTED
,'when wait timeout 900','change'); // it waits 15 minutes max.
$smachine->addTransition(STATE_HELP,STATE_DELIVERED
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1','change');
$msg=$smachine->fetchUI();
$smachine->checkAllJobs();
$smachine->viewUI(null,$msg); // null means it takes the current job
Let's say the next transition
$smachine->addTransition(STATE_PICK,STATE_CANCEL
,'when instock = 0 set abort = 1','stop');
The transition is written as follow: * initial state * end state * Transition language outcome, it could bechange(default value),stop,pause,continueandstay*
changemeans the state will change frominitial statetoend state* if it meets the condition (or timeout). It will only change if the state is active.
stop* means the state will change and the job will stop (end of the job)
pause* it means the state will change and the job will pause. A job paused can't change of state, even if it meets the condition.
continue* it means the state will change and the job will continue from pause.
stay* it means the state will not change (but it executes any other instruction).
> _when_ var1 = var2 and var3 = var4 or var4 = var5 > _set_ var1 = var2 , var3 = var4 > _timeout_ var1 > _fulltimeout_ var2 there are two operations we could dowhenand/orset*
The transition happens when this condition meets. For example: > when field=0 // it happens when the field is zero. > when $var='hi' // it happens when the global variable is 'hi' > when fn()=44 // the transition is triggered when the function fn() returns 44 > when always // its always true. It is the same than "when 1=1". The transition is always executed
It compares a constant. The binary operator for comparison are * = Equals <>* Not equals < <=* Less and less than > >=* Great and great than contain* If a text contains other. > when field contain 'text'
Values of the field could be as the next ones: field* = it is a field of the job. > when field = field2 // when field (of the job) is equals to field2 _idjob* = it is the number of the current job. It is calculated every time the job is evaluated > set id=_idjob $var* = it is a global variable (php) 777* = it is a numeric constant > when field = 777 // when field is equals to 777 "AAA",'aaa'* = it is a literal > when field = 'hello' // when field is equals to the textr hello function()* = it is a global function. Every function must have the parameter $job. > when field = somefunc() // function somefunc(Job $job) {...} null()* it is the null value > when field = null() true()* it is the true value (true) > when field = true() // when field is equals to true false()* it is the false value (false) > when field = true() // when field is equals to false on()* it is the on value (1) off()* it is the off value (0) undef()* it is the undefined value (-1) flip()* indicates that the value will be flipped (1=>0 and 0=>1). Example (x=1) x = flip(), now (x=0). If the value is not zero, then it's flipped to zero. > set field=flip() // it is only valid for set. now()* it defines the current timestamp (in seconds) interval()* it returns the current interval between now and the last state. fullinterval()* it returns the current interval between now and the start of the job.
For example > when field2 = 2 and field3 > someFunction() and field4=$var > > Where somefunction must be defined as someFunction(Job $job) {}
> set field = 0 , field2 = 3
It sets a field of the job.
> set field = 0
It sets the field to the value 0
> set field + 1
It increases the value of field by 1 (field=field+1)
> set field - 1
It decreases the value of field by 1 (field=field-1)
It sets the timeout between the time of current state and the current time. If a timeout happens, then the transition is executed.
> timeout 3600 // 1 hour timeout > timeout field // timeout by field, it is calculated each time.
It sets the timeout between the time of initial state and the current time. If a timeout happens, then the transition is executed.
> fulltimeout 3600 // 1 hour timeout > fulltimeout field // timeout by field, the field is evaluated each time.
This library has a build-in GUI for testing.
StateMachineOne It is the main class. Job It is the model class for the job Transition It is the model class for the transitions.
Dual license (LGPL 3.0 and Commercial). See LICENSE file.
Files |
File | Role | Description | ||
---|---|---|---|---|
Docs (5 files) | ||||
example (12 files, 1 directory) | ||||
lib (9 files) | ||||
tests (4 files, 1 directory) | ||||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
Job.md | Data | Auxiliary data | ||
Job.md | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation | ||
StateMachineOne.md | Data | Auxiliary data | ||
StateMachineOne.md | Data | Auxiliary data | ||
Transition.md | Data | Auxiliary data | ||
Transition.md | Data | Auxiliary data |
Files | / | Docs |
File | Role | Description |
---|---|---|
ChopSuey.jpg | Data | Auxiliary data |
cooker.jpg | Data | Auxiliary data |
deliveryboy.jpg | Data | Auxiliary data |
food.jpg | Data | Auxiliary data |
milk1.jpg | Data | Auxiliary data |
Files | / | example |
File | Role | Description | ||
---|---|---|---|---|
exampledb (1 directory) | ||||
BuyMilk.php | Example | Example script | ||
Car.php | Example | Example script | ||
Car.php | Example | Example script | ||
ChopSuey.php | Example | Example script | ||
ChopSueyDoc.php | Example | Example script | ||
ChopSueyDoc.php | Example | Example script | ||
example1.php | Aux. | Auxiliary script | ||
example2.php | Example | Example script | ||
example2withoutdatabase.php | Example | Example script | ||
example2withoutdatabase.php | Example | Example script | ||
hiring.php | Example | Example script | ||
hiring.php | Example | Example script |
Files | / | example | / | exampledb | / | chopsuey |
File | Role | Description |
---|---|---|
genseq_seq_chopsuey_jobs.dson.seq | Data | Auxiliary data |
genseq_seq_chopsuey_jobs.dson.seq | Data | Auxiliary data |
Files | / | lib |
File | Role | Description |
---|---|---|
Flags.php | Class | Class source |
Flags.php | Class | Class source |
Job.php | Class | Class source |
Pending.php | Class | Class source |
Pending.php | Class | Class source |
StateMachineOne.php | Class | Class source |
StateSerializable.php | Class | Class source |
StateSerializable.php | Class | Class source |
Transition.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
tmpdoc (2 files) | ||||
AbstractStateMachineOneTestCase.php | Class | Class source | ||
AbstractStateMachineOneTestCase.php | Class | Class source | ||
StateMachineTest.php | Class | Class source | ||
StateMachineTest.php | Class | Class source |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.