00001
00002 #ifndef INCLUDED_CompletionTrans_h_
00003 #define INCLUDED_CompletionTrans_h_
00004
00005 #include "Behaviors/StateNode.h"
00006 #include "Behaviors/Transition.h"
00007 #include "Events/EventRouter.h"
00008
00009
00010 class CompletionTrans : public Transition {
00011 protected:
00012 int minsrcs;
00013 bool *completions;
00014
00015 public:
00016 CompletionTrans(StateNode* destination, int n=0) :
00017 Transition("CompletionTrans",destination), minsrcs(n), completions(NULL) {};
00018
00019 CompletionTrans(const std::string& name, StateNode* destination, int n=0) :
00020 Transition("CompletionTrans",name,destination), minsrcs(n), completions(NULL) {};
00021
00022
00023 virtual void DoStart() {
00024 Transition::DoStart();
00025 unsigned int const numsrcs = getSources().size();
00026 completions = new bool[numsrcs];
00027 for (unsigned int i = 0; i < numsrcs; i++) {
00028 completions[i] = false;
00029 erouter->addListener(this,
00030 EventBase::stateMachineEGID,
00031 (unsigned int)getSources()[i],
00032 EventBase::statusETID);
00033 };
00034 }
00035
00036
00037 virtual void DoStop() {
00038 erouter->removeListener(this);
00039 delete completions;
00040 completions = NULL;
00041 Transition::DoStop();
00042 }
00043
00044
00045 virtual void processEvent(const EventBase &event) {
00046 int numcomplete = 0;
00047 for ( unsigned int i=0; i<getSources().size(); i++ ) {
00048 if ( event.getSourceID() == (unsigned int)getSources()[i] )
00049 completions[i] = true;
00050 if ( completions[i] ) ++numcomplete;
00051 };
00052 int const threshold = (minsrcs > 0 ? minsrcs : (int)getSources().size());
00053 if (numcomplete >= threshold) fire();
00054 }
00055
00056 protected:
00057
00058 CompletionTrans(const std::string& classname, const std::string& instancename, StateNode* destination, int n=0) :
00059 Transition(classname,instancename,destination), minsrcs(n), completions(NULL) {};
00060
00061
00062 CompletionTrans(const CompletionTrans&);
00063 CompletionTrans& operator=(const CompletionTrans&);
00064
00065
00066 };
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #endif