SMACC2
Loading...
Searching...
No Matches
smacc2::introspection::SmaccStateMachineInfo Class Reference

#include <smacc_state_machine_info.hpp>

Inheritance diagram for smacc2::introspection::SmaccStateMachineInfo:
Inheritance graph
Collaboration diagram for smacc2::introspection::SmaccStateMachineInfo:
Collaboration graph

Public Member Functions

 SmaccStateMachineInfo (rclcpp::Node::SharedPtr nh)
 
template<typename InitialStateType >
void buildStateMachineInfo ()
 
template<typename StateType >
std::shared_ptr< SmaccStateInfo > createState (std::shared_ptr< SmaccStateInfo > parent)
 
template<typename StateType >
bool containsState ()
 
template<typename StateType >
std::shared_ptr< SmaccStateInfo > getState ()
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger ()
 
template<typename StateType >
void addState (std::shared_ptr< StateType > &state)
 
void assembleSMStructureMessage (ISmaccStateMachine *sm)
 

Public Attributes

std::map< std::string, std::shared_ptr< SmaccStateInfo > > states
 
std::vector< smacc2_msgs::msg::SmaccState > stateMsgs
 

Private Attributes

rclcpp::Node::SharedPtr nh_
 

Detailed Description

Definition at line 44 of file smacc_state_machine_info.hpp.

Constructor & Destructor Documentation

◆ SmaccStateMachineInfo()

smacc2::introspection::SmaccStateMachineInfo::SmaccStateMachineInfo ( rclcpp::Node::SharedPtr nh)
inlineexplicit

Definition at line 47 of file smacc_state_machine_info.hpp.

Member Function Documentation

◆ addState()

template<typename StateType >
void smacc2::introspection::SmaccStateMachineInfo::addState ( std::shared_ptr< StateType > & state)

Definition at line 468 of file smacc_state_machine_info.hpp.

469{
470 states[state->fullStateName] = state;
471}
std::map< std::string, std::shared_ptr< SmaccStateInfo > > states

References states.

Referenced by createState().

Here is the caller graph for this function:

◆ assembleSMStructureMessage()

void smacc2::SmaccStateMachineInfo::assembleSMStructureMessage ( ISmaccStateMachine * sm)

Definition at line 26 of file smacc_state_machine_info.cpp.

