Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

StateNode.cc

Go to the documentation of this file.
00001 #include "StateNode.h"
00002 #include "Events/EventRouter.h"
00003 #include "Wireless/Wireless.h"
00004 
00005 StateNode::~StateNode() {
00006   ASSERT(!isActive(), "Destructing while active?")
00007   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++)
00008     (*it)->RemoveReference();
00009   if(issetup) {
00010     teardown();
00011     if(issetup) {
00012       serr->printf("WARNING %s doesn't seem to call StateNode::teardown() in its\n"
00013                    "        implementation of the function: issetup=%d, nodes.size()=%lu\n"
00014                    "        Attempting to recover...\n",getClassName().c_str(),issetup,(unsigned long)nodes.size());
00015       StateNode::teardown();
00016     }
00017   }
00018 }
00019 
00020 Transition* StateNode::addTransition(Transition* trans) {
00021   transitions.push_back(trans);
00022   trans->AddReference();
00023   trans->addSource(this);
00024   return trans;
00025 }
00026 
00027 StateNode* StateNode::addNode(StateNode* node) {
00028   nodes.push_back(node);
00029   node->AddReference();
00030   if ( node->parent == NULL )
00031     node->parent = this;
00032   return node;
00033 }
00034 
00035 void StateNode::DoStart() {
00036   if ( parent == NULL && transitions.size() > 0 )
00037     serr->printf("WARNING StateNode '%s' has transitions but no parent; you probably forgot to call addNode().\n",getName().c_str());
00038   BehaviorBase::DoStart();
00039   if(!issetup) {
00040     setup();
00041     issetup=true;
00042   }
00043   postStartEvent();
00044   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++) {
00045     if ( !(*it)->isActive()  )
00046       (*it)->DoStart();
00047     if(!isActive()) //a transition fired upon its start
00048       break;
00049   }
00050 }
00051 
00052 void StateNode::DoStop() {
00053   for(std::vector<Transition*>::iterator it=transitions.begin(); it!=transitions.end(); it++) {
00054     if((*it)->isActive())
00055       (*it)->DoStop();
00056   }
00057   for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00058     if((*it)->isActive())
00059       (*it)->DoStop();
00060   if(!retain && issetup) {
00061     teardown();
00062     if(issetup) {
00063       serr->printf("WARNING %s doesn't seem to call StateNode::teardown() in its\n"
00064                    "        implementation of the function: issetup=%d, nodes.size()=%lu\n"
00065                    "        Attempting to recover...\n",getClassName().c_str(),issetup,(unsigned long)nodes.size());
00066       StateNode::teardown();
00067     }
00068   }
00069   postStopEvent();
00070   BehaviorBase::DoStop();
00071 }
00072 
00073 void StateNode::teardown() {
00074   for(std::vector<StateNode*>::iterator it=nodes.begin(); it!=nodes.end(); it++)
00075     (*it)->RemoveReference();
00076   nodes.clear();
00077   issetup=false;
00078   /*std::cout << "Teardown!!!!!!!!" << std::endl;*/
00079 }
00080 
00081 void StateNode::postStartEvent() {
00082   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<size_t>(this),EventBase::activateETID,0,getName(),1);
00083 }
00084 
00085 void StateNode::postCompletionEvent(float magnitude/*=0*/) {
00086   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<size_t>(this),EventBase::statusETID,get_time()-startedTime,getName(),magnitude);
00087 }
00088 
00089 void StateNode::postStopEvent() {
00090   erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<size_t>(this),EventBase::deactivateETID,get_time()-startedTime,getName(),0);
00091 }
00092 
00093 /*! @file 
00094  * @brief Describes StateNode, which is both a state machine controller as well as a node within a state machine itself
00095  * @author ejt (Creator)
00096  *
00097  * $Author: ejt $
00098  * $Name: tekkotsu-4_0 $
00099  * $Revision: 1.26 $
00100  * $State: Exp $
00101  * $Date: 2005/11/08 21:36:06 $
00102  */
00103 

Tekkotsu v4.0
Generated Thu Nov 22 00:54:56 2007 by Doxygen 1.5.4