23#include <boost/intrusive_ptr.hpp>
37#include <boost/function_types/function_arity.hpp>
38#include <boost/function_types/function_type.hpp>
39#include <boost/function_types/parameter_types.hpp>
41#include <smacc2_msgs/msg/smacc_status.hpp>
47template <
typename TOrthogonal>
50 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
53 TOrthogonal * ret =
nullptr;
61 "Orthogonal %s resource is being required from some state, client or component. Found "
64 orthogonalkey.c_str());
65 ret =
dynamic_cast<TOrthogonal *
>(it->second.get());
71 ss <<
"Orthogonal not found " << orthogonalkey << std::endl;
72 ss <<
"The existing orthogonals are the following: " << std::endl;
75 ss <<
" - " << orthogonal.first << std::endl;
78 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
85template <
typename TOrthogonal>
88 std::lock_guard<std::recursive_mutex> guard(
m_mutex_);
94 auto ret = std::make_shared<TOrthogonal>();
95 orthogonals_[orthogonalkey] = dynamic_pointer_cast<smacc2::ISmaccOrthogonal>(ret);
97 ret->setStateMachine(
this);
99 RCLCPP_INFO(
getLogger(),
"%s Orthogonal is created", orthogonalkey.c_str());
104 getLogger(),
"There were already one existing orthogonal of type "
105 << orthogonalkey.c_str() <<
". Skipping creation orthogonal request. ");
106 std::stringstream ss;
107 ss <<
"The existing orthogonals are the following: " << std::endl;
110 ss <<
" - " << orthogonal.first << std::endl;
112 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
117template <
typename SmaccComponentType>
125 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
127 if (first !=
nullptr)
129 for (
auto & client : first->
clients_)
131 storage = client->getComponent<SmaccComponentType>();
132 if (storage !=
nullptr)
141 for (
auto & client : ortho.second->clients_)
143 storage = client->getComponent<SmaccComponentType>();
144 if (storage !=
nullptr)
152 getLogger(),
"component %s is required but it was not found in any orthogonal",
156 throw std::runtime_error(
"component is required but it was not found in any orthogonal");
159template <
typename SmaccComponentType>
166 getLogger(),
"component %s (named '%s') is required",
167 demangleSymbol(
typeid(SmaccComponentType).name()).c_str(), name.c_str());
168 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
170 if (first !=
nullptr)
172 for (
auto & client : first->
clients_)
174 storage = client->getComponent<SmaccComponentType>(name);
175 if (storage !=
nullptr)
184 for (
auto & client : ortho.second->clients_)
186 storage = client->getComponent<SmaccComponentType>(name);
187 if (storage !=
nullptr)
195 getLogger(),
"component %s (named '%s') is required but it was not found in any orthogonal",
196 demangleSymbol(
typeid(SmaccComponentType).name()).c_str(), name.c_str());
199 throw std::runtime_error(
"named component is required but it was not found in any orthogonal");
202template <
typename EventType>
206 this->
postEvent(boost::intrusive_ptr<EventType>(ev), evlifetime);
209template <
typename EventType>
211 const boost::intrusive_ptr<EventType> & evHolder,
EventLifeTime evlifetime)
217 EventType * ev = evHolder.get();
219#define eventtypename demangleSymbol<EventType>().c_str()
227 smacc2_msgs::msg::SmaccEvent event;
228 event.event_type = evinfo.getEventTypeName();
229 event.event_source = evinfo.getEventSourceName();
230 event.event_object_tag = evinfo.getOrthogonalName();
231 event.label = evinfo.label;
246 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
288template <
typename EventType>
292 RCLCPP_INFO_STREAM(
getLogger(),
"Event: " << evname);
299 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
301 bool success =
false;
316 ret = boost::any_cast<T>(v.second);
320 catch (boost::bad_any_cast & ex)
322 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
335 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
341 std::stringstream ss;
357 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
359 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
360 std::shared_ptr<CallbackCounterSemaphore> callbackCounter);
366 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
368 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
369 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
371 return signal.connect(
375 if (callbackCounter ==
nullptr)
377 (
object->*callback)();
379 else if (callbackCounter->acquire())
381 (
object->*callback)();
382 callbackCounter->release();
391 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
393 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
394 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
396 return signal.connect(
399 if (callbackCounter ==
nullptr)
401 return (object->*callback)(a1);
403 else if (callbackCounter->acquire())
405 (
object->*callback)(a1);
406 callbackCounter->release();
415 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
417 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
418 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
420 return signal.connect(
421 [=](
const auto & a1,
const auto & a2)
423 if (callbackCounter ==
nullptr)
425 return (object->*callback)(a1, a2);
427 else if (callbackCounter->acquire())
429 (
object->*callback)(a1, a2);
430 callbackCounter->release();
439 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
441 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
442 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
444 return signal.connect(
445 [=](
const auto & a1,
const auto & a2,
const auto & a3)
447 if (callbackCounter ==
nullptr)
449 return (object->*callback)(a1, a2, a3);
451 else if (callbackCounter->acquire())
453 (
object->*callback)(a1, a2, a3);
454 callbackCounter->release();
462template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
464 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object)
466 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
469 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
470 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
471 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
472 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
473 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
474 "Only are accepted smacc types as subscribers for smacc signals");
476 typedef decltype(callback) ft;
482 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
483 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
484 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
485 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
489 "[StateMachine] Long life-time SMACC signal subscription created. Subscriber is %s. Callback "
494 connection = binder.bindaux(signal, callback,
object,
nullptr);
497 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
498 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
499 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
503 "[StateMachine] Life-time constrained SMACC signal subscription created. Subscriber is %s",
506 std::shared_ptr<CallbackCounterSemaphore> callbackCounterSemaphore;
510 void * key =
dynamic_cast<void *
>(object);
517 callbackCounterSemaphore =
522 connection = binder.bindaux(signal, callback,
object, callbackCounterSemaphore);
523 callbackCounterSemaphore->addConnection(connection);
529 "[StateMachine] Connecting signal to an unknown object with unknown lifetime "
530 "behavior. An exception may occur if the object is destroyed during the execution.");
532 connection = binder.bindaux(signal, callback,
object,
nullptr);
538template <
typename StateType>
541 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
545 "[State Machine] Initializing a new state '%s' and updating current state. Getting state "
546 "meta-information. number of orthogonals: %ld",
554template <
typename StateType>
558 getLogger(),
"[%s] State OnEntry code finished.",
563 const bool enter = !currentState->initializationFailed();
568 RCLCPP_DEBUG(
getLogger(),
"Orthogonal onEntry: %s.", pair.second->getName().c_str());
569 auto & orthogonal = pair.second;
572 orthogonal->onEntry();
574 catch (
const std::exception & e)
578 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
579 pair.second->getName().c_str(), e.what());
583 for (
auto & sr : currentState->getStateReactors())
586 const auto & reactor = *sr;
588 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor onEntry: " << srname);
593 catch (
const std::exception & e)
597 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
599 srname.c_str(), e.what());
603 for (
auto & eg : currentState->getEventGenerators())
606 const auto & generator = *eg;
608 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator onEntry: " << egname);
613 catch (
const std::exception & e)
617 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
619 egname.c_str(), e.what());
624 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
630template <
typename StateType>
636 if (state->initializationFailed())
break;
638 auto & orthogonal = pair.second;
639 orthogonal->runtimeConfigure();
643 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
651template <
typename StateType>
657template <
typename StateType>
662 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
664 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state exit: leaving state " << state);
666 if (state->initializationFailed())
return;
669 auto & orthogonal = pair.second;
672 orthogonal->onExit();
674 catch (
const std::exception & e)
678 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
679 pair.second->getName().c_str(), e.what());
683 for (
auto & sr : state->getStateReactors())
686 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor OnExit: " << srname);
691 catch (
const std::exception & e)
695 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
697 srname.c_str(), e.what());
701 for (
auto & eg : state->getEventGenerators())
704 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator OnExit: " << egname);
709 catch (
const std::exception & e)
713 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
715 egname.c_str(), e.what());
720template <
typename StateType>
728 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
730 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state disposing: leaving state" << state);
733 auto & orthogonal = pair.second;
736 orthogonal->onDispose();
738 catch (
const std::exception & e)
742 "[Orthogonal %s] Exception onDispose - continuing with next orthogonal. Exception info: %s",
743 pair.second->getName().c_str(), e.what());
747 for (
auto & sr : state->getStateReactors())
752 RCLCPP_INFO(
getLogger(),
"State reactor disposing: %s", srname.c_str());
757 catch (
const std::exception & e)
761 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
763 srname.c_str(), e.what());
767 for (
auto & eg : state->getEventGenerators())
770 RCLCPP_INFO(
getLogger(),
"Event generator disposing: %s", egname.c_str());
775 catch (
const std::exception & e)
779 "[Event Generator %s] Exception on OnDispose - continuing with next event generator. "
780 "Exception info: %s",
781 egname.c_str(), e.what());
802 RCLCPP_WARN_STREAM(
getLogger(),
"State exit: " << fullname);
808template <
typename EventType>
817 const auto & reactor = *sb;
822 catch (
const std::exception & e)
825 getLogger(),
"[%s] state reactor threw on event %s: %s",
831 getLogger(),
"[%s] state reactor threw a non-standard exception on event %s",
843template <
typename InitialStateType>
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > orthogonals_
void setGlobalSMData(const std::string &name, T value)
std::vector< ISmaccState * > currentState_
std::recursive_mutex eventQueueMutex_
std::shared_ptr< SmaccStateInfo > currentStateInfo_
rclcpp::Node::SharedPtr getNode()
std::map< std::string, std::pair< std::function< std::string()>, boost::any > > globalData_
TOrthogonal * getOrthogonal()
void buildStateMachineInfo()
std::atomic< StateMachineInternalAction > stateMachineCurrentAction
void notifyOnRuntimeConfigurationFinished(StateType *state)
void disconnectSmaccSignalObject(void *object_ptr)
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
std::recursive_mutex m_mutex_
bool getGlobalSMData(const std::string &name, T &ret)
void notifyOnStateExited(StateType *state)
void notifyOnStateEntryEnd(StateType *state)
void propagateEventToStateReactors(ISmaccState *st, EventType *ev)
const SmaccStateMachineInfo & getStateMachineInfo()
static thread_local int reactorWalkDepth_
int64_t getCurrentStateCounter() const
void unlockStateMachine(const std::string &msg)
ISmaccState * getCurrentState() const
void checkStateMachineConsistence()
void notifyOnStateExiting(StateType *state)
SignalDetector * signalDetector_
std::map< void *, std::shared_ptr< CallbackCounterSemaphore > > stateCallbackConnections
void lockStateMachine(const std::string &msg)
rclcpp::Logger getLogger()
void notifyOnRuntimeConfigured(StateType *state)
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType, ISmaccOrthogonal *first=nullptr)
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 notifyStateInitializationFailed()
void queueWorkItem(const SmaccFifoWorker::work_item &item)
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()
std::string demangleType(const std::type_info *tinfo)
boost::signals2::connection SmaccSignalConnection
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)
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)