guide.txt
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 文本 代码 · 共 121 行
TXT
121 行
================= The FSM Package=================-------------------- User Documentation--------------------:Author: Jon Parise:Contact: jon@php.net:Date: $Date: 2004/01/30 08:55:17 $:Revision: $Revision: 1.1 $.. contents:: Contents.. section-numbering::About the FSM Package=====================The FSM Package implements a `Finite State Machine`_. In addition tomaintaining state, this FSM also maintains a user-defined payload. thereforeeffectively making the machine a Push-Down Automata (a finite state machinewith memory).This code is based largely on Noah Spurrier's excellent `FSM Python class`_... _Finite State Machine: http://en.wikipedia.org/wiki/Finite_state_machine.. _FSM Python class: http://www.noah.org/python/FSM/Building a Finite State Machine===============================The first step in building a Finite State Machine involves listing the finiteset of states. Then, all of the permissible transitions between these statesmust be defined. A symbol and an optional callback function are associatedwith each transition. The input processing routine will attempt to match itscurrent symbol against the list of registered transitions. If it a transitionfrom the current state using that symbol is found, the current state will beupdated to the new state specified by the transition and, if one has beenspecified, the associated callback function will be invoked.Creating A New FSM Object-------------------------Start by including the FSM package in your script:: require 'FSM.php';When constructing a new FSM object, you must specify the machine's initialstate and provide a payload container. The payload will be passed to all ofthe callback functions, allowing you to provide them with state informationwithout (ab)using global variables.In this example, we pass an array representing a stack as the payload. Themachine's initial state is set to ``START``:: $stack = array(); $fsm = new FSM('START', $stack);Defining Transitions--------------------We'll need to define some transitions in order to make our machine useful.Let's assume our machine has two additional states: ``MIDDLE`` and ``END``.Here's how we would define transitions to move us from ``START`` to ``MIDDLE``to ``END``:: function FirstCallback($symbol, $payload) { echo "First Transition\n"; } function SecondCallback($symbol, $payload) { echo "Second Transition\n"; } $fsm->addTransition('FIRST', 'START', 'MIDDLE', 'FirstCallback'); $fsm->addTransition('SECOND', 'MIDDLE', 'END', 'SecondCallback');Our machine is now aware of three states (``START``, ``MIDDLE``, ``END``) andtwo symbols (``FIRST``, ``SECOND``). Two transitions (``START`` to``MIDDLE``, and ``MIDDLE`` to ``END``) have been defined and associated withcallbacks. The following code will process the symbols ``FIRST`` and``SECOND`` and move us from our initial state (``START``) through the``MIDDLE`` state to the ``END`` state:: $fsm->process('FIRST'); $fsm->process('SECOND');The processing routine will invoke our two callbacks along the way, as well,resulting in the following being printed:: First Transition Second TransitionSetting Default Transitions---------------------------Now we'll set up a default transition. This transition will be used wheneverthe processing routine cannot find a better match for the current state andsymbol. For our example, we'll consider this an error and print a warning forthe user:: function ErrorCallback($symbol, $payload) { echo "This symbol does not compute: $symbol\n"; } $fsm->setDefaultTransition('START', 'ErrorCallback');Now let's process our symbols in an unexcepted order:: $fsm->process('SECOND'); $fsm->process('FIRST');Because the ``SECOND`` transition doesn't specify ``START`` as its initialstate, the default transition will be used and the error callback will beinvoked. The ``FIRST`` transition will work as expected, however, because themachine will remain in the ``START`` state... vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab textwidth=78:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?