AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
tree_document.hpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <tinyxml2.h>
18
19#include <functional>
20#include <iterator>
21#include <set>
22#include <string>
23#include <type_traits>
24#include <vector>
25
26#include "auto_apms_behavior_tree_core/definitions.hpp"
27#include "auto_apms_behavior_tree_core/node/node_manifest.hpp"
28#include "auto_apms_behavior_tree_core/node/node_registration_loader.hpp"
29#include "auto_apms_behavior_tree_core/node/node_registration_options.hpp"
30#include "auto_apms_behavior_tree_core/tree/script.hpp"
31#include "behaviortree_cpp/bt_factory.h"
32#include "behaviortree_cpp/tree_node.h"
33#include "rclcpp/rclcpp.hpp"
34
36{
37class SubTree;
38}
39
41{
42
43class TreeResource;
44class NodeModelType;
45
140class TreeDocument : private tinyxml2::XMLDocument
141{
142 using XMLElement = tinyxml2::XMLElement;
143
144 inline static const std::string LOGGER_NAME = "tree_document";
145
146public:
147 static inline const char BTCPP_FORMAT_ATTRIBUTE_NAME[] = "BTCPP_format";
148 static inline const char BTCPP_FORMAT_DEFAULT_VERSION[] = "4";
149 static inline const char ROOT_ELEMENT_NAME[] = "root";
150 static inline const char ROOT_TREE_ATTRIBUTE_NAME[] = "main_tree_to_execute";
151 static inline const char TREE_ELEMENT_NAME[] = "BehaviorTree";
152 static inline const char SUBTREE_ELEMENT_NAME[] = "SubTree";
153 static inline const char TREE_NAME_ATTRIBUTE_NAME[] = "ID";
154 static inline const char TREE_NODE_MODEL_ELEMENT_NAME[] = "TreeNodesModel";
155 static inline const char NODE_INSTANCE_NAME_ATTRIBUTE_NAME[] = "name";
156 static inline const char INCLUDE_ELEMENT_NAME[] = "include";
157 static inline const char INCLUDE_PATH_ATTRIBUTE_NAME[] = "path";
158 static inline const char INCLUDE_ROS_PKG_ATTRIBUTE_NAME[] = "ros_pkg";
159 static inline const char INLINE_NODE_REGISTRATION_OPTIONS_ATTRIBUTE_PREFIX[] = "_autoapms_reg_opt__";
160 static inline const char INCLUDE_RESOURCE_ATTRIBUTE_NAME[] = "autoapms";
161
162 class TreeElement;
163
177 {
178 public:
180 using PortValues = std::map<std::string, std::string>;
182 using DeepApplyCallback = std::function<bool(NodeElement &)>;
184 using ConstDeepApplyCallback = std::function<bool(const NodeElement &)>;
185
186 protected:
192 explicit NodeElement(TreeDocument * doc_ptr, XMLElement * ele_ptr);
193
194 public:
199 NodeElement(const NodeElement & ele) = default;
200
212 NodeElement & operator=(const NodeElement & other);
213
222 bool operator==(const NodeElement & other) const;
223
224 bool operator!=(const NodeElement & other) const;
225
239 NodeElement insertNode(const std::string & name, const NodeElement * before_this = nullptr);
240
255 const std::string & name, const NodeRegistrationOptions & registration_options,
256 const NodeElement * before_this = nullptr);
257
271 template <class T>
272 typename std::enable_if_t<std::is_base_of_v<NodeModelType, T> && !std::is_same_v<model::SubTree, T>, T> insertNode(
273 const NodeElement * before_this = nullptr);
274
290 template <class SubTreeT>
291 typename std::enable_if_t<std::is_same_v<model::SubTree, SubTreeT>, model::SubTree> insertNode(
292 const std::string & tree_name, const NodeElement * before_this = nullptr);
293
312 template <class SubTreeT>
313 typename std::enable_if_t<std::is_same_v<model::SubTree, SubTreeT>, model::SubTree> insertNode(
314 const TreeElement & tree, const NodeElement * before_this = nullptr);
315
328 model::SubTree insertSubTreeNode(const std::string & tree_name, const NodeElement * before_this = nullptr);
329
345 model::SubTree insertSubTreeNode(const TreeElement & tree, const NodeElement * before_this = nullptr);
346
367 const TreeElement & tree, const NodeElement * before_this = nullptr, bool auto_register_nodes = true);
368
391 const TreeDocument & doc, const std::string & tree_name, const NodeElement * before_this = nullptr,
392 bool auto_register_nodes = true);
393
417 const TreeDocument & doc, const NodeElement * before_this = nullptr, bool auto_register_nodes = true);
418
439 const std::string & tree_str, const std::string & tree_name, const NodeElement * before_this = nullptr);
440
460 NodeElement insertTreeFromString(const std::string & tree_str, const NodeElement * before_this = nullptr);
461
482 const std::string & path, const std::string & tree_name, const NodeElement * before_this = nullptr);
483
503 NodeElement insertTreeFromFile(const std::string & path, const NodeElement * before_this = nullptr);
504
535 const TreeResource & resource, const std::string & tree_name, const NodeElement * before_this = nullptr,
536 bool auto_register_nodes = true);
537
569 const TreeResource & resource, const NodeElement * before_this = nullptr, bool auto_register_nodes = true);
570
575 bool hasChildren() const;
576
577 // clang-format off
599 const std::string & registration_name = "", const std::string & instance_name = "",
600 bool deep_search = false) const;
601
602 // clang-format off
624 template <class T>
625 typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, T> getFirstNode(
626 const std::string & instance_name = "", bool deep_search = false) const;
627
628 // clang-format off
650 const std::string & registration_name = "", const std::string & instance_name = "",
651 bool deep_search = false);
652
653 // clang-format off
675 template <class T>
676 typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, NodeElement &> removeFirstChild(
677 const std::string & instance_name = "", bool deep_search = false);
678 // clang-format on
679
685
693 const std::vector<std::string> & getPortNames() const;
694
705 PortValues getPorts() const;
706
721 std::optional<NodeRegistrationOptions> getInlineRegistrationOptions() const;
722
733 NodeElement & setPorts(const PortValues & port_values);
734
748
761
770
781 NodeElement & setConditionalScript(BT::PreCond type, const Script & script);
782
789 NodeElement & setConditionalScript(BT::PostCond type, const Script & script);
790
796 virtual NodeElement & setName(const std::string & instance_name);
797
802 virtual std::string getRegistrationName() const;
803
808 virtual std::string getName() const;
809
814 std::string getFullyQualifiedName() const;
815
824 const TreeDocument & getParentDocument() const;
825
833 XMLElement * getXMLElement();
834
852 const std::vector<NodeElement> deepApplyConst(ConstDeepApplyCallback apply_callback) const;
853
869 std::vector<NodeElement> deepApply(DeepApplyCallback apply_callback);
870
878 {
879 friend class NodeElement;
880
881 public:
882 using iterator_category = std::forward_iterator_tag;
883 using value_type = NodeElement;
884 using difference_type = std::ptrdiff_t;
885 using pointer = value_type *;
886 using reference = value_type;
887
890
892 value_type operator*() const;
893
896
899
900 bool operator==(const ChildIterator & other) const;
901 bool operator!=(const ChildIterator & other) const;
902
903 private:
904 ChildIterator(TreeDocument * doc_ptr, tinyxml2::XMLElement * current);
905
906 TreeDocument * doc_ptr_;
907 tinyxml2::XMLElement * current_;
908 };
909
914 ChildIterator begin() const;
915
920 ChildIterator end() const;
921
922 private:
923 NodeElement insertBeforeImpl(const NodeElement * before_this, XMLElement * add_this);
924
928 void refreshPortInfoCache() const;
929
930 static void deepApplyImpl(
931 const NodeElement & parent, ConstDeepApplyCallback apply_callback, std::vector<NodeElement> & vec);
932
933 static void deepApplyImpl(NodeElement & parent, DeepApplyCallback apply_callback, std::vector<NodeElement> & vec);
934
935 protected:
939 tinyxml2::XMLElement * ele_ptr_;
940
941 private:
942 mutable std::vector<std::string> port_names_;
943 mutable PortValues port_default_values_;
944 };
945
955 {
956 friend class TreeDocument;
957
958 protected:
964 explicit TreeElement(TreeDocument * doc_ptr, XMLElement * ele_ptr);
965
966 public:
971 TreeElement(const TreeElement & ele) = default;
972
982 TreeElement & operator=(const TreeElement & other);
983
989 TreeElement & setName(const std::string & tree_name) override;
990
995 std::string getName() const override;
996
1005
1017
1032
1038 BT::Result verify() const;
1039
1050 std::string writeToString(bool remove_whitespace = false) const;
1051
1057 const std::string & registration_name = "", const std::string & instance_name = "", bool deep_search = false);
1058
1063 template <class T>
1064 typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, TreeElement &> removeFirstChild(
1065 const std::string & instance_name = "", bool deep_search = false);
1066
1072
1073 /* Not supported methods for TreeElement instances */
1074
1075 const std::vector<std::string> & getPortNames() = delete;
1076 PortValues getPorts() = delete;
1077 NodeElement & setPorts() = delete;
1078 NodeElement & resetPorts() = delete;
1079 NodeElement & setConditionalScript() = delete;
1080 };
1081
1093 const std::string & format_version = BTCPP_FORMAT_DEFAULT_VERSION,
1094 NodeRegistrationLoader::SharedPtr tree_node_loader = NodeRegistrationLoader::make_shared());
1095
1096 TreeDocument(const TreeDocument & other) = delete;
1097
1098 virtual ~TreeDocument() = default;
1099
1123 TreeDocument & mergeTreeDocument(const XMLDocument & other, bool adopt_root_tree = false);
1124
1149 const TreeDocument & other, bool adopt_root_tree = false, bool auto_register_nodes = true);
1150
1174 TreeDocument & mergeString(const std::string & tree_str, bool adopt_root_tree = false);
1175
1200 TreeDocument & mergeFile(const std::string & path, bool adopt_root_tree = false);
1201
1232 const TreeResource & resource, bool adopt_root_tree = false, bool auto_register_nodes = true);
1233
1254 TreeDocument & mergeTree(const TreeElement & tree, bool make_root_tree = false, bool auto_register_nodes = true);
1255
1263 TreeElement newTree(const std::string & tree_name);
1264
1282 TreeElement newTree(const TreeElement & other, bool auto_register_nodes = true);
1283
1306 const TreeDocument & other, const std::string & tree_name = "", bool auto_register_nodes = true);
1307
1325 TreeElement newTreeFromString(const std::string & tree_str, const std::string & tree_name = "");
1326
1344 TreeElement newTreeFromFile(const std::string & path, const std::string & tree_name = "");
1345
1374 const TreeResource & resource, const std::string & tree_name = "", bool auto_register_nodes = true);
1375
1381 bool hasTreeName(const std::string & tree_name) const;
1382
1390 TreeElement getTree(const std::string & tree_name);
1391
1399 TreeDocument & setRootTreeName(const std::string & tree_name);
1400
1405 bool hasRootTreeName() const;
1406
1413 std::string getRootTreeName() const;
1414
1422
1436 TreeDocument & removeTree(const std::string & tree_name);
1437
1450 TreeDocument & removeTree(const TreeElement & tree);
1451
1456 std::vector<std::string> getAllTreeNames() const;
1457
1470 const std::string & node_namespace,
1471 const std::string & sep = _AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP);
1472
1499 virtual TreeDocument & registerNodes(const NodeManifest & node_manifest, bool override = false);
1500
1507 std::set<std::string> getRegisteredNodeNames(bool include_native = true) const;
1508
1521 NodeManifest getRequiredNodeManifest(const std::string & tree_name = "") const;
1522
1538 TreeDocument & setInlineRegistrationOptions(const std::string & tree_name = "");
1539
1548
1556 static NodeModelMap getNodeModel(tinyxml2::XMLDocument & doc, const NodeManifest & manifest);
1557
1567 NodeModelMap getNodeModel(bool include_native = false) const;
1568
1574 BT::Result verify() const;
1575
1583 std::string writeToString(bool remove_whitespace = false) const;
1584
1593 void writeToFile(const std::string & path, bool remove_whitespace = false) const;
1594
1601 TreeDocument & reset(bool unregister_nodes = true);
1602
1603private:
1604 template <typename ReturnT, typename DocumentT>
1605 static ReturnT getXMLElementForTreeWithNameImpl(DocumentT & doc, const std::string & tree_name);
1606
1607 const XMLElement * getXMLElementForTreeWithName(const std::string & tree_name) const;
1608
1609 XMLElement * getXMLElementForTreeWithName(const std::string & tree_name);
1610
1618 TreeDocument & mergeFileImpl(const std::string & path, bool adopt_root_tree, std::set<std::string> & include_stack);
1619
1627 TreeDocument & mergeTreeDocumentImpl(
1628 const XMLDocument & other, bool adopt_root_tree, std::set<std::string> & include_stack);
1629
1630 const std::map<std::string, std::string> all_node_classes_package_map_;
1631 const std::set<std::string> native_node_names_;
1632 std::string format_version_;
1633 NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
1634
1635protected:
1636 NodeManifest registered_nodes_manifest_;
1637 BT::BehaviorTreeFactory factory_;
1638 rclcpp::Logger logger_;
1639 rclcpp::Node::WeakPtr ros_node_wptr_;
1640 rclcpp::CallbackGroup::WeakPtr tree_node_waitables_callback_group_wptr_;
1641 rclcpp::executors::SingleThreadedExecutor::WeakPtr tree_node_waitables_executor_wptr_;
1642 bool only_non_ros_nodes_ = false;
1643};
1644
1645// #####################################################################################################################
1646// ################################ DEFINITIONS ##############################################
1647// #####################################################################################################################
1648
1650template <typename T, typename = void>
1651struct has_static_method_registrationOptions : std::false_type
1652{
1653};
1654
1655template <typename T>
1656struct has_static_method_registrationOptions<
1657 T, typename std::enable_if_t<std::is_same_v<decltype(T::registrationOptions()), NodeRegistrationOptions>>>
1658: std::true_type
1659{
1660};
1662
1663template <class T>
1664inline typename std::enable_if_t<std::is_base_of_v<NodeModelType, T> && !std::is_same_v<model::SubTree, T>, T>
1666{
1667 // Determine if overload for automatically trying to register the node can be used. Models for native nodes don't
1668 // implement the required static method, thus the other overload must be used.
1669 if constexpr (has_static_method_registrationOptions<T>::value) {
1670 NodeElement ele = insertNode(T::name(), T::registrationOptions(), before_this);
1671 return T(ele.doc_ptr_, ele.ele_ptr_);
1672 } else {
1673 NodeElement ele = insertNode(T::name(), before_this);
1674 return T(ele.doc_ptr_, ele.ele_ptr_);
1675 }
1676}
1677
1678template <class T>
1679inline typename std::enable_if_t<std::is_same_v<model::SubTree, T>, model::SubTree>
1680TreeDocument::NodeElement::insertNode(const std::string & tree_name, const NodeElement * before_this)
1681{
1682 return insertSubTreeNode(tree_name, before_this);
1683}
1684
1685template <class T>
1686inline typename std::enable_if_t<std::is_same_v<model::SubTree, T>, model::SubTree>
1687TreeDocument::NodeElement::insertNode(const TreeElement & tree, const NodeElement * before_this)
1688{
1689 return insertSubTreeNode(tree, before_this);
1690}
1691
1692template <class T>
1693inline typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, T> core::TreeDocument::NodeElement::getFirstNode(
1694 const std::string & instance_name, bool deep_search) const
1695{
1696 const NodeElement ele = getFirstNode(T::name(), instance_name, deep_search);
1697 return T(ele.doc_ptr_, ele.ele_ptr_);
1698}
1699
1700template <class T>
1701inline typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, TreeDocument::NodeElement &>
1702core::TreeDocument::NodeElement::removeFirstChild(const std::string & instance_name, bool deep_search)
1703{
1704 return removeFirstChild(T::name(), instance_name, deep_search);
1705}
1706
1707template <class T>
1708inline typename std::enable_if_t<std::is_base_of_v<NodeModelType, T>, TreeDocument::TreeElement &>
1709TreeDocument::TreeElement::removeFirstChild(const std::string & instance_name, bool deep_search)
1710{
1711 NodeElement::removeFirstChild<T>(instance_name, deep_search);
1712 return *this;
1713}
1714
1715} // namespace auto_apms_behavior_tree::core
1716
1717// Include after TreeDocument is fully defined to provide complete type for model::SubTree
1718// which is forward declared above but used as return type in insertSubTreeNode methods.
1719#include "auto_apms_behavior_tree_core/node/node_model_type.hpp"
Data structure for information about which behavior tree node plugin to load and how to configure the...
Abstract base for all automatically generated behavior tree node model classes to be used in interact...
Class that encapsulates behavior tree script expressions.
Definition script.hpp:30
Forward iterator for traversing the first-level children of a node.
ChildIterator & operator++()
Pre-increment: advance to the next sibling element.
value_type operator*() const
Dereference the iterator to obtain a NodeElement handle for the current child.
Handle for a single node of a TreeDocument.
std::enable_if_t< std::is_base_of_v< NodeModelType, T >, T > getFirstNode(const std::string &instance_name="", bool deep_search=false) const
Get the first node with a particular instance name.
const TreeDocument & getParentDocument() const
Get a const view of this node's parent tree document.
const std::vector< std::string > & getPortNames() const
Get the names of the data ports implemented by the node represented by this element.
virtual std::string getRegistrationName() const
Get the name of this node given during registration representing its dynamic type.
XMLElement * getXMLElement()
Get a pointer to the underlying tinyxml2::XMLElement of this node.
std::function< bool(const NodeElement &)> ConstDeepApplyCallback
Callback invoked for every node found under another node. It cannot modify the current node.
NodeElement & resetPorts()
Delete all currently specified port values and reset with the defaults.
NodeElement(const NodeElement &ele)=default
Copy constructor for a node element.
std::function< bool(NodeElement &)> DeepApplyCallback
Callback invoked for every node found under another node. It may modify the current node.
NodeElement & setPorts(const PortValues &port_values)
Populate the the node's data ports.
std::enable_if_t< std::is_same_v< model::SubTree, SubTreeT >, model::SubTree > insertNode(const TreeElement &tree, const NodeElement *before_this=nullptr)
Add a subtree node for a specific tree element to the children of this node.
virtual NodeElement & setName(const std::string &instance_name)
Assign a name for this specific node instance.
NodeElement insertTreeFromResource(const TreeResource &resource, const std::string &tree_name, const NodeElement *before_this=nullptr, bool auto_register_nodes=true)
Concatenate a tree from one of the installed package's behavior tree resources and add its first chil...
NodeElement(TreeDocument *doc_ptr, XMLElement *ele_ptr)
Protected constructor intended for internal use only.
NodeElement insertTreeFromDocument(const TreeDocument &doc, const std::string &tree_name, const NodeElement *before_this=nullptr, bool auto_register_nodes=true)
Concatenate a tree from a document and add its first child node to the children of this node.
std::map< std::string, std::string > PortValues
Mapping of port names and its respective value encoded as string.
const std::vector< NodeElement > deepApplyConst(ConstDeepApplyCallback apply_callback) const
Recursively apply a callback to this node's children.
TreeDocument * doc_ptr_
Pointer to the tree document that created the tree this node belongs to.
NodeElement & setInlineRegistrationOptions()
Encode the registration options for this node element based on the currently registered node manifest...
ChildIterator end() const
Get a past-the-end iterator for the children of this node.
virtual std::string getName() const
Get the name of this node given to this specific instance by the developer.
PortValues getPorts() const
Assemble the values given to each data port implemented by this node.
NodeElement & setConditionalScript(BT::PreCond type, const Script &script)
Specify a script that is evaluated before this node is ticked.
NodeElement insertTreeFromString(const std::string &tree_str, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from a document created from a string and add its first child node to the children...
std::enable_if_t< std::is_base_of_v< NodeModelType, T >, NodeElement & > removeFirstChild(const std::string &instance_name="", bool deep_search=false)
Remove the first node with a particular instance name.
tinyxml2::XMLElement * ele_ptr_
Pointer to the corresponding XMLElement of the base document.
std::string getFullyQualifiedName() const
Create a string that uniquely identifies this node considering its registration and its instance name...
model::SubTree insertSubTreeNode(const std::string &tree_name, const NodeElement *before_this=nullptr)
Add a subtree node for a specific tree element to the children of this node.
std::optional< NodeRegistrationOptions > getInlineRegistrationOptions() const
Extract the node registration options by parsing the attributes of this node element.
bool hasChildren() const
Determine whether any children have been given to this node.
NodeElement insertTree(const TreeElement &tree, const NodeElement *before_this=nullptr, bool auto_register_nodes=true)
Concatenate a tree and add its first child node to the children of this node.
std::enable_if_t< std::is_same_v< model::SubTree, SubTreeT >, model::SubTree > insertNode(const std::string &tree_name, const NodeElement *before_this=nullptr)
Add a subtree node for a specific tree element to the children of this node.
bool operator==(const NodeElement &other) const
Determine if two node elements refer to the same node.
NodeElement getFirstNode(const std::string &registration_name="", const std::string &instance_name="", bool deep_search=false) const
Get the first node with a particular registration and instance name.
ChildIterator begin() const
Get an iterator to the first child of this node.
NodeElement insertNode(const std::string &name, const NodeElement *before_this=nullptr)
Add a new node to the children of this node.
NodeElement & removeFirstChild(const std::string &registration_name="", const std::string &instance_name="", bool deep_search=false)
Remove the first node with a particular registration and instance name.
NodeElement insertTreeFromFile(const std::string &path, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from a document created from a file and add its first child node to the children o...
NodeElement & removeChildren()
Recursively remove all children of this node element.
NodeElement & operator=(const NodeElement &other)
Replace this node with another.
std::vector< NodeElement > deepApply(DeepApplyCallback apply_callback)
Recursively apply a callback to this node's children.
Handle for a single behavior tree of a TreeDocument.
TreeElement & removeFirstChild(const std::string &registration_name="", const std::string &instance_name="", bool deep_search=false)
TreeElement(TreeDocument *doc_ptr, XMLElement *ele_ptr)
Protected constructor intended to be used only by certain factory methods of TreeDocument.
std::string getName() const override
Get the name of the behavior tree.
std::string writeToString(bool remove_whitespace=false) const
Write this behavior tree to an XML encoded in a string.
BT::Result verify() const
Verify that this behavior tree is structured correctly and can be created successfully.
NodeManifest getRequiredNodeManifest() const
Assemble the node manifest that is required for successfully creating an instance of this tree.
TreeElement & makeRoot()
Set this behavior tree as the root tree of the parent document.
TreeElement & operator=(const TreeElement &other)
Replace the behavior tree represented by this element with another.
TreeElement & setInlineRegistrationOptions()
Encode the registration options for all nodes in this tree based on the currently registered node man...
TreeElement & setName(const std::string &tree_name) override
Set the name of the behavior tree.
TreeElement(const TreeElement &ele)=default
Copy constructor for a tree element.
std::enable_if_t< std::is_base_of_v< NodeModelType, T >, TreeElement & > removeFirstChild(const std::string &instance_name="", bool deep_search=false)
TreeElement getRootTree()
Get the corresponding behavior tree element for the root tree of this document.
std::string getRootTreeName() const
Get the name of this document's root tree.
TreeDocument & mergeTreeDocument(const XMLDocument &other, bool adopt_root_tree=false)
Merge another tree document with this one.
TreeDocument & mergeString(const std::string &tree_str, bool adopt_root_tree=false)
Create a tree document from a string and merge it with this one.
TreeElement newTreeFromFile(const std::string &path, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one found inside the XML file.
TreeDocument & applyNodeNamespace(const std::string &node_namespace, const std::string &sep=_AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP)
Prepend a namespace to all nodes associated with this document.
std::string writeToString(bool remove_whitespace=false) const
Write the XML of this tree document to a string.
BT::Result verify() const
Verify that all behavior trees of this document are structured correctly and can be created successfu...
std::set< std::string > getRegisteredNodeNames(bool include_native=true) const
Get the names of all nodes that are known to this document.
TreeDocument & mergeResource(const TreeResource &resource, bool adopt_root_tree=false, bool auto_register_nodes=true)
Merge the behavior trees from one of the installed package's behavior tree resources.
TreeDocument & reset(bool unregister_nodes=true)
Clear this document and reset it to its initial state.
TreeElement newTreeFromString(const std::string &tree_str, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one found inside the XML string.
void writeToFile(const std::string &path, bool remove_whitespace=false) const
Write the XML of this tree document to a file.
virtual TreeDocument & registerNodes(const NodeManifest &node_manifest, bool override=false)
Load behavior tree node plugins and register them with the internal behavior tree factory.
TreeElement newTreeFromDocument(const TreeDocument &other, const std::string &tree_name="", bool auto_register_nodes=true)
Create a new behavior tree inside this document with the content of one found inside another tree doc...
TreeElement getTree(const std::string &tree_name)
Get the corresponding behavior tree element for a tree inside this document.
TreeDocument & setInlineRegistrationOptions(const std::string &tree_name="")
Encode the registration options for all nodes in this document based on the currently registered node...
TreeDocument & setRootTreeName(const std::string &tree_name)
Define the root tree of this document.
bool hasTreeName(const std::string &tree_name) const
Determine if this document contains a behavior tree with a particular name.
TreeDocument & mergeFile(const std::string &path, bool adopt_root_tree=false)
Create a tree document from a file and merge it with this one.
NodeManifest getRequiredNodeManifest(const std::string &tree_name="") const
Assemble the node manifest that is required for successfully creating an instance of any of the docum...
std::vector< std::string > getAllTreeNames() const
Get the names of all behavior trees inside this document.
TreeDocument & addNodeModel(NodeModelMap model_map)
Add a behavior tree node model element to the document by parsing the contents of model_map.
TreeElement newTreeFromResource(const TreeResource &resource, const std::string &tree_name="", bool auto_register_nodes=true)
Create a new behavior tree inside this document with the content of one the trees found inside a part...
TreeDocument & mergeTree(const TreeElement &tree, bool make_root_tree=false, bool auto_register_nodes=true)
Merge an existing behavior tree with this tree document.
bool hasRootTreeName() const
Determine if this document specifies which of its trees is the root tree.
TreeDocument & removeTree(const std::string &tree_name)
Remove a particular behavior tree from this document.
TreeDocument(const std::string &format_version=BTCPP_FORMAT_DEFAULT_VERSION, NodeRegistrationLoader::SharedPtr tree_node_loader=NodeRegistrationLoader::make_shared())
Create a an empty tree document.
static NodeModelMap getNodeModel(tinyxml2::XMLDocument &doc, const NodeManifest &manifest)
Convert a behavior tree node model document to the corresponding data structure.
TreeElement newTree(const std::string &tree_name)
Create a new behavior tree inside this document.
Class containing behavior tree resource data.
Subtree behavior tree node model.
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32
Models for all available behavior tree nodes.
std::map< std::string, NodeModel > NodeModelMap
Mapping of node registration names and their implementation details.
Parameters for loading and registering a behavior tree node class from a shared library using e....