index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Noa
  5. * Date: 9/11/2016
  6. * Time: 12:50 PM
  7. */
  8. require_once __DIR__.'/vendor/autoload.php';
  9. function cb0 () {
  10. return true;
  11. }
  12. function cb1($alpha) {
  13. if($alpha < 0) {
  14. return true;
  15. }
  16. return false;
  17. }
  18. function cb2($alpha) {
  19. if($alpha == 0) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. function cb3($alpha) {
  25. if($alpha > 0) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. function cb4($beta) {
  31. if($beta < 0) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. function cb5($beta) {
  37. if($beta > 0) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. function step1 () {
  43. echo "I'm step one".PHP_EOL;
  44. }
  45. function step2 () {
  46. echo "I'm step two".PHP_EOL;
  47. }
  48. function step3 () {
  49. echo "I'm step three".PHP_EOL;
  50. }
  51. function step4 () {
  52. echo "I'm step four".PHP_EOL;
  53. }
  54. function step5 () {
  55. echo "I'm step five".PHP_EOL;
  56. }
  57. function step6 () {
  58. echo "I'm step six".PHP_EOL;
  59. }
  60. // create a new state machine
  61. $sm = new Fr\RetailMeNot\Cloud\StateMachine\StateMachine();
  62. // affect a new environment
  63. $sm->setProperty('alpha', 1);
  64. $sm->setProperty('beta', -1);
  65. // create states
  66. $start = new Fr\RetailMeNot\Cloud\StateMachine\State('START', 'Fr\RetailMeNot\Cloud\StateMachine\CallbackRegistry::startCallback');
  67. $step1 = new Fr\RetailMeNot\Cloud\StateMachine\State('step1', 'step1');
  68. $step2 = new Fr\RetailMeNot\Cloud\StateMachine\State('step2', 'step2');
  69. $step3 = new Fr\RetailMeNot\Cloud\StateMachine\State('step3', 'step3');
  70. $step4 = new Fr\RetailMeNot\Cloud\StateMachine\State('step4', 'step4');
  71. $step5 = new Fr\RetailMeNot\Cloud\StateMachine\State('step5', 'step5');
  72. $step6 = new Fr\RetailMeNot\Cloud\StateMachine\State('step6', 'step6');
  73. $stop = new Fr\RetailMeNot\Cloud\StateMachine\State('STOP', 'Fr\RetailMeNot\Cloud\StateMachine\CallbackRegistry::stopCallback');
  74. // create transition
  75. $t0 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t0', 'cb0');
  76. $t1 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t1', 'cb1', array('alpha'));
  77. $t2 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t2', 'cb2', array('alpha'));
  78. $t3 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t3', 'cb3', array('alpha'));
  79. $t4 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t4', 'cb4', array('beta'));
  80. $t5 = new Fr\RetailMeNot\Cloud\StateMachine\Transition('t5', 'cb5', array('beta'));
  81. // create graph
  82. $sm->addStep($start, $step1, $t0);
  83. // alpha choice
  84. $sm->addStep($step1, $step2, $t1);
  85. $sm->addStep($step1, $step3, $t2);
  86. $sm->addStep($step1, $step4, $t3);
  87. // step2 loop
  88. $sm->addStep($step2, $step1, $t0);
  89. // step3 end
  90. $sm->addStep($step3, $stop, $t0);
  91. // beta choice
  92. $sm->addStep($step4, $step5, $t4);
  93. $sm->addStep($step4, $step6, $t5);
  94. // ends
  95. $sm->addStep($step5, $stop, $t0);
  96. $sm->addStep($step6, $stop, $t0);
  97. // define current step and end state
  98. $sm->setCurretState($start);
  99. $sm->setEndState($stop);
  100. // test
  101. $sm->start();
  102. while($sm->next());