PHP Classes
elePHPant
Icontem

File: example/example2.php

Recommend this page to a friend!
  Classes of Jorge Castro  >  PHP State Machine One  >  example/example2.php  >  Download  
File: example/example2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP State Machine One
Process transitions in a state machine
Author: By
Last change:
Date: 9 months ago
Size: 2,495 bytes
 

 

Contents

Class file image Download
<?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();
$smachine->setDebug(true);
$smachine->tableJobs="deliver_product";
$smachine->tableJobLogs="deliver_product_log";
$smachine->setDefaultInitState(STATE_PICK);
$smachine->fieldDefault=['customerpresent','addressnotfound','signeddeliver','abort','instock','picked'];
$smachine->setDB('localhost',"root","abc.123","statemachinedb");
$smachine->createDbTable(true); // you don't need to create this table every time.

$smachine->setStopTrigger(function($smo,$job) {echo "job is stopping<br>"; return true;});

//$smachine->loadDBActiveJobs();


$smachine->setStates([STATE_PICK,STATE_CANCEL,STATE_TRANSPORT,STATE_ABORTTRANSPORT,STATE_TODELIVER,STATE_HELP,STATE_DELIVERED,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',60*30,"stop");
$smachine->addTransition(STATE_PICK,STATE_TRANSPORT,'when picked = 1',60*30,"change");
$smachine->addTransition(STATE_TRANSPORT,STATE_TODELIVER,'when addressnotfound = 0',60*30,"change");
$smachine->addTransition(STATE_TRANSPORT,STATE_HELP,'when addressnotfound = 1',60*30,"change");
$smachine->addTransition(STATE_HELP,STATE_ABORTED,'when addressnotfound = 9999',1,"stop"); // we wait 2 seconds, then we give it up
$smachine->addTransition(STATE_HELP,STATE_TODELIVER,'when addressnotfound = 0',60*30,"change");

$smachine->addTransition(STATE_TODELIVER,STATE_DELIVERED
   
,'when signeddeliver = 1 set addressnotfound = 0 and customerpresent = 1',60*30,"stop");

$smachine->checkConsistency();

$job=$smachine->createJob(['idref'=>1]
        ,[
'customerpresent'=>''
       
,'addressnotfound'=>1
       
,'signeddeliver'=>1
       
,'abort'=>''
       
,'fieldnotstored'=>'hi'
       
,'instock'=>1
       
,'picked'=>1]);

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