NLLOGIC::CLogicState Class Reference

#include <logic_state.h>


Detailed Description

CLogicState

Author:
Stephane Coutelas

Nevrax France

Date:
2001

Definition at line 56 of file logic_state.h.

Public Member Functions

void addEvent (CLogicEvent event)
void addSIdMap (const TSIdMap &sIdMap)
 CLogicState ()
void enterState ()
void exitState ()
void fillVarMap (std::multimap< NLMISC::CEntityId, std::string > &stateMachineVariables)
void getMessagesToSend (std::multimap< NLMISC::CEntityId, NLNET::CMessage > &msgs)
std::string getName ()
void processLogic ()
void read (xmlNodePtr node)
void setLogicStateMachine (CLogicStateMachine *logicStateMachine)
void setName (std::string name)
void trySendEntryMessages ()
void trySendEventMessages ()
void write (xmlNodePtr node) const

Data Fields

std::vector< CLogicEventMessage_EntryMessages
 entry messages

std::vector< CLogicEvent_Events
 logic

std::vector< CLogicEventMessage_ExitMessages
 exit messages

CLogicStateMachine_LogicStateMachine
 state machine containing this state

std::multimap< NLMISC::CEntityId,
NLNET::CMessage
_MessagesToSend
 messages to send by the service

std::string _StateName
 state name


Constructor & Destructor Documentation

NLLOGIC::CLogicState::CLogicState  ) 
 

Default constructor

Definition at line 42 of file logic_state.cpp.

References _StateName.

00043 {
00044         _StateName = "no_state";
00045         _LogicStateMachine = 0;
00046 
00047 } // CLogicState //


Member Function Documentation

void NLLOGIC::CLogicState::addEvent CLogicEvent  event  ) 
 

Add an event

Parameters:
event is the event to add

Definition at line 81 of file logic_state.cpp.

References NLLOGIC::CLogicEvent::setLogicStateMachine().

00082 { 
00083         event.setLogicStateMachine( _LogicStateMachine );
00084         _Events.push_back( event ); 
00085 
00086 } // addEvent //

void NLLOGIC::CLogicState::addSIdMap const TSIdMap sIdMap  ) 
 

Associate message destination name with sid

Parameters:
sIdMap is the map associating destination name with a destination id

Definition at line 96 of file logic_state.cpp.

References _EntryMessages, _ExitMessages, trySendEntryMessages(), trySendEventMessages(), and NLLOGIC::TSIdMap.

00097 {
00098         vector<CLogicEventMessage>::iterator itMsg;
00099 
00101         for( itMsg = _EntryMessages.begin(); itMsg != _EntryMessages.end(); ++itMsg )
00102         {
00103                 TSIdMap::const_iterator itId = sIdMap.find( (*itMsg).Destination );
00104                 // if message destination exists in the map we associate the sid with the message
00105                 if( itId != sIdMap.end() )
00106                 {
00107                         (*itMsg).DestinationId = (*itId).second;
00108                 }
00109         }
00111         trySendEntryMessages();
00112                 
00113 
00114         // event messages
00115         vector<CLogicEvent>::iterator itEvt;
00116         for( itEvt = _Events.begin(); itEvt != _Events.end(); ++itEvt )
00117         {
00118                 string dest = (*itEvt).EventAction.EventMessage.Destination;
00119                 TSIdMap::const_iterator itId = sIdMap.find( dest );
00120                 // if message destination exists in the map we associate the sid with the message
00121                 if( itId != sIdMap.end() )
00122                 {
00123                         (*itEvt).EventAction.EventMessage.DestinationId = (*itId).second;
00124                 }
00125         }
00127         trySendEventMessages();
00128 
00129 
00131         for( itMsg = _ExitMessages.begin(); itMsg != _ExitMessages.end(); ++itMsg )
00132         {
00133                 TSIdMap::const_iterator itId = sIdMap.find( (*itMsg).Destination );
00134                 // if message destination exists in the map we associate the sid with the message
00135                 if( itId != sIdMap.end() )
00136                 {
00137                         (*itMsg).DestinationId = (*itId).second;
00138                 }
00139         }
00140         
00141 } // addSIdMap //

void NLLOGIC::CLogicState::enterState  ) 
 

send the entry messages

Definition at line 184 of file logic_state.cpp.

References trySendEntryMessages().

00185 {
00187         trySendEntryMessages();
00188 
00189 } // enterState //

void NLLOGIC::CLogicState::exitState  ) 
 

send the exit messages

