Skip to content

Commit 3413126

Browse files
2.26
1 parent 3c27d2d commit 3413126

File tree

6 files changed

+574
-378
lines changed

6 files changed

+574
-378
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The target of this library is to ease the process to create a state machine for
5050
* [Getting a job](#getting-a-job)
5151
* [Database and jobs.](#database-and-jobs)
5252
* [Fields used in Job](#fields-used-in-job)
53+
* [Flags](#flags)
5354
* [GUI](#gui)
5455
* [Classes](#classes)
5556
* [Cache Configuration](#cache-configuration)
@@ -286,8 +287,8 @@ Types of transition:
286287
It compares a constant. The binary operator for comparison are
287288
* = Equals
288289
* **<>** Not equals
289-
* **< <=** Less and less than
290-
* **> >=** Great and great than
290+
* **< <=** "Less" and "less than"
291+
* **> >=** "Great" and "greater than"
291292
* **contain** If a text contains other.
292293
```php
293294
"when field contain 'text'"
@@ -581,6 +582,23 @@ $smachine->deleteJobDB($job); // we delete a specific job.
581582
* **$isUpdate** bool If the job is updated. It is used to store into the database (update)
582583
* **$log** string[]
583584

585+
## Flags
586+
It is possible to store one or multiples flags inside a job.
587+
A flag is a state or value that:
588+
* It can have a name.
589+
* It could have a level, for example a level of severity.
590+
* It could have an expiration time.
591+
* can have an interchangeable value, such as switch on and switch off.
592+
* It is associate with a StateMachineOne instance and with a Job.
593+
594+
Example of creates an array of flags:
595+
596+
```php
597+
$flags=new Flags('flag1', true, $smachine); // the flags is called flag1, $smachine is a state machine
598+
$flags->setParent($job); // it is also associate with a job.
599+
```
600+
601+
584602

585603

586604
## GUI
@@ -655,6 +673,8 @@ Commonly, the log format could be of the type info or error. Flag could show a
655673
Dual license (LGPL 3.0 and Commercial). See LICENSE file.
656674

657675
## Version
676+
* 2.26 2024-07-20
677+
* added function setRecordEventState(),replayRecordInit() and replayRecordInsideLoop()
658678
* 2.25 2024-07-19
659679
* added function setTime()
660680
* 2.24 2024-07-18

example/exampletimeline.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
11
<?php
22

3+
use eftec\statemachineone\Flags;
34
use eftec\statemachineone\StateMachineOne;
45

56
include '../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();

lib/Job.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Class Job
88
* @package eftec\statemachineone
99
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
10-
* @version 2.25 2024-07-19
10+
* @version 2.27 2024-07-20
1111
* @link https://github.com/EFTEC/StateMachineOne
1212
*/
1313
class Job

0 commit comments

Comments
 (0)