Skip to content

Add onlyIf precondition #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions convert_v3_to_v4.py
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ def strtobool(val: typing.Union[str, int, bool]) -> bool:
SCRIPT_DIRECTIVES = [
"_successIf",
"_failureIf",
"_onlyIf",
"_skipIf",
"_while",
"_onSuccess",
1 change: 1 addition & 0 deletions include/behaviortree_cpp/tree_node.h
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ enum class PreCond
// order of the enums also tell us the execution order
FAILURE_IF = 0,
SUCCESS_IF,
ONLY_IF,
SKIP_IF,
WHILE_TRUE,
COUNT_
13 changes: 12 additions & 1 deletion src/tree_node.cpp
Original file line number Diff line number Diff line change
@@ -194,6 +194,7 @@ Expected<NodeStatus> TreeNode::checkPreConditions()
}

const PreCond preID = PreCond(index);
bool onlyIfIsPresent = false;

// Some preconditions are applied only when the node state is IDLE or SKIPPED
if(_p->status == NodeStatus::IDLE || _p->status == NodeStatus::SKIPPED)
@@ -209,7 +210,11 @@ Expected<NodeStatus> TreeNode::checkPreConditions()
{
return NodeStatus::SUCCESS;
}
else if(preID == PreCond::SKIP_IF)
else if(preID == PreCond::ONLY_IF)
{
onlyIfIsPresent = true;
}
else if(preID == PreCond::SKIP_IF && onlyIfIsPresent)
{
return NodeStatus::SKIPPED;
}
@@ -219,6 +224,10 @@ Expected<NodeStatus> TreeNode::checkPreConditions()
{
return NodeStatus::SKIPPED;
}
else if(preID == PreCond::ONLY_IF)
{
return NodeStatus::SKIPPED;
}
}
else if(_p->status == NodeStatus::RUNNING && preID == PreCond::WHILE_TRUE)
{
@@ -456,6 +465,8 @@ std::string toStr<PreCond>(const PreCond& pre)
return "_successIf";
case PreCond::FAILURE_IF:
return "_failureIf";
case PreCond::ONLY_IF:
return "_onlyIf";
case PreCond::SKIP_IF:
return "_skipIf";
case PreCond::WHILE_TRUE:
1 change: 1 addition & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
@@ -1296,6 +1296,7 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory)
</xs:complexType>
<xs:attributeGroup name="preconditionAttributeGroup">
<xs:attribute name="_failureIf" type="xs:string" use="optional"/>
<xs:attribute name="_onlyIf" type="xs:string" use="optional"/>
<xs:attribute name="_skipIf" type="xs:string" use="optional"/>
<xs:attribute name="_successIf" type="xs:string" use="optional"/>
<xs:attribute name="_while" type="xs:string" use="optional"/>
27 changes: 27 additions & 0 deletions tests/gtest_skipping.cpp
Original file line number Diff line number Diff line change
@@ -32,6 +32,33 @@ TEST(SkippingLogic, Sequence)
ASSERT_EQ(counters[1], 1);
}

TEST(SkippingLogic, Order)
{
BehaviorTreeFactory factory;
std::array<int, 3> counters;
RegisterTestTick(factory, "Test", counters);

const std::string xml_text = R"(

<root BTCPP_format="4" >
<BehaviorTree ID="MainTree">
<Sequence>
<Script code = "A:=1"/>
<TestA _onlyIf="A==1" _skipIf="A==1"/>
<TestB _onlyIf="A==0" _skipIf="A==0"/>
<TestC _successIf="A==1" _onlyIf="A==1"/>
</Sequence>
</BehaviorTree>
</root>)";

auto tree = factory.createTreeFromText(xml_text);
const auto status = tree.tickWhileRunning();
ASSERT_EQ(status, NodeStatus::SUCCESS);
ASSERT_EQ(counters[0], 1);
ASSERT_EQ(counters[1], 0);
ASSERT_EQ(counters[2], 0);
}

TEST(SkippingLogic, SkipAll)
{
BehaviorTreeFactory factory;