Definition at line 196 of file logic_state.cpp.

References _EntryMessages, _ExitMessages, _MessagesToSend, and NLMISC::CMemStream::serial().

00197 {
00198         vector<CLogicEventMessage>::iterator itMsg;
00199         for( itMsg = _ExitMessages.begin(); itMsg != _ExitMessages.end(); ++itMsg )
00200         {
00201                 if( (*itMsg).DestinationId != CEntityId() )
00202                 {
00203                         CMessage msgOut( (*itMsg).MessageId );
00204                         msgOut.serial( (*itMsg).Arguments );
00205                         
00206                         _MessagesToSend.insert( make_pair((*itMsg).DestinationId,msgOut) );
00207                 }
00208         }
00209 
00210         // reset the entry messages send status
00211         for( itMsg = _EntryMessages.begin(); itMsg != _EntryMessages.end(); ++itMsg )
00212         {
00213                 (*itMsg).ToSend = false;
00214                 (*itMsg).Sent = false;
00215         }
00216 
00217         // reset all events
00218         vector<CLogicEvent>::iterator itEvent;
00219         for( itEvent = _Events.begin(); itEvent != _Events.end(); ++itEvent )
00220         {
00221                 (*itEvent).reset();
00222         }
00223 
00224         // reset the exit messages send status
00225         for( itMsg = _ExitMessages.begin(); itMsg != _ExitMessages.end(); ++itMsg )
00226         {
00227                 (*itMsg).ToSend = false;
00228                 (*itMsg).Sent = false;
00229         }
00230         
00231 } // exitState //

void NLLOGIC::CLogicState::fillVarMap std::multimap< NLMISC::CEntityId, std::string > &  stateMachineVariables  ) 
 

Fill a map associating all the referenced var in the state with the id of service managing them (debug purpose)

Definition at line 315 of file logic_state.cpp.

References condition, and NLLOGIC::CLogicStateMachine::getCondition().

00316 {
00317         // events
00318         vector<CLogicEvent>::iterator itEvt;
00319         for( itEvt = _Events.begin(); itEvt != _Events.end(); ++itEvt )
00320         {
00321                 // get the condition used in the event
00322                 CLogicCondition condition;
00323                 if( _LogicStateMachine->getCondition((*itEvt).ConditionName,condition) )
00324                 {
00325                         // get vars used in the conditions
00326                         set<string> condVars;
00327                         condition.fillVarSet( condVars );
00328 
00329                         // add var with related service
00330                         set<string>::iterator itCV;
00331                         for( itCV = condVars.begin(); itCV != condVars.end(); ++itCV )
00332                         {
00333                                 stateMachineVariables.insert( make_pair((*itEvt).EventAction.EventMessage.DestinationId,*itCV) );
00334                         }
00335                 }
00336         }
00337 
00338 } // fillVarMap //

void NLLOGIC::CLogicState::getMessagesToSend std::multimap< NLMISC::CEntityId, NLNET::CMessage > &  msgs  ) 
 

Get the messages to send

Parameters:
msgs is the map associating all the message to send with their destination id

Definition at line 296 of file logic_state.cpp.

References _MessagesToSend.

00297 {
00298         multimap<CEntityId,CMessage>::iterator itMsg;
00299         for( itMsg = _MessagesToSend.begin(); itMsg != _MessagesToSend.end(); ++itMsg )
00300         {
00301                 msgs.insert( *itMsg );
00302         }
00303 
00304         // erase all the messages
00305         _MessagesToSend.clear();
00306 
00307 } // getMessagesToSend //

std::string NLLOGIC::CLogicState::getName void   )  [inline]
 

get the state name

Returns:
the state's name

Definition at line 104 of file logic_state.h.

References _StateName.

Referenced by NLLOGIC::CLogicStateMachine::addState().

00104 { return _StateName; }

void NLLOGIC::CLogicState::processLogic  ) 
 

Test the conditions of this state

Definition at line 148 of file logic_state.cpp.

References NLLOGIC::CLogicStateMachine::setCurrentState(), and trySendEventMessages().

