Skip to content

Commit 720cf63

Browse files
committed
improve blackboard exporting
1 parent 3cc7b4e commit 720cf63

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/blackboard.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "behaviortree_cpp/blackboard.h"
2+
#include <unordered_set>
23
#include "behaviortree_cpp/json_export.h"
34

45
namespace BT
@@ -166,15 +167,44 @@ void Blackboard::createEntry(const std::string& key, const TypeInfo& info)
166167

167168
void Blackboard::cloneInto(Blackboard& dst) const
168169
{
169-
std::unique_lock lk(dst.mutex_);
170-
dst.storage_.clear();
170+
std::unique_lock lk1(mutex_);
171+
std::unique_lock lk2(dst.mutex_);
171172

172-
for(const auto& [key, entry] : storage_)
173+
// keys that are not updated must be removed.
174+
std::unordered_set<std::string> keys_to_remove;
175+
auto& dst_storage = dst.storage_;
176+
for(const auto& [key, _] : dst_storage)
177+
{
178+
keys_to_remove.insert(key);
179+
}
180+
181+
// update or create entries in dst_storage
182+
for(const auto& [src_key, src_entry] : storage_)
183+
{
184+
keys_to_remove.erase(src_key);
185+
186+
auto it = dst_storage.find(src_key);
187+
if(it != dst_storage.end())
188+
{
189+
// overwite
190+
auto& dst_entry = it->second;
191+
dst_entry->string_converter = src_entry->string_converter;
192+
dst_entry->value = src_entry->value;
193+
dst_entry->info = src_entry->info;
194+
}
195+
else
196+
{
197+
// create new
198+
auto new_entry = std::make_shared<Entry>(src_entry->info);
199+
new_entry->value = src_entry->value;
200+
new_entry->string_converter = src_entry->string_converter;
201+
dst_storage.insert({ src_key, new_entry });
202+
}
203+
}
204+
205+
for(const auto& key : keys_to_remove)
173206
{
174-
auto new_entry = std::make_shared<Entry>(entry->info);
175-
new_entry->value = entry->value;
176-
new_entry->string_converter = entry->string_converter;
177-
dst.storage_.insert({ key, new_entry });
207+
dst_storage.erase(key);
178208
}
179209
}
180210

src/bt_factory.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,25 +697,32 @@ std::vector<Blackboard::Ptr> BlackboardBackup(const Tree& tree)
697697

698698
nlohmann::json ExportTreeToJSON(const Tree& tree)
699699
{
700-
std::vector<nlohmann::json> bbs;
700+
nlohmann::json out;
701701
for(const auto& subtree : tree.subtrees)
702702
{
703-
bbs.push_back(ExportBlackboardToJSON(*subtree->blackboard));
703+
nlohmann::json json_sub;
704+
auto sub_name = subtree->instance_name;
705+
if(sub_name.empty())
706+
{
707+
sub_name = subtree->tree_ID;
708+
}
709+
out[sub_name] = ExportBlackboardToJSON(*subtree->blackboard);
704710
}
705-
return bbs;
711+
return out;
706712
}
707713

708714
void ImportTreeFromJSON(const nlohmann::json& json, Tree& tree)
709715
{
710716
if(json.size() != tree.subtrees.size())
711717
{
712-
std::cerr << "Number of blackboards don't match:" << json.size() << "/"
713-
<< tree.subtrees.size() << "\n";
714718
throw std::runtime_error("Number of blackboards don't match:");
715719
}
716-
for(size_t i = 0; i < tree.subtrees.size(); i++)
720+
721+
size_t index = 0;
722+
for(auto& [key, array] : json.items())
717723
{
718-
ImportBlackboardFromJSON(json.at(i), *tree.subtrees.at(i)->blackboard);
724+
auto& subtree = tree.subtrees.at(index++);
725+
ImportBlackboardFromJSON(array, *subtree->blackboard);
719726
}
720727
}
721728

0 commit comments

Comments
 (0)