Skip to content

Commit 9fcf508

Browse files
fix @ root remapping in Blackboard::createEntryImpl (#1192)
1 parent c88a9f4 commit 9fcf508

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/blackboard.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ Blackboard::Ptr Blackboard::parent()
282282
std::shared_ptr<Blackboard::Entry> Blackboard::createEntryImpl(const std::string& key,
283283
const TypeInfo& info)
284284
{
285+
// special syntax: "@" always refers to the root blackboard, the same
286+
// redirection getEntry() applies. Without it, a key remapped to a root
287+
// entry (port="{@foo}") is created here as a literal "@foo" entry that
288+
// getEntry() then strips to "foo" and can never find again.
289+
if(StartWith(key, '@'))
290+
{
291+
return rootBlackboard()->createEntryImpl(key.substr(1, key.size() - 1), info);
292+
}
293+
285294
const std::unique_lock storage_lock(storage_mutex_);
286295
// This function might be called recursively, when we do remapping, because we move
287296
// to the top scope to find already existing entries

‎tests/gtest_blackboard.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,29 @@ TEST(BlackboardTest, RootBlackboard)
636636
ASSERT_EQ(4, tree.rootBlackboard()->get<int>("var5"));
637637
}
638638

639+
TEST(BlackboardTest, RemapToRootBlackboard)
640+
{
641+
// A subtree port remapped to a root key (XML: param="{@shared}") must resolve
642+
// to the "shared" entry in the root blackboard. createEntryImpl used to create
643+
// it as a literal "@shared" entry instead, so getEntry("param") -- which strips
644+
// the '@' and looks up "shared" in the root -- never found it again. That made
645+
// ImportBlackboardFromJSON dereference a null entry, and set()/get() disagree.
646+
auto root = Blackboard::create();
647+
auto child = Blackboard::create(root);
648+
child->addSubtreeRemapping("param", "@shared");
649+
650+
nlohmann::json js;
651+
js["param"] = 42;
652+
ASSERT_NO_THROW(ImportBlackboardFromJSON(js, *child));
653+
654+
ASSERT_EQ(42, child->get<int>("param"));
655+
ASSERT_EQ(42, root->get<int>("shared"));
656+
657+
child->set("param", 7);
658+
ASSERT_EQ(7, child->get<int>("param"));
659+
ASSERT_EQ(7, root->get<int>("shared"));
660+
}
661+
639662
TEST(BlackboardTest, TimestampedInterface)
640663
{
641664
auto bb = BT::Blackboard::create();

0 commit comments

Comments
 (0)