自动状态机,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里面的函数上了。总算省了点儿代码,不过还是需要一些耐心研究一下,分清每个状态到下一个状态,两个状态间的过度,还有就是需要的动作。
编程?没有止境,还是先睡觉吧。
- 自动状态机,FSM
- 表驱动的自动状态机(FSM)
- 状态机FSM
- FSM状态机
- 使用ModelSim自动生成状态机FSM的状态转换图
- 状态机FSM代码框架
- 可视化的状态机(FSM)
- 状态机FSM的描述方法
- 三消游戏FSM状态机设计图
- Unity3d编程,简易状态机(fsm)
- 通过Synplify Pro查看FSM状态机
- C# FSM (仿Unity 中的Mecanim动画系统的状态机)
- Unity3D游戏开发框架-FSM状态机的实现
- FSM(状态机)、HFSM(分层状态机)、BT(行为树)的区别
- FSM(状态机)、HFSM(分层状态机)、BT(行为树)的区别
- FSM(状态机)、HFSM(分层状态机)、BT(行为树)的区别
- FSM(状态机)、HFSM(分层状态机)、BT(行为树)的区别
- FSM
- 逝者安息,生者坚强!
- 趣味题系列(3):A升和B升的杯能否倒出C升的水
- Eclipse和MyEclipse的里程碑总结
- 反思自己用过的编程语言
- Web开发工具集
- 自动状态机,FSM
- 揭露丑恶:想不到美国股民也这么悲惨啊
- 编译基础
- 趣味题系列(4):
- 关于梧桐树下
- ExtJs 资料
- JAVA
- 我有一个强大的祖国(5月14日作于成都市抗灾救灾指挥部 成都市林业和园林管理局副局长 叶浪)
- this