| 65 | //-------------------------- |
| 66 | |
| 67 | TEST(ReactiveBackchaining, EnsureWarm) |
| 68 | { |
| 69 | // This test shows the basic structure of a PPA: a fallback |
| 70 | // of a postcondition and an action to make that |
| 71 | // postcondition true. |
| 72 | static const char* xml_text = R"( |
| 73 | <root BTCPP_format="4"> |
| 74 | <BehaviorTree ID="EnsureWarm"> |
| 75 | <ReactiveFallback> |
| 76 | <IsWarm name="warm"/> |
| 77 | <ReactiveSequence> |
| 78 | <IsHoldingJacket name="jacket" /> |
| 79 | <WearJacket name="wear" /> |
| 80 | </ReactiveSequence> |
| 81 | </ReactiveFallback> |
| 82 | </BehaviorTree> |
| 83 | </root> |
| 84 | )"; |
| 85 | |
| 86 | // The final condition of the PPA; the thing that make_warm achieves. |
| 87 | // For this example, we're only warm after WearJacket returns success. |
| 88 | BehaviorTreeFactory factory; |
| 89 | factory.registerNodeType<SimpleCondition>("IsWarm", "is_warm"); |
| 90 | factory.registerNodeType<SimpleCondition>("IsHoldingJacket", "holding_jacket"); |
| 91 | factory.registerNodeType<AsyncTestAction>("WearJacket", "is_warm"); |
| 92 | |
| 93 | Tree tree = factory.createTreeFromText(xml_text); |
| 94 | BT::TreeObserver observer(tree); |
| 95 | |
| 96 | auto& blackboard = tree.subtrees.front()->blackboard; |
| 97 | blackboard->set("is_warm", false); |
| 98 | blackboard->set("holding_jacket", true); |
| 99 | |
| 100 | // first tick: not warm, have a jacket: start wearing it |
| 101 | EXPECT_EQ(tree.tickExactlyOnce(), NodeStatus::RUNNING); |
| 102 | EXPECT_FALSE(blackboard->get<bool>("is_warm")); |
| 103 | |
| 104 | // second tick: not warm (still wearing) |
| 105 | EXPECT_EQ(tree.tickExactlyOnce(), NodeStatus::RUNNING); |
| 106 | EXPECT_FALSE(blackboard->get<bool>("is_warm")); |
| 107 | |
| 108 | // third tick: warm (wearing succeeded) |
| 109 | EXPECT_EQ(tree.tickExactlyOnce(), NodeStatus::SUCCESS); |
| 110 | EXPECT_TRUE(blackboard->get<bool>("is_warm")); |
| 111 | |
| 112 | // fourth tick: still warm (just the condition ticked) |
| 113 | EXPECT_EQ(tree.tickExactlyOnce(), NodeStatus::SUCCESS); |
| 114 | |
| 115 | EXPECT_EQ(observer.getStatistics("warm").failure_count, 3); |
| 116 | EXPECT_EQ(observer.getStatistics("warm").success_count, 1); |
| 117 | |
| 118 | EXPECT_EQ(observer.getStatistics("jacket").transitions_count, 3); |
| 119 | EXPECT_EQ(observer.getStatistics("jacket").success_count, 3); |
| 120 | |
| 121 | EXPECT_EQ(observer.getStatistics("wear").success_count, 1); |
| 122 | } |
| 123 | |
| 124 | TEST(ReactiveBackchaining, EnsureWarmWithEnsureHoldingHacket) |
nothing calls this directly
no test coverage detected