Skip to content

Commit fecfc32

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 fecfc32

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

include/behaviortree_cpp/bt_factory.h

+10-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"
@@ -154,6 +155,9 @@ class Tree
154155
//Call the visitor for each node of the tree.
155156
void applyVisitor(const std::function<void(TreeNode*)>& visitor);
156157

158+
/**
159+
* @brief Return the next node UID.
160+
*/
157161
[[nodiscard]] uint16_t getUID();
158162

159163
/// Get a list of nodes which fullPath() match a wildcard filter and
@@ -290,14 +294,17 @@ class BehaviorTreeFactory
290294
*
291295
* BehaviorTreeFactory::createTree(tree_id)
292296
*
293-
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
297+
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">.
298+
*
299+
* Return the attribute `main_tree_to_execute` if specified or if the file
300+
* defines a single SubTree, or an empty string otherwise.
294301
*
295302
*/
296-
void registerBehaviorTreeFromFile(const std::filesystem::path& filename);
303+
std::string registerBehaviorTreeFromFile(const std::filesystem::path& filename);
297304

298305
/// Same of registerBehaviorTreeFromFile, but passing the XML text,
299306
/// instead of the filename.
300-
void registerBehaviorTreeFromText(const std::string& xml_text);
307+
std::string registerBehaviorTreeFromText(const std::string& xml_text);
301308

302309
/// Returns the ID of the trees registered either with
303310
/// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.

include/behaviortree_cpp/bt_parser.h

+4-2
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,10 @@ class Parser
3638
Parser(Parser&& other) = default;
3739
Parser& operator=(Parser&& other) = default;
3840

39-
virtual void loadFromFile(const std::filesystem::path& filename,
41+
virtual std::string loadFromFile(const std::filesystem::path& filename,
4042
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, bool add_includes = true) = 0;
4345

4446
virtual std::vector<std::string> registeredBehaviorTrees() const = 0;
4547

include/behaviortree_cpp/xml_parsing.h

+3-2
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,10 @@ 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+
std::string loadFromFile(const std::filesystem::path& filename,
3031
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, bool add_includes = true) override;
3334

3435
[[nodiscard]] std::vector<std::string> registeredBehaviorTrees() const override;
3536

src/bt_factory.cpp

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

260-
void BehaviorTreeFactory::registerBehaviorTreeFromFile(
260+
std::string BehaviorTreeFactory::registerBehaviorTreeFromFile(
261261
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

+19-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,7 @@ 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, bool add_includes)
160160
{
161161
_p->opened_documents.emplace_back(new XMLDocument());
162162

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

166166
_p->current_path = std::filesystem::absolute(filepath.parent_path());
167167

168-
_p->loadDocImpl(doc, add_includes);
168+
return _p->loadDocImpl(doc, add_includes);
169169
}
170170

171-
void XMLParser::loadFromText(const std::string& xml_text, bool add_includes)
171+
std::string XMLParser::loadFromText(const std::string& xml_text, bool add_includes)
172172
{
173173
_p->opened_documents.emplace_back(new XMLDocument());
174174

175175
XMLDocument* doc = _p->opened_documents.back().get();
176176
doc->Parse(xml_text.c_str(), xml_text.size());
177177

178-
_p->loadDocImpl(doc, add_includes);
178+
return _p->loadDocImpl(doc, add_includes);
179179
}
180180

181181
std::vector<std::string> XMLParser::registeredBehaviorTrees() const
@@ -232,7 +232,7 @@ void BT::XMLParser::PImpl::loadSubtreeModel(const XMLElement* xml_root)
232232
}
233233
}
234234

235-
void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
235+
std::string XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
236236
{
237237
if(doc->Error())
238238
{
@@ -348,6 +348,19 @@ void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
348348

349349
tree_roots[tree_name] = bt_node;
350350
}
351+
352+
// Get the name of the tree to run (either explicit or single tree).
353+
std::string main_tree_to_execute;
354+
if (const auto main_tree_attribute = xml_root->Attribute("main_tree_to_execute"))
355+
{
356+
main_tree_to_execute = main_tree_attribute;
357+
}
358+
else if(xml_root->FirstChild() == xml_root->LastChild())
359+
{
360+
// special case: there is only one registered BT.
361+
main_tree_to_execute = xml_root->FirstChild()->ToElement()->Attribute("ID");
362+
}
363+
return main_tree_to_execute;
351364
}
352365

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

0 commit comments

Comments
 (0)