UML层次状态机向导2/2
3 状态机框架
应用框架是指一个可复用的、部分实现的软件制品,能够被实例化扩展,以生成特定的应用。它是一个不完全的系统,是一个特定领域内的应用程序的部分设计和实现。它能够被裁剪,从而创造一个完整的应用程序。典型的应用程序框架有:Microsoft Foundation Class(MFC), 它为C++用户提供一套面向对象程序开发的辅助工具,称之为类向导器(Class Wizard);Delphi和Visual Basic,被称之为一种程序快速开发工具,它们采用属性、方法、事件结构,是一种基于组件的开发应用框架。
我们定义了一系列特殊的宏作为状态机的映射数据。这些宏映射到状态枚举、事件处理函数声明、状态的事件处理函数表、状态树定义和应用程序变量定义。具体如下定义如下:
状态枚举:
#define SME_BEGIN_STATE_DECLARE(_app) enum _app##_state_enum_t \
{#define SME_STATE_DECLARE(_state) _state, #define SME_MAX_STATE(_app)\
_app##_max_state #define SME_END_STATE_DECLARE };
状态事件处理函数表定义:
#define SME_ENTRY_FUNC_IDX 0
#define SME_EXIT_FUNC_IDX 1
#define SME_EVENT_HANDLER_FUNC_IDX 2
#define SME_BEGIN_STATE_DEF(_app,_state) \
static const SME_EVENT_TABLE_T _app##_state##_event_hdl_tbl[]= \
{#define SME_STATE_ENTRY_FUNC( _EntryFunc) \
{ SME_INVALID_EVENT_ID, _EntryFunc, 0},
#define SME_STATE_EXIT_FUNC( _ExitFunc) \
{ SME_INVALID_EVENT_ID, _ExitFunc, 0},
#define SME_ON_EVENT(_EventID, _Handler, _NewState) \
{ _EventID, _Handler, _NewState},
#define SME_END_STATE_DEF { SME_INVALID_EVENT_ID, 0, SME_INVALID_STATE}
};
状态树定义:
#define SME_BEGIN_STATE_TREE_DEF(_app) \
extern const SME_STATE_TREE_TABLE_T _app##_state_tree[] = \
{#define SME_STATE(_app,_state,_state_parent,_def_substate) \
{(SME_EVENT_TABLE_T*)_app##_state##_event_hdl_tbl, \
_state,_state_parent,_def_substate},\
#define SME_END_STATE_TREE_DEF };
应用程序定义:
#define SME_APPLICATION_DEF(_app,_app_name) \
struct SME_APP_T _app##App = { \
_app_name, NULL, NULL, 0, NULL, NULL, _app##_state_tree, SME_INVALID_STATE};
#define SME_GET_APP_VAR(_app) _app##App
#define SME_DEC_EXT_APP_VAR(_app) extern SME_APP_T _app##App;
根据这些定义,状态机向导生成如下代码,它构造了RecorderApp状态机。对应于状态图,如图二所示,可生成相对应的状态树。
SME_BEGIN_STATE_DECLARE(RecorderApp)
/*{{SME_STATE_DECLARE(RecorderApp)*/
SME_STATE_DECLARE(RecorderApp)
SME_STATE_DECLARE(Off)
SME_STATE_DECLARE(On)
SME_STATE_DECLARE(Stop)
SME_STATE_DECLARE(Play)
SME_STATE_DECLARE(Record)
SME_MAX_STATE(RecorderApp)
/*}}SME_STATE_DECLARE*/
SME_END_STATE_DECLARE
SME_BEGIN_STATE_DEF(RecorderApp,RecorderApp)
/*{{SME_STATE_DEF(RecorderApp,RecorderApp)*/
SME_STATE_ENTRY_FUNC(NULL)
SME_STATE_EXIT_FUNC(NULL)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
SME_BEGIN_STATE_DEF(RecorderApp,Off)
/*{{SME_STATE_DEF(RecorderApp,Off)*/
SME_STATE_ENTRY_FUNC(NULL)
SME_STATE_EXIT_FUNC(NULL)
SME_ON_EVENT(EVENT_POWER,NULL,On)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
SME_BEGIN_STATE_DEF(RecorderApp,On)
/*{{SME_STATE_DEF(RecorderApp,On)*/
SME_STATE_ENTRY_FUNC(NULL)
SME_STATE_EXIT_FUNC(NULL)
SME_ON_EVENT(EVENT_POWER,OnOnEVENT_POWER,Off)
SME_ON_EVENT(EVENT_STOP,OnOnEVENT_STOP,Stop)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
SME_BEGIN_STATE_DEF(RecorderApp,Stop)
/*{{SME_STATE_DEF(RecorderApp,Stop)*/
SME_STATE_ENTRY_FUNC(StopEntry)
SME_STATE_EXIT_FUNC(StopExit)
SME_ON_EVENT(EVENT_PLAY,OnStopEVENT_PLAY,Play)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
SME_BEGIN_STATE_DEF(RecorderApp,Play)
/*{{SME_STATE_DEF(RecorderApp,Play)*/
SME_STATE_ENTRY_FUNC(PlayEntry)
SME_STATE_EXIT_FUNC(PlayExit)
SME_ON_EVENT(EVENT_RECORD,OnPlayEVENT_RECORD,Record)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
SME_BEGIN_STATE_DEF(RecorderApp,Record)
/*{{SME_STATE_DEF(RecorderApp,Record)*/
SME_STATE_ENTRY_FUNC(RecordEntry)
SME_STATE_EXIT_FUNC(RecordExit)
/*}}SME_STATE_DEF*/
SME_END_STATE_DEF
/*{{SME_STATE_STATETREE_SEPARATOR}}*/
SME_BEGIN_STATE_TREE_DEF(RecorderApp)
/*{{SME_STATE_TREE_DEF(RecorderApp)*/
SME_STATE(RecorderApp,RecorderApp,SME_INVALID_STATE,Off)
SME_STATE(RecorderApp,Off,0,-1)
SME_STATE(RecorderApp,On,0,Stop)
SME_STATE(RecorderApp,Stop,On,-1)
SME_STATE(RecorderApp,Play,On,-1)
SME_STATE(RecorderApp,Record,On,-1)
/*}}SME_STATE_TREE_DEF*/
SME_END_STATE_TREE_DEF
/*{{SME_DEC_IMP_SEPARATOR}}*/
SME_APPLICATION_DEF(RecorderApp, "RecorderApp")
图二:状态树
参考: 可访问www.intelliwizard.com 得到更多资料。
文章评论(0条评论)
登录后参与讨论