RTC Toolkit  1.0.0
loopaware.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_LOOPAWARE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_LOOPAWARE_HPP
14 
18 
19 namespace rtctk::componentFramework {
20 
21 template <typename Super>
22 struct Runnable;
23 
29 template <typename Super>
30 struct Loopaware : Super {
31  static_assert(std::is_base_of_v<RtcComponent, Super>, "'Loopaware' requires 'RtcComponent'");
32  static_assert(not is_base_of_template_v<Runnable, Super>, "'Loopaware' excludes 'Runnable'");
33 
34  class BizLogicIf : public Super::BizLogicIf {
35  public:
36  virtual void ActivityGoingRunning(StopToken st) {};
37  virtual void ActivityGoingIdle(StopToken st) {};
38  virtual void ActivityRecovering(StopToken st) {};
39  virtual void ActivityOpening(StopToken st) {};
40  virtual void ActivityClosing(StopToken st) {};
41  virtual void ActivityOpened(StopToken st) {};
42  virtual void ActivityClosed(StopToken st) {};
43  };
44 
45  class InputStage : public Super::InputStage {
46  public:
47  using Super::InputStage::InputStage;
48 
49  void Start() override {
50  Super::InputStage::Start();
51 
52  FuncCmdsImpl::Register(this->m_replier, this->m_engine);
53  LoopCmdsImpl::Register(this->m_replier, this->m_engine);
54  }
55  };
56 
57  class OutputStage : public Super::OutputStage {
58  public:
60  // Handlers ###########################################################################
61 
62  engine.RegisterRejectHandler<events::Run, RequestRejected>();
63  engine.RegisterRejectHandler<events::Idle, RequestRejected>();
64  engine.RegisterRejectHandler<events::Open, RequestRejected>();
65  engine.RegisterRejectHandler<events::Close, RequestRejected>();
66  engine.RegisterRejectHandler<events::Recover, RequestRejected>();
67 
68  // No Disable in states ###############################################################
69 
70  this->m_no_disable_in_states.push_back("On.Operational.Error");
71  this->m_no_disable_in_states.push_back("On.Operational.Recovering");
72  this->m_no_disable_in_states.push_back("On.Operational.GoingRunning");
73  this->m_no_disable_in_states.push_back("On.Operational.GoingIdle");
74  this->m_no_disable_in_states.push_back("On.Operational.Opened");
75  this->m_no_disable_in_states.push_back("On.Operational.Closed");
76  this->m_no_disable_in_states.push_back("On.Operational.Opening");
77  this->m_no_disable_in_states.push_back("On.Operational.Closing");
78 
79  // No Update in states ################################################################
80 
81  this->m_no_update_in_states.push_back("On.Operational.Error");
82  this->m_no_update_in_states.push_back("On.Operational.Recovering");
83  this->m_no_update_in_states.push_back("On.Operational.GoingRunning");
84  this->m_no_update_in_states.push_back("On.Operational.GoingIdle");
85  this->m_no_update_in_states.push_back("On.Operational.Opening");
86  this->m_no_update_in_states.push_back("On.Operational.Closing");
87 
88  // Actions ############################################################################
89 
90  engine.RegisterAction("ActionGoingRunningEntry", [this](auto c) {
91  this->m_tmp_request = GetPayloadNothrow<events::Run>(c);
92  });
93 
94  engine.RegisterAction("ActionGoingRunningDone", [this](auto c) {
95  if (this->m_tmp_request) {
96  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
97  this->m_tmp_request = nullptr;
98  }
99  });
100 
101  engine.RegisterAction("ActionGoingIdleEntry", [this](auto c) {
102  this->m_tmp_request = GetPayloadNothrow<events::Idle>(c);
103  });
104 
105  engine.RegisterAction("ActionGoingIdleDone", [this](auto c) {
106  if (this->m_tmp_request) {
107  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
108  this->m_tmp_request = nullptr;
109  }
110  });
111 
112  engine.RegisterAction("ActionOpeningEntry", [this](auto c) {
113  this->m_tmp_request = GetPayloadNothrow<events::Open>(c);
114  });
115 
116  engine.RegisterAction("ActionOpeningDone", [this](auto c) {
117  if (this->m_tmp_request) {
118  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
119  this->m_tmp_request = nullptr;
120  }
121  });
122 
123  engine.RegisterAction("ActionClosingEntry", [this](auto c) {
124  this->m_tmp_request = GetPayloadNothrow<events::Close>(c);
125  });
126 
127  engine.RegisterAction("ActionClosingDone", [this](auto c) {
128  if (this->m_tmp_request) {
129  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
130  this->m_tmp_request = nullptr;
131  }
132  });
133 
134  engine.RegisterAction("ActionErrorEntry", [this](auto c) {
135  if(auto eptr = GetPayloadNothrow<events::Error>(c); eptr) {
136  if (this->m_tmp_request) {
137  this->m_tmp_request->SetException(
139  this->m_tmp_request = nullptr;
140  }
141  }
142  });
143 
144  engine.RegisterAction("ActionRecoveringEntry", [this](auto c) {
145  this->m_tmp_request = GetPayloadNothrow<events::Recover>(c);
146  });
147 
148  engine.RegisterAction("ActionRecoveringDone", [this](auto c) {
149  if (this->m_tmp_request) {
150  this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
151  this->m_tmp_request = nullptr;
152  }
153  });
154 
155  // Activities #########################################################################
156 
157  engine.RegisterActivity("ActivityGoingRunning",
158  [this](StopToken stop_token) {
159  static_cast<BizLogicIf&>(this->m_logic).ActivityGoingRunning(stop_token);
160  }, this->m_success_handler, this->m_error_handler);
161 
162  engine.RegisterActivity("ActivityGoingIdle",
163  [this](StopToken stop_token) {
164  static_cast<BizLogicIf&>(this->m_logic).ActivityGoingIdle(stop_token);
165  }, this->m_success_handler, this->m_error_handler);
166 
167  engine.RegisterActivity("ActivityOpening",
168  [this](StopToken stop_token) {
169  static_cast<BizLogicIf&>(this->m_logic).ActivityOpening(stop_token);
170  }, this->m_success_handler, this->m_error_handler);
171 
172  engine.RegisterActivity("ActivityClosing",
173  [this](StopToken stop_token) {
174  static_cast<BizLogicIf&>(this->m_logic).ActivityClosing(stop_token);
175  }, this->m_success_handler, this->m_error_handler);
176 
177  engine.RegisterActivity("ActivityOpened",
178  [this](StopToken stop_token) {
179  static_cast<BizLogicIf&>(this->m_logic).ActivityOpened(stop_token);
180  }, {}, this->m_error_handler);
181 
182  engine.RegisterActivity("ActivityClosed",
183  [this](StopToken stop_token) {
184  static_cast<BizLogicIf&>(this->m_logic).ActivityClosed(stop_token);
185  }, {}, this->m_error_handler);
186 
187  engine.RegisterActivity("ActivityRecovering",
188  [this](StopToken stop_token) {
189  static_cast<BizLogicIf&>(this->m_logic).ActivityRecovering(stop_token);
190  }, this->m_success_handler, this->m_error_handler);
191  }
192  };
193 
194  class ModelBuilder : public Super::ModelBuilder {
195  public:
197  this->RegisterLayer({"Loopaware", {}});
198 
199  const std::string parent_region = "On.Operational.RegionMain";
200 
201  this->mm.AddState(Composite, parent_region, "On.Operational");
202 
203  this->mm.AddState(Initial,"On.Operational.Initial", parent_region);
204  this->mm.AddState(Simple, "On.Operational.Idle", parent_region);
205  this->mm.AddState(Simple, "On.Operational.Error", parent_region, "" , "ActionErrorEntry");
206  this->mm.AddState(Simple, "On.Operational.Recovering", parent_region, "ActivityRecovering" , "ActionRecoveringEntry");
207  this->mm.AddState(Simple, "On.Operational.GoingRunning", parent_region, "ActivityGoingRunning" , "ActionGoingRunningEntry");
208  this->mm.AddState(Simple, "On.Operational.GoingIdle", parent_region, "ActivityGoingIdle" , "ActionGoingIdleEntry");
209  this->mm.AddState(Simple, "On.Operational.Opened", parent_region, "ActivityOpened");
210  this->mm.AddState(Simple, "On.Operational.Closed", parent_region, "ActivityClosed");
211  this->mm.AddState(Simple, "On.Operational.Opening", parent_region, "ActivityOpening" , "ActionOpeningEntry");
212  this->mm.AddState(Simple, "On.Operational.Closing", parent_region, "ActivityClosing" , "ActionClosingEntry");
213 
214  this->mm.AddTrans("On.Operational.Initial" , "On.Operational.Idle" );
215  this->mm.AddTrans(parent_region , "On.Operational.Error" , "events.Error");
216 
217  this->mm.AddTrans("On.Operational.Error" , "On.Operational.Recovering" , "events.Recover");
218  this->mm.AddTrans("On.Operational.Recovering" , "On.Operational.Idle" , "events.Done", "" , "ActionRecoveringDone");
219 
220  this->mm.AddTrans("On.Operational.Idle" , "On.Operational.GoingRunning", "events.Run");
221  this->mm.AddTrans("On.Operational.GoingRunning" , "On.Operational.Opened" , "events.Done", "" , "ActionGoingRunningDone");
222 
223  this->mm.AddTrans("On.Operational.Opened" , "On.Operational.GoingIdle" , "events.Idle");
224  this->mm.AddTrans("On.Operational.GoingIdle" , "On.Operational.Idle" , "events.Done", "" , "ActionGoingIdleDone");
225 
226  this->mm.AddTrans("On.Operational.Opened" , "On.Operational.Closing" , "events.Close");
227  this->mm.AddTrans("On.Operational.Closing" , "On.Operational.Closed" , "events.Done", "" , "ActionClosingDone");
228  this->mm.AddTrans("On.Operational.Closed" , "On.Operational.Opening" , "events.Open");
229  this->mm.AddTrans("On.Operational.Opening" , "On.Operational.Opened" , "events.Done", "" , "ActionOpeningDone");
230  }
231  };
232 };
233 
234 } // namespace rtctk::componentFramework
235 
236 #endif
rtctk::componentFramework::Simple
@ Simple
Definition: model.hpp:23
rtctk::componentFramework::RequestRejected
Definition: stdComponent.hpp:52
rtctk::componentFramework::Loopaware
Life cycle extension to make RtcComponent Loopaware.
Definition: loopaware.hpp:30
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityOpened
virtual void ActivityOpened(StopToken st)
Definition: loopaware.hpp:41
rtctk::componentFramework::Loopaware::InputStage
Definition: loopaware.hpp:45
rtctk::componentFramework::Loopaware::OutputStage::OutputStage
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: loopaware.hpp:59
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::rtcSupervisor::Super
Runnable< RtcComponent > Super
Definition: rtcSupervisor.hpp:31
rtctk::componentFramework::Loopaware::ModelBuilder::ModelBuilder
ModelBuilder(StateMachineEngine &engine)
Definition: loopaware.hpp:196
rtctk::componentFramework::Loopaware::BizLogicIf
Definition: loopaware.hpp:34
rtctk::componentFramework::STD_OK_REPLY
const std::string STD_OK_REPLY
Definition: stdComponent.hpp:36
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:19
rtctk::componentFramework::StateMachineEngine
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::NestedExceptionPrinter
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: exceptions.hpp:157
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:62
rtctk::componentFramework::FuncCmdsImpl::Register
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: rtcCmdsImpl.hpp:34
loopCmdsImpl.hpp
Implementation of MAL commands for layer 'Loopaware'.
rtctk::componentFramework::Loopaware::InputStage::Start
void Start() override
Definition: loopaware.hpp:49
rtctk::componentFramework::Loopaware::ModelBuilder
Definition: loopaware.hpp:194
rtctk::componentFramework::NestedExceptionPrinter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: exceptions.hpp:175
rtctk::componentFramework::LoopCmdsImpl::Register
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition: loopCmdsImpl.hpp:34
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:95
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityRecovering
virtual void ActivityRecovering(StopToken st)
Definition: loopaware.hpp:38
stopToken.hpp
A simple Stop Token.
rtctk::componentFramework::Initial
@ Initial
Definition: model.hpp:26
rtcComponent.hpp
Lifecycle of a basic 'RtcComponent'.
rtctk::componentFramework::RequestFailed
Definition: stdComponent.hpp:65
rtctk::componentFramework::Loopaware::OutputStage
Definition: loopaware.hpp:57
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityGoingIdle
virtual void ActivityGoingIdle(StopToken st)
Definition: loopaware.hpp:37
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityClosing
virtual void ActivityClosing(StopToken st)
Definition: loopaware.hpp:40
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityGoingRunning
virtual void ActivityGoingRunning(StopToken st)
Definition: loopaware.hpp:36
rtctk::componentFramework::Composite
@ Composite
Definition: model.hpp:25
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityClosed
virtual void ActivityClosed(StopToken st)
Definition: loopaware.hpp:42
rtctk::componentFramework::StateMachineEngine::RegisterActivity
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:87
rtctk::componentFramework::Loopaware::BizLogicIf::ActivityOpening
virtual void ActivityOpening(StopToken st)
Definition: loopaware.hpp:39