1
0

u_fsm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Finite state machine skeleton
  4. * embodying a Moore FSM
  5. * (c) 2006 Ouest Systèmes Informatiques (OSI)
  6. * Licensed under the CeCILL 2.0 license
  7. *
  8. * $Id: u_fsm.php,v 1.2 2007-04-28 20:02:50 marand Exp $
  9. */
  10. require_once('misc.php'); // for func_name()
  11. /**
  12. * This class defines a possible outcome for a given FSM transition
  13. *
  14. */
  15. class fsm_result
  16. {
  17. /**
  18. * The name of the state to which the FSM must change. If NULL, do not change
  19. * the current state.
  20. *
  21. * @var string
  22. */
  23. public $fsm_state;
  24. /**
  25. * The name of an event to be fired after the state change has been applied
  26. *
  27. * @var string
  28. */
  29. public $fsm_action;
  30. /**
  31. * @param string $state
  32. * @param string $action
  33. * @return void
  34. */
  35. public function __construct($state = NULL, $action = NULL)
  36. {
  37. $this->fsm_state = $state;
  38. $this->fsm_action = $action;
  39. }
  40. }
  41. abstract class fsm
  42. {
  43. /**
  44. * the current state of the object
  45. * @var string
  46. */
  47. protected $f_state;
  48. /**
  49. * Transitions holds the transitions table
  50. * state1
  51. * event1
  52. * result1
  53. * state_name|fsm_result
  54. * event2
  55. * ...
  56. * ..
  57. * ..
  58. * @var array
  59. */
  60. protected $f_transitions = null;
  61. /**
  62. * constructor initializes the FSM to the first
  63. * state in the transitions table
  64. * @return void
  65. */
  66. public function __construct()
  67. {
  68. $this->_check_transitions();
  69. reset($this->f_transitions);
  70. $x = each($this->f_transitions);
  71. $x = $x[0];
  72. $this->f_state = $x;
  73. }
  74. /**
  75. * make sure a transitions graph has been defined
  76. * @return void
  77. */
  78. private function _check_transitions()
  79. {
  80. if (!isset($this->f_transitions))
  81. throw new Exception('No FSM processing is allowed without a transitions table');
  82. }
  83. /**
  84. * getter for f_state
  85. */
  86. public function get_state()
  87. {
  88. return $this->f_state;
  89. }
  90. /**
  91. * return the list of events accepted in the current state
  92. * @return array
  93. */
  94. public function get_accepted_events()
  95. {
  96. $this->_check_transitions();
  97. $events = array_keys($this->f_transitions[$this->f_state]);
  98. // echo func_name() . ": " . print_r($events, true). "\n";
  99. return $events;
  100. }
  101. /**
  102. * return the list of outcome accepted in the current state for the give event
  103. * @param string $event_name
  104. * @param mixed $outcome
  105. * @return array
  106. */
  107. public function get_accepted_outcomes($event_name)
  108. {
  109. // echo func_name() . "\n";
  110. $this->_check_transitions();
  111. if (!$this->is_event_allowed($event_name))
  112. throw new Exception(func_name() . ": event \"$event_name\" not allowed in state \"$this->f_state\"\n");
  113. $outcomes = array_keys($this->f_transitions[$this->f_state][$event_name]);
  114. //echo "outcomes for event $event_name: " . print_r($outcomes, true) . "\n";
  115. return $outcomes;
  116. }
  117. /**
  118. * is this event accepted in the current state
  119. * the FSM is in ?
  120. *
  121. * @param string $event_name
  122. * @return boolean
  123. */
  124. public function is_event_allowed($event_name)
  125. {
  126. // echo func_name() . "($event_name)";
  127. $this->_check_transitions();
  128. $ret = in_array($event_name, $this->get_accepted_events());
  129. // echo ", result = <$ret>\n";
  130. return $ret;
  131. }
  132. /**
  133. * is a given outcome available for a given event,
  134. * considering the current context ?
  135. *
  136. * @param string $event_name
  137. * @param mixed $outcome
  138. * @return boolean
  139. */
  140. public function is_outcome_allowed($event_name, $outcome)
  141. {
  142. $this->_check_transitions();
  143. if (!$this->is_event_allowed($event_name))
  144. return false;
  145. $ret = array_key_exists($outcome, $this->get_accepted_outcomes($event_name));
  146. return $ret;
  147. }
  148. /**
  149. * apply an event, and the resulting event chain if needed
  150. *
  151. * @param string $event_name
  152. * @param array $params the
  153. * @return string resulting state
  154. */
  155. public function apply_event($event_name)
  156. {
  157. //echo func_name() . "\n";
  158. do {
  159. $result = $this->apply_simple_event($event_name);
  160. $event_name = $result->fsm_action; // can be NULL
  161. } while($event_name);
  162. return $result;
  163. }
  164. /**
  165. * Helper for apply_event that does not implement the post-transition action
  166. *
  167. * @param string $event_name
  168. * @return fsm_result
  169. * @see apply_event()
  170. */
  171. private function apply_simple_event($event_name)
  172. {
  173. //echo func_name() . "\n";
  174. if (! $this->is_event_allowed($event_name))
  175. throw new Exception(func_name()
  176. . ": Event \"$event_name\" not accepted in current state \"$this->f_state\"");
  177. $method_name = "f_$event_name";
  178. $outcomes = $this->get_accepted_outcomes($event_name);
  179. $result = $this->$method_name();
  180. if (!is_object($result))
  181. {
  182. $result = new fsm_result($result, NULL);
  183. }
  184. if (!in_array($result->fsm_state, $outcomes))
  185. throw new Exception(func_name()
  186. . ": event guard. Transition on \"$event_name\" return invalid result: "
  187. . var_dump($result)
  188. . "\n");
  189. $this->f_state = $this->f_transitions[$this->f_state][$event_name][$result->fsm_state];
  190. // echo func_name() . ", new state: $this->f_state, action: $result->fsm_action\n";
  191. return $result;
  192. }
  193. }