Skip to content

Commit

Permalink
Add blackboard scope/hierarchy (issue #44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Faconti committed Jan 28, 2019
1 parent 697910e commit 9ca1072
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
40 changes: 36 additions & 4 deletions gtest/gtest_blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class BB_TestNode: public SyncActionNode
};




/****************TESTS START HERE***************************/

TEST(BlackboardTest, GetInputsFromBlackboard)
{
auto bb = Blackboard::create<BlackboardLocal>();
Expand Down Expand Up @@ -154,6 +150,42 @@ TEST(BlackboardTest, WithFactory)
ASSERT_EQ( bb->get<int>("my_output_port_A"), 22 );
ASSERT_EQ( bb->get<int>("my_output_port_B"), 84 );
ASSERT_EQ( bb->get<int>("my_input_port"), 84 );
}

TEST(BlackboardTest, NestedBlackboards)
{

auto bb_A = Blackboard::create<BlackboardLocal>();
auto bb_B = Blackboard::create<BlackboardLocal>();
auto bb_C = Blackboard::create<BlackboardLocal>();

bb_B->setParentBlackboard( bb_A );
bb_C->setParentBlackboard( bb_B );

bb_A->set("value", 11);
bb_B->set("value", 22);
bb_C->set("value", 33);

bb_A->set("number", 44);
bb_B->set("number", 55);

bb_A->set("answer", 66);

ASSERT_EQ( bb_A->get<int>("value"), 11 );
ASSERT_EQ( bb_B->get<int>("value"), 22 );
ASSERT_EQ( bb_C->get<int>("value"), 33 );

ASSERT_EQ( bb_A->get<int>("number"), 44 );
ASSERT_EQ( bb_B->get<int>("number"), 55 );
ASSERT_EQ( bb_C->get<int>("number"), 55 );

ASSERT_EQ( bb_A->get<int>("answer"), 66 );
ASSERT_EQ( bb_B->get<int>("answer"), 66 );
ASSERT_EQ( bb_C->get<int>("answer"), 66 );


ASSERT_ANY_THROW( bb_A->get<int>("not_there") );
ASSERT_ANY_THROW( bb_B->get<int>("not_there") );
ASSERT_ANY_THROW( bb_C->get<int>("not_there") );
}

34 changes: 23 additions & 11 deletions include/behaviortree_cpp/blackboard/blackboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ class Blackboard
template <typename T>
bool get(const std::string& key, T& value) const
{
const SafeAny::Any* val = nullptr;
const SafeAny::Any* val = getAny(key);
if (val)
{
std::unique_lock<std::mutex> lock(mutex_);
val = impl_->get(key);
value = val->cast<T>();
}
if (!val)
{
return false;
}
value = val->cast<T>();
return true;
return (bool)val;
}

/**
Expand All @@ -95,7 +90,17 @@ class Blackboard
const SafeAny::Any* getAny(const std::string& key) const
{
std::unique_lock<std::mutex> lock(mutex_);
return impl_->get(key);
auto val = impl_->get(key);

if (!val) // not found. try the parent
{
if( auto parent_bb = parent_blackboard_.lock() )
{
// this should work recursively
val = parent_bb->getAny(key);
}
}
return val;
}

/**
Expand All @@ -113,6 +118,11 @@ class Blackboard
return value;
}

void setParentBlackboard(const Blackboard::Ptr& parent_bb )
{
parent_blackboard_ = parent_bb;
}

/// Update the entry with the given key
template <typename T>
void set(const std::string& key, const T& value)
Expand All @@ -131,7 +141,9 @@ class Blackboard
private:
std::unique_ptr<BlackboardImpl> impl_;
mutable std::mutex mutex_;
mutable std::weak_ptr<Blackboard> parent_blackboard_;
};
}

} // end namespace

#endif // BLACKBOARD_H

0 comments on commit 9ca1072

Please sign in to comment.