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 > parentState)
 
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 470 of file smacc_state_machine_info.hpp.

471{
472 states[state->fullStateName] = state;
473}
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 = (int)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 if (smaccBehaviorInfoMappingByOrthogonalType.count(bhinfo.orthogonalType) == 0)
99 {
100 smaccBehaviorInfoMappingByOrthogonalType[bhinfo.orthogonalType] =
101 std::vector<smacc2::ClientBehaviorInfoEntry *>();
102 }
103
104 smaccBehaviorInfoMappingByOrthogonalType[bhinfo.orthogonalType].push_back(&bhinfo);
105 }
106 }
107
108 // Describe every runtime orthogonal with the behaviors and clients it holds for this state.
109 auto & runtimeOrthogonals = sm->getOrthogonals();
110
111 for (auto & orthogonal : runtimeOrthogonals)
112 {
113 smacc2_msgs::msg::SmaccOrthogonal orthogonalMsg;
114
115 const auto * orthogonaltid = &typeid(*(orthogonal.second));
116 orthogonalMsg.name = demangleSymbol(orthogonaltid->name());
117
118 ss << " - orthogonal: " << orthogonalMsg.name << std::endl;
119
120 // Client behaviors this state configures in this orthogonal.
121 if (smaccBehaviorInfoMappingByOrthogonalType[orthogonaltid].size() > 0)
122 {
123 auto & behaviors = smaccBehaviorInfoMappingByOrthogonalType[orthogonaltid];
124 for (auto & bhinfo : behaviors)
125 {
126 auto ClientBehaviorName = demangleSymbol(bhinfo->behaviorType->name());
127 orthogonalMsg.client_behavior_names.push_back(ClientBehaviorName);
128 ss << " - client behavior: " << ClientBehaviorName << std::endl;
129 }
130 }
131 else
132 {
133 ss << " - NO CLIENT BEHAVIORS -" << std::endl;
134 }
135
136 // Clients owned by the orthogonal for the whole state machine lifetime.
137 auto & clients = orthogonal.second->getClients();
138 if (clients.size() > 0)
139 {
140 for (auto & client : clients)
141 {
142 auto clientTid = client->getType();
143 auto clientName = clientTid->getNonTemplatedTypeName();
144 orthogonalMsg.client_names.push_back(clientName);
145 ss << " - client: " << clientName << std::endl;
146 }
147 }
148 else
149 {
150 ss << " - NO CLIENTS - " << std::endl;
151 }
152 stateMsg.orthogonals.push_back(orthogonalMsg);
153 }
154
155 // Event generators attached to this state.
156 ss << " State event generators:" << std::endl;
157 if (SmaccStateInfo::eventGeneratorsInfo.count(statetid) > 0)
158 {
159 int k = 0;
160 for (auto & eginfo : SmaccStateInfo::eventGeneratorsInfo[statetid])
161 {
162 smacc2_msgs::msg::SmaccEventGenerator eventGeneratorMsg;
163 eventGeneratorMsg.index = k++;
164 eventGeneratorMsg.type_name = eginfo->eventGeneratorType->getFullName();
165
166 ss << " - event generator: " << eventGeneratorMsg.type_name << std::endl;
167 if (eginfo->objectTagType != nullptr)
168 {
169 eventGeneratorMsg.object_tag = eginfo->objectTagType->getFullName();
170 ss << " - object tag: " << eventGeneratorMsg.object_tag << std::endl;
171 }
172 stateMsg.event_generators.push_back(eventGeneratorMsg);
173 }
174 }
175
176 // State reactors attached to this state, with the events each one listens to.
177 ss << " State reactors:" << std::endl;
178 if (SmaccStateInfo::stateReactorsInfo.count(statetid) > 0)
179 {
180 int k = 0;
181 for (auto & srinfo : SmaccStateInfo::stateReactorsInfo[statetid])
182 {
183 smacc2_msgs::msg::SmaccStateReactor stateReactorMsg;
184 stateReactorMsg.index = k++;
185 stateReactorMsg.type_name = srinfo->stateReactorType->getFullName();
186
187 ss << " - state reactor: " << stateReactorMsg.type_name << std::endl;
188 if (srinfo->objectTagType != nullptr)
189 {
190 stateReactorMsg.object_tag = srinfo->objectTagType->getFullName();
191 ss << " - object tag: " << stateReactorMsg.object_tag << std::endl;
192 }
193
194 // Record every source event that can trigger this reactor.
195 for (auto & tev : srinfo->sourceEventTypes)
196 {
197 // WE SHOULD CREATE A SMACC_EVENT_INFO TYPE, also using in typewalker transition
198 auto eventTypeName = tev->getEventTypeName();
199 smacc2_msgs::msg::SmaccEvent event;
200
201 ss << " - triggering event: " << tev->getEventTypeName() << std::endl;
202 event.event_type = eventTypeName;
203
204 event.event_source = tev->getEventSourceName();
205 ss << " - source type: " << event.event_source << std::endl;
206
207 event.event_object_tag = tev->getOrthogonalName();
208 ss << " - source object: " << event.event_object_tag << std::endl;
209
210 event.label = tev->label;
211 ss << " - event label: " << event.label << std::endl;
212
213 stateReactorMsg.event_sources.push_back(event);
214 }
215
216 stateMsg.state_reactors.push_back(stateReactorMsg);
217 }
218 }
219 else
220 {
221 ss << "- NO STATE REACTORS - " << std::endl;
222 }
223
224 ss << "----------------------------------------------------------" << std::endl;
225
226 // Emit the text summary at debug level and keep the message for the description topic.
227 auto resumeMsg = ss.str();
228 RCLCPP_DEBUG(getLogger(), "%s", resumeMsg.c_str());
229 stateMsgs.push_back(stateMsg);
230 }
231}
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 438 of file smacc_state_machine_info.hpp.

439{
440 auto initialState = this->createState<InitialStateType>(nullptr);
442}
std::shared_ptr< SmaccStateInfo > createState(std::shared_ptr< SmaccStateInfo > parentState)
static void walkStates(std::shared_ptr< SmaccStateInfo > &currentState, 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 > parentState)

Definition at line 445 of file smacc_state_machine_info.hpp.

447{
448 auto thisptr = this->shared_from_this();
449 auto * statetid = &(typeid(StateType));
450
451 auto demangledName = demangledTypeName<StateType>();
452 RCLCPP_INFO_STREAM(getLogger(), "Creating State Info: " << demangledName);
453
454 auto state = std::make_shared<SmaccStateInfo>(statetid, parent, thisptr);
455 state->demangledStateName = demangledName;
456 state->fullStateName = typeid(StateType).name();
457 state->stateIndex_ = states.size();
458
459 if (parent != nullptr)
460 {
461 parent->children_.push_back(state);
462 }
463
464 this->addState(state);
465
466 return state;
467}
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: