11<?php
22
3+ use eftec \statemachineone \Flags ;
34use eftec \statemachineone \StateMachineOne ;
45
56include '../vendor/autoload.php ' ;
6-
7- $ smachine =new StateMachineOne (null );
7+ echo "<h1>Example timeline</h1> " ;
8+ echo "This example shows to record a state machine event with times and later replicates it " ;
9+ echo "<h2>Initial</h2> " ;
10+ $ smachine = new StateMachineOne (null );
11+ $ smachine ->setRecordEventState (__DIR__ . '/exampletimeline.json ' , true );
812$ smachine ->setDefaultInitState (1 );
9- $ smachine ->setDocDB ('exampledb ' ,'col1 ' ,'folder ' );
10- $ smachine ->getDocOne ()->autoSerialize (true ,'json_array ' );
13+ $ smachine ->setDocDB ('exampledb ' , 'col1 ' , 'folder ' );
14+ try {
15+ $ smachine ->getDocOne ()->deleteCollection ('col1 ' );
16+ $ smachine ->getDocOne ()->createCollection ('col1 ' );
17+ } catch (Throwable $ e ) {
18+
19+ }
20+ $ smachine ->getDocOne ()->autoSerialize (true , 'json_array ' );
1121$ smachine ->setTime (1000 );
1222//$smachine->setTime(6000);
23+ $ smachine ->setStates ([1 => 'init ' , 2 => 'mid ' , 3 => 'end ' ]);
24+ $ smachine ->fieldDefault = ['v1 ' => 0 , 'flag1 ' => new Flags ('flag1 ' , true , $ smachine ), 'v3 ' => 'abc ' ];
25+ $ smachine ->addTransition (1 , 1 , 'where _time=1005 then flag1.push("a1","abc") and v3="second1005" ' , 'stay ' );
26+ $ smachine ->addTransition (1 , 2 , 'where _time>=1010 then flag1.push("a1","xyz") and v3="second1010" ' , 'change ' );
27+ $ smachine ->addTransition (2 , 3 , 'where _time>=1020 then and v3="second1020" ' , 'change ' );
28+ $ smachine ->setTime (1000 );
29+ $ job = $ smachine ->createJob ();
30+ $ job ->idJob =1 ;
31+ for ($ i = 1000 ; $ i <= 1020 ; $ i ++) {
32+ $ smachine ->setTime ($ i );
33+ $ smachine ->checkJob ($ job );
34+ //$smachine->recordEventState(__DIR__.'/event.php','php');
35+ $ r = $ job ->fields ;
36+ unset($ r ['flag1 ' ]);
37+ echo "State: " .json_encode ($ r )." time: $ i<br> " ;
38+ }
39+ $ smachine ->deleteJobDB ($ job );
40+ echo "<h2>replay</h2> \n" ;
41+ $ smachine ->setRecordEventState (__DIR__ . '/exampletimeline2.json ' , true ); // set a new record
42+ $ oldValues = $ smachine ->replayRecordInit ('exampletimeline.json ' );
43+ $ job = $ smachine ->createJob ();
1344
1445
15- $ smachine ->setStates ([1 =>'init ' ,2 =>'mid ' ,3 =>'end ' ]);
16- $ smachine ->fieldDefault =['v1 ' =>0 ];
17- $ smachine ->addTransition (1 ,2 ,'where _time>=1010 ' ,'change ' );
18- $ smachine ->addTransition (2 ,3 ,'where _time>=1020 ' ,'change ' );
1946
20- $ job =$ smachine ->createJob (['v1 ' =>1 ]);
47+ foreach ($ oldValues as $ row ) {
48+ $ smachine ->replayRecordInsideLoop ($ row ,$ job );
49+ //$r = $job->fields;
50+ //unset($r['flag1']);
2151
22- for ( $ i = 1000 ; $ i <= 1020 ; $ i ++) {
23- $ smachine -> setTime ( $ i ) ;
24- $ smachine -> checkAllJobs ( );
52+ $ smachine -> checkJob ( $ job );
53+ $ r = $ job -> fields ;
54+ unset( $ r [ ' flag1 ' ] );
2555
26- $ lastJob = $ smachine ->getLastJob ();
27- echo "State: $ lastJob ->state time: $ i<br> " ;
28- }
56+ echo "State: " .json_encode ($ r )." time: $ i<br> " ;
2957
3058
59+ }
60+ $ smachine ->deleteJobDB ($ job );
61+ //var_dump($txt);
3162//$smachine->saveDBAllJob();
0 commit comments