bt_observer.cpp
Go to the documentation of this file.
4 
5 namespace BT
6 {
7 TreeObserver::TreeObserver(const BT::Tree& tree) : StatusChangeLogger(tree.rootNode())
8 {
9  std::function<void(const TreeNode&)> recursiveStep;
10 
11  recursiveStep = [&](const TreeNode& node) {
12  if(auto control = dynamic_cast<const ControlNode*>(&node))
13  {
14  for(const auto& child : control->children())
15  {
16  recursiveStep(*child);
17  }
18  }
19  else if(auto decorator = dynamic_cast<const DecoratorNode*>(&node))
20  {
21  if(decorator->type() != NodeType::SUBTREE)
22  {
23  recursiveStep(*decorator->child());
24  }
25  }
26 
27  if(_path_to_uid.count(node.fullPath()) != 0)
28  {
29  throw LogicError("TreeObserver not built correctly. Report issue");
30  }
31  _path_to_uid[node.fullPath()] = node.UID();
32  };
33 
34  for(const auto& subtree : tree.subtrees)
35  {
36  recursiveStep(*subtree->nodes.front());
37  }
38 
39  for(const auto& [path, uid] : _path_to_uid)
40  {
41  _statistics[uid] = {};
42  _uid_to_path[uid] = path;
43  }
44 }
45 
47 {}
48 
49 void TreeObserver::callback(Duration timestamp, const TreeNode& node,
50  NodeStatus /*prev_status*/, NodeStatus status)
51 {
52  auto& statistics = _statistics[node.UID()];
53  statistics.current_status = status;
54  statistics.last_timestamp = timestamp;
55 
56  if(status == NodeStatus::IDLE)
57  {
58  return;
59  }
60 
61  statistics.transitions_count++;
62 
63  if(status == NodeStatus::SUCCESS)
64  {
65  statistics.last_result = status;
66  statistics.success_count++;
67  }
68  else if(status == NodeStatus::FAILURE)
69  {
70  statistics.last_result = status;
71  statistics.failure_count++;
72  }
73  else if(status == NodeStatus::SKIPPED)
74  {
75  statistics.skip_count++;
76  }
77 }
78 
80 TreeObserver::getStatistics(const std::string& path) const
81 {
82  auto it = _path_to_uid.find(path);
83  if(it == _path_to_uid.end())
84  {
85  throw RuntimeError("TreeObserver::getStatistics: Invalid pattern");
86  }
87  return getStatistics(it->second);
88 }
89 
91 {
92  auto it = _statistics.find(uid);
93  if(it == _statistics.end())
94  {
95  throw RuntimeError("TreeObserver::getStatistics: Invalid UID");
96  }
97  return it->second;
98 }
99 
100 const std::unordered_map<uint16_t, TreeObserver::NodeStatistics>&
102 {
103  return _statistics;
104 }
105 
106 const std::unordered_map<std::string, uint16_t>& TreeObserver::pathToUID() const
107 {
108  return _path_to_uid;
109 }
110 
111 const std::map<uint16_t, std::string>& TreeObserver::uidToPath() const
112 {
113  return _uid_to_path;
114 }
115 
116 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::TreeNode::UID
uint16_t UID() const
Definition: tree_node.cpp:336
BT::TreeObserver::getStatistics
const NodeStatistics & getStatistics(const std::string &path) const
Definition: bt_observer.cpp:80
BT::TreeObserver::callback
virtual void callback(Duration timestamp, const TreeNode &node, NodeStatus prev_status, NodeStatus status) override
Definition: bt_observer.cpp:49
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
bt_observer.h
BT::Tree
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:91
BT::NodeType::SUBTREE
@ SUBTREE
BT::DecoratorNode
Definition: decorator_node.h:8
BT::LogicError
Definition: exceptions.h:45
BT::StatusChangeLogger
Definition: abstract_logger.h:15
BT::Duration
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:628
BT::TreeObserver::NodeStatistics
Definition: bt_observer.h:28
detail::void
j template void())
Definition: json.hpp:4893
BT::NodeStatus::FAILURE
@ FAILURE
BT::NodeStatus::SKIPPED
@ SKIPPED
lexy_ext::child
auto child(const lexy::parse_tree< Reader, TokenKind, MemoryResource > &tree, typename lexy::parse_tree< Reader, TokenKind, MemoryResource >::node node, Predicate predicate) -> std::optional< typename lexy::parse_tree< Reader, TokenKind, MemoryResource >::node >
Returns the first child that matches predicate, if there is any.
Definition: parse_tree_algorithm.hpp:244
BT::TreeObserver::_path_to_uid
std::unordered_map< std::string, uint16_t > _path_to_uid
Definition: bt_observer.h:63
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeObserver::_statistics
std::unordered_map< uint16_t, NodeStatistics > _statistics
Definition: bt_observer.h:62
BT::TreeObserver::pathToUID
const std::unordered_map< std::string, uint16_t > & pathToUID() const
Definition: bt_observer.cpp:106
control_node.h
BT::TreeObserver::TreeObserver
TreeObserver(const BT::Tree &tree)
Definition: bt_observer.cpp:7
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::Tree::subtrees
std::vector< Subtree::Ptr > subtrees
Definition: bt_factory.h:104
lexyd::ascii::control
constexpr auto control
Definition: ascii.hpp:42
BT::NodeStatus::IDLE
@ IDLE
BT::TreeObserver::uidToPath
const std::map< uint16_t, std::string > & uidToPath() const
Definition: bt_observer.cpp:111
BT::TreeObserver::~TreeObserver
~TreeObserver() override
Definition: bt_observer.cpp:46
BT::TreeObserver::statistics
const std::unordered_map< uint16_t, NodeStatistics > & statistics() const
Definition: bt_observer.cpp:101
decorator_node.h
BT::ControlNode
Definition: control_node.h:21
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::TreeObserver::_uid_to_path
std::map< uint16_t, std::string > _uid_to_path
Definition: bt_observer.h:64


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16