@@ -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
156156XMLParser::~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
181181std::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
353366void VerifyXML (const std::string& xml_text,
0 commit comments