RTC Toolkit  1.0.0
stateMachineEngine.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
14 
17 
18 #include <rad/signal.hpp>
19 #include <rad/smAdapter.hpp>
20 
21 namespace rtctk::componentFramework {
22 
23 using ActionMethod = std::function<void(scxml4cpp::Context* c)>;
24 using GuardMethod = std::function<bool(scxml4cpp::Context* c)>;
25 using ActivityMethod = std::function<void(StopToken f)>;
26 using SuccessMethod = std::function<void()>;
27 using FailureMethod = std::function<void(std::exception_ptr)>;
28 using RejectMethod = std::function<void(const rad::AnyEvent&, const std::string& state)>;
29 using StateMethod = std::function<void(const std::string&)>;
30 
32 public:
33  StateMachineEngine(rad::UniqueEvent exit_event);
35 
44  void RegisterModel(const std::string& name, const std::string& model);
45 
56  void AppendModel(const std::string& name, const std::string& model);
57 
67  void RegisterAction(std::string const& id, ActionMethod action);
68 
78  template <class Event>
79  void RegisterActionStatic(std::string const& id, std::function<void(Event const&)> action) {
80  assert(action);
81  RegisterAction(id, [action = std::move(action)](scxml4cpp::Context* ctx) {
82  assert(ctx);
83  assert(ctx->getLastEvent());
84  rad::SMEvent& ev = static_cast<rad::SMEvent&>(*ctx->getLastEvent());
85  action(static_cast<Event const&>(ev.GetEv()));
86  });
87  }
88 
100  void RegisterGuard(std::string const& id, GuardMethod guard);
101 
111  template <class Event>
112  void RegisterGuardStatic(std::string const& id, std::function<bool(Event const&)> guard) {
113  assert(guard);
114  RegisterGuard(id, [guard = std::move(guard)](scxml4cpp::Context* ctx) -> bool {
115  assert(ctx);
116  assert(ctx->getLastEvent());
117  assert(ctx->getLastEvent()->getId() == Event::ID);
118  rad::SMEvent& ev = static_cast<rad::SMEvent&>(*ctx->getLastEvent());
119  return guard(static_cast<Event const&>(ev.GetEv()));
120  });
121  }
122 
138  void RegisterActivity(std::string const& id,
139  ActivityMethod activity,
140  SuccessMethod on_success,
141  FailureMethod on_failure);
142 
153  void RegisterRejectHandler(std::string const& id, RejectMethod reject);
154 
162  template <typename T, typename Ex>
164  RegisterRejectHandler(T::ID, [](const rad::AnyEvent& event, std::string const& state) {
165  if(auto req=rad::GetPayloadNothrow<T>(event); req) {
166  req->SetException(Ex{T::ID, state});
167  }
168  });
169  }
170 
179  void RegisterStateChangeHandler(StateMethod on_statechange);
180 
186  void PostEvent(rad::SharedEvent s);
187 
193  std::string GetState();
194 
200  void Work();
201 
205  void Stop();
206 
207 private:
208  boost::asio::io_service m_io_service;
209  scxml4cpp::Context m_fsm_ctx;
210  scxml4cpp::EventQueue m_event_queue;
211  rad::SMAdapter m_fsm_engine;
212  rad::Signal m_signal;
213 
214  struct Model {
215  std::string name;
216  std::string model;
217  };
218 
219  Model m_core_model;
220  std::list<Model> m_models_to_append;
221  std::list<scxml4cpp::Action*> m_action_list;
222  std::list<scxml4cpp::Activity*> m_activity_list;
223 };
224 
225 template <typename EVENT>
226 std::shared_ptr<typename EVENT::payload_t> GetPayloadNothrow(scxml4cpp::Context* c) {
227  rad::SMEvent* ev = dynamic_cast<rad::SMEvent*>(c->getLastEvent());
228  if (ev == nullptr) {
229  return nullptr;
230  } // Action has no event
231 
232  auto req = rad::GetPayloadNothrow<EVENT>(ev->GetEv());
233  if (req == nullptr) {
234  return nullptr;
235  } // Event has no associated request
236 
237  return std::make_shared<typename EVENT::payload_t>(*req);
238 }
239 
240 } // namespace rtctk::componentFramework
241 
242 #endif // RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::componentFramework::StateMachineEngine::AppendModel
void AppendModel(const std::string &name, const std::string &model)
Append state machine model fragment.
Definition: stateMachineEngine.cpp:54
rtctk::componentFramework::SuccessMethod
std::function< void()> SuccessMethod
Definition: stateMachineEngine.hpp:26
rtctk::componentFramework::StateMachineEngine::Work
void Work()
Runs the event loop of the state machine.
Definition: stateMachineEngine.cpp:130
wscript.name
name
Definition: wscript:15
rtctk::componentFramework::StateMachineEngine::RegisterModel
void RegisterModel(const std::string &name, const std::string &model)
Register a new state machine model.
Definition: stateMachineEngine.cpp:49
rtctk::componentFramework::ActivityMethod
std::function< void(StopToken f)> ActivityMethod
Definition: stateMachineEngine.hpp:25
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::RejectMethod
std::function< void(const rad::AnyEvent &, const std::string &state)> RejectMethod
Definition: stateMachineEngine.hpp:28
rtctk::componentFramework::StateMachineEngine::RegisterGuardStatic
void RegisterGuardStatic(std::string const &id, std::function< bool(Event const &)> guard)
Register guard for statically known event type.
Definition: stateMachineEngine.hpp:112
rtctk::componentFramework::FailureMethod
std::function< void(std::exception_ptr)> FailureMethod
Definition: stateMachineEngine.hpp:27
rtctk::componentFramework::StateMachineEngine::RegisterGuard
void RegisterGuard(std::string const &id, GuardMethod guard)
Register guard.
Definition: stateMachineEngine.cpp:74
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:19
rtctk::componentFramework::StateMachineEngine::RegisterActionStatic
void RegisterActionStatic(std::string const &id, std::function< void(Event const &)> action)
Register action for statically known event type.
Definition: stateMachineEngine.hpp:79
rtctk::componentFramework::StateMachineEngine
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::StateMachineEngine::RegisterAction
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:62
rtctk::componentFramework::StateMachineEngine::~StateMachineEngine
~StateMachineEngine()
Definition: stateMachineEngine.cpp:39
rtctk::componentFramework::StateMethod
std::function< void(const std::string &)> StateMethod
Definition: stateMachineEngine.hpp:29
rtctk::componentFramework::StateMachineEngine::RegisterRejectHandler
void RegisterRejectHandler()
Register default reject handler.
Definition: stateMachineEngine.hpp:163
rtctk::componentFramework::GuardMethod
std::function< bool(scxml4cpp::Context *c)> GuardMethod
Definition: stateMachineEngine.hpp:24
stopToken.hpp
A simple Stop Token.
rtctk::componentFramework::StateMachineEngine::RegisterStateChangeHandler
void RegisterStateChangeHandler(StateMethod on_statechange)
Register state changed handler.
Definition: stateMachineEngine.cpp:108
rtctk::componentFramework::StateMachineEngine::GetState
std::string GetState()
Queries the current state.
Definition: stateMachineEngine.cpp:126
rtctk::componentFramework::GetPayloadNothrow
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition: stateMachineEngine.hpp:226
rtctk::componentFramework::StateMachineEngine::PostEvent
void PostEvent(rad::SharedEvent s)
Injects a new event into the state machine engine.
Definition: stateMachineEngine.cpp:121
rtctk::componentFramework::StateMachineEngine::StateMachineEngine
StateMachineEngine(rad::UniqueEvent exit_event)
Definition: stateMachineEngine.cpp:30
rtctk::componentFramework::StateMachineEngine::Stop
void Stop()
Stops execution of the state machine event loop.
Definition: stateMachineEngine.cpp:152
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::ActionMethod
std::function< void(scxml4cpp::Context *c)> ActionMethod
Definition: stateMachineEngine.hpp:23