-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathblackboard.cpp
330 lines (293 loc) · 8.1 KB
/
blackboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "behaviortree_cpp/blackboard.h"
#include <unordered_set>
#include "behaviortree_cpp/json_export.h"
namespace BT
{
bool IsPrivateKey(StringView str)
{
return str.size() >= 1 && str.data()[0] == '_';
}
void Blackboard::enableAutoRemapping(bool remapping)
{
autoremapping_ = remapping;
}
AnyPtrLocked Blackboard::getAnyLocked(const std::string& key)
{
if(auto entry = getEntry(key))
{
return AnyPtrLocked(&entry->value, &entry->entry_mutex);
}
return {};
}
AnyPtrLocked Blackboard::getAnyLocked(const std::string& key) const
{
if(auto entry = getEntry(key))
{
return AnyPtrLocked(&entry->value, const_cast<std::mutex*>(&entry->entry_mutex));
}
return {};
}
const Any* Blackboard::getAny(const std::string& key) const
{
return getAnyLocked(key).get();
}
Any* Blackboard::getAny(const std::string& key)
{
return const_cast<Any*>(getAnyLocked(key).get());
}
const std::shared_ptr<Blackboard::Entry>
Blackboard::getEntry(const std::string& key) const
{
// special syntax: "@" will always refer to the root BB
if(StartWith(key, '@'))
{
return rootBlackboard()->getEntry(key.substr(1, key.size() - 1));
}
std::unique_lock<std::mutex> lock(mutex_);
auto it = storage_.find(key);
if(it != storage_.end())
{
return it->second;
}
// not found. Try autoremapping
if(auto parent = parent_bb_.lock())
{
auto remap_it = internal_to_external_.find(key);
if(remap_it != internal_to_external_.cend())
{
auto const& new_key = remap_it->second;
return parent->getEntry(new_key);
}
if(autoremapping_ && !IsPrivateKey(key))
{
return parent->getEntry(key);
}
}
return {};
}
std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string& key)
{
return static_cast<const Blackboard&>(*this).getEntry(key);
}
const TypeInfo* Blackboard::entryInfo(const std::string& key)
{
auto entry = getEntry(key);
return (!entry) ? nullptr : &(entry->info);
}
void Blackboard::addSubtreeRemapping(StringView internal, StringView external)
{
internal_to_external_.insert(
{ static_cast<std::string>(internal), static_cast<std::string>(external) });
}
void Blackboard::debugMessage() const
{
for(const auto& [key, entry] : storage_)
{
auto port_type = entry->info.type();
if(port_type == typeid(void))
{
port_type = entry->value.type();
}
std::cout << key << " (" << BT::demangle(port_type) << ")" << std::endl;
}
for(const auto& [from, to] : internal_to_external_)
{
std::cout << "[" << from << "] remapped to port of parent tree [" << to << "]"
<< std::endl;
continue;
}
}
std::vector<StringView> Blackboard::getKeys() const
{
if(storage_.empty())
{
return {};
}
std::vector<StringView> out;
out.reserve(storage_.size());
for(const auto& entry_it : storage_)
{
out.push_back(entry_it.first);
}
return out;
}
void Blackboard::clear()
{
std::unique_lock<std::mutex> lock(mutex_);
storage_.clear();
}
std::recursive_mutex& Blackboard::entryMutex() const
{
return entry_mutex_;
}
void Blackboard::createEntry(const std::string& key, const TypeInfo& info)
{
if(StartWith(key, '@'))
{
rootBlackboard()->createEntryImpl(key.substr(1, key.size() - 1), info);
}
else
{
createEntryImpl(key, info);
}
}
void Blackboard::cloneInto(Blackboard& dst) const
{
std::unique_lock lk1(mutex_);
std::unique_lock lk2(dst.mutex_);
// keys that are not updated must be removed.
std::unordered_set<std::string> keys_to_remove;
auto& dst_storage = dst.storage_;
for(const auto& [key, _] : dst_storage)
{
keys_to_remove.insert(key);
}
// update or create entries in dst_storage
for(const auto& [src_key, src_entry] : storage_)
{
keys_to_remove.erase(src_key);
auto it = dst_storage.find(src_key);
if(it != dst_storage.end())
{
// overwite
auto& dst_entry = it->second;
dst_entry->string_converter = src_entry->string_converter;
dst_entry->value = src_entry->value;
dst_entry->info = src_entry->info;
dst_entry->sequence_id++;
dst_entry->stamp = std::chrono::steady_clock::now().time_since_epoch();
}
else
{
// create new
auto new_entry = std::make_shared<Entry>(src_entry->info);
new_entry->value = src_entry->value;
new_entry->string_converter = src_entry->string_converter;
dst_storage.insert({ src_key, new_entry });
}
}
for(const auto& key : keys_to_remove)
{
dst_storage.erase(key);
}
}
Blackboard::Ptr Blackboard::parent()
{
if(auto parent = parent_bb_.lock())
{
return parent;
}
return {};
}
std::shared_ptr<Blackboard::Entry> Blackboard::createEntryImpl(const std::string& key,
const TypeInfo& info)
{
std::unique_lock<std::mutex> lock(mutex_);
// This function might be called recursively, when we do remapping, because we move
// to the top scope to find already existing entries
// search if exists already
auto storage_it = storage_.find(key);
if(storage_it != storage_.end())
{
const auto& prev_info = storage_it->second->info;
auto prev_type_demangled = BT::demangle(prev_info.type());
// Allow mismatch if going from vector -> vector<Any>.
bool previous_is_vector = BT::isVector(prev_type_demangled);
bool new_is_vector_any = info.type() == typeid(std::vector<Any>);
if(prev_info.type() != info.type() && prev_info.isStronglyTyped() &&
info.isStronglyTyped() && !(previous_is_vector && new_is_vector_any))
{
auto msg = StrCat("Blackboard entry [", key,
"]: once declared, the type of a port"
" shall not change. Previously declared type [",
prev_type_demangled, "], current type [",
BT::demangle(info.type()), "]");
throw LogicError(msg);
}
return storage_it->second;
}
// manual remapping first
auto remapping_it = internal_to_external_.find(key);
if(remapping_it != internal_to_external_.end())
{
const auto& remapped_key = remapping_it->second;
if(auto parent = parent_bb_.lock())
{
return parent->createEntryImpl(remapped_key, info);
}
throw RuntimeError("Missing parent blackboard");
}
// autoremapping second (excluding private keys)
if(autoremapping_ && !IsPrivateKey(key))
{
if(auto parent = parent_bb_.lock())
{
return parent->createEntryImpl(key, info);
}
throw RuntimeError("Missing parent blackboard");
}
// not remapped, not found. Create locally.
auto entry = std::make_shared<Entry>(info);
// even if empty, let's assign to it a default type
entry->value = Any(info.type());
storage_.insert({ key, entry });
return entry;
}
nlohmann::json ExportBlackboardToJSON(const Blackboard& blackboard)
{
nlohmann::json dest;
for(auto entry_name : blackboard.getKeys())
{
std::string name(entry_name);
if(auto any_ref = blackboard.getAnyLocked(name))
{
if(auto any_ptr = any_ref.get())
{
JsonExporter::get().toJson(*any_ptr, dest[name]);
}
}
}
return dest;
}
void ImportBlackboardFromJSON(const nlohmann::json& json, Blackboard& blackboard)
{
for(auto it = json.begin(); it != json.end(); ++it)
{
if(auto res = JsonExporter::get().fromJson(it.value()))
{
auto entry = blackboard.getEntry(it.key());
if(!entry)
{
blackboard.createEntry(it.key(), res->second);
entry = blackboard.getEntry(it.key());
}
entry->value = res->first;
}
}
}
Blackboard::Entry& Blackboard::Entry::operator=(const Entry& other)
{
value = other.value;
info = other.info;
string_converter = other.string_converter;
sequence_id = other.sequence_id;
stamp = other.stamp;
return *this;
}
Blackboard* BT::Blackboard::rootBlackboard()
{
auto bb = static_cast<const Blackboard&>(*this).rootBlackboard();
return const_cast<Blackboard*>(bb);
}
const Blackboard* BT::Blackboard::rootBlackboard() const
{
const Blackboard* bb = this;
Blackboard::Ptr prev = parent_bb_.lock();
while(prev)
{
bb = prev.get();
prev = bb->parent_bb_.lock();
}
return bb;
}
} // namespace BT