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