SMACC2
orthogonal.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 if (clBehavior != nullptr)
52 {
53 RCLCPP_INFO(
54 getLogger(), "[Orthogonal %s] adding client behavior: %s", this->getName().c_str(),
55 clBehavior->getName().c_str());
56 clBehavior->stateMachine_ = this->getStateMachine();
57 clBehavior->currentOrthogonal = this;
58
59 clientBehaviors_.push_back(clBehavior);
60 }
61 else
62 {
63 RCLCPP_INFO(
64 getLogger(), "[orthogonal %s] no client behaviors in this state", this->getName().c_str());
65 }
66}
67
69
70std::string ISmaccOrthogonal::getName() const { return demangleSymbol(typeid(*this).name()); }
71
73{
74 for (auto & clBehavior : clientBehaviors_)
75 {
76 RCLCPP_INFO(
77 getLogger(), "[Orthogonal %s] runtimeConfigure, current Behavior: %s",
78 this->getName().c_str(), clBehavior->getName().c_str());
79
80 clBehavior->runtimeConfigure();
81 }
82}
83
85{
86 if (clientBehaviors_.size() > 0)
87 {
88 for (auto & clBehavior : clientBehaviors_)
89 {
90 RCLCPP_INFO(
91 getLogger(), "[Orthogonal %s] OnEntry, current Behavior: %s", orthogonalName, cbName);
92
93 try
94 {
96 clBehavior->executeOnEntry();
97 }
98 catch (const std::exception & e)
99 {
100 RCLCPP_ERROR(
101 getLogger(),
102 "[ClientBehavior %s] Exception on Entry - continuing with next client behavior. "
103 "Exception info: "
104 "%s",
105 cbName, e.what());
106 }
108 }
109 }
110 else
111 {
112 RCLCPP_INFO(
113 getLogger(), "[Orthogonal %s] OnEntry -> empty orthogonal (no client behavior) ",
115 }
116}
117
119{
120 if (clientBehaviors_.size() > 0)
121 {
122 for (auto & clBehavior : clientBehaviors_)
123 {
124 RCLCPP_INFO(
125 getLogger(), "[Orthogonal %s] OnExit, current Behavior: %s", orthogonalName, cbName);
126 try
127 {
129 clBehavior->executeOnExit();
130 }
131 catch (const std::exception & e)
132 {
133 RCLCPP_ERROR(
134 getLogger(),
135 "[ClientBehavior %s] Exception onExit - continuing with next client behavior. Exception "
136 "info: %s",
137 cbName, e.what());
138 }
140 }
141
142 int i = 0;
143 for (auto & clBehavior : clientBehaviors_)
144 {
145 clBehavior->dispose();
146 clientBehaviors_[i] = nullptr;
147 }
148
149 clientBehaviors_.clear();
150 }
151 else
152 {
153 RCLCPP_INFO(getLogger(), "[Orthogonal %s] OnExit", orthogonalName);
154 }
155}
156} // namespace smacc2
std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > clientBehaviors_
ISmaccStateMachine * getStateMachine()
void setStateMachine(ISmaccStateMachine *value)
Definition: orthogonal.cpp:32
virtual void onInitialize()
Definition: orthogonal.cpp:68
void addClientBehavior(std::shared_ptr< smacc2::ISmaccClientBehavior > clientBehavior)
Definition: orthogonal.cpp:49
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
ISmaccStateMachine * stateMachine_
virtual std::string getName() const
Definition: orthogonal.cpp:70
rclcpp::Node::SharedPtr getNode()
Definition: orthogonal.cpp:47
rclcpp::Node::SharedPtr getNode()
std::string demangleSymbol(const std::string &name)
#define orthogonalName
Definition: orthogonal.cpp:27
#define statename
Definition: orthogonal.cpp:26
#define cbName
Definition: orthogonal.cpp:28
void TRACEPOINT(spinOnce)
smacc2_client_behavior_on_exit_end
smacc2_client_behavior_on_exit_start
smacc2_client_behavior_on_entry_start
smacc2_client_behavior_on_entry_end