2019独角兽企业重金招聘Python工程师标准>>>

FSM(有限状态机)是啥?

有限状态机(英语:finite-state machine,缩写:FSM)又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。

详细请看维基百科,FSM(http://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA)。自动机编程(http://zh.wikipedia.org/wiki/%E8%87%AA%E5%8A%A8%E6%9C%BA%E7%BC%96%E7%A8%8B)

简单来说,这玩意用于事物有多种状态,主要在状态之间变来变去的场景,你很想用个goto跳到上面的某个if的地方。 比如一个魔方,比如形式语言分析,比如魔兽世界战士战斗姿态防御姿态狂暴姿态角斗士姿态,比如模拟一堆电路元件…… 通常对于此类问题,如果输入条件是可以完全预知的,那么可以通过写while里套一堆if then,或者switch case来处理,或者通过构造一个“图”的数据结构更弹性些。(当然也有某些比较狂热的方式比如弄无数层对象来折腾程序员自己和机器)但是这些方式不够接近机器与数学的本质,也难以应对未知条件。 今天,为寻求远古强大的元素力量,我将带你深入不毛之地,研究研究php里pear-FSM。

下载地址是

http://pear.php.net/package/FSM

作者后来也把项目迁移到了github,目前版本都是1.3.1

https://github.com/pear/FSM

Current Release  
1.3.1 (stable) was released on 2011-02-16 (Changelog)

what? 看起来距今有些年代?没关系,C语言还在,很多60年代的技术,80年代的技术也至今经典不息。我们研究的是远古力量,内心要平静,不要有私心杂念。

下载后解开包看看。首先要看docs\guide.txt,请用能区分换行的文本编辑器看,比如notepad++。

这段英文主要说了这么回事情:

Building a Finite State Machine
===============================

The first step in building a Finite State Machine involves listing the finite
set of states.  Then, all of the permissible transitions between these states
must be defined.  A symbol and an optional callback function are associated
with each transition.  The input processing routine will attempt to match its
current symbol against the list of registered transitions.  If a transition
from the current state using that symbol is found, the machine will move to
the new state specified by the transition and, if one has been specified, the
associated callback function will be invoked.

这玩意咋用? 首先要弄出个带点起码状态的状态机。然后定义允许的状态转换方式。用符号+回调函数方式来定义转换。 定义好以后, 输入的信息就会经过符号的匹配来对应的转换。(强心针:晕了? 看看这句 $state=$arr['mySymbol']; 这样就是一种简单的符号-状态匹配,当然本文里实现的更复杂点点,不过也是通过数组查询来调用对应函数) 如果输入的符号,对应当前状态可以转换到别的状态,就转状态;有指明对应状态转换对应的回调函数就调用这个函数。

(强心针2: 又晕了? 说白就是这么回事。假如你脸朝向是个状态,现在向左(状态1)看着自己的屏幕。给自己下了死命令,除非右边来美女(符号)否则不扭头(状态转换)。然后右边来了个男的(符号),死命令查找不到这个性取向,不扭头。来了个美女(符号),查询死命令有这个状态转换,则扭头(状态转换),并且调用定义好的回调函数(打招呼,吹口哨,耍流氓……自便)

Creating a New FSM Object
-------------------------
Start by including the FSM package in your script::

下面是怎么用这个玩意的具体实战。第一步,php require会用?下面这句是php的,把fsm.php require进来。

require 'FSM.php';

When constructing a new FSM object, you must specify the machine's initial
state and provide a payload variable.  The payload will be passed to all of
the callback functions, supplying them with state information without
(ab)using global variables.

然后new 个FSM 对象出来,这new不能随便乱new, 脖子朝向不能null是吧? 得new出来个初始状态和负载变量。

负载变量用来把状态信息传递给所有回调函数,并且不用全局变量。说白了,看人用自己眼睛看,不要借助其他人的目光。

In this example, we pass an array representing a stack as the payload.  The
machine's initial state is set to ``START``.

下面来个例子,弄个数组当负载变量,初始化状态为“开始”
::

$stack = array();
    $fsm = new FSM('START', $stack);

如此简易的php代码看起来蛮轻松的。

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``
and from ``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');

从代码和上面说到的要做的事情,可以猜到

addTransition的参数,第2,3就是表示从哪个状态到哪个状态。第4个参数是回调函数名字。

Our machine is now aware of three states (``START``, ``MIDDLE``, and ``END``)
and two symbols (``FIRST`` and ``SECOND``).  Two transitions (``START`` to
``MIDDLE`` and ``MIDDLE`` to ``END``) have been defined and associated with
callbacks.  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.

现在我们的状态机有了3个状态(开始,中间过程,结束),2个符号(FIREST和SECEND),2个状态转换(开始->中间过程,中间过程->结束),并且有了状态转换对应的回调函数。接下来是过程转换实例:

::

$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 Transition

这样就是先后执行了从开始到中间过程,从中间过程到结束两个过程的转换。

Setting Default Transitions
---------------------------
Now we'll set up a default transition.  This transition will be used whenever
the processing routine cannot find a better match for the current state and
symbol.  For our example, we'll consider this an error and print a warning for
the user.

指定默认转换

现在我们来设定个默认转换。默认转换用来在符号匹配不了时调用(switch..case..default 懂?)。在我们例子里

默认认为是默认转换会出发错误,并且输出警告给用户。

::

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 initial
state, the default transition will be used and the error callback will be
invoked.  The ``FIRST`` transition will work as expected, however, because the
machine will still be in the ``START`` state.

这样一来,因为在开始状态下,没有定义SECOND对应的从开始状态到其他状态的转换,所以就报错了。接下来由于状态回归了“开始”,所以调用“FIRST”符号对应转换是可以按期望工作的。

Plotting a State Machine
========================

The FSM package optionally supports the ability to plot a machine's states
with the help of the `Image_GraphViz`_ package.  Doing so is as simple as
creating a new ``FSM_GraphViz`` object using an existing state machine
instance and then exporting the graph.

::

接下来是状态可视化工作。这个FSM包支持用"Image_GraphViz"包来输出状态机情况。只要new个FSM_GraphViz对象出来然后调用其实例的export就可以输出个图来看看了。

require_once 'FSM/GraphViz.php';
    $converter = new FSM_GraphViz($fsm);
    $graph = $converter->export();

The resulting graph object is an ``Image_GraphViz`` instance.  To export the
graph as an image, use the ``image()`` method::

$graph->image('png');

This will produce an image similar to the following:

.. figure:: graphviz.png
   :alt: Example State Machine Plot

Consult the `Image_GraphViz documentation`_ for additional usage information.

.. _Image_GraphViz: http://pear.php.net/package/Image_GraphViz
.. _Image_GraphViz documentation: http://pear.php.net/package/Image_GraphViz/docs

下面是联系方式,有想和作者联系的请自便。

Development and Support
=======================

Reporting Problems and Suggestions
----------------------------------
If you run into a problem or would like to make a suggestion, please use the
`PEAR Bug Tracker`_.  Feel free to contact me directly for other issues, but
please try to use the bug tracker whenever possible so that others in the
community will benefit from your feedback and my responses.

- `Open Bugs`_
- `Report a New Bug`_

.. _PEAR Bug Tracker: http://pear.php.net/bugs/
.. _Open Bugs: http://pear.php.net/package/FSM/bugs
.. _Report a New Bug: http://pear.php.net/bugs/report.php?package=FSM

Coming Soon
-----------
This section contains a list of "todo" items that will hopefully be addressed
in future releases.

- *No items at this time.*

If you have feature suggestions, please submit them using the `PEAR Bug
Tracker`_.

.. vim: syntax=rst tabstop=4 shiftwidth=4 softtabstop=4 expandtab textwidth=78:

接下来,来玩玩例子。

转载于:https://my.oschina.net/meikaiyuan/blog/351769

[PHP打野] 对pear-FSM的研究(一)基本了解相关推荐

  1. DNA甲基化重编程为红梨中光诱导的花青素生物合成提供了见解

    期刊:Plant Science 影响因子:5.363 发表时间:2022 样本类型:果皮 客户单位:南京农业大学 凌恩生物客户南京农业大学吴俊团队发表在<Plant Science>上的 ...

  2. PHP 4.4.7 中用 PEAR 类库操作 ZIP 压缩文件

    运行 pear install Archive_Tar 命令可以安装 PEAR 的 Tar 的操作类,但是 Tar 文件是个打包归档文件,并没有压缩而使文件占用的空间减少.而运行 pear insta ...

  3. 微博平台StatusNet研究(4):快速安装

    StatusNet研究系列 StatusNet研究(1):介绍 StatusNet研究(2):基本安装 StatusNet研究(3):友好URL与OpenID支持 StatusNet研究(4):快速安 ...

  4. c语言状态机_【C语言】有限状态机FSM

    有限状态状态机FSM(finite state machine)是为研究有限内存的计算过程和某些语言类而抽象出的一种计算模型.有限状态自动机拥有有限数量的状态,每个状态可以迁移到零个或多个状态,输入字 ...

  5. PHP包管理器PEAR 中爆多个缺陷可发动供应链攻击,已潜伏15年

     聚焦源代码安全,网罗国内外最新资讯! 编译:代码卫士团队 专栏·供应链安全 数字化时代,软件无处不在.软件如同社会中的"虚拟人",已经成为支撑社会正常运转的最基本元素之一,软件的 ...

  6. 什么是现场服务管理系统(FSM)?有什么好处?

    一.什么是现场服务管理系统(FSM)? FSM系统,也称为现场服务管理,其核心目的就是帮助公司更好地管理现场资源和运营.像企业原来的一些流程:开票.付款.订单管理和客户管理等等,均可以通过现场服务管理 ...

  7. 动植物代谢最新研究进展(2021年7月)

    01 Metabolites︱研究玉米棒对干旱胁迫响应的代谢组学方法 由一路向北编译 来自纽约大学的Isabella Gaffney和Jonathan Brett Sallach于2021年7月3日在 ...

  8. 《王者荣耀》伤害计算(数值研究)

    这个这么火的Moba手游,突然对它的伤害计算方式产生了兴趣,研究了下. 一.伤害计算方式 由防御数值越高免伤比例越高来看,战斗公式很有可能用的是乘法公式(MOBA游戏大部分用的均为乘法公式): 损血 ...

  9. League-X:深度学习+英雄联盟,英雄联盟小地图识别器,标定对面打野位置

    League-X:使用深度学习的英雄联盟小地图辅助器 简介 本人是一个英雄联盟爱好者,同时对人工智能,深度学习之类的课题很感兴趣,去年心血来潮,使用图像识别写了一个识别英雄联盟小地图的代码: 这是第一 ...

最新文章

  1. html xhtml and css,HTML与XHTML的重要区别
  2. 【机器学习实战】第7章 集成方法(随机森林和 AdaBoost)
  3. 字符编码在python中的处理与储存_python----字符编码与文件处理
  4. Python中的序列操作
  5. Linux chmod权限详解
  6. fd 句柄_文件描述符FD的含义/文件句柄
  7. 中国及中国各省矢量地图数据下载
  8. 人工雨量计_自动站与人工站遥测雨量计降水量对比分析
  9. PIN track 1000x1000's result
  10. 百度云网盘一直显示“下载请求中”,一个 解决办法
  11. ubuntu的不同版本
  12. 资深黄金专家李鑫:独创7套算点理论震惊市场
  13. HBuilderX发布小程序打不开微信小程序开发工具
  14. 软件测试面试中都会问到哪些关于Python的问题?
  15. 盘点波卡生态潜力项目 | 跨链特性促进多赛道繁荣
  16. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day5,java面向对象程序设计教程课后答案
  17. JY播放器【蜻蜓FM电脑端,附带下载功能】
  18. XPS 关闭 电源警告
  19. Linux开发环境相关包的下载路径
  20. android启动时加载引导图片并全屏显示

热门文章

  1. ASP.NET Core 2 学习笔记(四)依赖注入
  2. CentOS6软件包管理
  3. win7笔记本设置wifi热点
  4. C++中栈和堆上建立对象的区别
  5. C笔记(2014-12备份)
  6. java对于文件传输时---编码格式的一些设置方法
  7. 【新产品发布】【iHMI43 智能液晶模块 2013 版】
  8. ip dhcp snooping
  9. 使用EasyRecovery 恢复被误删除的数据
  10. 记录某项目中的踩坑与解决(持续更新)