otsdaq  v2_05_02_indev
VStateMachine.h
1 #ifndef _ots_VStateMachine_h_
2 #define _ots_VStateMachine_h_
3 
4 #include <string>
5 
6 namespace ots
7 {
8 class CoreSupervisorBase;
9 
11 {
12  public:
13  VStateMachine(const std::string& name) : iterationIndex_(0), subIterationIndex_(0), iterationWorkFlag_(false), subIterationWorkFlag_(false), name_(name)
14  {
15  ;
16  }
17  virtual ~VStateMachine(void) { ; }
18 
19  // Transitions
20  virtual void configure(void) = 0;
21  virtual void halt(void) = 0;
22  virtual void pause(void) = 0;
23  virtual void resume(void) = 0;
24  virtual void start(std::string runNumber) = 0;
25  virtual void stop(void) = 0;
26 
27  // States
28  virtual bool running(void) { return false; }
29  virtual void paused(void) { ; }
30  virtual void halted(void) { ; }
31  virtual void configured(void) { ; }
32  virtual void initial(void) { ; }
33  virtual void inError(void) { ; }
34 
35  // Status
36  //==============================================================================
37  // virtual progress detail string that can be overridden with more info
38  // e.g. step and sub-step aliases, etc
39  virtual std::string getStatusProgressDetail(void)
40  {
41  std::string progress = "";
42 
43  if(VStateMachine::getSubIterationWork())
44  {
45  // sub-steps going on
46  progress += name_ + ":";
47 
48  // check for iteration alias
49  if(iterationAliasMap_.find(transitionName_) != iterationAliasMap_.end() &&
50  iterationAliasMap_.at(transitionName_).find(VStateMachine::getIterationIndex()) != iterationAliasMap_.at(transitionName_).end())
51  progress += iterationAliasMap_.at(transitionName_).at(VStateMachine::getIterationIndex());
52  else
53  progress += std::to_string(VStateMachine::getIterationIndex()); // just index
54 
55  progress += ":";
56 
57  // check for sib-iteration alias
58  if(subIterationAliasMap_.find(transitionName_) != subIterationAliasMap_.end() &&
59  subIterationAliasMap_.at(transitionName_).find(VStateMachine::getSubIterationIndex()) != subIterationAliasMap_.at(transitionName_).end())
60  progress += subIterationAliasMap_.at(transitionName_).at(VStateMachine::getSubIterationIndex());
61  else
62  progress += std::to_string(VStateMachine::getSubIterationIndex()); // just index
63  }
64  else if(VStateMachine::getIterationWork())
65  {
66  // steps going on
67  progress += name_ + ":";
68 
69  // check for iteration alias
70  if(iterationAliasMap_.find(transitionName_) != iterationAliasMap_.end() &&
71  iterationAliasMap_.at(transitionName_).find(VStateMachine::getIterationIndex()) != iterationAliasMap_.at(transitionName_).end())
72  progress += iterationAliasMap_.at(transitionName_).at(VStateMachine::getIterationIndex());
73  else
74  progress += std::to_string(VStateMachine::getIterationIndex()); // just index
75  }
76  else if(transitionName_ != "")
77  progress += name_ + ":" + transitionName_;
78  // else Done
79 
80  if(progress.size())
81  __COUTV__(progress);
82 
83  return progress;
84  } // end getStatusProgressDetail()
85 
86  void setTransitionName (const std::string& transitionName) { transitionName_ = transitionName; }
87  const std::string& getTransitionName (void) {return transitionName_;}
88  void setIterationIndex (unsigned int i) { iterationIndex_ = i; }
89  void setSubIterationIndex (unsigned int i) { subIterationIndex_ = i; }
90  unsigned int getIterationIndex (void) { return iterationIndex_; }
91  unsigned int getSubIterationIndex(void) { return subIterationIndex_; }
92  void indicateIterationWork(void) { iterationWorkFlag_ = true; }
93  void clearIterationWork(void) { iterationWorkFlag_ = false; }
94  bool getIterationWork(void) { return iterationWorkFlag_; }
95  void indicateSubIterationWork(void) { subIterationWorkFlag_ = true; }
96  void clearSubIterationWork(void) { subIterationWorkFlag_ = false; }
97  bool getSubIterationWork(void) { return subIterationWorkFlag_; }
98 
99  CoreSupervisorBase* parentSupervisor_; // e.g. to communicate error fault and start
100  // transition to error for entire system
101  protected:
102  std::map<std::string /*transition*/, std::map<unsigned int /*step index*/, std::string /*step alias*/>> iterationAliasMap_;
103  std::map<std::string /*transition*/, std::map<unsigned int /*step index*/, std::string /*step alias*/>> subIterationAliasMap_;
104 
105  private:
106  unsigned int iterationIndex_, subIterationIndex_;
107  bool iterationWorkFlag_, subIterationWorkFlag_;
108  const std::string name_;
109  std::string transitionName_;
110 };
111 
112 } // namespace ots
113 
114 #endif