SMACC2
Loading...
Searching...
No Matches
smacc_state_machine_info.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <map>
24#include <memory>
25#include <string>
26#include <vector>
27
28#include <smacc2/common.hpp>
30
31// smacc_msgs
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>
37
38namespace smacc2
39{
40namespace introspection
41{
42extern rclcpp::Node::SharedPtr globalNh_;
43
44class SmaccStateMachineInfo : public std::enable_shared_from_this<SmaccStateMachineInfo>
45{
46public:
47 explicit SmaccStateMachineInfo(rclcpp::Node::SharedPtr nh) : nh_(nh) {}
48
49 std::map<std::string, std::shared_ptr<SmaccStateInfo>> states;
50
51 std::vector<smacc2_msgs::msg::SmaccState> stateMsgs;
52
53 template <typename InitialStateType>
55
56 template <typename StateType>
57 std::shared_ptr<SmaccStateInfo> createState(std::shared_ptr<SmaccStateInfo> parent);
58
59 template <typename StateType>
61 {
62 auto typeNameStr = typeid(StateType).name();
63
64 return states.count(typeNameStr) > 0;
65 }
66
67 template <typename StateType>
68 std::shared_ptr<SmaccStateInfo> getState()
69 {
70 if (this->containsState<StateType>())
71 {
72 return states[typeid(StateType).name()];
73 }
74 return nullptr;
75 }
76
77 inline rclcpp::Node::SharedPtr getNode() { return nh_; }
78
79 inline rclcpp::Logger getLogger() { return nh_->get_logger(); }
80
81 template <typename StateType>
82 void addState(std::shared_ptr<StateType> & state);
84
85private:
86 rclcpp::Node::SharedPtr nh_;
87};
88
89//---------------------------------------------
91{
92 std::shared_ptr<SmaccStateInfo> & parentState_;
93 explicit AddSubState(std::shared_ptr<SmaccStateInfo> & parentState) : parentState_(parentState) {}
94
95 template <typename T>
96 void operator()(T);
97};
98
99//---------------------------------------------
101{
102 std::shared_ptr<SmaccStateInfo> & currentState_;
103
104 explicit AddTransition(std::shared_ptr<SmaccStateInfo> & currentState)
105 : currentState_(currentState)
106 {
107 }
108
109 template <
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>);
113
114 template <
115 template <typename, typename> typename TTransition, typename TevSource,
116 template <typename> typename EvType, typename DestinyState>
117 void operator()(TTransition<EvType<TevSource>, DestinyState>);
118
119 template <typename T>
120 void operator()(T);
121};
122
123//---------------------------------------------
124template <typename InitialStateType>
126{
127 static void walkStates(std::shared_ptr<SmaccStateInfo> & parentState, bool rootInitialNode);
128};
129
130//---------------------------------------------
131template <typename T>
133{
134 using type_t = typename T::type;
136}
137
138//--------------------------------------------
139template <typename T>
140typename disable_if<boost::mpl::is_sequence<T>>::type processSubState(
141 std::shared_ptr<SmaccStateInfo> & parentState)
142{
143 WalkStatesExecutor<T>::walkStates(parentState, false);
144}
145
146//---------------------------------------------
147template <typename T>
148typename enable_if<boost::mpl::is_sequence<T>>::type processSubState(
149 std::shared_ptr<SmaccStateInfo> & parentState)
150{
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));
154}
155
156//--------------------------------------------
157/*Iterate on All Transitions*/
158template <typename T>
159typename enable_if<boost::mpl::is_sequence<T>>::type processTransitions(
160 std::shared_ptr<SmaccStateInfo> & sourceState)
161{
162 RCLCPP_INFO(
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;
167 RCLCPP_INFO(
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));
171 RCLCPP_INFO(
172 globalNh_->get_logger(), "[DIAG] Completed boost::mpl::for_each for state %s",
173 sourceState->fullStateName.c_str());
174}
175
176template <typename Ev, typename Dst, typename Tag>
178 smacc2::Transition<Ev, boost::statechart::deep_history<Dst>, Tag> *,
179 std::shared_ptr<SmaccStateInfo> & sourceState)
180{
181 RCLCPP_INFO(
182 globalNh_->get_logger(), "[DIAG] processTransition [deep_history] START for state %s",
183 sourceState->toShortName().c_str());
184 auto transitionTypeInfo = TypeInfo::getTypeInfoFromType<
186 RCLCPP_INFO(
187 globalNh_->get_logger(), "[DIAG] processTransition [deep_history] got TypeInfo for state %s",
188 sourceState->toShortName().c_str());
189 smacc2::Transition<Ev, Dst, Tag> * mock = nullptr;
190 processTransitionAux(mock, sourceState, true, transitionTypeInfo);
191}
192
193template <typename Ev, typename Dst, typename Tag>
195 smacc2::Transition<Ev, Dst, Tag> * t, std::shared_ptr<SmaccStateInfo> & sourceState)
196{
197 RCLCPP_INFO(
198 globalNh_->get_logger(), "[DIAG] processTransition START for state %s",
199 sourceState->toShortName().c_str());
201 RCLCPP_INFO(
202 globalNh_->get_logger(), "[DIAG] processTransition got TypeInfo for state %s",
203 sourceState->toShortName().c_str());
204 RCLCPP_INFO(
205 globalNh_->get_logger(), "State %s Walker transition: %s", sourceState->toShortName().c_str(),
206 demangleSymbol(typeid(Ev).name()).c_str());
207 processTransitionAux(t, sourceState, false, transitionTypeInfo);
208}
209
210template <typename Ev, typename Dst, typename Tag>
212 smacc2::Transition<Ev, Dst, Tag> *, std::shared_ptr<SmaccStateInfo> & sourceState, bool history,
213 TypeInfo::Ptr & transitionTypeInfo)
214{
215 RCLCPP_INFO(
216 globalNh_->get_logger(), "State %s Walker transition: %s", sourceState->toShortName().c_str(),
217 demangleSymbol(typeid(Ev).name()).c_str());
218 std::string transitionTag;
219 std::string transitionType;
220
221 if (typeid(Tag) != typeid(default_transition_name))
222 {
223 transitionTag = demangleSymbol<Tag>();
224 transitionType = getTransitionType<Tag>();
225 RCLCPP_DEBUG_STREAM(globalNh_->get_logger(), "TRANSITION TYPE:" << transitionType);
226 }
227 else
228 {
229 transitionTag = "";
230 automaticTransitionTag<Ev>(transitionTag);
231 automaticTransitionType<Ev>(transitionType);
232 }
233
234 RCLCPP_INFO_STREAM(globalNh_->get_logger(), "Transition tag: " << transitionTag);
235
236 if (!sourceState->getStateMachineInfo()->containsState<Dst>())
237 {
238 auto realparentState = sourceState->getStateMachineInfo()->getState<typename Dst::TContext>();
239 auto siblingnode = sourceState->getStateMachineInfo()->createState<Dst>(realparentState);
240
241 WalkStatesExecutor<Dst>::walkStates(siblingnode, true);
242 sourceState->declareTransition<Ev>(
243 siblingnode, transitionTag, transitionType, history, transitionTypeInfo);
244 }
245 else
246 {
247 auto siblingnode = sourceState->getStateMachineInfo()->getState<Dst>();
248 sourceState->declareTransition<Ev>(
249 siblingnode, transitionTag, transitionType, history, transitionTypeInfo);
250 }
251}
252
253//---------------------------------------------
254template <typename EvType>
256 std::shared_ptr<SmaccStateInfo> & dstState, const std::string & transitionTag,
257 const std::string & transitionType, bool history, TypeInfo::Ptr transitionTypeInfo)
258{
259 auto evtype = demangledTypeName<EvType>();
260
261 SmaccTransitionInfo transitionInfo;
262 transitionInfo.index = static_cast<int>(transitions_.size());
263 transitionInfo.sourceState = shared_from_this();
264 transitionInfo.destinyState = dstState;
265 transitionInfo.transitionTypeInfo = transitionTypeInfo;
266
267 if (transitionTag != "")
268 transitionInfo.transitionTag = transitionTag;
269 else
270 transitionInfo.transitionTag = "Transition_" + std::to_string(transitionInfo.index);
271
272 transitionInfo.transitionType = transitionType;
273 transitionInfo.historyNode = history;
274
275 transitionInfo.eventInfo =
276 std::make_shared<SmaccEventInfo>(transitionTypeInfo->templateParameters.front());
277
278 EventLabel<EvType>(transitionInfo.eventInfo->label);
279 // RCLCPP_DEBUG_STREAM(getLogger(), "LABEL: " << transitionInfo.eventInfo->label);
280
281 transitions_.push_back(transitionInfo);
282}
283//---------------------------------------------
284
285//------------------ staticConfigure -----------------------------
286
287// SFINAE test
288template <typename T>
290{
291private:
292 typedef char YesType[1];
293 typedef char NoType[2];
294
295 template <typename C>
296 static YesType & test(decltype(&C::staticConfigure));
297 template <typename C>
298 static NoType & test(...);
299
300public:
301 enum
302 {
303 value = sizeof(test<T>(0)) == sizeof(YesType)
304 };
305};
306
307//---------------------------------------------
308template <typename Ev, typename Dst>
309void processTransition(statechart::transition<Ev, Dst> *, std::shared_ptr<SmaccStateInfo> &)
310{
311}
312
313template <typename Ev>
314void processTransition(statechart::custom_reaction<Ev> *, std::shared_ptr<SmaccStateInfo> &)
315{
316}
317
318//---------------------------------------------
319// only reached if it is a leaf transition in the mpl::list
320
321template <typename T>
322typename disable_if<boost::mpl::is_sequence<T>>::type processTransitions(
323 std::shared_ptr<SmaccStateInfo> & sourceState)
324{
325 RCLCPP_INFO(
326 globalNh_->get_logger(), "[DIAG] processTransitions [single/leaf] called for state %s",
327 sourceState->fullStateName.c_str());
328 T * dummy = nullptr;
329 processTransition(dummy, sourceState);
330 RCLCPP_INFO(
331 globalNh_->get_logger(), "[DIAG] processTransitions [single/leaf] completed for state %s",
332 sourceState->fullStateName.c_str());
333}
334
335template <typename T>
336typename std::enable_if<HasOnDefinition<T>::value, void>::type CallOnDefinition()
337{
338 /* something when T has toString ... */
339 RCLCPP_INFO_STREAM(
340 globalNh_->get_logger(), "EXECUTING ONDEFINITION: " << demangleSymbol(typeid(T).name()));
341 T::staticConfigure();
342}
343
344template <typename T>
345typename std::enable_if<!HasOnDefinition<T>::value, void>::type CallOnDefinition()
346{
347 RCLCPP_INFO_STREAM(
348 globalNh_->get_logger(),
349 "static OnDefinition: dont exist for " << demangleSymbol(typeid(T).name()));
350 /* something when T has toString ... */
351}
352
353//--------------------------------------------
354
355template <
356 template <typename, typename, typename> typename TTransition, typename TevSource,
357 template <typename> typename EvType, typename Tag, typename DestinyState>
358void AddTransition::operator()(TTransition<EvType<TevSource>, DestinyState, Tag>)
359{
360 RCLCPP_INFO(
361 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [3-param] called for state %s",
362 currentState_->fullStateName.c_str());
364 RCLCPP_INFO(
365 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [3-param] completed for state %s",
366 currentState_->fullStateName.c_str());
367}
368
369//--------------------------------------------
370
371template <
372 template <typename, typename> typename TTransition, typename TevSource,
373 template <typename> typename EvType, typename DestinyState>
374void AddTransition::operator()(TTransition<EvType<TevSource>, DestinyState>)
375{
376 RCLCPP_INFO(
377 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [2-param] called for state %s",
378 currentState_->fullStateName.c_str());
380 RCLCPP_INFO(
381 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [2-param] completed for state %s",
382 currentState_->fullStateName.c_str());
383}
384
385template <typename TTrans>
387{
388 RCLCPP_INFO(
389 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [generic] called for state %s",
390 currentState_->fullStateName.c_str());
391 using type_t = typename TTrans::type;
393 RCLCPP_INFO(
394 globalNh_->get_logger(), "[DIAG] AddTransition::operator() [generic] completed for state %s",
395 currentState_->fullStateName.c_str());
396}
397
398//-----------------------------------------------------------------------------------
399template <typename InitialStateType>
401 std::shared_ptr<SmaccStateInfo> & parentState, bool rootInitialNode)
402{
403 auto currentname = demangledTypeName<InitialStateType>();
404
405 std::shared_ptr<SmaccStateInfo> targetState;
406
407 if (!rootInitialNode)
408 {
409 if (parentState->getStateMachineInfo()->containsState<InitialStateType>())
410 {
411 return;
412 }
413
414 targetState = parentState->createChildState<InitialStateType>();
415 }
416 else
417 {
418 targetState = parentState;
419 }
420
422
423 typedef
424 typename std::remove_pointer<decltype(InitialStateType::smacc_inner_type)>::type InnerType;
425 processSubState<InnerType>(targetState);
426
427 // -------------------- REACTIONS --------------------
428 typedef typename InitialStateType::reactions reactions;
429
431}
432
433//------------------------------------------------
434
435template <typename InitialStateType>
437{
438 auto initialState = this->createState<InitialStateType>(nullptr);
440}
441
442template <typename StateType>
443std::shared_ptr<SmaccStateInfo> SmaccStateMachineInfo::createState(
444 std::shared_ptr<SmaccStateInfo> parent)
445{
446 auto thisptr = this->shared_from_this();
447 auto * statetid = &(typeid(StateType));
448
449 auto demangledName = demangledTypeName<StateType>();
450 RCLCPP_INFO_STREAM(getLogger(), "Creating State Info: " << demangledName);
451
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());
456
457 if (parent != nullptr)
458 {
459 parent->children_.push_back(state);
460 }
461
462 this->addState(state);
463
464 return state;
465}
466
467template <typename StateType>
468void SmaccStateMachineInfo::addState(std::shared_ptr<StateType> & state)
469{
470 states[state->fullStateName] = state;
471}
472
473template <typename StateType>
474std::shared_ptr<SmaccStateInfo> SmaccStateInfo::createChildState()
475{
476 auto realparentState = this->getStateMachineInfo()->getState<typename StateType::TContext>();
477
478 auto childState = this->getStateMachineInfo()->createState<StateType>(realparentState);
479
480 RCLCPP_WARN_STREAM(
481 getLogger(), "Real parent state> " << demangleSymbol<typename StateType::TContext>());
482
483 return childState;
484}
485} // namespace introspection
486} // namespace smacc2
static YesType & test(decltype(&C::staticConfigure))
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
std::vector< SmaccTransitionInfo > transitions_
void addState(std::shared_ptr< StateType > &state)
std::map< std::string, std::shared_ptr< SmaccStateInfo > > states
std::vector< smacc2_msgs::msg::SmaccState > stateMsgs
std::shared_ptr< SmaccStateInfo > createState(std::shared_ptr< SmaccStateInfo > parent)
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 > &currentState)
void operator()(TTransition< EvType< TevSource >, DestinyState, Tag >)
std::shared_ptr< SmaccStateInfo > & currentState_
std::weak_ptr< const SmaccStateInfo > sourceState
std::shared_ptr< SmaccEventInfo > eventInfo
std::weak_ptr< const SmaccStateInfo > destinyState
smacc2::introspection::TypeInfo::Ptr transitionTypeInfo
static void walkStates(std::shared_ptr< SmaccStateInfo > &parentState, bool rootInitialNode)