tree_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 */
13 
14 #pragma once
15 
16 #include <condition_variable>
17 #include <mutex>
18 #include <map>
19 
26 
27 #ifdef _MSC_VER
28 #pragma warning(disable : 4127)
29 #endif
30 
31 namespace BT
32 {
35 {
37  std::string registration_ID;
39  std::string description;
40 };
41 
42 typedef std::unordered_map<std::string, std::string> PortsRemapping;
43 
45 {
47  {}
48 
52 };
53 
55 class TreeNode
56 {
57 public:
58  typedef std::shared_ptr<TreeNode> Ptr;
59 
70  TreeNode(std::string name, NodeConfiguration config);
71 
72  virtual ~TreeNode() = default;
73 
75  virtual BT::NodeStatus executeTick();
76 
79  virtual void halt() = 0;
80 
81  bool isHalted() const;
82 
83  NodeStatus status() const;
84 
86  const std::string& name() const;
87 
91 
92  virtual NodeType type() const = 0;
93 
97 
99  std::function<Optional<NodeStatus>(TreeNode&, NodeStatus)>;
101  std::function<Optional<NodeStatus>(TreeNode&, NodeStatus, NodeStatus)>;
102 
113 
124 
134 
135  // get an unique identifier of this instance of treeNode
136  uint16_t UID() const;
137 
139  const std::string& registrationName() const;
140 
143  const NodeConfiguration& config() const;
144 
152  template <typename T>
153  Result getInput(const std::string& key, T& destination) const;
154 
158  template <typename T>
159  Optional<T> getInput(const std::string& key) const
160  {
161  T out;
162  auto res = getInput(key, out);
163  return (res) ? Optional<T>(out) : nonstd::make_unexpected(res.error());
164  }
165 
166  template <typename T>
167  Result setOutput(const std::string& key, const T& value);
168 
169  // function provide mostrly for debugging purpose to see the raw value
170  // in the port (no remapping and no conversion to a type)
171  StringView getRawPortValue(const std::string& key) const;
172 
175  static bool isBlackboardPointer(StringView str);
176 
178 
180  StringView remapping_value);
181 
182  // Notify the tree should be ticked again()
183  void emitStateChanged();
184 
185 protected:
187  virtual BT::NodeStatus tick() = 0;
188 
189  friend class BehaviorTreeFactory;
190  friend class DecoratorNode;
191  friend class ControlNode;
192  friend class Tree;
193 
194  // Only BehaviorTreeFactory should call this
195  void setRegistrationID(StringView ID);
196 
197  void setWakeUpInstance(std::shared_ptr<WakeUpSignal> instance);
198 
199  void modifyPortsRemapping(const PortsRemapping& new_remapping);
200 
201  void setStatus(NodeStatus new_status);
202 
204  void resetStatus();
205 
206 private:
207  const std::string name_;
208 
210 
211  std::condition_variable state_condition_variable_;
212 
214 
216 
217  const uint16_t uid_;
218 
220 
221  std::string registration_ID_;
222 
224 
226 
227  std::shared_ptr<WakeUpSignal> wake_up_;
228 };
229 
230 //-------------------------------------------------------
231 template <typename T>
232 inline Result TreeNode::getInput(const std::string& key, T& destination) const
233 {
234  auto remap_it = config_.input_ports.find(key);
235  if (remap_it == config_.input_ports.end())
236  {
237  return nonstd::make_unexpected(StrCat("getInput() failed because "
238  "NodeConfiguration::input_ports "
239  "does not contain the key: [",
240  key, "]"));
241  }
242  auto remapped_res = getRemappedKey(key, remap_it->second);
243  try
244  {
245  if (!remapped_res)
246  {
247  destination = convertFromString<T>(remap_it->second);
248  return {};
249  }
250  const auto& remapped_key = remapped_res.value();
251 
252  if (!config_.blackboard)
253  {
254  return nonstd::make_unexpected("getInput() trying to access a Blackboard(BB) "
255  "entry, "
256  "but BB is invalid");
257  }
258 
259  std::unique_lock<std::mutex> entry_lock(config_.blackboard->entryMutex());
260  const Any* val = config_.blackboard->getAny(static_cast<std::string>(remapped_key));
261 
262  if(!val)
263  {
264  return nonstd::make_unexpected(StrCat("getInput() failed because it was unable to "
265  "find the port [", key,
266  "] remapped to BB [", remapped_key, "]"));
267  }
268 
269  if(val->empty())
270  {
271  return nonstd::make_unexpected(StrCat("getInput() failed because the port [", key,
272  "] remapped to BB [", remapped_key, "] was found,"
273  "but its content was not initialized correctly"));
274  }
275 
276  if (!std::is_same<T, std::string>::value && val->type() == typeid(std::string))
277  {
278  destination = convertFromString<T>(val->cast<std::string>());
279  }
280  else
281  {
282  destination = val->cast<T>();
283  }
284  return {};
285  }
286  catch (std::exception& err)
287  {
288  return nonstd::make_unexpected(err.what());
289  }
290 }
291 
292 template <typename T>
293 inline Result TreeNode::setOutput(const std::string& key, const T& value)
294 {
295  if (!config_.blackboard)
296  {
297  return nonstd::make_unexpected("setOutput() failed: trying to access a "
298  "Blackboard(BB) entry, but BB is invalid");
299  }
300 
301  auto remap_it = config_.output_ports.find(key);
302  if (remap_it == config_.output_ports.end())
303  {
304  return nonstd::make_unexpected(StrCat("setOutput() failed: "
305  "NodeConfiguration::output_ports "
306  "does not "
307  "contain the key: [",
308  key, "]"));
309  }
310  StringView remapped_key = remap_it->second;
311  if (remapped_key == "=")
312  {
313  remapped_key = key;
314  }
315  if (isBlackboardPointer(remapped_key))
316  {
317  remapped_key = stripBlackboardPointer(remapped_key);
318  }
319  config_.blackboard->set(static_cast<std::string>(remapped_key), value);
320 
321  return {};
322 }
323 
324 // Utility function to fill the list of ports using T::providedPorts();
325 template <typename T>
327 {
328  for (const auto& it : getProvidedPorts<T>())
329  {
330  const auto& port_name = it.first;
331  const auto direction = it.second.direction();
332  if (direction != PortDirection::OUTPUT)
333  {
334  config.input_ports[port_name] = "=";
335  }
336  if (direction != PortDirection::INPUT)
337  {
338  config.output_ports[port_name] = "=";
339  }
340  }
341 }
342 
343 } // namespace BT
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
BT
Definition: ex01_wrap_legacy.cpp:29
BT::Optional
nonstd::expected< T, std::string > Optional
Definition: basic_types.h:197
BT::Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >::Subscriber
std::shared_ptr< CallableFunction > Subscriber
Definition: signal.h:19
BT::TreeNode::setPreTickOverrideFunction
void setPreTickOverrideFunction(PreTickOverrideCallback callback)
BT::TreeNode::setPostTickOverrideFunction
void setPostTickOverrideFunction(PostTickOverrideCallback callback)
BT::TreeNode::state_change_signal_
StatusChangeSignal state_change_signal_
Definition: tree_node.h:215
BT::TreeNode::pre_condition_callback_
PreTickOverrideCallback pre_condition_callback_
Definition: tree_node.h:223
BT::TreeNodeManifest::ports
PortsList ports
Definition: tree_node.h:38
exceptions.h
BT::TreeNode::UID
uint16_t UID() const
Definition: tree_node.cpp:117
BT::NodeType
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
BT::Any
Definition: safe_any.hpp:22
minitrace::mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
BT::TreeNode::StatusChangeSubscriber
StatusChangeSignal::Subscriber StatusChangeSubscriber
Definition: tree_node.h:95
basic_types.h
BT::TreeNodeManifest::description
std::string description
Definition: tree_node.h:39
BT::TreeNode::TreeNode
TreeNode(std::string name, NodeConfiguration config)
TreeNode main constructor.
Definition: tree_node.cpp:25
BT::TreeNode::emitStateChanged
void emitStateChanged()
Definition: tree_node.cpp:193
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:55
BT::PortsRemapping
std::unordered_map< std::string, std::string > PortsRemapping
Definition: tree_node.h:42
strcat.hpp
BT::Tree
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:125
BT::NodeConfiguration::NodeConfiguration
NodeConfiguration()
Definition: tree_node.h:46
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
BT::TreeNode::name_
const std::string name_
Definition: tree_node.h:207
BT::TreeNode::Ptr
std::shared_ptr< TreeNode > Ptr
Definition: tree_node.h:58
BT::TreeNodeManifest::type
NodeType type
Definition: tree_node.h:36
BT::DecoratorNode
Definition: decorator_node.h:8
BT::NodeConfiguration::input_ports
PortsRemapping input_ports
Definition: tree_node.h:50
BT::Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >::CallableFunction
std::function< void(CallableArgs...)> CallableFunction
Definition: signal.h:18
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::TreeNode::post_condition_callback_
PostTickOverrideCallback post_condition_callback_
Definition: tree_node.h:225
BT::Result
Optional< void > Result
Definition: basic_types.h:217
BT::TreeNode::isBlackboardPointer
static bool isBlackboardPointer(StringView str)
Definition: tree_node.cpp:145
BT::NodeConfiguration
Definition: tree_node.h:44
BT::PortDirection::OUTPUT
@ OUTPUT
BT::TreeNode::status_
NodeStatus status_
Definition: tree_node.h:209
BT::NodeConfiguration::output_ports
PortsRemapping output_ports
Definition: tree_node.h:51
BT::TreeNode::registrationName
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:122
BT::TreeNode::~TreeNode
virtual ~TreeNode()=default
BT::TreeNode::wake_up_
std::shared_ptr< WakeUpSignal > wake_up_
Definition: tree_node.h:227
BT::TreeNode::state_mutex_
std::mutex state_mutex_
Definition: tree_node.h:213
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:84
BT::TreeNode::getInput
Optional< T > getInput(const std::string &key) const
Definition: tree_node.h:159
BT::TreeNode::waitValidStatus
BT::NodeStatus waitValidStatus()
Definition: tree_node.cpp:90
BT::TreeNodeManifest::registration_ID
std::string registration_ID
Definition: tree_node.h:37
BT::TreeNode::setStatus
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63
BT::Any::empty
bool empty() const noexcept
Definition: safe_any.hpp:153
BT::TreeNode::getRawPortValue
StringView getRawPortValue(const std::string &key) const
Definition: tree_node.cpp:132
BT::TreeNode::halt
virtual void halt()=0
BT::TreeNode::state_condition_variable_
std::condition_variable state_condition_variable_
Definition: tree_node.h:211
BT::TreeNode::getRemappedKey
static Optional< StringView > getRemappedKey(StringView port_name, StringView remapping_value)
Definition: tree_node.cpp:179
BT::TreeNodeManifest
This information is used mostly by the XMLParser.
Definition: tree_node.h:34
BT::TreeNode::type
virtual NodeType type() const =0
signal.h
BT::PortDirection::INPUT
@ INPUT
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
BT::Any::cast
T cast() const
Definition: safe_any.hpp:120
BT::StrCat
std::string StrCat()
Definition: strcat.hpp:47
BT::TreeNode::setOutput
Result setOutput(const std::string &key, const T &value)
Definition: tree_node.h:293
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
BT::TreeNode::tick
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
BT::TreeNode::modifyPortsRemapping
void modifyPortsRemapping(const PortsRemapping &new_remapping)
Definition: tree_node.cpp:211
BT::NodeConfiguration::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:49
BT::TreeNode::setWakeUpInstance
void setWakeUpInstance(std::shared_ptr< WakeUpSignal > instance)
Definition: tree_node.cpp:206
BT::TreeNode::uid_
const uint16_t uid_
Definition: tree_node.h:217
BT::TreeNode::subscribeToStatusChange
StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback)
subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber ...
Definition: tree_node.cpp:112
BT::assignDefaultRemapping
void assignDefaultRemapping(NodeConfiguration &config)
Definition: tree_node.h:326
BT::TreeNode::resetStatus
void resetStatus()
Equivalent to setStatus(NodeStatus::IDLE)
Definition: tree_node.cpp:79
wakeup_signal.hpp
BT::TreeNode::isHalted
bool isHalted() const
Definition: tree_node.cpp:106
BT::Any::type
const std::type_info & type() const noexcept
Definition: safe_any.hpp:143
BT::Blackboard::Ptr
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:25
BT::TreeNode::PostTickOverrideCallback
std::function< Optional< NodeStatus >(TreeNode &, NodeStatus, NodeStatus)> PostTickOverrideCallback
Definition: tree_node.h:101
BT::TreeNode::config_
NodeConfiguration config_
Definition: tree_node.h:219
BT::Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >
blackboard.h
BT::TreeNode::PreTickOverrideCallback
std::function< Optional< NodeStatus >(TreeNode &, NodeStatus)> PreTickOverrideCallback
Definition: tree_node.h:99
BT::StringView
nonstd::string_view StringView
Definition: basic_types.h:55
BT::ControlNode
Definition: control_node.h:21
BT::TreeNode::StatusChangeCallback
StatusChangeSignal::CallableFunction StatusChangeCallback
Definition: tree_node.h:96
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
BT::TreeNode::registration_ID_
std::string registration_ID_
Definition: tree_node.h:221
BT::TreeNode::stripBlackboardPointer
static StringView stripBlackboardPointer(StringView str)
Definition: tree_node.cpp:162


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19