Skip to content

Return the tree ID when registering a file #949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/behaviortree_cpp/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <memory>
#include <unordered_map>
#include <set>
#include <string>
#include <vector>

#include "behaviortree_cpp/contrib/magic_enum.hpp"
Expand Down Expand Up @@ -290,14 +291,17 @@ class BehaviorTreeFactory
*
* BehaviorTreeFactory::createTree(tree_id)
*
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">.
*
* Return the attribute `main_tree_to_execute` if specified or if the file
* defines a single SubTree, or an empty string otherwise.
*
*/
void registerBehaviorTreeFromFile(const std::filesystem::path& filename);
std::string registerBehaviorTreeFromFile(const std::filesystem::path& filename);

/// Same of registerBehaviorTreeFromFile, but passing the XML text,
/// instead of the filename.
void registerBehaviorTreeFromText(const std::string& xml_text);
std::string registerBehaviorTreeFromText(const std::string& xml_text);

/// Returns the ID of the trees registered either with
/// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
Expand Down
9 changes: 6 additions & 3 deletions include/behaviortree_cpp/bt_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#pragma once

#include <filesystem>
#include <string>

#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/blackboard.h"

Expand All @@ -36,10 +38,11 @@ class Parser
Parser(Parser&& other) = default;
Parser& operator=(Parser&& other) = default;

virtual void loadFromFile(const std::filesystem::path& filename,
bool add_includes = true) = 0;
virtual std::string loadFromFile(const std::filesystem::path& filename,
bool add_includes = true) = 0;

virtual void loadFromText(const std::string& xml_text, bool add_includes = true) = 0;
virtual std::string loadFromText(const std::string& xml_text,
bool add_includes = true) = 0;

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

Expand Down
8 changes: 5 additions & 3 deletions include/behaviortree_cpp/xml_parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "behaviortree_cpp/bt_parser.h"

#include <filesystem>
#include <string>
#include <unordered_map>

namespace BT
Expand All @@ -26,10 +27,11 @@ class XMLParser : public Parser
XMLParser(XMLParser&& other) noexcept;
XMLParser& operator=(XMLParser&& other) noexcept;

void loadFromFile(const std::filesystem::path& filename,
bool add_includes = true) override;
std::string loadFromFile(const std::filesystem::path& filename,
bool add_includes = true) override;

void loadFromText(const std::string& xml_text, bool add_includes = true) override;
std::string loadFromText(const std::string& xml_text,
bool add_includes = true) override;

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

Expand Down
10 changes: 5 additions & 5 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ void BehaviorTreeFactory::registerFromROSPlugins()
}
#endif

void BehaviorTreeFactory::registerBehaviorTreeFromFile(
const std::filesystem::path& filename)
std::string
BehaviorTreeFactory::registerBehaviorTreeFromFile(const std::filesystem::path& filename)
{
_p->parser->loadFromFile(filename);
return _p->parser->loadFromFile(filename);
}

void BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
std::string BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
{
_p->parser->loadFromText(xml_text);
return _p->parser->loadFromText(xml_text);
}

std::vector<std::string> BehaviorTreeFactory::registeredBehaviorTrees() const
Expand Down
30 changes: 24 additions & 6 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct XMLParser::PImpl
void getPortsRecursively(const XMLElement* element,
std::vector<std::string>& output_ports);

void loadDocImpl(XMLDocument* doc, bool add_includes);
std::string loadDocImpl(XMLDocument* doc, bool add_includes);

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

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

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

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

_p->loadDocImpl(doc, add_includes);
return _p->loadDocImpl(doc, add_includes);
}

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

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

_p->loadDocImpl(doc, add_includes);
return _p->loadDocImpl(doc, add_includes);
}

std::vector<std::string> XMLParser::registeredBehaviorTrees() const
Expand Down Expand Up @@ -232,7 +233,7 @@ void BT::XMLParser::PImpl::loadSubtreeModel(const XMLElement* xml_root)
}
}

void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
std::string XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)
{
if(doc->Error())
{
Expand Down Expand Up @@ -348,6 +349,23 @@ void XMLParser::PImpl::loadDocImpl(XMLDocument* doc, bool add_includes)

tree_roots[tree_name] = bt_node;
}

// Get the name of the tree to run (either explicit or single tree).
std::string main_tree_to_execute;
if(const auto main_tree_attribute = xml_root->Attribute("main_tree_to_execute"))
{
main_tree_to_execute = main_tree_attribute;
}
else if(xml_root->FirstChild() == xml_root->LastChild())
{
// special case: there is only one registered BT.
const auto& e = xml_root->FirstChildElement();
if(e->FindAttribute("ID"))
{
main_tree_to_execute = e->Attribute("ID");
}
}
return main_tree_to_execute;
}

void VerifyXML(const std::string& xml_text,
Expand Down
Loading