00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "fsm.hpp"
00026 #include "../commands/cmd_generic.hpp"
00027
00028
00029 void FSM::addState( const string &state )
00030 {
00031 m_states.insert( state );
00032 }
00033
00034
00035 void FSM::addTransition( const string &state1, const string &event,
00036 const string &state2, CmdGeneric *pCmd )
00037 {
00038
00039 if( m_states.find( state1 ) == m_states.end() ||
00040 m_states.find( state2 ) == m_states.end() )
00041 {
00042 msg_Warn( getIntf(),
00043 "FSM: ignoring transition between invalid states" );
00044 return;
00045 }
00046
00047 Key_t key( state1, event );
00048 Data_t data( state2, pCmd );
00049
00050
00051 if( m_transitions.find( key ) != m_transitions.end() )
00052 {
00053 msg_Warn( getIntf(), "FSM: transition already exists" );
00054 return;
00055 }
00056
00057 m_transitions[key] = data;
00058 }
00059
00060
00061 void FSM::setState( const string &state )
00062 {
00063 if( m_states.find( state ) == m_states.end() )
00064 {
00065 msg_Warn( getIntf(), "FSM: trying to set an invalid state" );
00066 return;
00067 }
00068 m_currentState = state;
00069 }
00070
00071
00072 void FSM::handleTransition( const string &event )
00073 {
00074 string tmpEvent = event;
00075 Key_t key( m_currentState, event );
00076 map<Key_t, Data_t>::const_iterator it;
00077
00078
00079 it = m_transitions.find( key );
00080
00081
00082
00083 while( it == m_transitions.end() &&
00084 tmpEvent.rfind( ":", tmpEvent.size() ) != string::npos )
00085 {
00086
00087 tmpEvent = tmpEvent.substr( 0, tmpEvent.rfind( ":", tmpEvent.size() ) );
00088
00089 key.second = tmpEvent;
00090 it = m_transitions.find( key );
00091 }
00092
00093
00094 if( it == m_transitions.end() )
00095 {
00096 return;
00097 }
00098
00099
00100 m_currentState = (*it).second.first;
00101
00102
00103 CmdGeneric *pCmd = (*it).second.second;
00104 if( pCmd != NULL )
00105 {
00106 pCmd->execute();
00107 }
00108 }