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
00017 CompletionTrans(StateNode* destination, int n=0) :
00018 Transition("CompletionTrans",destination), minsrcs(n), completions(NULL) {};
00019
00020
00021 CompletionTrans(const std::string& name, StateNode* destination, int n=0) :
00022 Transition("CompletionTrans",name,destination), minsrcs(n), completions(NULL) {};
00023
00024
00025 virtual void DoStart() {
00026 Transition::DoStart();
00027 unsigned int const numsrcs = getSources().size();
00028 completions = new bool[numsrcs];
00029 for (unsigned int i = 0; i < numsrcs; i++) {
00030 completions[i] = false;
00031 erouter->addListener(this,
00032 EventBase::stateMachineEGID,
00033 reinterpret_cast<size_t>(getSources()[i]),
00034 EventBase::statusETID);
00035 };
00036 }
00037
00038
00039 virtual void DoStop() {
00040 erouter->removeListener(this);
00041 delete completions;
00042 completions = NULL;
00043 Transition::DoStop();
00044 }
00045
00046
00047 virtual void processEvent(const EventBase &event) {
00048 int numcomplete = 0;
00049 for ( unsigned int i=0; i<getSources().size(); i++ ) {
00050 if ( event.getSourceID() == reinterpret_cast<size_t>(getSources()[i]) )
00051 completions[i] = true;
00052 if ( completions[i] ) ++numcomplete;
00053 };
00054 int const threshold = (minsrcs > 0 ? minsrcs : (int)getSources().size());
00055 if (numcomplete >= threshold) fire();
00056 }
00057
00058 protected:
00059
00060 CompletionTrans(const std::string& classname, const std::string& instancename, StateNode* destination, int n=0) :
00061 Transition(classname,instancename,destination), minsrcs(n), completions(NULL) {};
00062
00063
00064 CompletionTrans(const CompletionTrans&);
00065 CompletionTrans& operator=(const CompletionTrans&);
00066
00067
00068 };
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #endif