00149 {
00150         // test all conditions managed by this state
00151         vector<CLogicEvent>::iterator itEvent;
00152         for( itEvent = _Events.begin(); itEvent != _Events.end(); ++itEvent )
00153         {
00154                 if( (*itEvent).testCondition() )
00155                 {
00156                         //nlinfo("The condition %s is valid",(*itEvent).ConditionName.c_str());
00157                         if( (*itEvent).EventAction.IsStateChange )
00158                         {
00159                                 _LogicStateMachine->setCurrentState( (*itEvent).EventAction.StateChange );
00160                         }
00161                         else
00162                         {
00163                                 // this message will be sent as soon as the dest id will be given
00164                                 (*itEvent).EventAction.enableSendMessage();
00165 
00167                                 trySendEventMessages();
00168                         }
00169                 }
00170                 else
00171                 {
00172                         // reset message send status here to be able to send messages several times in the logic state
00173                         // --> this has to be done if we want messages to be sent every time the condition becomes verified
00174                 }
00175         }
00176 
00177 } // processLogic //

void NLLOGIC::CLogicState::read xmlNodePtr  node  ) 
 

Definition at line 379 of file logic_state.cpp.

References _EntryMessages, _ExitMessages, _StateName, NLLOGIC::getXMLProp(), uint, v, and NLLOGIC::xmlCheckNodeName().

00380 {
00381         xmlCheckNodeName (node, "STATE");
00382 
00383         _StateName = getXMLProp (node, "Name");
00384 
00385         {
00386                 // Count the parent
00387                 uint nb = CIXml::countChildren (node, "ENTRY_EVENT_MESSAGE");
00388                 uint i = 0;
00389                 xmlNodePtr parent = CIXml::getFirstChildNode (node, "ENTRY_EVENT_MESSAGE");
00390                 while (i<nb)
00391                 {
00392                         CLogicEventMessage v;
00393                         v.read(parent);
00394                         _EntryMessages.push_back(v);
00395 
00396                         // Next parent
00397                         parent = CIXml::getNextChildNode (parent, "ENTRY_EVENT_MESSAGE");
00398                         i++;
00399                 }
00400         }
00401         
00402         {
00403                 // Count the parent
00404                 uint nb = CIXml::countChildren (node, "EXIT_EVENT_MESSAGE");
00405                 uint i = 0;
00406                 xmlNodePtr parent = CIXml::getFirstChildNode (node, "EXIT_EVENT_MESSAGE");
00407                 while (i<nb)
00408                 {
00409                         CLogicEventMessage v;
00410                         v.read(parent);
00411                         _ExitMessages.push_back(v);
00412 
00413                         // Next parent
00414                         parent = CIXml::getNextChildNode (parent, "EXIT_EVENT_MESSAGE");
00415                         i++;
00416                 }
00417         }
00418 
00419         {
00420                 // Count the parent
00421                 uint nb = CIXml::countChildren (node, "EVENT");
00422                 uint i = 0;
00423                 xmlNodePtr parent = CIXml::getFirstChildNode (node, "EVENT");
00424                 while (i<nb)
00425                 {
00426                         CLogicEvent v;
00427                         v.read(parent);
00428                         _Events.push_back(v);
00429 
00430                         // Next parent
00431                         parent = CIXml::getNextChildNode (parent, "EVENT");
00432                         i++;
00433                 }
00434         }
00435 
00436 }

void NLLOGIC::CLogicState::setLogicStateMachine CLogicStateMachine logicStateMachine  ) 
 

Set the state machine which contains this state

Parameters:
logicStateMachine is the state machine containing this block

Definition at line 54 of file logic_state.cpp.

References nlwarning.

Referenced by NLLOGIC::CLogicStateMachine::addState().

00055 { 
00056         if( logicStateMachine == 0 )
00057         {
00058                 nlwarning("(LOGIC)<CLogicCondition::setLogicStateMachine> The state machine is null");
00059         }
00060         else
00061         {
00062                 // init the logic state machine for this state
00063                 _LogicStateMachine = logicStateMachine; 
00064 
00065                 // init the logic state machine in each event
00066                 vector<CLogicEvent>::iterator itEvent;
00067                 for( itEvent = _Events.begin(); itEvent != _Events.end(); ++itEvent )
00068                 {
00069                         (*itEvent).setLogicStateMachine( logicStateMachine );
00070                 }
00071         }
00072 
00073 } // setLogicStateMachine //

void NLLOGIC::CLogicState::setName std::string  name  )  [inline]
 

set the state name

Parameters:
name is the new state's name

Definition at line 97 of file logic_state.h.

References _StateName.

00097 { _StateName = name; }

void NLLOGIC::CLogicState::trySendEntryMessages  ) 
 

Try to send the entry messages

Definition at line 239 of file logic_state.cpp.

References _EntryMessages, _MessagesToSend, and NLMISC::CMemStream::serial().

Referenced by addSIdMap(), and enterState().

