-
Notifications
You must be signed in to change notification settings - Fork 659
/
bt_factory.cpp
731 lines (633 loc) · 21 KB
/
bt_factory.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/* Copyright (C) 2018-2022 Davide Faconti, Eurecat - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <filesystem>
#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/utils/shared_library.h"
#include "behaviortree_cpp/contrib/json.hpp"
#include "behaviortree_cpp/xml_parsing.h"
#include "wildcards/wildcards.hpp"
#ifdef USING_ROS
#include <ros/package.h>
#endif
namespace BT
{
bool WildcardMatch(std::string const& str, StringView filter)
{
return wildcards::match(str, filter);
}
struct BehaviorTreeFactory::PImpl
{
std::unordered_map<std::string, NodeBuilder> builders;
std::unordered_map<std::string, TreeNodeManifest> manifests;
std::set<std::string> builtin_IDs;
std::unordered_map<std::string, Any> behavior_tree_definitions;
std::shared_ptr<std::unordered_map<std::string, int>> scripting_enums;
std::shared_ptr<BT::Parser> parser;
std::unordered_map<std::string, SubstitutionRule> substitution_rules;
};
BehaviorTreeFactory::BehaviorTreeFactory() : _p(new PImpl)
{
_p->parser = std::make_shared<XMLParser>(*this);
registerNodeType<FallbackNode>("Fallback");
registerNodeType<FallbackNode>("AsyncFallback", true);
registerNodeType<SequenceNode>("Sequence");
registerNodeType<SequenceNode>("AsyncSequence", true);
registerNodeType<SequenceWithMemory>("SequenceWithMemory");
#ifdef USE_BTCPP3_OLD_NAMES
registerNodeType<SequenceWithMemory>("SequenceStar"); // backward compatibility
#endif
registerNodeType<ParallelNode>("Parallel");
registerNodeType<ParallelAllNode>("ParallelAll");
registerNodeType<ReactiveSequence>("ReactiveSequence");
registerNodeType<ReactiveFallback>("ReactiveFallback");
registerNodeType<IfThenElseNode>("IfThenElse");
registerNodeType<WhileDoElseNode>("WhileDoElse");
registerNodeType<InverterNode>("Inverter");
registerNodeType<RetryNode>("RetryUntilSuccessful");
registerNodeType<KeepRunningUntilFailureNode>("KeepRunningUntilFailure");
registerNodeType<RepeatNode>("Repeat");
registerNodeType<TimeoutNode>("Timeout");
registerNodeType<DelayNode>("Delay");
registerNodeType<RunOnceNode>("RunOnce");
registerNodeType<ForceSuccessNode>("ForceSuccess");
registerNodeType<ForceFailureNode>("ForceFailure");
registerNodeType<AlwaysSuccessNode>("AlwaysSuccess");
registerNodeType<AlwaysFailureNode>("AlwaysFailure");
registerNodeType<ScriptNode>("Script");
registerNodeType<ScriptCondition>("ScriptCondition");
registerNodeType<SetBlackboardNode>("SetBlackboard");
registerNodeType<SleepNode>("Sleep");
registerNodeType<UnsetBlackboardNode>("UnsetBlackboard");
registerNodeType<SubTreeNode>("SubTree");
registerNodeType<PreconditionNode>("Precondition");
registerNodeType<SwitchNode<2>>("Switch2");
registerNodeType<SwitchNode<3>>("Switch3");
registerNodeType<SwitchNode<4>>("Switch4");
registerNodeType<SwitchNode<5>>("Switch5");
registerNodeType<SwitchNode<6>>("Switch6");
registerNodeType<LoopNode<int>>("LoopInt");
registerNodeType<LoopNode<bool>>("LoopBool");
registerNodeType<LoopNode<double>>("LoopDouble");
registerNodeType<LoopNode<std::string>>("LoopString");
registerNodeType<EntryUpdatedAction>("WasEntryUpdated");
registerNodeType<EntryUpdatedDecorator>("SkipUnlessUpdated", NodeStatus::SKIPPED);
registerNodeType<EntryUpdatedDecorator>("WaitValueUpdate", NodeStatus::RUNNING);
for(const auto& it : _p->builders)
{
_p->builtin_IDs.insert(it.first);
}
_p->scripting_enums = std::make_shared<std::unordered_map<std::string, int>>();
}
BehaviorTreeFactory::BehaviorTreeFactory(BehaviorTreeFactory&& other) noexcept
{
this->_p = std::move(other._p);
}
BehaviorTreeFactory& BehaviorTreeFactory::operator=(BehaviorTreeFactory&& other) noexcept
{
this->_p = std::move(other._p);
return *this;
}
BehaviorTreeFactory::~BehaviorTreeFactory()
{}
bool BehaviorTreeFactory::unregisterBuilder(const std::string& ID)
{
if(builtinNodes().count(ID))
{
throw LogicError("You can not remove the builtin registration ID [", ID, "]");
}
auto it = _p->builders.find(ID);
if(it == _p->builders.end())
{
return false;
}
_p->builders.erase(ID);
_p->manifests.erase(ID);
return true;
}
void BehaviorTreeFactory::registerBuilder(const TreeNodeManifest& manifest,
const NodeBuilder& builder)
{
auto it = _p->builders.find(manifest.registration_ID);
if(it != _p->builders.end())
{
throw BehaviorTreeException("ID [", manifest.registration_ID, "] already registered");
}
_p->builders.insert({ manifest.registration_ID, builder });
_p->manifests.insert({ manifest.registration_ID, manifest });
}
void BehaviorTreeFactory::registerSimpleCondition(
const std::string& ID, const SimpleConditionNode::TickFunctor& tick_functor,
PortsList ports)
{
NodeBuilder builder = [tick_functor, ID](const std::string& name,
const NodeConfig& config) {
return std::make_unique<SimpleConditionNode>(name, tick_functor, config);
};
TreeNodeManifest manifest = { NodeType::CONDITION, ID, std::move(ports), {} };
registerBuilder(manifest, builder);
}
void BehaviorTreeFactory::registerSimpleAction(
const std::string& ID, const SimpleActionNode::TickFunctor& tick_functor,
PortsList ports)
{
NodeBuilder builder = [tick_functor, ID](const std::string& name,
const NodeConfig& config) {
return std::make_unique<SimpleActionNode>(name, tick_functor, config);
};
TreeNodeManifest manifest = { NodeType::ACTION, ID, std::move(ports), {} };
registerBuilder(manifest, builder);
}
void BehaviorTreeFactory::registerSimpleDecorator(
const std::string& ID, const SimpleDecoratorNode::TickFunctor& tick_functor,
PortsList ports)
{
NodeBuilder builder = [tick_functor, ID](const std::string& name,
const NodeConfig& config) {
return std::make_unique<SimpleDecoratorNode>(name, tick_functor, config);
};
TreeNodeManifest manifest = { NodeType::DECORATOR, ID, std::move(ports), {} };
registerBuilder(manifest, builder);
}
void BehaviorTreeFactory::registerFromPlugin(const std::string& file_path)
{
BT::SharedLibrary loader;
loader.load(file_path);
typedef void (*Func)(BehaviorTreeFactory&);
if(loader.hasSymbol(PLUGIN_SYMBOL))
{
Func func = (Func)loader.getSymbol(PLUGIN_SYMBOL);
func(*this);
}
else
{
std::cout << "ERROR loading library [" << file_path << "]: can't find symbol ["
<< PLUGIN_SYMBOL << "]" << std::endl;
}
}
#ifdef USING_ROS
#ifdef _WIN32
const char os_pathsep(';'); // NOLINT
#else
const char os_pathsep(':'); // NOLINT
#endif
// This function is a copy from the one in class_loader_imp.hpp in ROS pluginlib
// package, licensed under BSD.
// https://github.com/ros/pluginlib
std::vector<std::string> getCatkinLibraryPaths()
{
std::vector<std::string> lib_paths;
const char* env = std::getenv("CMAKE_PREFIX_PATH");
if(env)
{
const std::string env_catkin_prefix_paths(env);
std::vector<BT::StringView> catkin_prefix_paths =
splitString(env_catkin_prefix_paths, os_pathsep);
for(BT::StringView catkin_prefix_path : catkin_prefix_paths)
{
std::filesystem::path path(static_cast<std::string>(catkin_prefix_path));
std::filesystem::path lib("lib");
lib_paths.push_back((path / lib).string());
}
}
return lib_paths;
}
void BehaviorTreeFactory::registerFromROSPlugins()
{
std::vector<std::string> plugins;
ros::package::getPlugins("behaviortree_cpp", "bt_lib_plugin", plugins, true);
std::vector<std::string> catkin_lib_paths = getCatkinLibraryPaths();
for(const auto& plugin : plugins)
{
auto filename = std::filesystem::path(plugin + BT::SharedLibrary::suffix());
for(const auto& lib_path : catkin_lib_paths)
{
const auto full_path = std::filesystem::path(lib_path) / filename;
if(std::filesystem::exists(full_path))
{
std::cout << "Registering ROS plugins from " << full_path.string() << std::endl;
registerFromPlugin(full_path.string());
break;
}
}
}
}
#else
void BehaviorTreeFactory::registerFromROSPlugins()
{
throw RuntimeError("Using attribute [ros_pkg] in <include>, but this library was "
"compiled without ROS support. Recompile the BehaviorTree.CPP "
"using catkin");
}
#endif
void BehaviorTreeFactory::registerBehaviorTreeFromFile(
const std::filesystem::path& filename)
{
_p->parser->loadFromFile(filename);
}
void BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
{
_p->parser->loadFromText(xml_text);
}
std::vector<std::string> BehaviorTreeFactory::registeredBehaviorTrees() const
{
return _p->parser->registeredBehaviorTrees();
}
void BehaviorTreeFactory::clearRegisteredBehaviorTrees()
{
_p->parser->clearInternalState();
}
std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
const std::string& name, const std::string& ID, const NodeConfig& config) const
{
auto idNotFound = [this, ID] {
std::cerr << ID << " not included in this list:" << std::endl;
for(const auto& builder_it : _p->builders)
{
std::cerr << builder_it.first << std::endl;
}
throw RuntimeError("BehaviorTreeFactory: ID [", ID, "] not registered");
};
auto it_manifest = _p->manifests.find(ID);
if(it_manifest == _p->manifests.end())
{
idNotFound();
}
std::unique_ptr<TreeNode> node;
bool substituted = false;
for(const auto& [filter, rule] : _p->substitution_rules)
{
if(filter == name || filter == ID || wildcards::match(config.path, filter))
{
// first case: the rule is simply a string with the name of the
// node to create instead
if(const auto substituted_ID = std::get_if<std::string>(&rule))
{
auto it_builder = _p->builders.find(*substituted_ID);
if(it_builder != _p->builders.end())
{
auto& builder = it_builder->second;
node = builder(name, config);
}
else
{
throw RuntimeError("Substituted Node ID [", *substituted_ID, "] not found");
}
substituted = true;
break;
}
else if(const auto test_config = std::get_if<TestNodeConfig>(&rule))
{
// second case, the varian is a TestNodeConfig
auto test_node = new TestNode(name, config, *test_config);
node.reset(test_node);
substituted = true;
break;
}
}
}
// No substitution rule applied: default behavior
if(!substituted)
{
auto it_builder = _p->builders.find(ID);
if(it_builder == _p->builders.end())
{
idNotFound();
}
auto& builder = it_builder->second;
node = builder(name, config);
}
node->setRegistrationID(ID);
node->config().enums = _p->scripting_enums;
auto AssignConditions = [](auto& conditions, auto& executors) {
for(const auto& [cond_id, script] : conditions)
{
if(auto executor = ParseScript(script))
{
executors[size_t(cond_id)] = executor.value();
}
else
{
throw LogicError("Error in the script \"", script, "\"\n", executor.error());
}
}
};
AssignConditions(config.pre_conditions, node->preConditionsScripts());
AssignConditions(config.post_conditions, node->postConditionsScripts());
return node;
}
const std::unordered_map<std::string, NodeBuilder>& BehaviorTreeFactory::builders() const
{
return _p->builders;
}
const std::unordered_map<std::string, TreeNodeManifest>&
BehaviorTreeFactory::manifests() const
{
return _p->manifests;
}
const std::set<std::string>& BehaviorTreeFactory::builtinNodes() const
{
return _p->builtin_IDs;
}
Tree BehaviorTreeFactory::createTreeFromText(const std::string& text,
Blackboard::Ptr blackboard)
{
if(!_p->parser->registeredBehaviorTrees().empty())
{
std::cout << "WARNING: You executed BehaviorTreeFactory::createTreeFromText "
"after registerBehaviorTreeFrom[File/Text].\n"
"This is NOT, probably, what you want to do.\n"
"You should probably use BehaviorTreeFactory::createTree, instead"
<< std::endl;
}
XMLParser parser(*this);
parser.loadFromText(text);
auto tree = parser.instantiateTree(blackboard);
tree.manifests = this->manifests();
return tree;
}
Tree BehaviorTreeFactory::createTreeFromFile(const std::filesystem::path& file_path,
Blackboard::Ptr blackboard)
{
if(!_p->parser->registeredBehaviorTrees().empty())
{
std::cout << "WARNING: You executed BehaviorTreeFactory::createTreeFromFile "
"after registerBehaviorTreeFrom[File/Text].\n"
"This is NOT, probably, what you want to do.\n"
"You should probably use BehaviorTreeFactory::createTree, instead"
<< std::endl;
}
XMLParser parser(*this);
parser.loadFromFile(file_path);
auto tree = parser.instantiateTree(blackboard);
tree.manifests = this->manifests();
return tree;
}
Tree BehaviorTreeFactory::createTree(const std::string& tree_name,
Blackboard::Ptr blackboard)
{
auto tree = _p->parser->instantiateTree(blackboard, tree_name);
tree.manifests = this->manifests();
return tree;
}
void BehaviorTreeFactory::addMetadataToManifest(const std::string& node_id,
const KeyValueVector& metadata)
{
auto it = _p->manifests.find(node_id);
if(it == _p->manifests.end())
{
throw std::runtime_error("addMetadataToManifest: wrong ID");
}
it->second.metadata = metadata;
}
void BehaviorTreeFactory::registerScriptingEnum(StringView name, int value)
{
const auto str = std::string(name);
auto it = _p->scripting_enums->find(str);
if(it == _p->scripting_enums->end())
{
_p->scripting_enums->insert({ str, value });
}
else
{
if(it->second != value)
{
throw LogicError(
StrCat("Registering the enum [", name, "] twice with different values, first ",
std::to_string(it->second), " and later ", std::to_string(value)));
}
}
}
void BehaviorTreeFactory::clearSubstitutionRules()
{
_p->substitution_rules.clear();
}
void BehaviorTreeFactory::addSubstitutionRule(StringView filter, SubstitutionRule rule)
{
_p->substitution_rules[std::string(filter)] = rule;
}
void BehaviorTreeFactory::loadSubstitutionRuleFromJSON(const std::string& json_text)
{
auto const json = nlohmann::json::parse(json_text);
std::unordered_map<std::string, TestNodeConfig> configs;
auto test_configs = json.at("TestNodeConfigs");
for(auto const& [name, test_config] : test_configs.items())
{
auto& config = configs[name];
auto return_status = test_config.at("return_status").get<std::string>();
config.return_status = convertFromString<NodeStatus>(return_status);
if(test_config.contains("async_delay"))
{
config.async_delay =
std::chrono::milliseconds(test_config["async_delay"].get<int>());
}
if(test_config.contains("post_script"))
{
config.post_script = test_config["post_script"].get<std::string>();
}
if(test_config.contains("success_script"))
{
config.success_script = test_config["success_script"].get<std::string>();
}
if(test_config.contains("failure_script"))
{
config.failure_script = test_config["failure_script"].get<std::string>();
}
}
auto substitutions = json.at("SubstitutionRules");
for(auto const& [node_name, test] : substitutions.items())
{
auto test_name = test.get<std::string>();
auto it = configs.find(test_name);
if(it == configs.end())
{
addSubstitutionRule(node_name, test_name);
}
else
{
addSubstitutionRule(node_name, it->second);
}
}
}
const std::unordered_map<std::string, BehaviorTreeFactory::SubstitutionRule>&
BehaviorTreeFactory::substitutionRules() const
{
return _p->substitution_rules;
}
Tree& Tree::operator=(Tree&& other)
{
subtrees = std::move(other.subtrees);
manifests = std::move(other.manifests);
wake_up_ = other.wake_up_;
return *this;
}
Tree::Tree()
{}
Tree::Tree(Tree&& other)
{
(*this) = std::move(other);
}
void Tree::initialize()
{
wake_up_ = std::make_shared<WakeUpSignal>();
for(auto& subtree : subtrees)
{
for(auto& node : subtree->nodes)
{
node->setWakeUpInstance(wake_up_);
}
}
}
void Tree::haltTree()
{
if(!rootNode())
{
return;
}
// the halt should propagate to all the node if the nodes
// have been implemented correctly
rootNode()->haltNode();
//but, just in case.... this should be no-op
auto visitor = [](BT::TreeNode* node) { node->haltNode(); };
BT::applyRecursiveVisitor(rootNode(), visitor);
rootNode()->resetStatus();
}
TreeNode* Tree::rootNode() const
{
if(subtrees.empty())
{
return nullptr;
}
auto& subtree_nodes = subtrees.front()->nodes;
return subtree_nodes.empty() ? nullptr : subtree_nodes.front().get();
}
void Tree::sleep(std::chrono::system_clock::duration timeout)
{
wake_up_->waitFor(std::chrono::duration_cast<std::chrono::milliseconds>(timeout));
}
Tree::~Tree()
{
haltTree();
}
NodeStatus Tree::tickExactlyOnce()
{
return tickRoot(EXACTLY_ONCE, std::chrono::milliseconds(0));
}
NodeStatus Tree::tickOnce()
{
return tickRoot(ONCE_UNLESS_WOKEN_UP, std::chrono::milliseconds(0));
}
NodeStatus Tree::tickWhileRunning(std::chrono::milliseconds sleep_time)
{
return tickRoot(WHILE_RUNNING, sleep_time);
}
Blackboard::Ptr Tree::rootBlackboard()
{
if(subtrees.size() > 0)
{
return subtrees.front()->blackboard;
}
return {};
}
void Tree::applyVisitor(const std::function<void(const TreeNode*)>& visitor)
{
BT::applyRecursiveVisitor(static_cast<const TreeNode*>(rootNode()), visitor);
}
void Tree::applyVisitor(const std::function<void(TreeNode*)>& visitor)
{
BT::applyRecursiveVisitor(static_cast<TreeNode*>(rootNode()), visitor);
}
uint16_t Tree::getUID()
{
auto uid = ++uid_counter_;
return uid;
}
NodeStatus Tree::tickRoot(TickOption opt, std::chrono::milliseconds sleep_time)
{
NodeStatus status = NodeStatus::IDLE;
if(!wake_up_)
{
initialize();
}
if(!rootNode())
{
throw RuntimeError("Empty Tree");
}
while(status == NodeStatus::IDLE ||
(opt == TickOption::WHILE_RUNNING && status == NodeStatus::RUNNING))
{
status = rootNode()->executeTick();
// Inner loop. The previous tick might have triggered the wake-up
// in this case, unless TickOption::EXACTLY_ONCE, we tick again
while(opt != TickOption::EXACTLY_ONCE && status == NodeStatus::RUNNING &&
wake_up_->waitFor(std::chrono::milliseconds(0)))
{
status = rootNode()->executeTick();
}
if(isStatusCompleted(status))
{
rootNode()->resetStatus();
}
if(status == NodeStatus::RUNNING && sleep_time.count() > 0)
{
sleep(std::chrono::milliseconds(sleep_time));
}
}
return status;
}
void BlackboardRestore(const std::vector<Blackboard::Ptr>& backup, Tree& tree)
{
assert(backup.size() == tree.subtrees.size());
for(size_t i = 0; i < tree.subtrees.size(); i++)
{
backup[i]->cloneInto(*(tree.subtrees[i]->blackboard));
}
}
std::vector<Blackboard::Ptr> BlackboardBackup(const Tree& tree)
{
std::vector<Blackboard::Ptr> bb;
bb.reserve(tree.subtrees.size());
for(const auto& sub : tree.subtrees)
{
bb.push_back(BT::Blackboard::create());
sub->blackboard->cloneInto(*bb.back());
}
return bb;
}
nlohmann::json ExportTreeToJSON(const Tree& tree)
{
nlohmann::json out;
for(const auto& subtree : tree.subtrees)
{
nlohmann::json json_sub;
auto sub_name = subtree->instance_name;
if(sub_name.empty())
{
sub_name = subtree->tree_ID;
}
out[sub_name] = ExportBlackboardToJSON(*subtree->blackboard);
}
return out;
}
void ImportTreeFromJSON(const nlohmann::json& json, Tree& tree)
{
if(json.size() != tree.subtrees.size())
{
throw std::runtime_error("Number of blackboards don't match:");
}
size_t index = 0;
for(auto& [key, array] : json.items())
{
auto& subtree = tree.subtrees.at(index++);
ImportBlackboardFromJSON(array, *subtree->blackboard);
}
}
} // namespace BT