Skip to content

Commit dae4cac

Browse files
committed
add builtin models to WriteTreeToXML
1 parent 2b14fbb commit dae4cac

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

examples/generate_log.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "behaviortree_cpp/loggers/groot2_publisher.h"
33
#include "behaviortree_cpp/loggers/bt_file_logger_v2.h"
44
#include "behaviortree_cpp/loggers/bt_sqlite_logger.h"
5+
#include "behaviortree_cpp/loggers/bt_cout_logger.h"
56

67
// clang-format on
78

@@ -26,6 +27,7 @@ int main(int argc, char** argv)
2627
tree = factory.createTreeFromFile(file);
2728
}
2829

30+
BT::StdCoutLogger cout_logger(tree);
2931
BT::Groot2Publisher publisher(tree);
3032
BT::FileLogger2 file_logger(tree, "./generated_log.btlog");
3133
BT::SqliteLogger sqlite_logger(tree, "./generated_log.db3");

examples/t12_groot_howto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main()
5959

6060

6161
std::cout << "----------- XML file ----------\n"
62-
<< BT::WriteTreeToXML(tree, false)
62+
<< BT::WriteTreeToXML(tree, false, false)
6363
<< "--------------------------------\n";
6464

6565
// Connect the Groot2Publisher. This will allow Groot2 to

include/behaviortree_cpp/xml_parsing.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,32 @@ class XMLParser : public Parser
4343
void VerifyXML(const std::string& xml_text,
4444
const std::unordered_map<std::string, NodeType>& registered_nodes);
4545

46+
/**
47+
* @brief writeTreeNodesModelXML generates an XMl that contains the manifests in the
48+
* <TreeNodesModel>
49+
*
50+
* @param factory the factory with the registered types
51+
* @param include_builtin if true, include the builtin Nodes
52+
*
53+
* @return string containing the XML.
54+
*/
4655
[[nodiscard]]
4756
std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory,
4857
bool include_builtin = false);
4958

59+
/**
60+
* @brief WriteTreeToXML create a string that contains the XML that corresponds to a given tree.
61+
* When using this function with a logger, you should probably set both add_metadata and
62+
* add_builtin_models to true.
63+
*
64+
* @param tree the input tree
65+
* @param add_metadata if true, the attributes "_uid" and "_fullPath" will be added to the nodes
66+
* @param add_builtin_models if true, include the builtin Nodes into the <TreeNodesModel>
67+
*
68+
* @return string containing the XML.
69+
*/
5070
[[nodiscard]]
51-
std::string WriteTreeToXML(const Tree& tree, bool add_metadata = false);
71+
std::string WriteTreeToXML(const Tree& tree, bool add_metadata, bool add_builtin_models);
5272

5373
} // namespace BT
5474

src/loggers/bt_file_logger_v2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& file
1818
//-------------------------------------
1919
file_stream_.open(filepath, std::ofstream::binary | std::ofstream::out);
2020

21-
std::string const xml = WriteTreeToXML(tree, true);
21+
std::string const xml = WriteTreeToXML(tree, true, true);
2222

2323
// serialize the length of the buffer in the first 4 bytes
2424
char write_buffer[8];

src/loggers/bt_sqlite_logger.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ SqliteLogger::SqliteLogger(const Tree &tree,
1313

1414
db_ = std::make_unique<sqlite::Connection>(file.string());
1515

16-
sqlite::Statement(*db_, "PRAGMA journal_mode=WAL;");
17-
sqlite::Statement(*db_, "PRAGMA synchronous = normal;");
18-
sqlite::Statement(*db_, "PRAGMA temp_store = memory;");
19-
2016
sqlite::Statement(*db_,
2117
"CREATE TABLE IF NOT EXISTS Transitions ("
2218
"timestamp INTEGER PRIMARY KEY NOT NULL, "
@@ -37,7 +33,7 @@ SqliteLogger::SqliteLogger(const Tree &tree,
3733
sqlite::Statement(*db_, "DELETE from Definitions;");
3834
}
3935

40-
auto tree_xml = WriteTreeToXML(tree, true);
36+
auto tree_xml = WriteTreeToXML(tree, true, true);
4137
sqlite::Statement(*db_,
4238
"INSERT into Definitions (date, xml_tree) "
4339
"VALUES (datetime('now','localtime'),?);",

src/loggers/groot2_publisher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Groot2Publisher::Groot2Publisher(const BT::Tree& tree,
7373
Groot2Publisher::used_ports.insert(server_port+1);
7474
}
7575

76-
tree_xml_ = WriteTreeToXML(tree, true);
76+
tree_xml_ = WriteTreeToXML(tree, true, true);
7777

7878
//-------------------------------
7979
// Prepare the status buffer

src/xml_parsing.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,8 @@ void addNodeModelToXML(const TreeNodeManifest& model,
909909
void addTreeToXML(const Tree& tree,
910910
XMLDocument& doc,
911911
XMLElement* rootXML,
912-
bool add_metadata)
912+
bool add_metadata,
913+
bool add_builtin_models)
913914
{
914915
std::function<void(const TreeNode&, XMLElement*)> addNode;
915916
addNode = [&](const TreeNode& node,
@@ -991,7 +992,7 @@ void addTreeToXML(const Tree& tree,
991992
std::map<std::string, const TreeNodeManifest*> ordered_models;
992993
for (const auto& [registration_ID, model] : tree.manifests)
993994
{
994-
if(temp_factory.builtinNodes().count(registration_ID) == 0)
995+
if(add_builtin_models || !temp_factory.builtinNodes().count(registration_ID))
995996
{
996997
ordered_models.insert( {registration_ID, &model} );
997998
}
@@ -1052,15 +1053,15 @@ Tree buildTreeFromFile(const BehaviorTreeFactory& factory,
10521053
return parser.instantiateTree(blackboard);
10531054
}
10541055

1055-
std::string WriteTreeToXML(const Tree &tree, bool add_metadata)
1056+
std::string WriteTreeToXML(const Tree &tree, bool add_metadata, bool add_builtin_models)
10561057
{
10571058
XMLDocument doc;
10581059

10591060
XMLElement* rootXML = doc.NewElement("root");
10601061
rootXML->SetAttribute("BTCPP_format", 4);
10611062
doc.InsertFirstChild(rootXML);
10621063

1063-
addTreeToXML(tree, doc, rootXML, add_metadata);
1064+
addTreeToXML(tree, doc, rootXML, add_metadata, add_builtin_models);
10641065

10651066
XMLPrinter printer;
10661067
doc.Print(&printer);

0 commit comments

Comments
 (0)