00240 {
00242         vector<CLogicEventMessage>::iterator itMsg;
00243         for( itMsg = _EntryMessages.begin(); itMsg != _EntryMessages.end(); ++itMsg )
00244         {
00245                 if( !(*itMsg).Sent && (*itMsg).DestinationId.getType() != 0xfe )
00246                 {
00247                         CMessage msgOut( (*itMsg).MessageId );
00248                         msgOut.serial( (*itMsg).Arguments );
00249 
00250                         _MessagesToSend.insert( make_pair((*itMsg).DestinationId,msgOut) );
00251                         
00252                         (*itMsg).ToSend = false;
00253                         (*itMsg).Sent = true;
00254                 }
00255         }
00256 
00257 } // trySendEntryMessages //

void NLLOGIC::CLogicState::trySendEventMessages  ) 
 

Try to send the event messages

Definition at line 265 of file logic_state.cpp.

References _MessagesToSend, and NLMISC::CMemStream::serial().

Referenced by addSIdMap(), and processLogic().

00266 {
00267         // test all conditions managed by this state
00268         vector<CLogicEvent>::iterator itEvent;
00269         for( itEvent = _Events.begin(); itEvent != _Events.end(); ++itEvent )
00270         {
00271                 if( (*itEvent).EventAction.EventMessage.ToSend == true )
00272                 {
00273                         if( (*itEvent).EventAction.EventMessage.Sent == false )
00274                         {
00275                                 if( (*itEvent).EventAction.EventMessage.DestinationId.getType() != 0xfe )
00276                                 {
00277                                         CMessage msgOut( (*itEvent).EventAction.EventMessage.MessageId );
00278                                         msgOut.serial( (*itEvent).EventAction.EventMessage.Arguments );
00279                                         
00280                                         _MessagesToSend.insert( make_pair((*itEvent).EventAction.EventMessage.DestinationId,msgOut) );
00281 
00282                                         (*itEvent).EventAction.EventMessage.ToSend = false;
00283                                         (*itEvent).EventAction.EventMessage.Sent = true;
00284                                 }
00285                         }
00286                 }
00287         }
00288 
00289 } // trySendEventMessages //

void NLLOGIC::CLogicState::write xmlNodePtr  node  )  const
 

serial

Definition at line 359 of file logic_state.cpp.

References _EntryMessages, _ExitMessages, _StateName, and uint.

00360 {
00361         xmlNodePtr elmPtr = xmlNewChild ( node, NULL, (const xmlChar*)"STATE", NULL);
00362         xmlSetProp (elmPtr, (const xmlChar*)"Name", (const xmlChar*)_StateName.c_str());
00363 
00364         uint i;
00365         for (i = 0; i < _EntryMessages.size(); i++)
00366         {
00367                 _EntryMessages[i].write(elmPtr, "ENTRY_");
00368         }
00369         for (i = 0; i < _ExitMessages.size(); i++)
00370         {
00371                 _ExitMessages[i].write(elmPtr, "EXIT_");
00372         }
00373         for (i = 0; i < _Events.size(); i++)
00374         {
00375                 _Events[i].write(elmPtr);
00376         }
00377 }


Field Documentation

std::vector<CLogicEventMessage> NLLOGIC::CLogicState::_EntryMessages
 

entry messages

Definition at line 64 of file logic_state.h.

Referenced by addSIdMap(), exitState(), read(), trySendEntryMessages(), and write().

std::vector<CLogicEvent> NLLOGIC::CLogicState::_Events
 

logic

Definition at line 70 of file logic_state.h.

std::vector<CLogicEventMessage> NLLOGIC::CLogicState::_ExitMessages
 

exit messages

Definition at line 67 of file logic_state.h.

Referenced by addSIdMap(), exitState(), read(), and write().

CLogicStateMachine* NLLOGIC::CLogicState::_LogicStateMachine
 

state machine containing this state

Definition at line 73 of file logic_state.h.

std::multimap<NLMISC::CEntityId, NLNET::CMessage> NLLOGIC::CLogicState::_MessagesToSend
 

messages to send by the service

Definition at line 76 of file logic_state.h.

Referenced by exitState(), getMessagesToSend(), trySendEntryMessages(), and trySendEventMessages().

std::string NLLOGIC::CLogicState::_StateName
 

state name

Definition at line 61 of file logic_state.h.

Referenced by CLogicState(), getName(), read(), setName(), and write().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 12:54:18 2004 for NeL by doxygen 1.3.6