SMACC2
Loading...
Searching...
No Matches
smacc_state_machine_info.cpp
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
23
24namespace smacc2
25{
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}
227} // namespace smacc2
const std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > & getOrthogonals() const
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::map< std::string, std::shared_ptr< SmaccStateInfo > > states
std::vector< smacc2_msgs::msg::SmaccState > stateMsgs
std::string demangleSymbol()
void transitionInfoToMsg(const SmaccTransitionInfo &transition, smacc2_msgs::msg::SmaccTransition &transitionMsg)