t10_observer.cpp
Go to the documentation of this file.
3 
7 // clang-format off
8 
9 static const char* xml_text = R"(
10 <root BTCPP_format="4">
11 
12  <BehaviorTree ID="MainTree">
13  <Sequence>
14  <Fallback>
15  <AlwaysFailure name="failing_action"/>
16  <SubTree ID="SubTreeA" name="mysub"/>
17  </Fallback>
18  <AlwaysSuccess name="last_action"/>
19  </Sequence>
20  </BehaviorTree>
21 
22  <BehaviorTree ID="SubTreeA">
23  <Sequence>
24  <AlwaysSuccess name="action_subA"/>
25  <SubTree ID="SubTreeB" name="sub_nested"/>
26  <SubTree ID="SubTreeB" />
27  </Sequence>
28  </BehaviorTree>
29 
30  <BehaviorTree ID="SubTreeB">
31  <AlwaysSuccess name="action_subB"/>
32  </BehaviorTree>
33 
34 </root>
35  )";
36 
37 // clang-format on
38 
39 int main()
40 {
42 
44  auto tree = factory.createTree("MainTree");
45 
46  // Helper function to print the tree.
47  BT::printTreeRecursively(tree.rootNode());
48 
49  // The purpose of the observer is to save some statistics about the number of times
50  // a certain node returns SUCCESS or FAILURE.
51  // This is particularly useful to create unit tests and to check if
52  // a certain set of transitions happened as expected
53  BT::TreeObserver observer(tree);
54 
55  std::map<int, std::string> UID_to_path;
56 
57  // Print the unique ID and the corresponding human readable path
58  // Path is also expected to be unique.
59  tree.applyVisitor([&UID_to_path](BT::TreeNode* node) {
60  UID_to_path[node->UID()] = node->fullPath();
61  std::cout << node->UID() << " -> " << node->fullPath() << std::endl;
62  });
63 
64  tree.tickWhileRunning();
65 
66  // You can access a specific statistic, using is full path or the UID
67  const auto& last_action_stats = observer.getStatistics("last_action");
68  assert(last_action_stats.transitions_count > 0);
69 
70  std::cout << "----------------" << std::endl;
71  // print all the statistics
72  for(const auto& [uid, name] : UID_to_path)
73  {
74  const auto& stats = observer.getStatistics(uid);
75 
76  std::cout << "[" << name << "] \tT/S/F: " << stats.transitions_count << "/"
77  << stats.success_count << "/" << stats.failure_count << std::endl;
78  }
79 
80  return 0;
81 }
BT::TreeNode::UID
uint16_t UID() const
Definition: tree_node.cpp:336
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
BT::TreeObserver::getStatistics
const NodeStatistics & getStatistics(const std::string &path) const
Definition: bt_observer.cpp:80
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
bt_observer.h
BT::TreeObserver::NodeStatistics::transitions_count
unsigned transitions_count
Definition: bt_observer.h:36
bt_factory.h
main
int main()
Definition: t10_observer.cpp:39
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::TreeObserver
The TreeObserver is used to collect statistics about which nodes are executed and their returned stat...
Definition: bt_observer.h:17
BT::printTreeRecursively
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
Definition: behavior_tree.cpp:66
xml_text
static const char * xml_text
Definition: t10_observer.cpp:9
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277
BT::TreeNode::fullPath
const std::string & fullPath() const
Definition: tree_node.cpp:341


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