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())
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
00079 }
00080
00081 void StateNode::postStartEvent() {
00082 erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::activateETID,0,getName(),1);
00083 }
00084
00085 void StateNode::postCompletionEvent(float magnitude) {
00086 erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::statusETID,get_time()-startedTime,getName(),magnitude);
00087 }
00088
00089 void StateNode::postStopEvent() {
00090 erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::deactivateETID,get_time()-startedTime,getName(),0);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103