-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBuyMilk.php
89 lines (71 loc) · 2.82 KB
/
BuyMilk.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?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('INITIAL_STATE',1);
define('DRIVING_TO_BUY_MILK',2);
define('CANCEL_DRIVING',3);
define('PICKING_THE_MILK',4);
define('PAYING_FOR_THE_MILK',5);
define('UNABLE_TO_PURCHASE',6);
define('DRIVE_BACK_HOME',7);
$smachine->setDefaultInitState(INITIAL_STATE);
$smachine->setStates([
INITIAL_STATE=>'Initial State',
DRIVING_TO_BUY_MILK=>'Driving to buy milk',
CANCEL_DRIVING=>'Cancel driving.',
PICKING_THE_MILK=>'Picking the milk',
PAYING_FOR_THE_MILK=>'Paying for the milk',
UNABLE_TO_PURCHASE=>'Unable to purchase',
DRIVE_BACK_HOME=>'Drive back home.'
]);
$smachine->fieldUI=[
'milk'=>'READWRITE'
,'money'=>['nothing'=>0,'little'=>20,'lot'=>2000]
,'price'=>'NUMERIC'
,'stock_milk'=>['many'=>10,'none'=>0]
,'store_open'=>'ONOFF'
,'gas'=>'READWRITE'];
$smachine->fieldDefault=[
'milk'=>0
,'money'=>9999
,'price'=>null
,'stock_milk'=>null
,'store_open'=>null
,'gas'=>10];
// database configuration
$smachine->tableJobs= 'buymilk_jobs';
$smachine->tableJobLogs= 'buymilk_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(INITIAL_STATE,DRIVING_TO_BUY_MILK
,'when milk = 0 and gas > 0');
$smachine->addTransition(INITIAL_STATE,CANCEL_DRIVING
,'when gas = 0','stop'); // null means, no timeout and stop means, the job will stop
$smachine->addTransition(DRIVING_TO_BUY_MILK,PICKING_THE_MILK
,'when store_open = 1 and stock_milk > 0');
$smachine->addTransition(DRIVING_TO_BUY_MILK,UNABLE_TO_PURCHASE
,'when store_open = 0 or stock_milk = 0');
$smachine->addTransition(PICKING_THE_MILK,PAYING_FOR_THE_MILK
,'when money >= price set milk = 1');
$smachine->addTransition(PICKING_THE_MILK,UNABLE_TO_PURCHASE
,'when money < price');
$smachine->addTransition(UNABLE_TO_PURCHASE,DRIVE_BACK_HOME
,'when always','stop');
$smachine->addTransition(PAYING_FOR_THE_MILK,DRIVE_BACK_HOME
,'when always','stop');
$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