Skip to content
Merged
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
5 changes: 5 additions & 0 deletions include/behaviortree_cpp_v3/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ class BehaviorTreeFactory
/// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
std::vector<std::string> registeredBehaviorTrees() const;

/**
* @brief Clear previously-registered behavior trees.
*/
void clearRegisteredBehaviorTrees();

/**
* @brief instantiateTreeNode creates an instance of a previously registered TreeNode.
*
Expand Down
2 changes: 2 additions & 0 deletions include/behaviortree_cpp_v3/bt_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Parser

virtual Tree instantiateTree(const Blackboard::Ptr& root_blackboard,
std::string tree_name = {}) = 0;

virtual void clearInternalState() {};
};

} // namespace BT
2 changes: 2 additions & 0 deletions include/behaviortree_cpp_v3/xml_parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class XMLParser : public Parser
Tree instantiateTree(const Blackboard::Ptr& root_blackboard,
std::string main_tree_to_execute = {}) override;

void clearInternalState() override;

private:
struct Pimpl;
Pimpl* _p;
Expand Down
7 changes: 6 additions & 1 deletion src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,19 @@ void BehaviorTreeFactory::registerBehaviorTreeFromFile(const std::string& filena

void BehaviorTreeFactory::registerBehaviorTreeFromText(const std::string& xml_text)
{
parser_->loadFromText(xml_text);
parser_->loadFromText(xml_text, true /* add_includes */);
}

std::vector<std::string> BehaviorTreeFactory::registeredBehaviorTrees() const
{
return parser_->registeredBehaviorTrees();
}

void BehaviorTreeFactory::clearRegisteredBehaviorTrees()
{
parser_->clearInternalState();
}

std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
const std::string& name, const std::string& ID, const NodeConfiguration& config) const
{
Expand Down
5 changes: 5 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ Tree XMLParser::instantiateTree(const Blackboard::Ptr& root_blackboard,
return output_tree;
}

void XMLParser::clearInternalState()
{
_p->clear();
}

TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
const Blackboard::Ptr& blackboard,
const TreeNode::Ptr& node_parent)
Expand Down
46 changes: 46 additions & 0 deletions tests/gtest_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,49 @@ TEST(BehaviorTreeFactory, DecoratorWithTwoChildrenThrows)

ASSERT_THROW(factory.createTreeFromText(xml_text), BehaviorTreeException);
}

TEST(BehaviorTreeFactory, ParserClearRegisteredBehaviorTrees)
{
const std::string tree_xml = R"(
<root>
<BehaviorTree ID="Main">
<AlwaysSuccess />
</BehaviorTree>
</root>
)";

BehaviorTreeFactory factory;
XMLParser parser(factory);

ASSERT_NO_THROW(parser.loadFromText(tree_xml));

const auto trees = parser.registeredBehaviorTrees();
ASSERT_FALSE(trees.empty());

parser.clearInternalState();

const auto trees_after_clear = parser.registeredBehaviorTrees();
EXPECT_TRUE(trees_after_clear.empty());
}

TEST(BehaviorTreeFactory, FactoryClearRegisteredBehaviorTrees)
{
BehaviorTreeFactory factory;
const std::string tree_xml = R"(
<root>
<BehaviorTree ID="Main">
<AlwaysSuccess />
</BehaviorTree>
</root>
)";

ASSERT_NO_THROW(factory.registerBehaviorTreeFromText(tree_xml));

const auto trees = factory.registeredBehaviorTrees();
ASSERT_FALSE(trees.empty());

factory.clearRegisteredBehaviorTrees();

const auto trees_after_clear = factory.registeredBehaviorTrees();
EXPECT_TRUE(trees_after_clear.empty());
}