Skip to content

Commit 085297e

Browse files
committed
fix(precondition): 재활성화 시 조건 평가 상태 초기화
1 parent a9d6e56 commit 085297e

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎include/behaviortree_cpp/decorators/script_precondition.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class PreconditionNode : public DecoratorNode
6060
}
6161

6262
private:
63+
void halt() override
64+
{
65+
_children_running = false;
66+
DecoratorNode::halt();
67+
}
68+
6369
virtual BT::NodeStatus tick() override
6470
{
6571
loadExecutor();
@@ -72,19 +78,18 @@ class PreconditionNode : public DecoratorNode
7278

7379
// Only check the 'if' script if we haven't started ticking the children yet.
7480
Ast::Environment env = { config().blackboard, config().enums };
75-
bool tick_children =
76-
_children_running || (_children_running = _executor(env).cast<bool>());
81+
bool tick_children = _children_running || _executor(env).cast<bool>();
7782

7883
if(!tick_children)
7984
{
8085
return else_return;
8186
}
8287

8388
auto const child_status = child_node_->executeTick();
89+
_children_running = (child_status == NodeStatus::RUNNING);
8490
if(isStatusCompleted(child_status))
8591
{
8692
resetChild();
87-
_children_running = false;
8893
}
8994
return child_status;
9095
}

‎tests/gtest_preconditions.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,53 @@ TEST(PreconditionsDecorator, ChecksConditionOnce)
168168
EXPECT_EQ(tree.tickOnce(), NodeStatus::SUCCESS);
169169
}
170170

171+
TEST(PreconditionsDecorator, ChecksConditionAfterHalt)
172+
{
173+
BehaviorTreeFactory factory;
174+
factory.registerNodeType<KeepRunning>("KeepRunning");
175+
176+
const std::string xml_text = R"(
177+
<root BTCPP_format="4">
178+
<BehaviorTree ID="MainTree">
179+
<Precondition if="A==0" else="FAILURE">
180+
<KeepRunning/>
181+
</Precondition>
182+
</BehaviorTree>
183+
</root>)";
184+
185+
auto tree = factory.createTreeFromText(xml_text);
186+
tree.rootBlackboard()->set("A", 0);
187+
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
188+
189+
tree.haltTree();
190+
tree.rootBlackboard()->set("A", 1);
191+
EXPECT_EQ(tree.tickOnce(), NodeStatus::FAILURE);
192+
}
193+
194+
TEST(PreconditionsDecorator, ChecksConditionAfterSkippedChild)
195+
{
196+
BehaviorTreeFactory factory;
197+
factory.registerNodeType<KeepRunning>("KeepRunning");
198+
199+
const std::string xml_text = R"(
200+
<root BTCPP_format="4">
201+
<BehaviorTree ID="MainTree">
202+
<Precondition if="A==0" else="FAILURE">
203+
<KeepRunning _skipIf="skip"/>
204+
</Precondition>
205+
</BehaviorTree>
206+
</root>)";
207+
208+
auto tree = factory.createTreeFromText(xml_text);
209+
tree.rootBlackboard()->set("A", 0);
210+
tree.rootBlackboard()->set("skip", true);
211+
EXPECT_EQ(tree.tickOnce(), NodeStatus::SKIPPED);
212+
213+
tree.rootBlackboard()->set("A", 1);
214+
tree.rootBlackboard()->set("skip", false);
215+
EXPECT_EQ(tree.tickOnce(), NodeStatus::FAILURE);
216+
}
217+
171218
TEST(PreconditionsDecorator, CanRunChildrenMultipleTimes)
172219
{
173220
BehaviorTreeFactory factory;

0 commit comments

Comments
 (0)