27{
28 std::stringstream ss;
29
30 ss << "----------- PRINT STATE MACHINE STRUCTURE -------------------" << std::endl;
31 // Rebuild the state message list from scratch on every call.
32 stateMsgs.clear();
33 // Walk every registered state and describe it in a message and in the text summary.
34 for (auto & val : this->states)
35 {
36 smacc2_msgs::msg::SmaccState stateMsg;
37 auto state = val.second;
38 // Basic identity: index, demangled name and depth in the state hierarchy.
39 stateMsg.index = state->stateIndex_;
40
41 ss << "**** State: " << demangleSymbol(val.first.c_str()) << std::endl;
42
43 stateMsg.name = state->getDemangledFullName();
44 stateMsg.level = static_cast<int8_t>(state->getStateLevel());
45
46 ss << "Index: " << stateMsg.index << std::endl;
47 ss << "StateLevel: " << stateMsg.level << std::endl;
48
49 ss << " Childstates:" << std::endl;
50
51 // List the direct child states.
52 for (auto & child : state->children_)
53 {
54 auto childStateName = child->getDemangledFullName();
55 stateMsg.children_states.push_back(childStateName);
56
57 ss << " - " << childStateName << std::endl;
58 }
59
60 ss << " Transitions:" << std::endl;
61
62 // Convert each transition table row into a message and print its details.
63 for (auto & transition : state->transitions_)
64 {
65 smacc2_msgs::msg::SmaccTransition transitionMsg;
66
67 transitionInfoToMsg(transition, transitionMsg);
68
69 ss << " - Transition. " << std::endl;
70 ss << " - Index: " << transitionMsg.index << std::endl;
71 ss << " - Transition Name: " << transitionMsg.transition_name << std::endl;
72 ss << " - Transition Type: " << transitionMsg.transition_type << std::endl;
73 ss << " - Event Type :" << transitionMsg.event.event_type << std::endl;
74 ss << " - Event Source: " << transitionMsg.event.event_source << std::endl;
75 ss << " - Event ObjectTag: " << transitionMsg.event.event_object_tag << std::endl;
76 ss << " - Event Label: " << transitionMsg.event.label << std::endl;
77 ss << " - Destiny State: " << transitionMsg.destiny_state_name << std::endl;
78 ss << " - Owner State: " << transitionMsg.source_state_name << std::endl;
79 ss << " - Is History Node: " << std::to_string(transitionMsg.history_node) << std::endl;
80 ss << " - TransitionC++Type: " << transition.transitionTypeInfo->getFullName()
81 << std::endl;
82 ss << " - EventC++Type: " << transition.eventInfo->eventType->getFullName() << std::endl;
83
84 stateMsg.transitions.push_back(transitionMsg);
85 }
86
87 const std::type_info * statetid = state->tid_;
88
89 // Group the client behaviors statically configured for this state by orthogonal type.
90 std::map<const std::type_info *, std::vector<smacc2::ClientBehaviorInfoEntry *>>
91 smaccBehaviorInfoMappingByOrthogonalType;
92
93 ss << " Orthogonals:" << std::endl;
94 if (SmaccStateInfo::staticBehaviorInfo.count(statetid) > 0)
95 {
96 for (auto & bhinfo : SmaccStateInfo::staticBehaviorInfo[statetid])
97 {
98 smaccBehaviorInfoMappingByOrthogonalType[bhinfo.orthogonalType].push_back(&bhinfo);
99 }
100 }
101
102 // Describe every runtime orthogonal with the behaviors and clients it holds for this state.
103 auto & runtimeOrthogonals = sm->getOrthogonals();
104
105 for (auto & orthogonal : runtimeOrthogonals)
106 {
107 smacc2_msgs::msg::SmaccOrthogonal orthogonalMsg;
108
109 const auto & orthogonalRef = *(orthogonal.second);
110 const auto * orthogonaltid = &typeid(orthogonalRef);
111 orthogonalMsg.name = demangleSymbol(orthogonaltid->name());
112
113 ss << " - orthogonal: " << orthogonalMsg.name << std::endl;
114
115 // Client behaviors this state configures in this orthogonal.
116 if (smaccBehaviorInfoMappingByOrthogonalType[orthogonaltid].size() > 0)
117 {
118 auto & behaviors = smaccBehaviorInfoMappingByOrthogonalType[orthogonaltid];
119 for (auto & bhinfo : behaviors)
120 {
121 auto ClientBehaviorName = demangleSymbol(bhinfo->behaviorType->name());
122 orthogonalMsg.client_behavior_names.push_back(ClientBehaviorName);
123 ss << " - client behavior: " << ClientBehaviorName << std::endl;
124 }
125 }
126 else
127 {
128 ss << " - NO CLIENT BEHAVIORS -" << std::endl;
129 }
130
131 // Clients owned by the orthogonal for the whole state machine lifetime.
132 auto & clients = orthogonal.second->getClients();
133 if (clients.size() > 0)
134 {
135 for (auto & client : clients)
136 {
137 auto clientTid = client->getType();
138 auto clientName = clientTid->getNonTemplatedTypeName();
139 orthogonalMsg.client_names.push_back(clientName);
140 ss << " - client: " << clientName << std::endl;
141 }
142 }
143 else
144 {
145 ss << " - NO CLIENTS - " << std::endl;
146 }
147 stateMsg.orthogonals.push_back(orthogonalMsg);
148 }
149
150 // Event generators attached to this state.
151 ss << " State event generators:" << std::endl;
152 if (SmaccStateInfo::eventGeneratorsInfo.count(statetid) > 0)
153 {
154 int k = 0;
155 for (const auto & eginfo : SmaccStateInfo::eventGeneratorsInfo[statetid])
156 {
157 smacc2_msgs::msg::SmaccEventGenerator eventGeneratorMsg;
158 eventGeneratorMsg.index = k++;
159 eventGeneratorMsg.type_name = eginfo->eventGeneratorType->getFullName();
160
161 ss << " - event generator: " << eventGeneratorMsg.type_name << std::endl;
162 if (eginfo->objectTagType != nullptr)
163 {
164 eventGeneratorMsg.object_tag = eginfo->objectTagType->getFullName();
165 ss << " - object tag: " << eventGeneratorMsg.object_tag << std::endl;
166 }
167 stateMsg.event_generators.push_back(eventGeneratorMsg);
168 }
169 }
170
171 // State reactors attached to this state, with the events each one listens to.
172 ss << " State reactors:" << std::endl;
173 if (SmaccStateInfo::stateReactorsInfo.count(statetid) > 0)
174 {
175 int k = 0;
176 for (const auto & srinfo : SmaccStateInfo::stateReactorsInfo[statetid])
177 {
178 smacc2_msgs::msg::SmaccStateReactor stateReactorMsg;
179 stateReactorMsg.index = k++;
180 stateReactorMsg.type_name = srinfo->stateReactorType->getFullName();
181
182 ss << " - state reactor: " << stateReactorMsg.type_name << std::endl;
183 if (srinfo->objectTagType != nullptr)
184 {
185 stateReactorMsg.object_tag = srinfo->objectTagType->getFullName();
186 ss << " - object tag: " << stateReactorMsg.object_tag << std::endl;
187 }
188
189 // Record every source event that can trigger this reactor.
190 for (auto & tev : srinfo->sourceEventTypes)
191 {
192 // WE SHOULD CREATE A SMACC_EVENT_INFO TYPE, also using in typewalker transition
193 auto eventTypeName = tev->getEventTypeName();
194 smacc2_msgs::msg::SmaccEvent event;
195
196 ss << " - triggering event: " << tev->getEventTypeName() << std::endl;
197 event.event_type = eventTypeName;
198
199 event.event_source = tev->getEventSourceName();
200 ss << " - source type: " << event.event_source << std::endl;
201
202 event.event_object_tag = tev->getOrthogonalName();
203 ss << " - source object: " << event.event_object_tag << std::endl;
204
205 event.label = tev->label;
206 ss << " - event label: " << event.label << std::endl;
207
208 stateReactorMsg.event_sources.push_back(event);
209 }
210
211 stateMsg.state_reactors.push_back(stateReactorMsg);
212 }
213 }
214 else
215 {
216 ss << "- NO STATE REACTORS - " << std::endl;
217 }
218
219 ss << "----------------------------------------------------------" << std::endl;
220
221 // Emit the text summary at debug level and keep the message for the description topic.
222 auto resumeMsg = ss.str();
223 RCLCPP_DEBUG(getLogger(), "%s", resumeMsg.c_str());
224 stateMsgs.push_back(stateMsg);
225 }
226}
static std::map< const std::type_info *, std::vector< std::shared_ptr< SmaccStateReactorInfo > > > stateReactorsInfo
static std::map< const std::type_info *, std::vector< std::shared_ptr< SmaccEventGeneratorInfo > > > eventGeneratorsInfo
static std::map< const std::type_info *, std::vector< ClientBehaviorInfoEntry > > staticBehaviorInfo
std::vector< smacc2_msgs::msg::SmaccState > stateMsgs
std::string demangleSymbol()
void transitionInfoToMsg(const SmaccTransitionInfo &transition, smacc2_msgs::msg::SmaccTransition &transitionMsg)

