2
0

FSM.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Search_Lucene_FSMAction */
  21. require_once 'Zend/Search/Lucene/FSMAction.php';
  22. /**
  23. * Abstract Finite State Machine
  24. *
  25. * Take a look on Wikipedia state machine description: http://en.wikipedia.org/wiki/Finite_state_machine
  26. *
  27. * Any type of Transducers (Moore machine or Mealy machine) also may be implemented by using this abstract FSM.
  28. * process() methods invokes a specified actions which may construct FSM output.
  29. * Actions may be also used to signal, that we have reached Accept State
  30. *
  31. * @category Zend
  32. * @package Zend_Search_Lucene
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Search_Lucene_FSM
  37. {
  38. /**
  39. * Machine States alphabet
  40. *
  41. * @var array
  42. */
  43. private $_states = array();
  44. /**
  45. * Current state
  46. *
  47. * @var integer|string
  48. */
  49. private $_currentState = null;
  50. /**
  51. * Input alphabet
  52. *
  53. * @var array
  54. */
  55. private $_inputAphabet = array();
  56. /**
  57. * State transition table
  58. *
  59. * [sourceState][input] => targetState
  60. *
  61. * @var array
  62. */
  63. private $_rules = array();
  64. /**
  65. * List of entry actions
  66. * Each action executes when entering the state
  67. *
  68. * [state] => action
  69. *
  70. * @var array
  71. */
  72. private $_entryActions = array();
  73. /**
  74. * List of exit actions
  75. * Each action executes when exiting the state
  76. *
  77. * [state] => action
  78. *
  79. * @var array
  80. */
  81. private $_exitActions = array();
  82. /**
  83. * List of input actions
  84. * Each action executes when entering the state
  85. *
  86. * [state][input] => action
  87. *
  88. * @var array
  89. */
  90. private $_inputActions = array();
  91. /**
  92. * List of input actions
  93. * Each action executes when entering the state
  94. *
  95. * [state1][state2] => action
  96. *
  97. * @var array
  98. */
  99. private $_transitionActions = array();
  100. /**
  101. * Finite State machine constructor
  102. *
  103. * $states is an array of integers or strings with a list of possible machine states
  104. * constructor treats fist list element as a sturt state (assignes it to $_current state).
  105. * It may be reassigned by setState() call.
  106. * States list may be empty and can be extended later by addState() or addStates() calls.
  107. *
  108. * $inputAphabet is the same as $states, but represents input alphabet
  109. * it also may be extended later by addInputSymbols() or addInputSymbol() calls.
  110. *
  111. * $rules parameter describes FSM transitions and has a structure:
  112. * array( array(sourseState, input, targetState[, inputAction]),
  113. * array(sourseState, input, targetState[, inputAction]),
  114. * array(sourseState, input, targetState[, inputAction]),
  115. * ...
  116. * )
  117. * Rules also can be added later by addRules() and addRule() calls.
  118. *
  119. * FSM actions are very flexible and may be defined by addEntryAction(), addExitAction(),
  120. * addInputAction() and addTransitionAction() calls.
  121. *
  122. * @param array $states
  123. * @param array $inputAphabet
  124. * @param array $rules
  125. */
  126. public function __construct($states = array(), $inputAphabet = array(), $rules = array())
  127. {
  128. $this->addStates($states);
  129. $this->addInputSymbols($inputAphabet);
  130. $this->addRules($rules);
  131. }
  132. /**
  133. * Add states to the state machine
  134. *
  135. * @param array $states
  136. */
  137. public function addStates($states)
  138. {
  139. foreach ($states as $state) {
  140. $this->addState($state);
  141. }
  142. }
  143. /**
  144. * Add state to the state machine
  145. *
  146. * @param integer|string $state
  147. */
  148. public function addState($state)
  149. {
  150. $this->_states[$state] = $state;
  151. if ($this->_currentState === null) {
  152. $this->_currentState = $state;
  153. }
  154. }
  155. /**
  156. * Set FSM state.
  157. * No any action is invoked
  158. *
  159. * @param integer|string $state
  160. * @throws Zend_Search_Exception
  161. */
  162. public function setState($state)
  163. {
  164. if (!isset($this->_states[$state])) {
  165. require_once 'Zend/Search/Exception.php';
  166. throw new Zend_Search_Exception('State \'' . $state . '\' is not on of the possible FSM states.');
  167. }
  168. $this->_currentState = $state;
  169. }
  170. /**
  171. * Get FSM state.
  172. *
  173. * @return integer|string $state|null
  174. */
  175. public function getState()
  176. {
  177. return $this->_currentState;
  178. }
  179. /**
  180. * Add symbols to the input alphabet
  181. *
  182. * @param array $inputAphabet
  183. */
  184. public function addInputSymbols($inputAphabet)
  185. {
  186. foreach ($inputAphabet as $inputSymbol) {
  187. $this->addInputSymbol($inputSymbol);
  188. }
  189. }
  190. /**
  191. * Add symbol to the input alphabet
  192. *
  193. * @param integer|string $inputSymbol
  194. */
  195. public function addInputSymbol($inputSymbol)
  196. {
  197. $this->_inputAphabet[$inputSymbol] = $inputSymbol;
  198. }
  199. /**
  200. * Add transition rules
  201. *
  202. * array structure:
  203. * array( array(sourseState, input, targetState[, inputAction]),
  204. * array(sourseState, input, targetState[, inputAction]),
  205. * array(sourseState, input, targetState[, inputAction]),
  206. * ...
  207. * )
  208. *
  209. * @param array $rules
  210. */
  211. public function addRules($rules)
  212. {
  213. foreach ($rules as $rule) {
  214. $this->addrule($rule[0], $rule[1], $rule[2], isset($rule[3])?$rule[3]:null);
  215. }
  216. }
  217. /**
  218. * Add symbol to the input alphabet
  219. *
  220. * @param integer|string $sourceState
  221. * @param integer|string $input
  222. * @param integer|string $targetState
  223. * @param Zend_Search_Lucene_FSMAction|null $inputAction
  224. * @throws Zend_Search_Exception
  225. */
  226. public function addRule($sourceState, $input, $targetState, $inputAction = null)
  227. {
  228. if (!isset($this->_states[$sourceState])) {
  229. require_once 'Zend/Search/Exception.php';
  230. throw new Zend_Search_Exception('Undefined source state (' . $sourceState . ').');
  231. }
  232. if (!isset($this->_states[$targetState])) {
  233. require_once 'Zend/Search/Exception.php';
  234. throw new Zend_Search_Exception('Undefined target state (' . $targetState . ').');
  235. }
  236. if (!isset($this->_inputAphabet[$input])) {
  237. require_once 'Zend/Search/Exception.php';
  238. throw new Zend_Search_Exception('Undefined input symbol (' . $input . ').');
  239. }
  240. if (!isset($this->_rules[$sourceState])) {
  241. $this->_rules[$sourceState] = array();
  242. }
  243. if (isset($this->_rules[$sourceState][$input])) {
  244. require_once 'Zend/Search/Exception.php';
  245. throw new Zend_Search_Exception('Rule for {state,input} pair (' . $sourceState . ', '. $input . ') is already defined.');
  246. }
  247. $this->_rules[$sourceState][$input] = $targetState;
  248. if ($inputAction !== null) {
  249. $this->addInputAction($sourceState, $input, $inputAction);
  250. }
  251. }
  252. /**
  253. * Add state entry action.
  254. * Several entry actions are allowed.
  255. * Action execution order is defined by addEntryAction() calls
  256. *
  257. * @param integer|string $state
  258. * @param Zend_Search_Lucene_FSMAction $action
  259. */
  260. public function addEntryAction($state, Zend_Search_Lucene_FSMAction $action)
  261. {
  262. if (!isset($this->_states[$state])) {
  263. require_once 'Zend/Search/Exception.php';
  264. throw new Zend_Search_Exception('Undefined state (' . $state. ').');
  265. }
  266. if (!isset($this->_entryActions[$state])) {
  267. $this->_entryActions[$state] = array();
  268. }
  269. $this->_entryActions[$state][] = $action;
  270. }
  271. /**
  272. * Add state exit action.
  273. * Several exit actions are allowed.
  274. * Action execution order is defined by addEntryAction() calls
  275. *
  276. * @param integer|string $state
  277. * @param Zend_Search_Lucene_FSMAction $action
  278. */
  279. public function addExitAction($state, Zend_Search_Lucene_FSMAction $action)
  280. {
  281. if (!isset($this->_states[$state])) {
  282. require_once 'Zend/Search/Exception.php';
  283. throw new Zend_Search_Exception('Undefined state (' . $state. ').');
  284. }
  285. if (!isset($this->_exitActions[$state])) {
  286. $this->_exitActions[$state] = array();
  287. }
  288. $this->_exitActions[$state][] = $action;
  289. }
  290. /**
  291. * Add input action (defined by {state, input} pair).
  292. * Several input actions are allowed.
  293. * Action execution order is defined by addInputAction() calls
  294. *
  295. * @param integer|string $state
  296. * @param integer|string $input
  297. * @param Zend_Search_Lucene_FSMAction $action
  298. */
  299. public function addInputAction($state, $inputSymbol, Zend_Search_Lucene_FSMAction $action)
  300. {
  301. if (!isset($this->_states[$state])) {
  302. require_once 'Zend/Search/Exception.php';
  303. throw new Zend_Search_Exception('Undefined state (' . $state. ').');
  304. }
  305. if (!isset($this->_inputAphabet[$inputSymbol])) {
  306. require_once 'Zend/Search/Exception.php';
  307. throw new Zend_Search_Exception('Undefined input symbol (' . $inputSymbol. ').');
  308. }
  309. if (!isset($this->_inputActions[$state])) {
  310. $this->_inputActions[$state] = array();
  311. }
  312. if (!isset($this->_inputActions[$state][$inputSymbol])) {
  313. $this->_inputActions[$state][$inputSymbol] = array();
  314. }
  315. $this->_inputActions[$state][$inputSymbol][] = $action;
  316. }
  317. /**
  318. * Add transition action (defined by {state, input} pair).
  319. * Several transition actions are allowed.
  320. * Action execution order is defined by addTransitionAction() calls
  321. *
  322. * @param integer|string $sourceState
  323. * @param integer|string $targetState
  324. * @param Zend_Search_Lucene_FSMAction $action
  325. */
  326. public function addTransitionAction($sourceState, $targetState, Zend_Search_Lucene_FSMAction $action)
  327. {
  328. if (!isset($this->_states[$sourceState])) {
  329. require_once 'Zend/Search/Exception.php';
  330. throw new Zend_Search_Exception('Undefined source state (' . $sourceState. ').');
  331. }
  332. if (!isset($this->_states[$targetState])) {
  333. require_once 'Zend/Search/Exception.php';
  334. throw new Zend_Search_Exception('Undefined source state (' . $targetState. ').');
  335. }
  336. if (!isset($this->_transitionActions[$sourceState])) {
  337. $this->_transitionActions[$sourceState] = array();
  338. }
  339. if (!isset($this->_transitionActions[$sourceState][$targetState])) {
  340. $this->_transitionActions[$sourceState][$targetState] = array();
  341. }
  342. $this->_transitionActions[$sourceState][$targetState][] = $action;
  343. }
  344. /**
  345. * Process an input
  346. *
  347. * @param mixed $input
  348. * @throws Zend_Search_Exception
  349. */
  350. public function process($input)
  351. {
  352. if (!isset($this->_rules[$this->_currentState])) {
  353. require_once 'Zend/Search/Exception.php';
  354. throw new Zend_Search_Exception('There is no any rule for current state (' . $this->_currentState . ').');
  355. }
  356. if (!isset($this->_rules[$this->_currentState][$input])) {
  357. require_once 'Zend/Search/Exception.php';
  358. throw new Zend_Search_Exception('There is no any rule for {current state, input} pair (' . $this->_currentState . ', ' . $input . ').');
  359. }
  360. $sourceState = $this->_currentState;
  361. $targetState = $this->_rules[$this->_currentState][$input];
  362. if ($sourceState != $targetState && isset($this->_exitActions[$sourceState])) {
  363. foreach ($this->_exitActions[$sourceState] as $action) {
  364. $action->doAction();
  365. }
  366. }
  367. if (isset($this->_inputActions[$sourceState]) &&
  368. isset($this->_inputActions[$sourceState][$input])) {
  369. foreach ($this->_inputActions[$sourceState][$input] as $action) {
  370. $action->doAction();
  371. }
  372. }
  373. $this->_currentState = $targetState;
  374. if (isset($this->_transitionActions[$sourceState]) &&
  375. isset($this->_transitionActions[$sourceState][$targetState])) {
  376. foreach ($this->_transitionActions[$sourceState][$targetState] as $action) {
  377. $action->doAction();
  378. }
  379. }
  380. if ($sourceState != $targetState && isset($this->_entryActions[$targetState])) {
  381. foreach ($this->_entryActions[$targetState] as $action) {
  382. $action->doAction();
  383. }
  384. }
  385. }
  386. public function reset()
  387. {
  388. if (count($this->_states) == 0) {
  389. require_once 'Zend/Search/Exception.php';
  390. throw new Zend_Search_Exception('There is no any state defined for FSM.');
  391. }
  392. $this->_currentState = $this->_states[0];
  393. }
  394. }