RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
runnable.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
14#define RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
15
16#include <Rtctkif.hpp>
19
21
22template <typename Super>
23struct Loopaware;
24
30template <typename Super>
31struct Runnable : Super {
32 static_assert(std::is_base_of_v<RtcComponent, Super>, "'Runnable' requires 'RtcComponent'");
33 static_assert(not is_base_of_template_v<Loopaware, Super>, "'Runnable' excludes 'Loopaware'");
34
35 class BizLogicIf : public Super::BizLogicIf {
36 public:
38 virtual void ActivityGoingIdle(StopToken st) {};
39 virtual void ActivityRunning(StopToken st) {};
41 };
42
43 class InputStage : public Super::InputStage {
44 public:
45 using Super::InputStage::InputStage;
46
47 void Start() override {
48 Super::InputStage::Start();
49
50 FuncCmdsImpl::Register(this->m_replier, this->m_engine);
51 RecoverCmdsImpl::Register(this->m_replier, this->m_engine);
52 }
53 };
54
55 class OutputStage : public Super::OutputStage {
56 public:
58 // Handlers ###################################################################
59
60 engine.RegisterRejectHandler<events::Run, RequestRejected>();
61 engine.RegisterRejectHandler<events::Idle, RequestRejected>();
62 engine.RegisterRejectHandler<events::Recover, RequestRejected>();
63
64 // No Disable in states ###############################################################
65
66 this->m_no_disable_in_states.push_back("On::Operational::Error");
67 this->m_no_disable_in_states.push_back("On::Operational::Recovering");
68 this->m_no_disable_in_states.push_back("On::Operational::GoingRunning");
69 this->m_no_disable_in_states.push_back("On::Operational::GoingIdle");
70 this->m_no_disable_in_states.push_back("On::Operational::Running");
71
72 // No Update in states ################################################################
73
74 this->m_no_update_in_states.push_back("On::Operational::GoingRunning");
75 this->m_no_update_in_states.push_back("On::Operational::GoingIdle");
76 this->m_no_update_in_states.push_back("On::Operational::Error");
77 this->m_no_update_in_states.push_back("On::Operational::Recovering");
78
79 // Run #####################################################################
80
81 engine.RegisterAction("ActionGoingRunningEntry", [this](auto c) {
82 this->m_tmp_request = GetPayloadNothrow<events::Run>(c);
83 });
84
85 engine.RegisterAction("ActionGoingRunningDone", [this](auto c) {
86 if (this->m_tmp_request) {
87 this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
88 this->m_tmp_request = nullptr;
89 }
90 });
91
92 // Idle #####################################################################
93
94 engine.RegisterAction("ActionGoingIdleEntry", [this](auto c) {
95 this->m_tmp_request = GetPayloadNothrow<events::Idle>(c);
96 });
97
98 engine.RegisterAction("ActionGoingIdleDone", [this](auto c) {
99 if (this->m_tmp_request) {
100 this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
101 this->m_tmp_request = nullptr;
102 }
103 });
104
105 // Error #####################################################################
106
107 engine.RegisterAction("ActionErrorEntry", [this](auto c) {
109 if (this->m_tmp_request) {
110 this->m_tmp_request->SetException(
112 this->m_tmp_request = nullptr;
113 }
114 }
115 });
116
117 // Recover #####################################################################
118
119 engine.RegisterAction("ActionRecoveringEntry", [this](auto c) {
120 this->m_tmp_request = GetPayloadNothrow<events::Recover>(c);
121 });
122
123 engine.RegisterAction("ActionRecoveringDone", [this](auto c) {
124 if (this->m_tmp_request) {
125 this->m_tmp_request->SetReplyValue(STD_OK_REPLY);
126 this->m_tmp_request = nullptr;
127 }
128 });
129
130 // Activities #####################################################################
131
132 engine.RegisterActivity(
133 "ActivityGoingRunning",
134 [this](StopToken stop_token) {
135 static_cast<BizLogicIf&>(this->m_logic).ActivityGoingRunning(stop_token);
136 },
137 this->m_success_handler,
138 this->m_error_handler);
139
140 engine.RegisterActivity(
141 "ActivityGoingIdle",
142 [this](StopToken stop_token) {
143 static_cast<BizLogicIf&>(this->m_logic).ActivityGoingIdle(stop_token);
144 },
145 this->m_success_handler,
146 this->m_error_handler);
147
148 engine.RegisterActivity(
149 "ActivityRunning",
150 [this](StopToken stop_token) {
151 static_cast<BizLogicIf&>(this->m_logic).ActivityRunning(stop_token);
152 },
153 this->m_success_handler,
154 this->m_error_handler);
155
156 engine.RegisterActivity(
157 "ActivityRecovering",
158 [this](StopToken stop_token) {
159 static_cast<BizLogicIf&>(this->m_logic).ActivityRecovering(stop_token);
160 },
161 this->m_success_handler,
162 this->m_error_handler);
163 }
164 };
165
166 struct ModelBuilder : public Super::ModelBuilder {
167 public:
169 // clang-format off
170 this->mm.ModStateType("On::Operational", Parallel);
171
172 const std::string parent_region = "On::Operational:";
173
174 this->mm.AddState(Composite, parent_region, "On::Operational");
175
176 this->mm.AddState(Initial, "On::Operational::Initial", parent_region);
177 this->mm.AddState(Simple, "On::Operational::Idle", parent_region);
178 this->mm.AddState(Simple, "On::Operational::Error", parent_region ,"" ,"ActionErrorEntry");
179 this->mm.AddState(Simple, "On::Operational::Running", parent_region ,"ActivityRunning");
180 this->mm.AddState(Simple, "On::Operational::Recovering", parent_region ,"ActivityRecovering" ,"ActionRecoveringEntry");
181 this->mm.AddState(Simple, "On::Operational::GoingRunning", parent_region ,"ActivityGoingRunning" ,"ActionGoingRunningEntry");
182 this->mm.AddState(Simple, "On::Operational::GoingIdle", parent_region ,"ActivityGoingIdle" ,"ActionGoingIdleEntry");
183
184 this->mm.AddTrans("On::Operational::Initial" , "On::Operational::Idle" );
185 this->mm.AddTrans(parent_region , "On::Operational::Error" , "events.Error");
186
187 this->mm.AddTrans("On::Operational::Error" , "On::Operational::Recovering" , "events.Recover");
188 this->mm.AddTrans("On::Operational::Recovering" , "On::Operational::Idle" , "events.Done", "" ,"ActionRecoveringDone");
189
190 this->mm.AddTrans("On::Operational::Idle" , "On::Operational::GoingRunning" , "events.Run");
191 this->mm.AddTrans("On::Operational::GoingRunning" , "On::Operational::Running" , "events.Done", "" ,"ActionGoingRunningDone");
192
193 this->mm.AddTrans("On::Operational::Running" , "On::Operational::GoingIdle" , "events.Idle");
194 this->mm.AddTrans("On::Operational::GoingIdle" , "On::Operational::Idle" , "events.Done", "" ,"ActionGoingIdleDone");
195
196 this->mm.AddTrans("On::Operational::Running" , "On::Operational::GoingIdle" , "events.Done");
197 // clang-format on
198 }
199 };
200};
201
202} // namespace rtctk::componentFramework
203
204#endif // RTCTK_COMPONENTFRAMEWORK_RUNABLE_HPP
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition rtcCmdsImpl.hpp:37
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:159
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:177
static void Register(CommandReplier &replier, StateMachineEngine &engine)
Definition rtcCmdsImpl.hpp:68
Thrown if the command was accepted but the task to run failed.
Definition rtcComponent.hpp:53
Thrown if a command is not allowed in current state or guard.
Definition rtcComponent.hpp:40
virtual void ActivityGoingRunning(StopToken st)
Definition runnable.hpp:37
virtual void ActivityRunning(StopToken st)
Definition runnable.hpp:39
virtual void ActivityRecovering(StopToken st)
Definition runnable.hpp:40
virtual void ActivityGoingIdle(StopToken st)
Definition runnable.hpp:38
void Start() override
Definition runnable.hpp:47
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition runnable.hpp:57
Definition stateMachineEngine.hpp:35
Definition commandReplier.cpp:22
@ Simple
Definition model.hpp:23
@ Composite
Definition model.hpp:23
@ Parallel
Definition model.hpp:23
@ Initial
Definition model.hpp:23
rad::StopToken StopToken
Definition stopToken.hpp:20
const std::string STD_OK_REPLY
Definition stdComponent.hpp:86
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
Lifecycle of a basic 'RtcComponent'.
A simple Stop Token.
ModelBuilder(StateMachineEngine &engine)
Definition runnable.hpp:168
Life cycle extension to make RtcComponent Runnable.
Definition runnable.hpp:31