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;
211 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
233template <
typename EventType>
237 RCLCPP_INFO_STREAM(
getLogger(),
"Event: " << evname);
238 auto * ev =
new EventType();
245 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
247 bool success =
false;
262 ret = boost::any_cast<T>(v.second);
266 catch (boost::bad_any_cast & ex)
268 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
281 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
287 std::stringstream ss;
324 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
326 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
327 std::shared_ptr<CallbackCounterSemaphore> callbackCounter);
333 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
335 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
336 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
338 return signal.connect(
342 if (callbackCounter ==
nullptr)
344 (
object->*callback)();
346 else if (callbackCounter->acquire())
348 (
object->*callback)();
349 callbackCounter->release();
358 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
360 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
361 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
363 return signal.connect(
366 if (callbackCounter ==
nullptr)
368 return (object->*callback)(a1);
370 else if (callbackCounter->acquire())
372 (
object->*callback)(a1);
373 callbackCounter->release();
382 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
384 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
385 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
387 return signal.connect(
388 [=](
auto a1,
auto a2)
390 if (callbackCounter ==
nullptr)
392 return (object->*callback)(a1, a2);
394 else if (callbackCounter->acquire())
396 (
object->*callback)(a1, a2);
397 callbackCounter->release();
406 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
408 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object,
409 std::shared_ptr<CallbackCounterSemaphore> callbackCounter)
411 return signal.connect(
412 [=](
auto a1,
auto a2,
auto a3)
414 if (callbackCounter ==
nullptr)
416 return (object->*callback)(a1, a2, a3);
418 else if (callbackCounter->acquire())
420 (
object->*callback)(a1, a2, a3);
421 callbackCounter->release();
429template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
431 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType *
object)
433 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
436 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
437 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
438 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
439 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
440 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
441 "Only are accepted smacc types as subscribers for smacc signals");
443 typedef decltype(callback) ft;
449 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
450 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
451 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
452 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
456 "[StateMachine] Long life-time SMACC signal subscription created. Subscriber is %s. Callback "
461 connection = binder.bindaux(signal, callback,
object,
nullptr);
464 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
465 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
466 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
470 "[StateMachine] Life-time constrained SMACC signal subscription created. Subscriber is %s",
473 std::shared_ptr<CallbackCounterSemaphore> callbackCounterSemaphore;
480 callbackCounterSemaphore =
485 connection = binder.bindaux(signal, callback,
object, callbackCounterSemaphore);
486 callbackCounterSemaphore->addConnection(connection);
492 "[StateMachine] Connecting signal to an unknown object with unknown lifetime "
493 "behavior. An exception may occur if the object is destroyed during the execution.");
495 connection = binder.bindaux(signal, callback,
object,
nullptr);
501template <
typename StateType>
504 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
508 "[State Machine] Initializing a new state '%s' and updating current state. Getting state "
509 "meta-information. number of orthogonals: %ld",
517template <
typename StateType>
521 getLogger(),
"[%s] State OnEntry code finished.",
527 RCLCPP_DEBUG(
getLogger(),
"Orthogonal onEntry: %s.", pair.second->getName().c_str());
528 auto & orthogonal = pair.second;
531 orthogonal->onEntry();
533 catch (
const std::exception & e)
537 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
538 pair.second->getName().c_str(), e.what());
542 for (
auto & sr : currentState->getStateReactors())
545 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor onEntry: " << srname);
550 catch (
const std::exception & e)
554 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
556 srname.c_str(), e.what());
560 for (
auto & eg : currentState->getEventGenerators())
563 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator onEntry: " << egname);
568 catch (
const std::exception & e)
572 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
574 egname.c_str(), e.what());
579 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
585template <
typename StateType>
591 auto & orthogonal = pair.second;
592 orthogonal->runtimeConfigure();
596 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
604template <
typename StateType>
610template <
typename StateType>
615 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
618 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state exit: leaving state " << state);
621 auto & orthogonal = pair.second;
624 orthogonal->onExit();
626 catch (
const std::exception & e)
630 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
631 pair.second->getName().c_str(), e.what());
635 for (
auto & sr : state->getStateReactors())
638 RCLCPP_INFO_STREAM(
getLogger(),
"State reactor OnExit: " << srname);
643 catch (
const std::exception & e)
647 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
649 srname.c_str(), e.what());
653 for (
auto & eg : state->getEventGenerators())
656 RCLCPP_INFO_STREAM(
getLogger(),
"Event generator OnExit: " << egname);
661 catch (
const std::exception & e)
665 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
667 egname.c_str(), e.what());
672template <
typename StateType>
680 RCLCPP_WARN_STREAM(
getLogger(),
"Exiting state: " << fullname);
682 RCLCPP_INFO_STREAM(
getLogger(),
"Notification state disposing: leaving state" << state);
685 auto & orthogonal = pair.second;
688 orthogonal->onDispose();
690 catch (
const std::exception & e)
694 "[Orthogonal %s] Exception onDispose - continuing with next orthogonal. Exception info: %s",
695 pair.second->getName().c_str(), e.what());
699 for (
auto & sr : state->getStateReactors())
702 RCLCPP_INFO(
getLogger(),
"State reactor disposing: %s", srname);
707 catch (
const std::exception & e)
711 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
717 for (
auto & eg : state->getEventGenerators())
720 RCLCPP_INFO(
getLogger(),
"Event generator disposing: %s", egname);
725 catch (
const std::exception & e)
729 "[State Reactor %s] Exception on OnDispose - continuing with next state reactor. Exception "
739 RCLCPP_WARN_STREAM(
getLogger(),
"State exit: " << fullname);
745template <
typename EventType>
763template <
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)