Skip to content

Commit b3c3c25

Browse files
committed
add private ports to exclude from autoremapping #706
1 parent 65504b0 commit b3c3c25

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

include/behaviortree_cpp/decorators/subtree_node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace BT
1111
* If you want to have data flow through ports, you need to explicitly
1212
* remap the ports.
1313
*
14+
* NOTE: _autoremap will exclude all the ports which name start with underscore '_'
15+
*
1416
* Consider this example:
1517
1618
<root main_tree_to_execute = "MainTree" >

src/blackboard.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace BT
44
{
55

6+
bool IsPrivateKey(StringView str)
7+
{
8+
return str.size() >= 1 && str.data()[0] == '_';
9+
}
10+
611
void Blackboard::enableAutoRemapping(bool remapping)
712
{
813
autoremapping_ = remapping;
@@ -52,7 +57,7 @@ const std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string
5257
auto const& new_key = remap_it->second;
5358
return parent->getEntry(new_key);
5459
}
55-
if(autoremapping_)
60+
if(autoremapping_ && !IsPrivateKey(key))
5661
{
5762
return parent->getEntry(key);
5863
}
@@ -83,7 +88,7 @@ std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string &key)
8388
}
8489
return entry;
8590
}
86-
if(autoremapping_)
91+
if(autoremapping_ && !IsPrivateKey(key))
8792
{
8893
auto entry = parent->getEntry(key);
8994
if(entry)
@@ -202,7 +207,7 @@ Blackboard::createEntryImpl(const std::string& key, const TypeInfo& info)
202207
entry = parent->createEntryImpl(remapped_key, info);
203208
}
204209
}
205-
else if(autoremapping_)
210+
else if(autoremapping_ && !IsPrivateKey(key))
206211
{
207212
if (auto parent = parent_bb_.lock())
208213
{

tests/gtest_subtree.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,41 @@ TEST(SubTree, RemappingIssue696)
615615
ASSERT_EQ(console[3], "bar");
616616
}
617617

618+
TEST(SubTree, PrivateAutoRemapping)
619+
{
620+
// clang-format off
621+
622+
static const char* xml_text = R"(
623+
<root BTCPP_format="4">
624+
<BehaviorTree ID="Subtree">\n"
625+
<Sequence>
626+
<SetBlackboard output_key="public_value" value="hello"/>
627+
<SetBlackboard output_key="_private_value" value="world"/>
628+
</Sequence>
629+
</BehaviorTree>
630+
631+
<BehaviorTree ID="MainTree">
632+
<Sequence>
633+
<SubTree ID="Subtree" _autoremap="true"/>
634+
<PrintToConsole message="{public_value}"/>
635+
<PrintToConsole message="{_private_value}"/>
636+
</Sequence>
637+
</BehaviorTree>
638+
</root>
639+
)";
640+
641+
// clang-format on
642+
BehaviorTreeFactory factory;
643+
std::vector<std::string> console;
644+
factory.registerNodeType<PrintToConsole>("PrintToConsole", &console);
645+
646+
factory.registerBehaviorTreeFromText(xml_text);
647+
auto tree = factory.createTree("MainTree");
648+
const auto res = tree.tickWhileRunning();
649+
650+
// should fail because _private_value is not autoremapped
651+
ASSERT_EQ(res, BT::NodeStatus::FAILURE);
652+
ASSERT_EQ(console.size(), 1);
653+
ASSERT_EQ(console[0], "hello");
654+
}
655+

0 commit comments

Comments
 (0)