32#include <smacc2_msgs/msg/smacc_event_generator.hpp>
33#include <smacc2_msgs/msg/smacc_orthogonal.hpp>
34#include <smacc2_msgs/msg/smacc_state.hpp>
35#include <smacc2_msgs/msg/smacc_state_reactor.hpp>
36#include <smacc2_msgs/msg/smacc_transition.hpp>
40namespace introspection
49 std::map<std::string, std::shared_ptr<SmaccStateInfo>>
states;
51 std::vector<smacc2_msgs::msg::SmaccState>
stateMsgs;
53 template <
typename InitialStateType>
56 template <
typename StateType>
57 std::shared_ptr<SmaccStateInfo>
createState(std::shared_ptr<SmaccStateInfo> parent);
59 template <
typename StateType>
62 auto typeNameStr =
typeid(StateType).name();
64 return states.count(typeNameStr) > 0;
67 template <
typename StateType>
72 return states[
typeid(StateType).name()];
81 template <
typename StateType>
82 void addState(std::shared_ptr<StateType> & state);
86 rclcpp::Node::SharedPtr
nh_;
110 template <
typename,
typename,
typename>
typename TTransition,
typename TevSource,
111 template <
typename>
typename EvType,
typename Tag,
typename DestinyState>
112 void operator()(TTransition<EvType<TevSource>, DestinyState, Tag>);
115 template <
typename,
typename>
typename TTransition,
typename TevSource,
116 template <
typename>
typename EvType,
typename DestinyState>
117 void operator()(TTransition<EvType<TevSource>, DestinyState>);
119 template <
typename T>
124template <
typename InitialStateType>
127 static void walkStates(std::shared_ptr<SmaccStateInfo> & parentState,
bool rootInitialNode);
134 using type_t =
typename T::type;
141 std::shared_ptr<SmaccStateInfo> & parentState)
149 std::shared_ptr<SmaccStateInfo> & parentState)
151 using boost::mpl::_1;
152 using wrappedList =
typename boost::mpl::transform<T, add_type_wrapper<_1>>::type;
153 boost::mpl::for_each<wrappedList>(
AddSubState(parentState));
160 std::shared_ptr<SmaccStateInfo> & sourceState)
163 globalNh_->get_logger(),
"State %s Walker has transition list",
164 sourceState->fullStateName.c_str());
165 using boost::mpl::_1;
166 using wrappedList =
typename boost::mpl::transform<T, add_type_wrapper<_1>>::type;
168 globalNh_->get_logger(),
"[DIAG] About to call boost::mpl::for_each for state %s",
169 sourceState->fullStateName.c_str());
170 boost::mpl::for_each<wrappedList>(
AddTransition(sourceState));
172 globalNh_->get_logger(),
"[DIAG] Completed boost::mpl::for_each for state %s",
173 sourceState->fullStateName.c_str());
176template <
typename Ev,
typename Dst,
typename Tag>
179 std::shared_ptr<SmaccStateInfo> & sourceState)
182 globalNh_->get_logger(),
"[DIAG] processTransition [deep_history] START for state %s",
183 sourceState->toShortName().c_str());
187 globalNh_->get_logger(),
"[DIAG] processTransition [deep_history] got TypeInfo for state %s",
188 sourceState->toShortName().c_str());
193template <
typename Ev,
typename Dst,
typename Tag>
198 globalNh_->get_logger(),
"[DIAG] processTransition START for state %s",
199 sourceState->toShortName().c_str());
202 globalNh_->get_logger(),
"[DIAG] processTransition got TypeInfo for state %s",
203 sourceState->toShortName().c_str());
205 globalNh_->get_logger(),
"State %s Walker transition: %s", sourceState->toShortName().c_str(),
210template <
typename Ev,
typename Dst,
typename Tag>
216 globalNh_->get_logger(),
"State %s Walker transition: %s", sourceState->toShortName().c_str(),
218 std::string transitionTag;
219 std::string transitionType;
225 RCLCPP_DEBUG_STREAM(
globalNh_->get_logger(),
"TRANSITION TYPE:" << transitionType);
234 RCLCPP_INFO_STREAM(
globalNh_->get_logger(),
"Transition tag: " << transitionTag);
236 if (!sourceState->getStateMachineInfo()->containsState<Dst>())
238 auto realparentState = sourceState->getStateMachineInfo()->getState<
typename Dst::TContext>();
239 auto siblingnode = sourceState->getStateMachineInfo()->createState<Dst>(realparentState);
242 sourceState->declareTransition<Ev>(
243 siblingnode, transitionTag, transitionType, history, transitionTypeInfo);
247 auto siblingnode = sourceState->getStateMachineInfo()->getState<Dst>();
248 sourceState->declareTransition<Ev>(
249 siblingnode, transitionTag, transitionType, history, transitionTypeInfo);
254template <
typename EvType>
256 std::shared_ptr<SmaccStateInfo> & dstState,
const std::string & transitionTag,
257 const std::string & transitionType,
bool history,
TypeInfo::Ptr transitionTypeInfo)
267 if (transitionTag !=
"")
276 std::make_shared<SmaccEventInfo>(transitionTypeInfo->templateParameters.front());
295 template <
typename C>
297 template <
typename C>
308template <
typename Ev,
typename Dst>
313template <
typename Ev>
323 std::shared_ptr<SmaccStateInfo> & sourceState)
326 globalNh_->get_logger(),
"[DIAG] processTransitions [single/leaf] called for state %s",
327 sourceState->fullStateName.c_str());
331 globalNh_->get_logger(),
"[DIAG] processTransitions [single/leaf] completed for state %s",
332 sourceState->fullStateName.c_str());
341 T::staticConfigure();
349 "static OnDefinition: dont exist for " <<
demangleSymbol(
typeid(T).name()));
356 template <
typename,
typename,
typename>
typename TTransition,
typename TevSource,
357 template <
typename>
typename EvType,
typename Tag,
typename DestinyState>
361 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [3-param] called for state %s",
365 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [3-param] completed for state %s",
372 template <
typename,
typename>
typename TTransition,
typename TevSource,
373 template <
typename>
typename EvType,
typename DestinyState>
377 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [2-param] called for state %s",
381 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [2-param] completed for state %s",
385template <
typename TTrans>
389 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [generic] called for state %s",
391 using type_t =
typename TTrans::type;
394 globalNh_->get_logger(),
"[DIAG] AddTransition::operator() [generic] completed for state %s",
399template <
typename InitialStateType>
401 std::shared_ptr<SmaccStateInfo> & parentState,
bool rootInitialNode)
405 std::shared_ptr<SmaccStateInfo> targetState;
407 if (!rootInitialNode)
409 if (parentState->getStateMachineInfo()->containsState<InitialStateType>())
414 targetState = parentState->createChildState<InitialStateType>();
418 targetState = parentState;
424 typename std::remove_pointer<
decltype(InitialStateType::smacc_inner_type)>::type InnerType;
428 typedef typename InitialStateType::reactions reactions;
435template <
typename InitialStateType>
442template <
typename StateType>
444 std::shared_ptr<SmaccStateInfo> parent)
446 auto thisptr = this->shared_from_this();
447 auto * statetid = &(
typeid(StateType));
450 RCLCPP_INFO_STREAM(
getLogger(),
"Creating State Info: " << demangledName);
452 auto state = std::make_shared<SmaccStateInfo>(statetid, parent, thisptr);
453 state->demangledStateName = demangledName;
454 state->fullStateName =
typeid(StateType).name();
455 state->stateIndex_ =
static_cast<int>(
states.size());
457 if (parent !=
nullptr)
459 parent->children_.push_back(state);
467template <
typename StateType>
470 states[state->fullStateName] = state;
473template <
typename StateType>
476 auto realparentState = this->
getStateMachineInfo()->getState<
typename StateType::TContext>();
static YesType & test(decltype(&C::staticConfigure))
static NoType & test(...)
std::shared_ptr< SmaccStateInfo > createChildState()
void declareTransition(std::shared_ptr< SmaccStateInfo > &dstState, const std::string &transitionTag, const std::string &transitionType, bool history, TypeInfo::Ptr transitionTypeInfo)
std::shared_ptr< SmaccStateMachineInfo > getStateMachineInfo() const
rclcpp::Logger getLogger()
std::vector< SmaccTransitionInfo > transitions_
rclcpp::Node::SharedPtr nh_
rclcpp::Logger getLogger()
void addState(std::shared_ptr< StateType > &state)
rclcpp::Node::SharedPtr getNode()
std::map< std::string, std::shared_ptr< SmaccStateInfo > > states
std::vector< smacc2_msgs::msg::SmaccState > stateMsgs
SmaccStateMachineInfo(rclcpp::Node::SharedPtr nh)
void buildStateMachineInfo()
std::shared_ptr< SmaccStateInfo > getState()
std::shared_ptr< SmaccStateInfo > createState(std::shared_ptr< SmaccStateInfo > parent)
void assembleSMStructureMessage(ISmaccStateMachine *sm)
static TypeInfo::Ptr getTypeInfoFromType()
std::shared_ptr< TypeInfo > Ptr
std::enable_if< HasAutomaticTransitionType< T >::value, void >::type automaticTransitionType(std::string &transition_type)
disable_if< boost::mpl::is_sequence< T > >::type processSubState(std::shared_ptr< SmaccStateInfo > &parentState)
rclcpp::Node::SharedPtr globalNh_
std::enable_if< HasEventLabel< T >::value, void >::type EventLabel(std::string &label)
void processTransition(smacc2::Transition< Ev, boost::statechart::deep_history< Dst >, Tag > *, std::shared_ptr< SmaccStateInfo > &sourceState)
static std::string getTransitionType()
std::string demangleSymbol()
enable_if< boost::mpl::is_sequence< T > >::type processTransitions(std::shared_ptr< SmaccStateInfo > &sourceState)
void processTransitionAux(smacc2::Transition< Ev, Dst, Tag > *, std::shared_ptr< SmaccStateInfo > &sourceState, bool history, TypeInfo::Ptr &transitionTypeInfo)
std::enable_if< HasOnDefinition< T >::value, void >::type CallOnDefinition()
std::enable_if< HasAutomaticTransitionTag< T >::value, void >::type automaticTransitionTag(std::string &transition_name)
std::string demangledTypeName()
std::shared_ptr< SmaccStateInfo > & parentState_
AddSubState(std::shared_ptr< SmaccStateInfo > &parentState)
AddTransition(std::shared_ptr< SmaccStateInfo > ¤tState)
void operator()(TTransition< EvType< TevSource >, DestinyState, Tag >)
std::shared_ptr< SmaccStateInfo > & currentState_
std::weak_ptr< const SmaccStateInfo > sourceState
std::shared_ptr< SmaccEventInfo > eventInfo
std::string transitionTag
std::weak_ptr< const SmaccStateInfo > destinyState
std::string transitionType
smacc2::introspection::TypeInfo::Ptr transitionTypeInfo
static void walkStates(std::shared_ptr< SmaccStateInfo > &parentState, bool rootInitialNode)