44#include <boost/function_types/function_arity.hpp>
45#include <boost/function_types/function_type.hpp>
46#include <boost/function_types/parameter_types.hpp>
48#include <smacc2_msgs/msg/smacc_status.hpp>
54template <
typename TOrthogonal>
57 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
68 "Orthogonal %s resource is being required from some state, client or component. Found "
71 orthogonalkey.c_str());
72 ret =
dynamic_cast<TOrthogonal *
>(it->second.get());
78 ss <<
"Orthogonal not found " << orthogonalkey.c_str() << std::endl;
79 ss <<
"The existing orthogonals are the following: " << std::endl;
82 ss <<
" - " << orthogonal.first << std::endl;
85 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
92template <
typename TOrthogonal>
96 std::lock_guard<std::recursive_mutex> guard(
m_mutex_);
102 auto ret = std::make_shared<TOrthogonal>();
103 orthogonals_[orthogonalkey] = dynamic_pointer_cast<smacc2::ISmaccOrthogonal>(ret);
105 ret->setStateMachine(
this);
107 RCLCPP_INFO(
getLogger(),
"%s Orthogonal is created", orthogonalkey.c_str());
112 getLogger(),
"There were already one existing orthogonal of type "
113 << orthogonalkey.c_str() <<
". Skipping creation orthogonal request. ");
114 std::stringstream ss;
115 ss <<
"The existing orthogonals are the following: " << std::endl;
118 ss <<
" - " << orthogonal.first << std::endl;
120 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
126template <
typename SmaccComponentType>
134 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
138 for (
auto & client : ortho.second->clients_)
140 storage = client->getComponent<SmaccComponentType>();
141 if (storage !=
nullptr)
149 getLogger(),
"component %s is required but it was not found in any orthogonal",
153 throw std::runtime_error(
"component is required but it was not found in any orthogonal");
179template <
typename EventType>
184#define eventtypename demangleSymbol<EventType>().c_str()
192 smacc2_msgs::msg::SmaccEvent event;
193 event.event_type = evinfo.getEventTypeName();
194 event.event_source = evinfo.getEventSourceName();
195 event.event_object_tag = evinfo.getOrthogonalName();
196 event.label = evinfo.label;
208 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
230template <
typename EventType>
234 RCLCPP_INFO_STREAM(
getLogger(),
"Event: " << evname);
235 auto * ev =
new EventType();
242 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
244 bool success =
false;
259 ret = boost::any_cast<T>(v.second);
263 catch (boost::bad_any_cast & ex)
265 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
278 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
284 std::stringstream ss;
321 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
323 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
324 std::shared_ptr<CallbackCounterSemaphore> callbackCounter);
330 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
332 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
333 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
335 return signal.connect(
339 if (callbackCounter ==
nullptr)
341 (
object->*callback)();
343 else if (callbackCounter->acquire())
345 (
object->*callback)();
346 callbackCounter->release();
355 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
357 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
358 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
360 return signal.connect(
363 if (callbackCounter ==
nullptr)
365 return (object->*callback)(a1);
367 else if (callbackCounter->acquire())
369 (
object->*callback)(a1);
370 callbackCounter->release();
379 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
381 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
382 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
384 return signal.connect(
385 [=](
auto a1,
auto a2)
387 if (callbackCounter ==
nullptr)
389 return (object->*callback)(a1, a2);
391 else if (callbackCounter->acquire())
393 (
object->*callback)(a1, a2);
394 callbackCounter->release();
403 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
405 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
406 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
408 return signal.connect(
409 [=](
auto a1,
auto a2,
auto a3)
411 if (callbackCounter ==
nullptr)
413 return (object->*callback)(a1, a2, a3);
415 else if (callbackCounter->acquire())
417 (
object->*callback)(a1, a2, a3);
418 callbackCounter->release();
426template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
428 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object)
430 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
433 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
434 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
435 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
436 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
437 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
438 "Only are accepted smacc types as subscribers for smacc signals");
440 typedef decltype(callback) ft;
442 boost::signals2::connection connection;
446 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
447 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
448 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
449 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
453 "[StateMachine] Long life-time SMACC signal subscription created. Subscriber is %s. Callback "
458 connection = binder.bindaux(signal, callback,
object,
nullptr);
461 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
462 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
463 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
467 "[StateMachine] Life-time constrained SMACC signal subscription created. Subscriber is %s",
470 std::shared_ptr<CallbackCounterSemaphore> callbackCounterSemaphore;
477 callbackCounterSemaphore =
482 connection = binder.bindaux(signal, callback,
object, callbackCounterSemaphore);
483 callbackCounterSemaphore->addConnection(connection);
489 "[StateMachine] Connecting signal to an unknown object with unknown lifetime "
490 "behavior. An exception may occur if the object is destroyed during the execution.");
492 connection = binder.bindaux(signal, callback,
object,
nullptr);
498template <
typename StateType>
501 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
505 "[State Machine] Initializing a new state '%s' and updating current state. Getting state "
506 "meta-information. number of orthogonals: %ld",
514template <
typename StateType>
518 getLogger(),
"[%s] State OnEntry code finished.",
524 RCLCPP_DEBUG(
getLogger(),
"Orthogonal onEntry: %s.", pair.second->getName().c_str());
525 auto & orthogonal = pair.second;
528 orthogonal->onEntry();
530 catch (
const std::exception & e)
534 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
535 pair.second->getName().c_str(), e.what());
539 for (
auto & sr : currentState->getStateReactors())
542 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor onEntry: " << srname);
547 catch (
const std::exception & e)
551 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
553 srname.c_str(), e.what());
557 for (
auto & eg : currentState->getEventGenerators())
560 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator onEntry: " << egname);
565 catch (
const std::exception & e)
569 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
571 egname.c_str(), e.what());
576 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
582template <
typename StateType>
588 auto & orthogonal = pair.second;
589 orthogonal->runtimeConfigure();
593 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
601template <
typename StateType>
607template <
typename StateType>
612 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
615 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state exit: leaving state " << state);
618 auto & orthogonal = pair.second;
621 orthogonal->onExit();
623 catch (
const std::exception & e)
627 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
628 pair.second->getName().c_str(), e.what());
632 for (
auto & sr : state->getStateReactors())
635 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor OnExit: " << srname);
640 catch (
const std::exception & e)
644 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
646 srname.c_str(), e.what());
650 for (
auto & eg : state->getEventGenerators())
653 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator OnExit: " << egname);
658 catch (
const std::exception & e)
662 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
664 egname.c_str(), e.what());
669template <
typename StateType>
677 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
679 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state disposing: leaving state" << state);
682 auto & orthogonal = pair.second;
685 orthogonal->onDispose();
687 catch (
const std::exception & e)
691 "[Orthogonal %s] Exception onDispose - continuing with next orthogonal. Exception info: %s",
692 pair.second->getName().c_str(), e.what());
696 for (
auto & sr : state->getStateReactors())
699 RCLCPP_INFO(
getLogger(),
"State reactor disposing: %s", srname);
704 catch (
const std::exception & e)
708 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
714 for (
auto & eg : state->getEventGenerators())
717 RCLCPP_INFO(
getLogger(),
"Event generator disposing: %s", egname);
722 catch (
const std::exception & e)
726 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
736 RCLCPP_WARN_STREAM(
getLogger(),
"State exit: " << fullname);
742template <
typename EventType>
760template <
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)
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
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)
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 bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object, std::shared_ptr< CallbackCounterSemaphore > callbackCounter)