SMACC2
Loading...
Searching...
No Matches
orthogonal.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
25
26#define statename stateMachine_->getCurrentState()->getClassName().c_str()
27#define orthogonalName getName().c_str()
28#define cbName clBehavior->getName().c_str()
29
30namespace smacc2
31{
33{
34 this->stateMachine_ = value;
35 this->onInitialize();
36 this->initializeClients();
37}
38
40{
41 for (auto & c : this->clients_)
42 {
43 c->initialize();
44 }
45}
46
47rclcpp::Node::SharedPtr ISmaccOrthogonal::getNode() { return this->stateMachine_->getNode(); }
48
49void ISmaccOrthogonal::addClientBehavior(std::shared_ptr<smacc2::ISmaccClientBehavior> clBehavior)
50{
51 std::lock_guard<std::mutex> lock(this->mutex_);
52
53 if (clBehavior != nullptr)
54 {
55 RCLCPP_INFO(
56 getLogger(), "[Orthogonal: %s] Adding client behavior: %s.", this->getName().c_str(),
57 clBehavior->getName().c_str());
58 clBehavior->stateMachine_ = this->getStateMachine();
59 clBehavior->currentOrthogonal = this;
60 clBehavior->currentState = clBehavior->stateMachine_->getCurrentState();
61
62 clientBehaviors_.back().push_back(clBehavior);
63 }
64 else
65 {
66 RCLCPP_INFO(
67 getLogger(), "[Orthogonal: %s] No client behaviors in this state.", this->getName().c_str());
68 }
69}
70
72
74{
75 RCLCPP_INFO(
76 getLogger(), "[Orthogonal: %s] InitState: %s.", this->getName().c_str(),
77 state->getClassName().c_str());
78 clientBehaviors_.push_back(std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>>());
79}
80
81std::string ISmaccOrthogonal::getName() const { return demangleSymbol(typeid(*this).name()); }
82
83// Snapshot of the current state's behavior list, taken under mutex_. The callers iterate the
84// copy with the mutex released, so behavior code never runs while this orthogonal's mutex is
85// held (see the comment on mutex_ in the header). The copied shared_ptrs keep the behaviors
86// alive for the duration of the loop; disposal runs on the same thread, after these loops.
87std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>>
89{
90 std::lock_guard<std::mutex> lock(this->mutex_);
91 return clientBehaviors_.back();
92}
93
95{
96 const auto behaviors = this->currentClientBehaviors();
97
98 for (auto & clBehavior : behaviors)
99 {
100 RCLCPP_INFO(
101 getLogger(), "[Orthogonal: %s] runtimeConfigure(), current behavior: %s.",
102 this->getName().c_str(), clBehavior->getName().c_str());
103
104 try
105 {
106 clBehavior->runtimeConfigure();
107 }
108 catch (const std::exception & e)
109 {
110 RCLCPP_ERROR(
111 getLogger(),
112 "[ClientBehavior: %s] Exception in runtimeConfigure - continuing with next client "
113 "behavior. "
114 "Exception info: %s",
115 clBehavior->getName().c_str(), e.what());
116 clBehavior->postInitializationFailureEvent(e.what());
117 }
118 }
119}
120
122{
123 const auto behaviors = this->currentClientBehaviors();
124
125 if (behaviors.size() > 0)
126 {
127 for (auto & clBehavior : behaviors)
128 {
129 RCLCPP_INFO(
130 getLogger(), "[Orthogonal: %s] OnEntry, current Behavior: %s", orthogonalName, cbName);
131
132 try
133 {
134 TRACETOOLS_TRACEPOINT(
136 clBehavior->executeOnEntry();
137 }
138 catch (const std::exception & e)
139 {
140 RCLCPP_ERROR(
141 getLogger(),
142 "[ClientBehavior: %s] Exception on Entry - continuing with next client behavior. "
143 "Exception info: "
144 "%s",
145 cbName, e.what());
146 clBehavior->postInitializationFailureEvent(e.what());
147 }
149 }
150 }
151 else
152 {
153 RCLCPP_INFO(
154 getLogger(), "[Orthogonal: %s] OnEntry -> empty orthogonal (no client behavior).",
156 }
157}
158
160{
161 const auto behaviors = this->currentClientBehaviors();
162
163 if (behaviors.size() > 0)
164 {
165 for (auto & clBehavior : behaviors)
166 {
167 RCLCPP_INFO(
168 getLogger(), "[Orthogonal: %s] OnExit, current Behavior: %s", orthogonalName, cbName);
169 try
170 {
171 TRACETOOLS_TRACEPOINT(
173 clBehavior->executeOnExit();
174 }
175 catch (const std::exception & e)
176 {
177 RCLCPP_ERROR(
178 getLogger(),
179 "[ClientBehavior: %s] Exception onExit - continuing with next client behavior. Exception "
180 "info: %s",
181 cbName, e.what());
182 }
184 }
185 }
186 else
187 {
188 RCLCPP_INFO(getLogger(), "[Orthogonal %s] OnExit().", orthogonalName);
189 }
190}
191
193{
194 for (auto & clBehavior : clientBehaviors_.back())
195 {
196 clBehavior->dispose();
197 // No cast: the typed overload normalizes the key to the complete object's address.
198 this->getStateMachine()->disconnectSmaccSignalObject(clBehavior.get());
199 }
200
201 clientBehaviors_.back().clear();
202 clientBehaviors_.pop_back();
203}
204
205} // namespace smacc2
std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > currentClientBehaviors()
void addClientBehavior(std::shared_ptr< smacc2::ISmaccClientBehavior > clBehavior)
ISmaccStateMachine * getStateMachine()
void setStateMachine(ISmaccStateMachine *value)
virtual void onInitialize()
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
void initState(ISmaccState *state)
ISmaccStateMachine * stateMachine_
std::vector< std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > > clientBehaviors_
virtual std::string getName() const
rclcpp::Node::SharedPtr getNode()
rclcpp::Node::SharedPtr getNode()
void disconnectSmaccSignalObject(void *object_ptr)
virtual std::string getClassName()
std::string demangleSymbol()
#define orthogonalName
#define statename
#define cbName
smacc2_client_behavior_on_exit_end
smacc2_client_behavior_on_exit_start
smacc2_client_behavior_on_entry_start
smacc2_client_behavior_on_entry_end