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