| 37 | // clang-format on |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | BT::BehaviorTreeFactory factory; |
| 42 | |
| 43 | factory.registerBehaviorTreeFromText(xml_text); |
| 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 | } |
nothing calls this directly
no test coverage detected