36#include <boost/function_types/function_arity.hpp>
37#include <boost/function_types/function_type.hpp>
38#include <boost/function_types/parameter_types.hpp>
40#include <smacc2_msgs/msg/smacc_status.hpp>
46template <
typename TOrthogonal>
49 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
60 "Orthogonal %s resource is being required from some state, client or component. Found "
63 orthogonalkey.c_str());
64 ret =
dynamic_cast<TOrthogonal *
>(it->second.get());
70 ss <<
"Orthogonal not found " << orthogonalkey.c_str() << std::endl;
71 ss <<
"The existing orthogonals are the following: " << std::endl;
74 ss <<
" - " << orthogonal.first << std::endl;
77 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
84template <
typename TOrthogonal>
87 std::lock_guard<std::recursive_mutex> guard(
m_mutex_);
93 auto ret = std::make_shared<TOrthogonal>();
94 orthogonals_[orthogonalkey] = dynamic_pointer_cast<smacc2::ISmaccOrthogonal>(ret);
96 ret->setStateMachine(
this);
98 RCLCPP_INFO(
getLogger(),
"%s Orthogonal is created", orthogonalkey.c_str());
103 getLogger(),
"There were already one existing orthogonal of type "
104 << orthogonalkey.c_str() <<
". Skipping creation orthogonal request. ");
105 std::stringstream ss;
106 ss <<
"The existing orthogonals are the following: " << std::endl;
109 ss <<
" - " << orthogonal.first << std::endl;
111 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
116template <
typename SmaccComponentType>
124 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
128 for (
auto & client : ortho.second->clients_)
130 storage = client->getComponent<SmaccComponentType>();
131 if (storage !=
nullptr)
139 getLogger(),
"component %s is required but it was not found in any orthogonal",
143 throw std::runtime_error(
"component is required but it was not found in any orthogonal");
146template <
typename EventType>
151#define eventtypename demangleSymbol<EventType>().c_str()
159 smacc2_msgs::msg::SmaccEvent event;
160 event.event_type = evinfo.getEventTypeName();
161 event.event_source = evinfo.getEventSourceName();
162 event.event_object_tag = evinfo.getOrthogonalName();
163 event.label = evinfo.label;
178 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
200template <
typename EventType>
204 RCLCPP_INFO_STREAM(
getLogger(),
"Event: " << evname);
205 auto * ev =
new EventType();
212 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
214 bool success =
false;
229 ret = boost::any_cast<T>(v.second);
233 catch (boost::bad_any_cast & ex)
235 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
248 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
254 std::stringstream ss;
270 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
272 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
273 std::shared_ptr<CallbackCounterSemaphore> callbackCounter);
279 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
281 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
282 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
284 return signal.connect(
288 if (callbackCounter ==
nullptr)
290 (
object->*callback)();
292 else if (callbackCounter->acquire())
294 (
object->*callback)();
295 callbackCounter->release();
304 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
306 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
307 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
309 return signal.connect(
312 if (callbackCounter ==
nullptr)
314 return (object->*callback)(a1);
316 else if (callbackCounter->acquire())
318 (
object->*callback)(a1);
319 callbackCounter->release();
328 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
330 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
331 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
333 return signal.connect(
334 [=](
auto a1,
auto a2)
336 if (callbackCounter ==
nullptr)
338 return (object->*callback)(a1, a2);
340 else if (callbackCounter->acquire())
342 (
object->*callback)(a1, a2);
343 callbackCounter->release();
352 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
354 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
355 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
357 return signal.connect(
358 [=](
auto a1,
auto a2,
auto a3)
360 if (callbackCounter ==
nullptr)
362 return (object->*callback)(a1, a2, a3);
364 else if (callbackCounter->acquire())
366 (
object->*callback)(a1, a2, a3);
367 callbackCounter->release();
375template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
377 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object)
379 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
382 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
383 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
384 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
385 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
386 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
387 "Only are accepted smacc types as subscribers for smacc signals");
389 typedef decltype(callback) ft;
395 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
396 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
397 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
398 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
402 "[StateMachine] Long life-time SMACC signal subscription created. Subscriber is %s. Callback "
407 connection = binder.bindaux(signal, callback,
object,
nullptr);
410 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
411 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
412 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
416 "[StateMachine] Life-time constrained SMACC signal subscription created. Subscriber is %s",
419 std::shared_ptr<CallbackCounterSemaphore> callbackCounterSemaphore;
426 callbackCounterSemaphore =
431 connection = binder.bindaux(signal, callback,
object, callbackCounterSemaphore);
432 callbackCounterSemaphore->addConnection(connection);
438 "[StateMachine] Connecting signal to an unknown object with unknown lifetime "
439 "behavior. An exception may occur if the object is destroyed during the execution.");
441 connection = binder.bindaux(signal, callback,
object,
nullptr);
447template <
typename StateType>
450 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
454 "[State Machine] Initializing a new state '%s' and updating current state. Getting state "
455 "meta-information. number of orthogonals: %ld",
463template <
typename StateType>
467 getLogger(),
"[%s] State OnEntry code finished.",
473 RCLCPP_DEBUG(
getLogger(),
"Orthogonal onEntry: %s.", pair.second->getName().c_str());
474 auto & orthogonal = pair.second;
477 orthogonal->onEntry();
479 catch (
const std::exception & e)
483 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
484 pair.second->getName().c_str(), e.what());
488 for (
auto & sr : currentState->getStateReactors())
491 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor onEntry: " << srname);
496 catch (
const std::exception & e)
500 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
502 srname.c_str(), e.what());
506 for (
auto & eg : currentState->getEventGenerators())
509 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator onEntry: " << egname);
514 catch (
const std::exception & e)
518 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
520 egname.c_str(), e.what());
525 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
531template <
typename StateType>
537 auto & orthogonal = pair.second;
538 orthogonal->runtimeConfigure();
542 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
550template <
typename StateType>
556template <
typename StateType>
561 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
563 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state exit: leaving state " << state);
566 auto & orthogonal = pair.second;
569 orthogonal->onExit();
571 catch (
const std::exception & e)
575 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
576 pair.second->getName().c_str(), e.what());
580 for (
auto & sr : state->getStateReactors())
583 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor OnExit: " << srname);
588 catch (
const std::exception & e)
592 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
594 srname.c_str(), e.what());
598 for (
auto & eg : state->getEventGenerators())
601 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator OnExit: " << egname);
606 catch (
const std::exception & e)
610 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
612 egname.c_str(), e.what());
617template <
typename StateType>
625 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
627 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state disposing: leaving state" << state);
630 auto & orthogonal = pair.second;
633 orthogonal->onDispose();
635 catch (
const std::exception & e)
639 "[Orthogonal %s] Exception onDispose - continuing with next orthogonal. Exception info: %s",
640 pair.second->getName().c_str(), e.what());
644 for (
auto & sr : state->getStateReactors())
647 RCLCPP_INFO(
getLogger(),
"State reactor disposing: %s", srname);
652 catch (
const std::exception & e)
656 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
662 for (
auto & eg : state->getEventGenerators())
665 RCLCPP_INFO(
getLogger(),
"Event generator disposing: %s", egname);
670 catch (
const std::exception & e)
674 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
684 RCLCPP_WARN_STREAM(
getLogger(),
"State exit: " << fullname);
690template <
typename EventType>
708template <
typename InitialStateType>
std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > orthogonals_
std::vector< ISmaccState * > currentState_
std::recursive_mutex eventQueueMutex_
bool getGlobalSMData(std::string name, T &ret)
StateMachineInternalAction stateMachineCurrentAction
std::shared_ptr< SmaccStateInfo > currentStateInfo_
rclcpp::Node::SharedPtr getNode()
std::map< std::string, std::pair< std::function< std::string()>, boost::any > > globalData_
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType)
void setGlobalSMData(std::string name, T value)
TOrthogonal * getOrthogonal()
void buildStateMachineInfo()
void notifyOnRuntimeConfigurationFinished(StateType *state)
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
std::recursive_mutex m_mutex_
void notifyOnStateExited(StateType *state)
void lockStateMachine(std::string msg)
void notifyOnStateEntryEnd(StateType *state)
void propagateEventToStateReactors(ISmaccState *st, EventType *ev)
void disconnectSmaccSignalObject(void *object)
const SmaccStateMachineInfo & getStateMachineInfo()
void unlockStateMachine(std::string msg)
int64_t getCurrentStateCounter() const
ISmaccState * getCurrentState() const
void checkStateMachineConsistence()
void notifyOnStateExiting(StateType *state)
SignalDetector * signalDetector_
std::map< void *, std::shared_ptr< CallbackCounterSemaphore > > stateCallbackConnections
rclcpp::Logger getLogger()
void notifyOnRuntimeConfigured(StateType *state)
std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo_
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
rclcpp::Publisher< smacc2_msgs::msg::SmaccEvent >::SharedPtr eventsLogPub_
void updateStatusMessage()
void notifyOnStateEntryStart(StateType *state)
ISmaccState * getParentState()
virtual std::string getClassName()
std::vector< std::shared_ptr< StateReactor > > & getStateReactors()
void postEvent(EventType *ev)
void notifyStateExited(ISmaccState *currentState)
void notifyStateConfigured(ISmaccState *currentState)
static TypeInfo::Ptr getTypeInfoFromType()
std::enable_if< HasEventLabel< T >::value, void >::type EventLabel(std::string &label)
std::string demangleSymbol()
std::string demangledTypeName()
boost::signals2::connection SmaccSignalConnection
smacc2::SmaccSignalConnection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
smacc2::SmaccSignalConnection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
smacc2::SmaccSignalConnection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
smacc2::SmaccSignalConnection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
smacc2::SmaccSignalConnection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)