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>
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());
118template <
typename SmaccComponentType>
126 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
130 for (
auto & client : ortho.second->clients_)
132 storage = client->getComponent<SmaccComponentType>();
133 if (storage !=
nullptr)
141 getLogger(),
"component %s is required but it was not found in any orthogonal",
145 throw std::runtime_error(
"component is required but it was not found in any orthogonal");
171template <
typename EventType>
176#define eventtypename demangleSymbol<EventType>().c_str()
184 smacc2_msgs::msg::SmaccEvent event;
185 event.event_type = evinfo.getEventTypeName();
186 event.event_source = evinfo.getEventSourceName();
187 event.event_object_tag = evinfo.getOrthogonalName();
188 event.label = evinfo.label;
203 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
225template <
typename EventType>
229 RCLCPP_INFO_STREAM(
getLogger(),
"Event: " << evname);
230 auto * ev =
new EventType();
237 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
239 bool success =
false;
254 ret = boost::any_cast<T>(v.second);
258 catch (boost::bad_any_cast & ex)
260 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
273 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
279 std::stringstream ss;
316 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
318 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
319 std::shared_ptr<CallbackCounterSemaphore> callbackCounter);
325 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
327 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
328 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
330 return signal.connect(
334 if (callbackCounter ==
nullptr)
336 (
object->*callback)();
338 else if (callbackCounter->acquire())
340 (
object->*callback)();
341 callbackCounter->release();
350 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
352 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
353 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
355 return signal.connect(
358 if (callbackCounter ==
nullptr)
360 return (object->*callback)(a1);
362 else if (callbackCounter->acquire())
364 (
object->*callback)(a1);
365 callbackCounter->release();
374 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
376 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
377 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
379 return signal.connect(
380 [=](
auto a1,
auto a2)
382 if (callbackCounter ==
nullptr)
384 return (object->*callback)(a1, a2);
386 else if (callbackCounter->acquire())
388 (
object->*callback)(a1, a2);
389 callbackCounter->release();
398 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
400 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
401 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
403 return signal.connect(
404 [=](
auto a1,
auto a2,
auto a3)
406 if (callbackCounter ==
nullptr)
408 return (object->*callback)(a1, a2, a3);
410 else if (callbackCounter->acquire())
412 (
object->*callback)(a1, a2, a3);
413 callbackCounter->release();
421template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
423 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object)
425 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
428 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
429 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
430 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
431 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
432 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
433 "Only are accepted smacc types as subscribers for smacc signals");
435 typedef decltype(callback) ft;
441 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
442 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
443 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
444 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
448 "[StateMachine] Long life-time SMACC signal subscription created. Subscriber is %s. Callback "
453 connection = binder.bindaux(signal, callback,
object,
nullptr);
456 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
457 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
458 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
462 "[StateMachine] Life-time constrained SMACC signal subscription created. Subscriber is %s",
465 std::shared_ptr<CallbackCounterSemaphore> callbackCounterSemaphore;
472 callbackCounterSemaphore =
477 connection = binder.bindaux(signal, callback,
object, callbackCounterSemaphore);
478 callbackCounterSemaphore->addConnection(connection);
484 "[StateMachine] Connecting signal to an unknown object with unknown lifetime "
485 "behavior. An exception may occur if the object is destroyed during the execution.");
487 connection = binder.bindaux(signal, callback,
object,
nullptr);
493template <
typename StateType>
496 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
500 "[State Machine] Initializing a new state '%s' and updating current state. Getting state "
501 "meta-information. number of orthogonals: %ld",
509template <
typename StateType>
513 getLogger(),
"[%s] State OnEntry code finished.",
519 RCLCPP_DEBUG(
getLogger(),
"Orthogonal onEntry: %s.", pair.second->getName().c_str());
520 auto & orthogonal = pair.second;
523 orthogonal->onEntry();
525 catch (
const std::exception & e)
529 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
530 pair.second->getName().c_str(), e.what());
534 for (
auto & sr : currentState->getStateReactors())
537 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor onEntry: " << srname);
542 catch (
const std::exception & e)
546 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
548 srname.c_str(), e.what());
552 for (
auto & eg : currentState->getEventGenerators())
555 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator onEntry: " << egname);
560 catch (
const std::exception & e)
564 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
566 egname.c_str(), e.what());
571 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
577template <
typename StateType>
583 auto & orthogonal = pair.second;
584 orthogonal->runtimeConfigure();
588 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
596template <
typename StateType>
602template <
typename StateType>
607 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
610 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state exit: leaving state " << state);
613 auto & orthogonal = pair.second;
616 orthogonal->onExit();
618 catch (
const std::exception & e)
622 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
623 pair.second->getName().c_str(), e.what());
627 for (
auto & sr : state->getStateReactors())
630 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor OnExit: " << srname);
635 catch (
const std::exception & e)
639 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
641 srname.c_str(), e.what());
645 for (
auto & eg : state->getEventGenerators())
648 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator OnExit: " << egname);
653 catch (
const std::exception & e)
657 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
659 egname.c_str(), e.what());
664template <
typename StateType>
672 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
674 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state disposing: leaving state" << state);
677 auto & orthogonal = pair.second;
680 orthogonal->onDispose();
682 catch (
const std::exception & e)
686 "[Orthogonal %s] Exception onDispose - continuing with next orthogonal. Exception info: %s",
687 pair.second->getName().c_str(), e.what());
691 for (
auto & sr : state->getStateReactors())
694 RCLCPP_INFO(
getLogger(),
"State reactor disposing: %s", srname);
699 catch (
const std::exception & e)
703 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
709 for (
auto & eg : state->getEventGenerators())
712 RCLCPP_INFO(
getLogger(),
"Event generator disposing: %s", egname);
717 catch (
const std::exception & e)
721 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
731 RCLCPP_WARN_STREAM(
getLogger(),
"State exit: " << fullname);
737template <
typename EventType>
755template <
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 notifyOnStateExitting(StateType *state)
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()
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)