-
Notifications
You must be signed in to change notification settings - Fork 13
/
Car.php
65 lines (47 loc) · 1.71 KB
/
Car.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
* @link https://github.com/EFTEC/StateMachineOne
*/
use eftec\statemachineone\Job;
use eftec\statemachineone\StateMachineOne;
use eftec\statemachineone\Transition;
// we use autoload's composer, so we call it here.
include "../vendor/autoload.php";
$smachine=new StateMachineOne(null);
$smachine->setDebug(true);
// it is specific for this project
define('PARKED',1);
define('IDLING',2);
define('DRIVING',3);
$smachine->setDefaultInitState(PARKED);
$smachine->setStates([
PARKED=>'Parked',
IDLING=>'Idling',
DRIVING=>'Driving'
]);
$smachine->fieldDefault=[
'pedal'=>0
,'turnkey'=>0
,'gas'=>100
,'brake'=>0
,'speed'=>0];
// database configuration
$smachine->tableJobs="car_jobs";
$smachine->tableJobLogs="car_logs"; // it is optional
$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.
//$smachine->loadDBActiveJobs(); // use this in production, we don't need stopped job every time.
// business rules
$smachine->addTransition(PARKED,IDLING
,'when pedal = 1 and turnkey = 1 and gas > 0');
$smachine->addTransition(IDLING,DRIVING
,'when gas > 0 and speed > 0');
$smachine->addTransition(DRIVING,IDLING
,'when brake = 1 and speed = 0');
$smachine->addTransition(IDLING,PARKED
,'when turnkey = 0 and speed = 0');
$msg=$smachine->fetchUI(); // we show a visual id (it is optional and it's only for debug purpose)
$smachine->checkAllJobs(); // we check every (active,pause,continue) job available.
$smachine->viewUI(null,$msg); // null means it takes the current job