FSM(finite state machine)
2009-08-17 16:34
295 查看
基于面向对象的FSM实现技术
——用一个类实现FSM
class DoorFSM {
private:
States __Y;
std::queue<Event> __events;
void __processEvent( Event e );
protected:
virtual void enterOpened() = 0;
virtual void enterLocked() = 0;
virtual void enterUnlocked() = 0;
virtual void enterClosed() = 0;
public:
/* States */
enum States { Closed, Unlocked, Locked, Opened }; // states
/*Events*/
enum Event { Lock, Unlock, Open, Close };
/// Constructor
DoorFSM() { __Y = Opened; }
/// Destructor
virtual ~DoorFSM() {}
/** Get current FSM state
@returns current FSM state
*/
States currentState() { return __Y; }
/** Send event to FSM
Use this function to send event to DoorFSM After you call it given event will be handled, and, if some of transition conditions match, appropriate transition will be triggered, and currentState() will be changed. If this function is called during existing event handling process, given event will be added to pending event queue, and will be handled after current transition. See examples for details.
*/
void A( Event e );
};
void DoorFSM::__processEvent( Event e )
{
States yOld = __Y;
bool pass = false;
switch( __Y ) { //transitions
case Closed:
if( e == Open ) {
//outcome actions
__Y = Opened;
pass = true;
}
else if( e == Lock ) {
//outcome actions
__Y = Locked;
pass = true;
}
break;
case Unlocked:
if( e == Lock ) {
//outcome actions
__Y = Locked;
pass = true;
}
else if( e == Open ) {
//outcome actions
__Y = Opened;
pass = true;
}
break;
case Locked:
if( e == Unlock ) {
//outcome actions
__Y = Unlocked;
pass = true;
}
break;
case Opened:
if( e == Close ) {
//outcome actions
__Y = Closed;
pass = true;
}
break;
}
if( yOld == __Y && !pass ) { return; }
switch( __Y ) { // income actions
case Closed:
enterClosed();
break;
case Unlocked:
enterUnlocked();
break;
case Locked:
enterLocked();
break;
case Opened:
enterOpened();
break;
}
}
void DoorFSM::A( Event e )
{
bool __empty = __events.empty();
__events.push( e );
if( __empty ) {
while( !__events.empty() ) {
__processEvent( __events.front() );
__events.pop();
}
}
}
class DoorFSMLogic : public DoorFSM
{
protected:
virtual void enterOpened(){std::cout << "Enter Opened state." << std::endl;}
virtual void enterLocked() {std::cout << "Enter Closed state." << std::endl;}
virtual void enterUnlocked() {std::cout << "Enter Locked state." << std::endl;}
virtual void enterClosed() {std::cout << "Enter Unlocked state." << std::endl;}
};
测试程序
int main()
{
DoorFSMLogic door;
door.A(DoorFSM::Close);
door.A(DoorFSM::Lock);
door.A(DoorFSM::Unlock);
door.A(DoorFSM::Open);
}
——用一个类实现FSM
class DoorFSM {
private:
States __Y;
std::queue<Event> __events;
void __processEvent( Event e );
protected:
virtual void enterOpened() = 0;
virtual void enterLocked() = 0;
virtual void enterUnlocked() = 0;
virtual void enterClosed() = 0;
public:
/* States */
enum States { Closed, Unlocked, Locked, Opened }; // states
/*Events*/
enum Event { Lock, Unlock, Open, Close };
/// Constructor
DoorFSM() { __Y = Opened; }
/// Destructor
virtual ~DoorFSM() {}
/** Get current FSM state
@returns current FSM state
*/
States currentState() { return __Y; }
/** Send event to FSM
Use this function to send event to DoorFSM After you call it given event will be handled, and, if some of transition conditions match, appropriate transition will be triggered, and currentState() will be changed. If this function is called during existing event handling process, given event will be added to pending event queue, and will be handled after current transition. See examples for details.
*/
void A( Event e );
};
void DoorFSM::__processEvent( Event e )
{
States yOld = __Y;
bool pass = false;
switch( __Y ) { //transitions
case Closed:
if( e == Open ) {
//outcome actions
__Y = Opened;
pass = true;
}
else if( e == Lock ) {
//outcome actions
__Y = Locked;
pass = true;
}
break;
case Unlocked:
if( e == Lock ) {
//outcome actions
__Y = Locked;
pass = true;
}
else if( e == Open ) {
//outcome actions
__Y = Opened;
pass = true;
}
break;
case Locked:
if( e == Unlock ) {
//outcome actions
__Y = Unlocked;
pass = true;
}
break;
case Opened:
if( e == Close ) {
//outcome actions
__Y = Closed;
pass = true;
}
break;
}
if( yOld == __Y && !pass ) { return; }
switch( __Y ) { // income actions
case Closed:
enterClosed();
break;
case Unlocked:
enterUnlocked();
break;
case Locked:
enterLocked();
break;
case Opened:
enterOpened();
break;
}
}
void DoorFSM::A( Event e )
{
bool __empty = __events.empty();
__events.push( e );
if( __empty ) {
while( !__events.empty() ) {
__processEvent( __events.front() );
__events.pop();
}
}
}
class DoorFSMLogic : public DoorFSM
{
protected:
virtual void enterOpened(){std::cout << "Enter Opened state." << std::endl;}
virtual void enterLocked() {std::cout << "Enter Closed state." << std::endl;}
virtual void enterUnlocked() {std::cout << "Enter Locked state." << std::endl;}
virtual void enterClosed() {std::cout << "Enter Unlocked state." << std::endl;}
};
测试程序
int main()
{
DoorFSMLogic door;
door.A(DoorFSM::Close);
door.A(DoorFSM::Lock);
door.A(DoorFSM::Unlock);
door.A(DoorFSM::Open);
}
相关文章推荐
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(二段式)
- Are Osworkflow and Activiti based on Finite State Machine(FSM)?
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)
- FSM (Finite State Machine Framework)源码共享
- Finite State Machine
- Recommended FSM (Finite State Machine) Library for Java [closed]
- Finite State Machines (FSM)
- Finite State Machine
- The finite state machine
- Unity 有限状态机(Finite State Machine)的理解 与 实现简单的可插拔(Pluggable)AI脚本对象。
- 有限状态机(Finite State Machine)在游戏中的应用和实现
- 【Unity】Finite State Machine 有限状态机
- 有限状态机——The finite state machine
- Finite State Machine 是什么?
- 译:Finite State Machine Datapath Design, Optimization, and Implementation(一)
- Finite State Machine library Introduction
- Finite State Machine library V1.0 Release
- Finite State Machine 有限状态机
- lucene源代码学习之FST(Finite State Transducer)在SynonymFilter中的实现思想