References smacc2::introspection::demangleSymbol(), smacc2::introspection::SmaccStateInfo::eventGeneratorsInfo, getLogger(), smacc2::ISmaccStateMachine::getOrthogonals(), stateMsgs, smacc2::introspection::SmaccStateInfo::stateReactorsInfo, states, smacc2::introspection::SmaccStateInfo::staticBehaviorInfo, and smacc2::introspection::transitionInfoToMsg().

Here is the call graph for this function:

◆ buildStateMachineInfo()

template<typename InitialStateType >
void smacc2::introspection::SmaccStateMachineInfo::buildStateMachineInfo ( )

Definition at line 436 of file smacc_state_machine_info.hpp.

437{
438 auto initialState = this->createState<InitialStateType>(nullptr);
440}
std::shared_ptr< SmaccStateInfo > createState(std::shared_ptr< SmaccStateInfo > parent)
static void walkStates(std::shared_ptr< SmaccStateInfo > &parentState, bool rootInitialNode)

References createState(), and smacc2::introspection::WalkStatesExecutor< InitialStateType >::walkStates().

Here is the call graph for this function:

◆ containsState()

template<typename StateType >
bool smacc2::introspection::SmaccStateMachineInfo::containsState ( )
inline

Definition at line 60 of file smacc_state_machine_info.hpp.

61 {
62 auto typeNameStr = typeid(StateType).name();
63
64 return states.count(typeNameStr) > 0;
65 }

References states.

Referenced by getState().

Here is the caller graph for this function:

◆ createState()

template<typename StateType >
std::shared_ptr< SmaccStateInfo > smacc2::introspection::SmaccStateMachineInfo::createState ( std::shared_ptr< SmaccStateInfo > parent)

Definition at line 443 of file smacc_state_machine_info.hpp.

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}
void addState(std::shared_ptr< StateType > &state)
std::string demangledTypeName()

References addState(), smacc2::introspection::demangledTypeName(), getLogger(), and states.

Referenced by buildStateMachineInfo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLogger()

rclcpp::Logger smacc2::introspection::SmaccStateMachineInfo::getLogger ( )
inline

Definition at line 79 of file smacc_state_machine_info.hpp.

79{ return nh_->get_logger(); }

References nh_.

Referenced by assembleSMStructureMessage(), and createState().

Here is the caller graph for this function:

◆ getNode()

rclcpp::Node::SharedPtr smacc2::introspection::SmaccStateMachineInfo::getNode ( )
inline

Definition at line 77 of file smacc_state_machine_info.hpp.

77{ return nh_; }

References nh_.

◆ getState()

template<typename StateType >
std::shared_ptr< SmaccStateInfo > smacc2::introspection::SmaccStateMachineInfo::getState ( )
inline

Definition at line 68 of file smacc_state_machine_info.hpp.

69 {
70 if (this->containsState<StateType>())
71 {
72 return states[typeid(StateType).name()];
73 }
74 return nullptr;
75 }

References containsState().

Here is the call graph for this function:

Member Data Documentation

◆ nh_

rclcpp::Node::SharedPtr smacc2::introspection::SmaccStateMachineInfo::nh_
private

Definition at line 86 of file smacc_state_machine_info.hpp.

Referenced by getLogger(), and getNode().

◆ stateMsgs

std::vector<smacc2_msgs::msg::SmaccState> smacc2::introspection::SmaccStateMachineInfo::stateMsgs

Definition at line 51 of file smacc_state_machine_info.hpp.

Referenced by assembleSMStructureMessage().

◆ states

std::map<std::string, std::shared_ptr<SmaccStateInfo> > smacc2::introspection::SmaccStateMachineInfo::states

The documentation for this class was generated from the following files: