# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

automata_desc.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX D.T.C. SYSTEM.
00010  * NEVRAX D.T.C. SYSTEM is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX D.T.C. SYSTEM is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX D.T.C. SYSTEM; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "nel/ai/tools/automata_desc.h"
00027 #include <fstream.h>
00028 
00029 const sint32 CAutomataDesc::IDSUCCESS   = -1;
00030 const sint32 CAutomataDesc::IDFAIL              = -2;
00031 
00032         /*
00033          * Constructor
00034          */
00035         CAutomataDesc::CAutomataDesc()
00036         {
00037                 addState(IDSUCCESS, "AUTOMAT SUCCESS STATE");
00038                 addState(IDFAIL, "AUTOMAT FAIL STATE");
00039         }
00040 
00041         std::string     CAutomataDesc::getAutomatName() const
00042         {
00043                 return _AutomatName;
00044         }
00045 
00046         const std::list<sint32> &CAutomataDesc::getEntryStates() const
00047         {
00048                 return _EntryStates;
00049         }
00050 
00051         const std::list<sint32> &CAutomataDesc::getSuccessStates(sint32 stateId) const
00052         {
00053                 std::map<sint32,CState>::const_iterator iStates;
00054                 iStates = _States.find(stateId);
00055                 return (*iStates).second.SuccessStates;
00056         }
00057 
00058         const std::list<sint32> &CAutomataDesc::getFailStates(sint32 stateId) const
00059         {
00060                 std::map<sint32,CState>::const_iterator iStates;
00061                 iStates = _States.find(stateId);
00062                 return (*iStates).second.FailStates;
00063         }
00064 
00065         std::string CAutomataDesc::getStateName(sint32 stateId) const
00066         {
00067                 std::map<sint32,CState>::const_iterator iStates;
00068                 iStates = _States.find(stateId);
00069                 if (iStates != _States.end())
00070                 {
00071                         return (*iStates).second.Name;
00072                 }
00073                 else
00074                 {
00075                         return "ERROR : Invalid State ID";
00076                 }
00077         }
00078 
00079         void CAutomataDesc::setAutomatName(std::string name)
00080         {
00081                 _AutomatName = name;
00082         }
00083 
00084         void CAutomataDesc::addState(sint32 id, std::string name)
00085         {
00086                 if (!exploredState(id))
00087                 {
00088                         _States[id].Name = name;
00089                 }
00090         }
00091 
00092         void CAutomataDesc::addSuccessState(sint32 id, sint32 successId)
00093         {
00094                 if (!exploredState(id))
00095                 {
00096                         _States[id].SuccessStates.push_back(successId);
00097                 }
00098         }
00099 
00100         void CAutomataDesc::addFailState(sint32 id, sint32 faileId)
00101         {
00102                 if (!exploredState(id))
00103                 {
00104                         _States[id].FailStates.push_back(faileId);
00105                 }
00106         }
00107 
00108         void CAutomataDesc::addEntryState(sint32 entryId)
00109         {
00110                 _EntryStates.push_back(entryId);
00111         }
00112 
00113         bool CAutomataDesc::exploredState(sint32 stateId)
00114         {
00115                 std::set<sint32>::const_iterator iSet = _ExploredState.find(stateId);
00116                 return (iSet != _ExploredState.end());
00117         }
00118 
00119         void CAutomataDesc::setExploredState(sint32 stateId)
00120         {
00121                 _ExploredState.insert(stateId);
00122         }       
00123 
00124         std::string CAutomataDesc::getClassName()
00125         {
00126                 return "CAutomataDesc";
00127         }
00128 
00129         void CAutomataDesc::generateScript()
00130         {
00131                 ofstream tmp_script( "fsm_script.ras");
00132                 
00133                 // Succes and failure states
00134 /*              tmp_script << "From Actor : Define SuccessState" << endl << "{" << endl;
00135                 tmp_script << "}" << endl << endl;
00136                 tmp_script << "From Actor : Define FailureState" << endl << "{" << endl;
00137                 tmp_script << "}" << endl << endl;
00138 */
00139 
00140                 tmp_script << "// " << getAutomatName() << " FSM definition generated script" << endl << endl;
00141                 std::string name;
00142                 // Generates states
00143                 std::map<sint32,CState>::const_iterator it_m = _States.begin();
00144                 while ( it_m != _States.end() )
00145                 {
00146                         sint32 state_id = (*it_m).first;
00147                         
00148                         name = getStateName( state_id );
00149                         // State name
00150                         std::string state_name = removeSpaces( name );
00151 
00152                         tmp_script << "From Actor : Define " << "Actor" << state_name << endl << "{" << endl;
00153 
00154                         if ( state_id != IDSUCCESS && state_id != IDFAIL )
00155                         {
00156 #ifdef NL_DEBUG
00157                                 tmp_script << "\tOnActivate()" << endl;
00158                                 tmp_script << "\t\tPrint('Etat " << state_name << " actif');" << endl;
00159                                 tmp_script << "\tEnd" << endl << endl;
00160 #endif
00161 
00162                                 // Success transitions
00163                                 tmp_script << "\tRunTell(SuccessMsg msg)" << endl;
00164                                 std::list<sint32>::const_iterator it_s = getSuccessStates( state_id ).begin();
00165                                 while ( it_s != getSuccessStates( state_id ).end() )
00166                                 {
00167                                         name = getStateName( *it_s );
00168                                         state_name = removeSpaces( name );
00169                                         tmp_script << "\t\tswitch('" << state_name << "');" << endl;
00170                                         it_s++;
00171                                 }
00172                                 tmp_script << "\t\tReturn msg;" << endl;
00173                                 tmp_script << "\tEnd" << endl << endl;
00174 
00175                                 // Failure transitions
00176                                 tmp_script << "\tRunTell(FailureMsg msg)" << endl;
00177                                 std::list<sint32>::const_iterator it_f = getFailStates( state_id ).begin();
00178                                 while ( it_f != getFailStates( state_id ).end() )
00179                                 {
00180                                         name = getStateName( *it_f );
00181                                         state_name = removeSpaces( name );
00182                                         tmp_script << "\t\tswitch('" << state_name << "');" << endl;
00183                                         it_f++;
00184                                 }
00185                                 tmp_script << "\t\tReturn msg;" << endl;
00186                                 tmp_script << "\tEnd" << endl;
00187                         }
00188                         else
00189                         {
00190                                 tmp_script << "\tRun()" << endl;
00191                                 if ( state_id == IDSUCCESS )
00192                                         tmp_script << "\t\tFather().Send(TELL, new SuccessMsg(0.0));" << endl;
00193                                 else
00194                                         tmp_script << "\t\tFather().Send(TELL, new FailureMsg(0.0));" << endl;
00195                                 tmp_script << "\tEnd" << endl;
00196                         }
00197 
00198                         // End of the State class
00199                         tmp_script << "}" << endl << endl;
00200 
00201                         it_m++;
00202                 }
00203 
00204                 // Generates FSM
00205                 std::string automateName = getAutomatName();
00206                 std::string fsm_name = removeSpaces( automateName );
00207                 tmp_script << "From Fsm : Define " << fsm_name << endl << "{" << endl;
00208 
00209                 // Generates states as static components of the FSM
00210                 tmp_script << "\tComponent:" << endl;
00211                 
00212                 it_m = _States.begin();
00213                 while ( it_m != _States.end() )
00214                 {
00215                         // State name
00216                         name = getStateName( (*it_m).first );
00217                         std::string state_name = removeSpaces( name );
00218                         tmp_script << "\t\tActor" << state_name << "<'" << state_name << "'>;" << endl;
00219                         it_m++;
00220                 }
00221                 tmp_script << "\tEnd" << endl << endl;
00222 
00223                 // Activates entry states
00224                 tmp_script << "\tConstructor()" << endl;
00225                 std::list<sint32>::const_iterator it_e = getEntryStates().begin();
00226                 while ( it_e != getEntryStates().end() )
00227                 {
00228                         name = getStateName( *it_e );
00229                         std::string state_name = removeSpaces( name );
00230                         tmp_script << "\t\t" << state_name << ".activate();" << endl;
00231                         it_e++;
00232                 }
00233                 tmp_script << "\tEnd" << endl;
00234 
00235                 // End of the Fsm Class
00236                 tmp_script << "}" << endl;
00237 
00238                 tmp_script.close();
00239         }
00240 
00241         std::string CAutomataDesc::removeSpaces(std::string &txt)
00242         {
00243                 int i = 0;
00244                 char buffer[1024];
00245                 while ( i != (int)txt.size() )
00246                 {
00247                         if ( txt[i] == ' ')
00248                                 buffer[i] = '_';
00249                         else
00250                                 buffer[i] = txt[i];
00251                         i++;
00252                 }
00253                 buffer[i] = 0;
00254                 return std::string( buffer );
00255         }