自动状态机,FSM

来源:互联网 发布:淘宝购物流程步骤 编辑:程序博客网 时间:2024/05/22 14:49

这个东西很有技术含量吧,我下载了一个smc,自动状态机生成工具。以前是因为它能自动生成C++代码,所以我下载它,也没怎么用过。

最近使用了state模式,也就顺便熟悉了一下这个东西。其实来说,这个东西就是一个规范,或者算是一个和正则表达式类似的东西。

把前两天看到的python有限状态机FSM,蝶恋花兄弟的大作。自己在SMC上试着写了写。呵呵,顺利通过了,有些晚了,贴上代码算了。

//car.sm
%{
//
// Copyright (c) 2005 Acme, Inc.
// All rights reserved.
//
// Acme - a name you can trust!
//
// Author: Wil E. Coyote (Hungericus Vulgarus)
//
%}

// This FSM works for the Task class only and only the Task
// class may instantiate it.

%start Map1::Stopped
%class car

%map Map1
%%
//  State      
//  Transition      End State       Action(s)
Stopped
{
    run             Running         {driveon();}
    stop            nil             {}
}

Running
{
    run             nil             {}
    stop            Stopped         {driveoff();}
}

%%

//appclass.py

#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is State Machine Compiler (SMC).
#
# The Initial Developer of the Original Code is Charles W. Rapp.
# Portions created by Charles W. Rapp are
# Copyright (C) 2000 - 2005 Charles W. Rapp.
# All Rights Reserved.
#
# Contributor(s):
#       Port to Python by Francois Perrad, francois.perrad@gadz.org
#
# Function
#   Main
#
# Description
#  This routine starts the finite state machine running.
#
# RCS ID
# $Id: AppClass.py,v 1.2 2005/06/03 19:58:28 cwrapp Exp $
#
# CHANGE LOG
# $Log: AppClass.py,v $
# Revision 1.2  2005/06/03 19:58:28  cwrapp
# Further updates for release 4.0.0
#
# Revision 1.1  2005/05/28 17:48:29  cwrapp
# Added Python examples 1 - 4 and 7.
#
#

import car_sm

class AppClass:

    def __init__(self):
        self._fsm = car_sm.car_sm(self)
        self._is_running = False

        # Uncomment to see debug output.
        #self._fsm.setDebugFlag(True)

    def CheckStatus(self, string):
        for c in string:
            if c == '0':
                self._fsm.run()
            elif c == '1':
                self._fsm.stop()
            else:
                pass                       
        return self._is_running

    def driveon(self):
        self._is_running = True

    def driveoff(self):
        self._is_running = False

// checkstatus.py


import sys
import time
import random
import AppClass

class World(object):
    def init(self):
        self.appobject = AppClass.AppClass()
        self.str = ""
       
    def run(self):
        while True:
            strtest = str(random.randint(0, 1))
            if self.appobject.CheckStatus(strtest) == False:
                result = "stopped"
                retcode = 1
            else:
                result = "running"
            print 'The string "%s" is %s./n' % (strtest, result)            
           
            time.sleep(0.5)

if __name__ == "__main__":
    world = World()
    world.init()
    world.run()

就这么些东西,挺简单,不过还是需要绕不少湾。至少从我认为的角度,就需要不断适应这样的东西。

其实SMC就是帮你实现状态之间的约束,比如QQ的上线隐身等状态,至于怎么操作,回调给appclass的两个你提前编写在sm里面的函数上了。总算省了点儿代码,不过还是需要一些耐心研究一下,分清每个状态到下一个状态,两个状态间的过度,还有就是需要的动作。

编程?没有止境,还是先睡觉吧。