StatechartUtilities.h
Go to the documentation of this file.
00001 #ifndef STATECHARTUTILITIES_H_
00002 #define STATECHARTUTILITIES_H_
00003 
00004 #include <boost/shared_ptr.hpp>
00005 #include <boost/statechart/state_machine.hpp>
00006 #include <boost/regex.hpp>
00007 #include <iostream>
00008 #include <iomanip>
00009 
00010 namespace StatechartUtilities
00011 {
00012     const boost::regex TYPEID_NAME("([[:digit:]]+)([[:word:]]+)");
00013 
00014     template <class T>
00015     std::vector<std::string> getCurrentStates(boost::shared_ptr<T> fsm)
00016     {
00017         std::vector<std::string> states;
00018         std::string state = "";
00019 
00020         for ( typename T::state_iterator pLeafState = fsm->state_begin();
00021                 pLeafState != fsm->state_end(); ++pLeafState )
00022         {
00023             const typename T::state_base_type* pState = &*pLeafState;
00024 
00025             while ( pState != 0 )
00026             {
00027                 const std::string name = typeid(*pState).name();
00028 
00029                 if (boost::regex_match(name, TYPEID_NAME))
00030                 {
00031                     boost::match_results<std::string::const_iterator> matches;
00032                     boost::regex_search(name.begin(), name.end(), matches, TYPEID_NAME);
00033                     state = std::string(matches[2].first, matches[2].second);
00034                 }
00035 
00036                 states.push_back(state);
00037 
00038                 pState = pState->outer_state_ptr();
00039             }
00040         }
00041 
00042         std::sort(states.begin(), states.end());
00043         std::vector<std::string>::iterator it = std::unique(states.begin(), states.end());
00044         states.resize(std::distance(states.begin(), it));
00045 
00046         return states;
00047     }
00048 
00049     template <class T>
00050     bool isActiveState(boost::shared_ptr<T> fsm, const std::string& state)
00051     {
00052         std::vector<std::string> states = getCurrentStates<T>(fsm);
00053         return std::find(states.begin(), states.end(), state) != states.end();
00054     }
00055 
00056     template <class T>
00057     void displayStateConfiguration(boost::shared_ptr<T> fsm)
00058     {
00059         char orthogonalRegion = 'a';
00060 
00061         for ( typename T::state_iterator pLeafState = fsm->state_begin();
00062                 pLeafState != fsm->state_end(); ++pLeafState )
00063         {
00064             std::cout << "Orthogonal region " << orthogonalRegion << ": ";
00065 
00066             const typename T::state_base_type* pState = &*pLeafState;
00067 
00068             while ( pState != 0 )
00069             {
00070                 if ( pState != &*pLeafState )
00071                 {
00072                     std::cout << " -> ";
00073                 }
00074 
00075                 std::cout << std::setw( 15 ) << typeid( *pState ).name();
00076 
00077                 pState = pState->outer_state_ptr();
00078             }
00079 
00080             std::cout << std::endl;
00081             ++orthogonalRegion;
00082         }
00083 
00084         std::cout << std::endl;
00085     }
00086 }
00087 
00088 #endif


fsm_utils
Author(s):
autogenerated on Thu Jun 6 2019 21:59:42