SourceForge Project Site
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FiniteStateMachine.h

Go to the documentation of this file.
00001 #if !defined(AFX_FSMACTION_H__7C6ABC20_3696_40ED_9DC4_6666666__INCLUDED_)
00002 #define AFX_FSMACTION_H__7C6ABC20_3696_40ED_9DC4_6666666__INCLUDED_
00003 
00004 #if _MSC_VER > 1000
00005 #pragma once
00006 #endif // _MSC_VER > 1000
00007 
00008 /*
00009  * The Finite State Machine (FSM) Library
00010  * Copyright (C) 2002 Siemens C-LAB Paderborn
00011  * All rights reserved.
00012  *
00013  * This program is  free  software;  you can redistribute it and/or modify it
00014  * under the terms of the  GNU Lesser General Public License  as published by 
00015  * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
00016  * your option) any later version.
00017  *
00018  * This  program  is  distributed in  the  hope that it will  be useful,  but
00019  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020  * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
00021  * License for more details.
00022  *
00023  * You should  have received  a copy of the GNU Lesser General Public License
00024  * along with  this program;  if not, write to the  Free Software Foundation,
00025  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
00026  */
00027 
00028 // $Log: FiniteStateMachine.h,v $
00029 // Revision 1.4  2002/08/20 14:48:41  vofka
00030 // FiniteStateMachine-config.h removed from installing. Loading exported to the loader class. Saver introduced. Error codes exported to FSMErrorCodes.
00031 //
00032 // Revision 1.3  2002/08/13 11:06:32  vofka
00033 // automake support added
00034 //
00035 // Revision 1.2  2002/07/22 17:21:00  vofka
00036 // SimpleTransition, PushTransition, PopTransition added, not implemented yet
00037 //
00038 // Revision 1.1  2002/06/28 11:10:36  vofka
00039 // first time on sourceforge.net cvs
00040 //
00041 // Revision 1.10  2002/06/20 12:22:10  bobka
00042 // LGPL license
00043 //
00044 // Revision 1.9  2002/05/03 23:21:57  bobka
00045 // Loader externalisiert, aber der alte ist noch drin.
00046 // Iterators for states und variables added.
00047 // some minor bugs fixed.
00048 //
00049 // Revision 1.8  2002/03/07 10:02:35  bobka
00050 // Loader separated. Only Linux tested.
00051 //
00052 // Revision 1.7  2001/12/17 15:10:26  bobka
00053 // no SGI-STL,
00054 // some little bugs fixed
00055 // not testet on LINUX!
00056 //
00057 // Revision 1.6  2001/12/07 13:50:59  bobka
00058 // clean up improved (delete tokenizer)
00059 // CTokenizer exported
00060 // LoadFSM public
00061 //
00062 // Revision 1.5  2001/12/06 16:30:37  bobka
00063 // comments <!-- ... --> support added
00064 // closed tag < .. /> support added
00065 //
00066 // Revision 1.4  2001/12/03 10:24:55  bobka
00067 // uses iError on WinCE
00068 //
00069 // Revision 1.3  2001/11/28 15:42:00  bobka
00070 // a lot of changes
00071 //
00072 // Revision 1.2  2001/11/23 09:27:58  bobka
00073 // Load/Save funktioniert.
00074 // FSM* im Fileformat entfernt.
00075 // streams entfernt.
00076 // Laeuft unter Win32 und unter WinCE (WinCE_STL Modul auschecken)
00077 // static library unter WinCE funktioniert nicht, nur DLL.
00078 //
00079 // Revision 1.1  2001/11/15 10:37:33  bobka
00080 // initial checkin
00081 // not tested on linux!
00082 //
00083 
00090 #include <map>
00091 #include <list>
00092 #include <string>
00093 
00094 
00096 namespace FSM {
00097         using std::map;
00098         using std::list;
00099         using std::string;
00100         using std::less;
00101         
00102 
00103 // forward reference
00104 class CFiniteStateMachine;
00105 class CFSMObject;
00106 class CFSMState;
00107 class CFSMTransition;
00108 class CFSMAction;
00109 class CFSMCondition;
00110 
00111 typedef map< string, CFSMState* > State_Map;
00112 typedef State_Map::value_type SM_VT;
00113 
00114 typedef map< string, int > Var_Map;
00115 typedef Var_Map::value_type VM_VT;
00116 
00117 typedef void (*InitClassF)(CFiniteStateMachine* fsm);
00118 typedef CFSMObject* (*FSMObjectCreateMethod)();
00119 
00120 typedef map< string, FSMObjectCreateMethod > FSMObjectCreate_Map;
00121 typedef FSMObjectCreate_Map::value_type FSM_C_VT;
00122 
00123 class CFiniteStateMachine
00124 {
00125         public:
00126                 //Constructor
00127                 CFiniteStateMachine();
00128 
00129                 //Destructor
00130                 ~CFiniteStateMachine();
00131 
00132                 // States
00133                 CFSMState* AddState(const char *stateName, const char *className="State");
00134                 CFSMState* GetState(const char *stateName) const;
00135                 bool       DeleteState(CFSMState *state);
00136                 bool       DeleteState(const char *stateName);
00137 
00143                 State_Map::const_iterator GetStatesBegin() const;
00144 
00146                 State_Map::const_iterator GetStatesEnd() const;
00147 
00148                 // Transitions
00149                 CFSMTransition* AddTransition(const char* fromStateName, const char* onEvent="ANY", const char* className="SimpleTransition");
00150                 CFSMTransition* AddSimpleTransition(const char* fromStateName, const char* toStateName, const char* onEvent="ANY");
00151 
00160                 //bool DeleteTransition(const char* fromState=NULL, const char* event=NULL, const char* toState=NULL);
00161 
00167                 bool DeleteTransition(const char* fromState, int index);
00168 
00169                 // Conditions
00177                 CFSMCondition* AddCondition(const char *stateName, int transition, const char *className="TestVariable");
00178 
00179                 // Actions
00183                 CFSMAction* AddEnterAction(const char *stateName, const char *className);
00184 
00188                 CFSMAction* AddLeaveAction(const char *stateName, const char *className);
00189 
00197                 CFSMAction* AddAction(const char *stateName, int transition, const char *className);
00198 
00199                 // Variables
00203                 bool AddVariable(const char *varName, const int initValue = 0);
00204                 
00207                 bool DeleteVariable(const char *varName);
00208                 
00212                 bool SetVariable(const char *varName, const int value);
00213                 
00217                 bool GetVariable(const char *varName, int &value) const;
00218 
00220                 Var_Map::const_iterator GetVarsBegin() const;
00221 
00223                 Var_Map::const_iterator GetVarsEnd() const;
00224 
00225                 // Operate FSM
00226                 bool SetInitialState(const char *stateName);
00227                 const char* GetInitialState() const;
00228                 bool Start(const char *stateName=0);
00229                 void Stop();
00230                 bool ProcessEvent(const char *Event, void *a=NULL, void *b=NULL, bool *consumed=NULL);
00231                 CFSMState *GetCurrentState() const;
00232                 bool Clear(); // delete all states and all variables.
00233  
00235                 bool SwitchToState(CFSMState* newState);
00236                 
00240                 bool PushState(CFSMState* newState);
00241                 
00245                 bool PopState();
00246                 
00247                 void Register(const char *className, FSMObjectCreateMethod create);
00248                 
00250                 CFSMObject* Create(const char *className);
00251 
00252         protected:
00253 #ifndef _WIN32_WCE
00254                 bool LoadClass(const char *className);
00255 #endif
00256 
00257 
00258         private:
00259                 void RegisterBaseTypes();
00260 
00261                 // Global registration map
00262                 FSMObjectCreate_Map FSMObjectCreationMethods;
00263                 State_Map   m_States;
00264                 Var_Map     m_Variables;
00265                 CFSMState*  m_CurrentState;
00266                 string      m_InitialState;
00267                 bool        m_Running;
00268 };
00269 
00270 }; // namespace FSM
00271 
00272 #endif

© 2002 by C-LAB
generated by doxygen