|
| 1 | +#include "dummy_nodes.h" |
| 2 | + |
| 3 | +#include "behaviortree_cpp/bt_factory.h" |
| 4 | + |
| 5 | +// clang-format off |
| 6 | +namespace |
| 7 | +{ |
| 8 | +const char* xml_text = R"( |
| 9 | +<root BTCPP_format="4"> |
| 10 | +
|
| 11 | + <BehaviorTree ID="MainTree"> |
| 12 | + <Sequence> |
| 13 | + <SaySomething name="talk" message="hello world"/> |
| 14 | +
|
| 15 | + <SubTree ID="MySub" name="mysub"/> |
| 16 | +
|
| 17 | + <Script name="set_mock_flag" code="mock_should_fail:= false"/> |
| 18 | + <Script name="set_message" code="msg:= 'the original message' "/> |
| 19 | + <SaySomething message="{msg}"/> |
| 20 | +
|
| 21 | + <Sequence name="counting"> |
| 22 | + <SaySomething message="1"/> |
| 23 | + <SaySomething message="2"/> |
| 24 | + <SaySomething message="3"/> |
| 25 | + </Sequence> |
| 26 | + </Sequence> |
| 27 | + </BehaviorTree> |
| 28 | +
|
| 29 | + <BehaviorTree ID="MySub"> |
| 30 | + <Sequence> |
| 31 | + <AlwaysSuccess name="action_subA"/> |
| 32 | + <AlwaysSuccess name="action_subB"/> |
| 33 | + </Sequence> |
| 34 | + </BehaviorTree> |
| 35 | +
|
| 36 | +</root> |
| 37 | + )"; |
| 38 | +} // namespace |
| 39 | +// clang-format on |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Companion to tutorial 15 that preserves strict failure propagation. |
| 43 | + * |
| 44 | + * If the substituted TestNode resolves to FAILURE, the enclosing Sequence stops |
| 45 | + * immediately. The final blackboard message is still printed by this executable, |
| 46 | + * but the tree itself does not log the failure branch through a fallback. |
| 47 | + */ |
| 48 | + |
| 49 | +int main(int /*argc*/, char** /*argv*/) |
| 50 | +{ |
| 51 | + using namespace DummyNodes; |
| 52 | + BT::BehaviorTreeFactory factory; |
| 53 | + factory.registerNodeType<SaySomething>("SaySomething"); |
| 54 | + factory.registerBehaviorTreeFromText(xml_text); |
| 55 | + |
| 56 | + { |
| 57 | + auto tree = factory.createTree("MainTree"); |
| 58 | + |
| 59 | + std::cout << "----- Nodes fullPath() -------\n"; |
| 60 | + tree.applyVisitor( |
| 61 | + [](BT::TreeNode* node) { std::cout << node->fullPath() << std::endl; }); |
| 62 | + |
| 63 | + std::cout << "\n------ Output (original) ------\n"; |
| 64 | + auto status = tree.tickWhileRunning(); |
| 65 | + std::cout << "Original status: " << BT::toStr(status, false) << std::endl; |
| 66 | + } |
| 67 | + |
| 68 | + factory.registerSimpleAction("DummyAction", [](BT::TreeNode& self) { |
| 69 | + std::cout << "DummyAction substituting node with fullPath(): " << self.fullPath() |
| 70 | + << std::endl; |
| 71 | + return BT::NodeStatus::SUCCESS; |
| 72 | + }); |
| 73 | + |
| 74 | + factory.registerSimpleAction("DummySaySomething", [](BT::TreeNode& self) { |
| 75 | + auto msg = self.getInput<std::string>("message"); |
| 76 | + std::cout << "DummySaySomething: " << msg.value() << std::endl; |
| 77 | + return BT::NodeStatus::SUCCESS; |
| 78 | + }); |
| 79 | + |
| 80 | + BT::TestNodeConfig test_config; |
| 81 | + test_config.return_status_script = "(mock_should_fail == true) ? FAILURE : SUCCESS"; |
| 82 | + test_config.async_delay = std::chrono::milliseconds(2000); |
| 83 | + test_config.success_script = "msg := 'message SUBSTITUTED' "; |
| 84 | + test_config.failure_script = "msg := 'message FAILURE branch' "; |
| 85 | + |
| 86 | + BT::TestNodeConfig counting_config; |
| 87 | + counting_config.return_status = BT::NodeStatus::SUCCESS; |
| 88 | + |
| 89 | + factory.addSubstitutionRule("mysub/action_*", "DummyAction"); |
| 90 | + factory.addSubstitutionRule("talk", "DummySaySomething"); |
| 91 | + factory.addSubstitutionRule("set_message", test_config); |
| 92 | + factory.addSubstitutionRule("counting", counting_config); |
| 93 | + |
| 94 | + auto blackboard = BT::Blackboard::create(); |
| 95 | + auto tree = factory.createTree("MainTree", blackboard); |
| 96 | + std::cout << "\n------ Output (substituted, strict failure) ------\n"; |
| 97 | + auto status = tree.tickWhileRunning(); |
| 98 | + std::cout << "Substituted status: " << BT::toStr(status, false) << std::endl; |
| 99 | + if(blackboard->getEntry("msg")) |
| 100 | + { |
| 101 | + std::cout << "Substituted final msg: " << blackboard->get<std::string>("msg") |
| 102 | + << std::endl; |
| 103 | + } |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +/* Expected output: |
| 109 | +
|
| 110 | +----- Nodes fullPath() ------- |
| 111 | +Sequence::1 |
| 112 | +talk |
| 113 | +mysub |
| 114 | +mysub/Sequence::4 |
| 115 | +mysub/action_subA |
| 116 | +mysub/action_subB |
| 117 | +set_mock_flag |
| 118 | +set_message |
| 119 | +SaySomething::9 |
| 120 | +counting |
| 121 | +SaySomething::11 |
| 122 | +SaySomething::12 |
| 123 | +SaySomething::13 |
| 124 | +
|
| 125 | +------ Output (original) ------ |
| 126 | +Robot says: hello world |
| 127 | +Robot says: the original message |
| 128 | +Robot says: 1 |
| 129 | +Robot says: 2 |
| 130 | +Robot says: 3 |
| 131 | +Original status: SUCCESS |
| 132 | +
|
| 133 | +------ Output (substituted, strict failure) ------ |
| 134 | +DummySaySomething: hello world |
| 135 | +DummyAction substituting node with fullPath(): mysub/action_subA |
| 136 | +DummyAction substituting node with fullPath(): mysub/action_subB |
| 137 | +Substituted status: SUCCESS |
| 138 | +Substituted final msg: message SUBSTITUTED |
| 139 | +
|
| 140 | +If you change mock_should_fail to true in the XML above, the substituted status |
| 141 | +becomes FAILURE and the final msg becomes "message FAILURE branch". |
| 142 | +
|
| 143 | +*/ |
0 commit comments