t17_blackboard_backup.cpp
Go to the documentation of this file.
2 #include "dummy_nodes.h"
3 
4 using namespace BT;
5 
6 // clang-format off
7 static const char* xml_tree = R"(
8 <root BTCPP_format="4">
9  <BehaviorTree ID="MainTree">
10  <Sequence>
11  <Script code="val_A:= 'john' "/>
12  <Script code="val_B:= 42 "/>
13  <SaySomething message="{val_A}" />
14  <SaySomething message="hello world" />
15  <SubTree ID="Sub" val="{val_A}" _autoremap="true" />
16  <SaySomething message="{reply}" />
17  </Sequence>
18  </BehaviorTree>
19  <BehaviorTree ID="Sub">
20  <Sequence>
21  <SaySomething message="{val}" />
22  <SaySomething message="{val_B}" />
23  <Script code="reply:= 'done' "/>
24  </Sequence>
25  </BehaviorTree>
26 </root>
27  )";
28 
29 // clang-format on
30 
31 int main()
32 {
33  BehaviorTreeFactory factory;
34  factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
36 
37  auto tree = factory.createTree("MainTree");
38 
39  // We want to create a memory of the blackboard in memory.
40  // This is conveninet when we want to reset its state to the
41  // original one.
42  // It is certainly more efficient than destroying and creating the tree again,
43  // in many casess.
44 
45  const auto backup_before_tick = BlackboardBackup(tree);
46  tree.tickWhileRunning();
47 
48  // Restore the original status of the blackboard
49  BlackboardRestore(backup_before_tick, tree);
50  tree.tickWhileRunning();
51 
52  // Alternatively, we may want to save he values of the element in the blackboard
53  // to file, to restore them again. We use JSON serialization for that.
54  nlohmann::json json_after_tick = ExportTreeToJSON(tree);
55 
56  // The JSOn object can be saved to file. See https://github.com/nlohmann/json
57  // for details. For the time being, we just print it in the console
58 
59  std::cout << "--- blackboard serialized as JSON: ----\n"
60  << json_after_tick.dump(2) << std::endl;
61 
62  // We can restore the values of the blackboards using the JSON
63  ImportTreeFromJSON(json_after_tick, tree);
64 
65  return 0;
66 }
BT
Definition: ex01_wrap_legacy.cpp:29
BT::BlackboardRestore
void BlackboardRestore(const std::vector< Blackboard::Ptr > &backup, BT::Tree &tree)
BlackboardRestore uses Blackboard::cloneInto to restore all the blackboards of the tree.
Definition: bt_factory.cpp:679
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
xml_tree
static const char * xml_tree
Definition: t17_blackboard_backup.cpp:7
bt_factory.h
dummy_nodes.h
main
int main()
Definition: t17_blackboard_backup.cpp:31
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
BT::ExportTreeToJSON
nlohmann::json ExportTreeToJSON(const BT::Tree &tree)
ExportTreeToJSON it calls ExportBlackboardToJSON for all the blackboards in the tree.
Definition: bt_factory.cpp:700
BT::ImportTreeFromJSON
void ImportTreeFromJSON(const nlohmann::json &json, BT::Tree &tree)
ImportTreeFromJSON it calls ImportBlackboardFromJSON for all the blackboards in the tree.
Definition: bt_factory.cpp:716
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::BlackboardBackup
std::vector< Blackboard::Ptr > BlackboardBackup(const BT::Tree &tree)
BlackboardBackup uses Blackboard::cloneInto to backup all the blackboards of the tree.
Definition: bt_factory.cpp:688
json
basic_json<> json
default specialization
Definition: json.hpp:3422
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08