Skip to content

Commit 7f02c44

Browse files
author
Gaël Écorchard
committed
Return the tree ID when registering a file
Return the attribute "main_tree_to_execute" or the ID of the single behavior-tree. Signed-off-by: Gaël Écorchard <[email protected]>
1 parent 14589e5 commit 7f02c44

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

include/behaviortree_cpp/bt_factory.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <memory>
2020
#include <unordered_map>
2121
#include <set>
22+
#include <string>
2223
#include <vector>
2324

2425
#include "behaviortree_cpp/contrib/magic_enum.hpp"
@@ -290,14 +291,17 @@ class BehaviorTreeFactory
290291
*
291292
* BehaviorTreeFactory::createTree(tree_id)
292293
*
293-
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
294+
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">.
295+
*
296+
* Return the attribute `main_tree_to_execute` if specified or if the file
297+
* defines a single SubTree, or an empty string otherwise.
294298
*
295299
*/
296-
void registerBehaviorTreeFromFile(const std::filesystem::path& filename);
300+
std::string registerBehaviorTreeFromFile(const std::filesystem::path& filename);
297301

298302
/// Same of registerBehaviorTreeFromFile, but passing the XML text,
299303
/// instead of the filename.
300-
void registerBehaviorTreeFromText(const std::string& xml_text);
304+
std::string registerBehaviorTreeFromText(const std::string& xml_text);
301305

302306
/// Returns the ID of the trees registered either with
303307
/// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.

include/behaviortree_cpp/bt_parser.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#pragma once
1414

1515
#include <filesystem>
16+
#include <string>
17+
1618
#include "behaviortree_cpp/bt_factory.h"
1719
#include "behaviortree_cpp/blackboard.h"
1820

@@ -36,10 +38,11 @@ class Parser
3638
Parser(Parser&& other) = default;
3739
Parser& operator=(Parser&& other) = default;
3840

39-
virtual void loadFromFile(const std::filesystem::path& filename,
40-
bool add_includes = true) = 0;
41+
virtual std::string loadFromFile(const std::filesystem::path& filename,
42+
bool add_includes = true) = 0;
4143

42-
virtual void loadFromText(const std::string& xml_text, bool add_includes = true) = 0;
44+
virtual std::string loadFromText(const std::string& xml_text,
45+
bool add_includes = true) = 0;
4346

4447
virtual std::vector<std::string> registeredBehaviorTrees() const = 0;
4548

include/behaviortree_cpp/xml_parsing.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "behaviortree_cpp/bt_parser.h"
55

66
#include <filesystem>
7+
#include <string>
78
#include <unordered_map>
89

910
namespace BT
@@ -26,10 +27,11 @@ class XMLParser : public Parser
2627
XMLParser(XMLParser&& other) noexcept;
2728
XMLParser& operator=(XMLParser&& other) noexcept;
2829

29-
void loadFromFile(const std::filesystem::path& filename,
30-
bool add_includes = true) override;
30+
std::string loadFromFile(const std::filesystem::path& filename,
31+
bool add_includes = true) override;
3132

32-
void loadFromText(const std::string& xml_text, bool add_includes = true) override;
33+
std::string loadFromText(const std::string& xml_text,
34+
bool add_includes = true) override;
3335

3436
[[nodiscard]] std::vector<std::string> registeredBehaviorTrees() const override;
3537

src/bt_factory.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ void BehaviorTreeFactory::registerFromROSPlugins()
257257
}
258258
#endif
259259

260-
void BehaviorTreeFactory::registerBehaviorTreeFromFile(
261-
const std::filesystem::path& filename)
260+
std::string
261+
BehaviorTreeFactory::registerBehaviorTreeFromFile(const std::filesystem::path& filename)
262262
{
263-
_p->parser->loadFromFile(filename);
263+
return _p->parser->loadFromFile(filename);
264264
}
265265

266-
void BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
266+
std::string BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
267267
{
268-
_p->parser->loadFromText(xml_text);
268+
return _p->parser->loadFromText(xml_text);
269269
}
270270

271271
std::vector<std::string> BehaviorTreeFactory::registeredBehaviorTrees() const

src/xml_parsing.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ struct XMLParser::PImpl
107107
void getPortsRecursively(const XMLElement* element,
108108
std::vector<std::string>& output_ports);
109109

110-
void loadDocImpl(XMLDocument* doc, bool add_includes);
110+
std::string loadDocImpl(XMLDocument* doc, bool add_includes);
111111

112112
std::list<std::unique_ptr<XMLDocument> > opened_documents;
113113
std::map<std::string, const XMLElement*> tree_roots;
@@ -156,7 +156,8 @@ XMLParser& XMLParser::operator=(XMLParser&& other) noexcept
156156
XMLParser::~XMLParser()
157157
{}
158158

159-
void XMLParser::loadFromFile(const std::filesystem::path& filepath, bool add_includes)
159+
std::string XMLParser::loadFromFile(const std::filesystem::path& filepath,
160+
bool add_includes)
160161
{
161162
_p->opened_documents.emplace_back(new XMLDocument());
162163

@@ -165,17 +166,17 @@ void XMLParser::loadFromFile(const std::filesystem::path& filepath, bool add_inc
165166

166167
_p->current_path = std::filesystem::absolute(filepath.parent_path());
167168

168-
_p->loadDocImpl(doc, add_includes);
169+
return _p->loadDocImpl(doc, add_includes);
169170
}
170171

171-
void XMLParser::loadFromText(const std::string& xml_text, bool add_includes)
172+
std::string XMLParser::loadFromText(const std::string& xml_text, bool add_includes)
172173
{
173174
_p->opened_documents.emplace_back(new XMLDocument());
174175

175176
XMLDocument* doc = _p->opened_documents.back().get();
176177
doc->Parse(xml_text.c_str(), xml_text.size());
177178

178-
_p->loadDocImpl(doc, add_includes);
179+
return _p->loadDocImpl(doc, add_includes);
179180
}
180181

181182
std::vector<std::string> XMLParser::registeredBehaviorTrees() const
@@ -232,7 +233,7 @@ void BT::XMLParser::PImpl::loadSubtreeModel(const XMLElement* xml_root)
232233
}
233234
}
234235

235-
void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
236+
std::string XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
236237
{
237238
if(doc->Error())
238239
{
@@ -348,6 +349,23 @@ void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
348349

349350
tree_roots[tree_name] = bt_node;
350351
}
352+
353+
// Get the name of the tree to run (either explicit or single tree).
354+
std::string main_tree_to_execute;
355+
if(const auto main_tree_attribute = xml_root->Attribute("main_tree_to_execute"))
356+
{
357+
main_tree_to_execute = main_tree_attribute;
358+
}
359+
else if(xml_root->FirstChild() == xml_root->LastChild())
360+
{
361+
// special case: there is only one registered BT.
362+
const auto& e = xml_root->FirstChildElement();
363+
if(e->FindAttribute("ID"))
364+
{
365+
main_tree_to_execute = e->Attribute("ID");
366+
}
367+
}
368+
return main_tree_to_execute;
351369
}
352370

353371
void VerifyXML(const std::string& xml_text,

0 commit comments

Comments
 (0)