AutoAPMS
Resilient Robot Mission Management
|
This class offers a programmatic approach for building behavior trees and stores the registration data of all tree nodes. More...
#include <auto_apms_behavior_tree_core/tree/tree_document.hpp>
Classes | |
class | NodeElement |
Handle for a single node of a TreeDocument. More... | |
struct | NodeModel |
Data structure encapsulating the information of all ports implemented by a behavior tree node. More... | |
struct | NodePortInfo |
Implementation details of a single data port. More... | |
class | TreeElement |
Handle for a single behavior tree of a TreeDocument. More... | |
Public Types | |
using | NodeModelMap = std::map<std::string, NodeModel> |
Mapping of node registration names and their implementation details. | |
Public Member Functions | |
TreeDocument (const std::string &format_version=BTCPP_FORMAT_DEFAULT_VERSION, NodeRegistrationLoader::SharedPtr tree_node_loader=NodeRegistrationLoader::make_shared()) | |
Create a an empty tree document. | |
TreeDocument & | mergeTreeDocument (const XMLDocument &other, bool adopt_root_tree=false) |
Merge another tree document with this one. | |
TreeDocument & | mergeTreeDocument (const TreeDocument &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. | |
TreeDocument & | mergeFile (const std::string &path, bool adopt_root_tree=false) |
Create a tree document from a file and merge it with this one. | |
TreeDocument & | mergeResource (const TreeResource &resource, bool adopt_root_tree=false) |
Merge the behavior trees from one of the installed package's behavior tree resources. | |
TreeDocument & | mergeTree (const TreeElement &tree, bool make_root_tree=false) |
Merge an existing behavior tree with this tree document. | |
TreeElement | newTree (const std::string &tree_name) |
Create a new behavior tree inside this document. | |
TreeElement | newTree (const TreeElement &other_tree) |
Create a new behavior tree inside this document with the same content of another. | |
TreeElement | newTreeFromDocument (const TreeDocument &other, const std::string &tree_name="") |
Create a new behavior tree inside this document with the content of one found inside another tree document. | |
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. | |
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. | |
TreeElement | newTreeFromResource (const TreeResource &resource, const std::string &tree_name="") |
Create a new behavior tree inside this document with the content of one the trees found inside a particular behavior tree resource. | |
bool | hasTreeName (const std::string &tree_name) const |
Determine if this document specifies a behavior tree with a particular name. | |
TreeElement | getTree (const std::string &tree_name) |
Get the corresponding behavior tree element for a tree inside this document. | |
TreeDocument & | setRootTreeName (const std::string &tree_name) |
Define the root tree of this document. | |
bool | hasRootTreeName () const |
Determine if this document specifies which of its trees is the root tree. | |
std::string | getRootTreeName () const |
Get the name of this document's root tree. | |
TreeElement | getRootTree () |
Get the corresponding behavior tree element for the root tree of this document. | |
TreeDocument & | removeTree (const std::string &tree_name) |
Remove a particular behavior tree from this document. | |
TreeDocument & | removeTree (const TreeElement &tree) |
Remove a particular behavior tree from this document. | |
std::vector< std::string > | getAllTreeNames () const |
Get the names of all behavior trees inside this document. | |
virtual TreeDocument & | registerNodes (const NodeManifest &tree_node_manifest, bool override=false) |
Load behavior tree node plugins and register them with the internal behavior tree factory. | |
std::set< std::string > | getRegisteredNodeNames (bool include_native=true) const |
Get the names of all nodes that are known to this document. | |
NodeManifest | getRequiredNodeManifest () const |
Assemble the node manifest that is required for successfully creating an instance of any of the document's trees. | |
TreeDocument & | addNodeModel (bool include_native=false) |
Add an behavior tree node model element to the document. | |
NodeModelMap | getNodeModel (bool include_native=false) const |
Create a behavior tree node model for all nodes registered with this document. | |
BT::Result | verify () const |
Verify that all behavior trees of this document are structured correctly and can be created successfully. | |
std::string | writeToString () const |
Write the XML of this tree document to a string. | |
void | writeToFile (const std::string &path) const |
Write the XML of this tree document to a file. | |
TreeDocument & | reset () |
Clear this document and reset it to its initial state. | |
Static Public Member Functions | |
static NodeModelMap | getNodeModel (tinyxml2::XMLDocument &doc) |
Convert a behavior tree node model document to the corresponding data structure. | |
This class offers a programmatic approach for building behavior trees and stores the registration data of all tree nodes.
A single tree document may contain multiple behavior trees (represented by TreeElement). Each behavior tree may contain an arbitrary amount of tree nodes (represented by NodeElement). There are various different types of nodes. For each one of the standard nodes, there is a model available under the auto_apms_behavior_tree::model
namespace. Refer to the BehaviorTree.CPP website for more infos on the basic concepts of behavior trees used in this implementation.
A TreeDocument instance must be kept alive for as long as its behavior trees are accessed in any way. When a document instance is destroyed, the memory of the associated behavior trees is deleted. Hence, a TreeDocument acts the same as a tinyxml2::XMLDocument
.
Copy assignment is not supported to enforce the builder design pattern. To copy certain behavior trees from one document to another, the user must explicitly invoke TreeDocument::mergeTreeDocument of another document instance.
The XML schema of a tree document is defined here and looks similar to this:
The content of the document can be configured as desired using its member functions. For example like this:
Alternatively (and preferably), the user may profit from static type validation and more convenience methods by incorporating node models as template arguments:
The former approach can be achieved using the API offered by the auto_apms_behavior_tree_core
package, while the latter requires to build and link against the auto_apms_behavior_tree
package, since the node models are generated automatically by that package.
Definition at line 133 of file tree_document.hpp.
using NodeModelMap = std::map<std::string, NodeModel> |
Mapping of node registration names and their implementation details.
Definition at line 179 of file tree_document.hpp.
TreeDocument | ( | const std::string & | format_version = BTCPP_FORMAT_DEFAULT_VERSION, |
NodeRegistrationLoader::SharedPtr | tree_node_loader = NodeRegistrationLoader::make_shared() ) |
Create a an empty tree document.
You may specify a format version. Two tree documents must have the same format version for them to be mergeable.
Additionally, you may pass a pointer to an already existing behavior tree node loader. By default, a new one is created with default arguments.
format_version | Format version number encoded in a string. |
tree_node_loader | Shared pointer to an existing behavior tree node loader. |
Definition at line 598 of file tree_document.cpp.
TreeDocument & mergeTreeDocument | ( | const XMLDocument & | other, |
bool | adopt_root_tree = false ) |
Merge another tree document with this one.
This function copies all behavior trees of other
and adds them to this document. The other document must either have a root element <root>
that may contain an arbitrary amount of <BehaviorTree>
child elements, or a single <BehaviorTree>
element as its root element.
If adopt_root_tree
is true
and there is only a single behavior tree, this tree will be considered the new root tree of this document. However, if there are multiple behavior trees under a top-level <root>
element and this element has the optional main_tree_to_execute
attribute specifying the root tree name, the value of this attribute will be copied to this document. If the other root element doesn't have this attribute, the current root tree name is kept.
other | Low-level XML document containing the trees to be merged. |
adopt_root_tree | Set to true if you additionally want to update this document's root tree name according to other . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the tree names found in other are already taken by this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the format of other is not the same as the format of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the trees inside other are malformed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the content of other cannot be interpreted. |
Definition at line 614 of file tree_document.cpp.
TreeDocument & mergeTreeDocument | ( | const TreeDocument & | other, |
bool | adopt_root_tree = false ) |
Merge another tree document with this one.
This function copies all behavior trees of other
and adds them to this document.
If adopt_root_tree
is true
and there is only a single behavior tree, this tree will be considered the new root tree of this document. However, if there are multiple behavior trees, the root tree name of the other document will be copied to this document. If the other document doesn't specify which tree is the root tree, the current one is kept.
other | Other tree document containing the trees to be merged. |
adopt_root_tree | Set to true if you additionally want to update this document's root tree name according to other . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the tree names found in other are already taken by this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the format of other is not the same as the format of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the trees inside other are malformed. |
Definition at line 705 of file tree_document.cpp.
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.
This function copies all behavior trees found inside tree_str
and adds them to this document. The XML string must either have a root element <root>
that may contain an arbitrary amount of <BehaviorTree>
child elements, or a single <BehaviorTree>
element as its root element.
If adopt_root_tree
is true
and there is only a single behavior tree, this tree will be considered the new root tree of this document. However, if there are multiple behavior trees under a top-level <root>
element and this element has the optional main_tree_to_execute
attribute specifying the root tree name, the value of this attribute will be copied to this document. If the other root element doesn't have this attribute, the current root tree name is kept.
tree_str | XML string specifying the tree document to be merged. |
adopt_root_tree | Set to true if you additionally want to update this document's root tree name according to tree_str . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the tree names found in tree_str are already taken by this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the format of the XML in tree_str is not the same as the format of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the trees inside tree_str are malformed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the content of tree_str cannot be interpreted. |
Definition at line 711 of file tree_document.cpp.
TreeDocument & mergeFile | ( | const std::string & | path, |
bool | adopt_root_tree = false ) |
Create a tree document from a file and merge it with this one.
This function copies all behavior trees found inside path
and adds them to this document. The XML file must either have a root element <root>
that may contain an arbitrary amount of <BehaviorTree>
child elements, or a single <BehaviorTree>
element as its root element.
If adopt_root_tree
is true
and there is only a single behavior tree, this tree will be considered the new root tree of this document. However, if there are multiple behavior trees under a top-level <root>
element and this element has the optional main_tree_to_execute
attribute specifying the root tree name, the value of this attribute will be copied to this document. If the other root element doesn't have this attribute, the current root tree name is kept.
path | Path to an XML file specifying the tree document to be merged. |
adopt_root_tree | Set to true if you additionally want to update this document's root tree name according to path . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the tree names found under path are already taken by this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the format of the XML in path is not the same as the format of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the trees inside the XML in path are malformed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the content of path cannot be interpreted. |
Definition at line 720 of file tree_document.cpp.
TreeDocument & mergeResource | ( | const TreeResource & | resource, |
bool | adopt_root_tree = false ) |
Merge the behavior trees from one of the installed package's behavior tree resources.
This function parses the XML and the node manifest associated with resource
. First, it registers all behavior tree nodes specified by the manifest with this document and then merges the tree document created from the resource's XML file.
If adopt_root_tree
is true
and there is only a single behavior tree, this tree will be considered the new root tree of this document. However, if there are multiple behavior trees, the root tree name of the other document will be copied to this document. If the other document doesn't specify which one is the root tree, the current one is kept.
resource | Behavior tree resource that specifies the tree document to be merged. |
adopt_root_tree | Set to true if you additionally want to update this document's root tree name according to resource . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the tree names found under resource are already taken by this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the format of the XML associated with resource is not the same as the format of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if any of the trees associated with resource are malformed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the content of the XML associated with resource cannot be interpreted. |
Definition at line 729 of file tree_document.cpp.
TreeDocument & mergeTree | ( | const TreeElement & | tree, |
bool | make_root_tree = false ) |
Merge an existing behavior tree with this tree document.
Before merging the behavior tree represented by tree
, all required nodes are automatically registered with this document.
This function is intended for copying a behavior tree from another tree document. If you call this with a tree element that was created using this document or the other tree has the same name as one of the existing trees, an error is raised since a document's tree names must be unique.
tree | Tree element representing the tree to merge. |
make_root_tree | Set to true if you want to make tree the new root tree of this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the tree's name already exists inside this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the tree is malformed. |
Definition at line 735 of file tree_document.cpp.
TreeDocument::TreeElement newTree | ( | const std::string & | tree_name | ) |
Create a new behavior tree inside this document.
tree_name | Name of the behavior tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if a tree with name tree_name already exists inside this document. |
Definition at line 744 of file tree_document.cpp.
TreeDocument::TreeElement newTree | ( | const TreeElement & | other_tree | ) |
Create a new behavior tree inside this document with the same content of another.
Before creating a new tree using other_tree
, all required nodes are automatically registered with this document.
This function is intended for copying a behavior tree from another tree document. If you call this with a tree element that was created using this document or the other tree has the same name as one of the existing trees, an error is raised since a document's tree names must be unique.
other_tree | Other behavior tree to be copied. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if other_tree has the same name as one of this document's trees. |
Definition at line 763 of file tree_document.cpp.
TreeDocument::TreeElement newTreeFromDocument | ( | const TreeDocument & | other, |
const std::string & | tree_name = "" ) |
Create a new behavior tree inside this document with the content of one found inside another tree document.
Which behavior tree will be used to create the new tree is determined by tree_name
. By default, this argument is empty, which means that the root tree will be used. If other
only contains a single tree, this one is considered the root tree.
other | Other tree document that contains the tree to be copied. |
tree_name | Name of the tree to be copied. It must exist inside other . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name is empty but other doesn't specify which tree is the root tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name cannot be found in other . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if a tree with name tree_name already exists inside this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the respective tree contains any nodes that are not available with the currently configured node manifest. |
Definition at line 768 of file tree_document.cpp.
TreeDocument::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.
Which behavior tree will be used to create the new tree is determined by tree_name
. By default, this argument is empty, which means that the root tree will be used. If tree_str
only contains a single tree, this one is considered the root tree.
tree_str | String specifying the XML of a tree document that holds the tree to be copied. |
tree_name | Name of the tree to be copied. It must exist inside tree_str . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name is empty but tree_str doesn't specify which tree is the root tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name cannot be found in tree_str . |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if a tree with name tree_name already exists inside this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the respective tree contains any nodes that are not available with the currently configured node manifest. |
Definition at line 787 of file tree_document.cpp.
TreeDocument::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.
Which behavior tree will be used to create the new tree is determined by tree_name
. By default, this argument is empty, which means that the root tree will be used. If the file under path
only contains a single tree, this one is considered the root tree.
path | Path to an XML file specifying the tree document that holds the tree to be copied. |
tree_name | Name of the tree to be copied. It must exist inside the given file. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name is empty but the given file doesn't specify which tree is the root tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name cannot be found in the given file. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if a tree with name tree_name already exists inside this document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if the respective tree contains any nodes that are not available with the currently configured node manifest. |
Definition at line 794 of file tree_document.cpp.
TreeDocument::TreeElement newTreeFromResource | ( | const TreeResource & | resource, |
const std::string & | tree_name = "" ) |
Create a new behavior tree inside this document with the content of one the trees found inside a particular behavior tree resource.
Before creating a new tree using resource
, all required nodes are automatically registered with this document.
Which behavior tree will be used to create the new tree is determined by tree_name
. By default, this argument is empty, which means that the root tree will be used. If the given resource only contains a single tree, this one is considered the root tree. Another way to define the root tree with this function is to specify the <tree_name>
token when creating the resource object with an identity string. However, the tree_name
argument takes precedence over this token.
resource | Behavior tree resource that specifies the tree to be copied. |
tree_name | Name of the tree to be copied. It must exist inside the given file. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name is empty but the root tree cannot be determined. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree_name cannot be found in the resource's tree document. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if a tree with name tree_name already exists inside this document. |
Definition at line 801 of file tree_document.cpp.
bool hasTreeName | ( | const std::string & | tree_name | ) | const |
Determine if this document specifies a behavior tree with a particular name.
tree_name | Name of the tree. |
true
if a tree named tree_name
exists in this document, false
otherwise. Definition at line 811 of file tree_document.cpp.
TreeDocument::TreeElement getTree | ( | const std::string & | tree_name | ) |
Get the corresponding behavior tree element for a tree inside this document.
tree_name | Name of the tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if no tree named tree_name exists inside this document. |
Definition at line 817 of file tree_document.cpp.
TreeDocument & setRootTreeName | ( | const std::string & | tree_name | ) |
Define the root tree of this document.
tree_name | Name of an existing tree to be considered the root tree. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if no tree named tree_name exists inside this document. |
Definition at line 822 of file tree_document.cpp.
bool hasRootTreeName | ( | ) | const |
Determine if this document specifies which of its trees is the root tree.
true
if this document specifies a root tree, false
otherwise. Definition at line 835 of file tree_document.cpp.
std::string getRootTreeName | ( | ) | const |
Get the name of this document's root tree.
auto_apms_behavior_tree::exceptions::TreeDocumentError | if this document doesn't specify which tree is the root tree. |
Definition at line 841 of file tree_document.cpp.
TreeDocument::TreeElement getRootTree | ( | ) |
Get the corresponding behavior tree element for the root tree of this document.
auto_apms_behavior_tree::exceptions::TreeDocumentError | if this document doesn't specify which tree is the root tree. |
Definition at line 849 of file tree_document.cpp.
TreeDocument & removeTree | ( | const std::string & | tree_name | ) |
Remove a particular behavior tree from this document.
We only remove the tree and it's child nodes, but not the corresponding node registrations with the behavior tree factory, since this just means more work and is really unnecessary, especially if you consider that if we remove any node registration now and insert it again later somewhere inside the document, we would need to register the node again. Since we assume that the amount of memory is not an issue (and the callbacks that we store when registering don't need much anyways), it would just be extra work.
tree_name | Name of the tree to be removed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if no tree named tree_name exists inside this document. |
Definition at line 851 of file tree_document.cpp.
TreeDocument & removeTree | ( | const TreeElement & | tree | ) |
Remove a particular behavior tree from this document.
We only remove the tree and it's child nodes, but not the corresponding node registrations with the behavior tree factory, since this just means more work and is really unnecessary, especially if you consider that if we remove any node registration now and insert it again later somewhere inside the document, we would need to register the node again. Since we assume that the amount of memory is not an issue (and the callbacks that we store when registering don't need much anyways), it would just be extra work.
tree | Tree element representing the tree to be removed. |
auto_apms_behavior_tree::exceptions::TreeDocumentError | if tree does not belong to this document. |
Definition at line 857 of file tree_document.cpp.
std::vector< std::string > getAllTreeNames | ( | ) | const |
Get the names of all behavior trees inside this document.
Definition at line 859 of file tree_document.cpp.
|
virtual |
Load behavior tree node plugins and register them with the internal behavior tree factory.
This makes it possible to add any nodes specified in tree_node_manifest
to the tree.
tree_node_manifest | Parameters for locating and configuring the behavior tree node plugins. |
override | If tree_node_manifest specifies nodes that have already been registered, unregister the existing plugin and use the new one instead. |
auto_apms_behavior_tree::exceptions::NodeRegistrationError | if registration fails. |
Reimplemented in TreeBuilder.
Definition at line 861 of file tree_document.cpp.
std::set< std::string > getRegisteredNodeNames | ( | bool | include_native = true | ) | const |
Get the names of all nodes that are known to this document.
include_native | Set to true if the native BehaviorTree.CPP nodes should be included, false to only consider node plugins. |
Definition at line 950 of file tree_document.cpp.
NodeManifest getRequiredNodeManifest | ( | ) | const |
Assemble the node manifest that is required for successfully creating an instance of any of the document's trees.
auto_apms_behavior_tree::exceptions::NodeManifestError | if there are no registration options for a specific node. |
Definition at line 958 of file tree_document.cpp.
TreeDocument & addNodeModel | ( | bool | include_native = false | ) |
Add an behavior tree node model element to the document.
This is required when using the Groot2 visual editor.
include_native | Set to true if the native BehaviorTree.CPP nodes should be included, false to only consider registered node plugins. |
Definition at line 970 of file tree_document.cpp.
|
static |
Convert a behavior tree node model document to the corresponding data structure.
doc | XML document containing the node model. |
doc
. Definition at line 993 of file tree_document.cpp.
TreeDocument::NodeModelMap getNodeModel | ( | bool | include_native = false | ) | const |
Create a behavior tree node model for all nodes registered with this document.
include_native | Set to true if the native BehaviorTree.CPP nodes should be included, false to only consider registered node plugins. |
Definition at line 1062 of file tree_document.cpp.
BT::Result verify | ( | ) | const |
Verify that all behavior trees of this document are structured correctly and can be created successfully.
true
if verification succeeded. Otherwise, it encapsulates a reason why it failed. Definition at line 1073 of file tree_document.cpp.
std::string writeToString | ( | ) | const |
Write the XML of this tree document to a string.
Definition at line 1088 of file tree_document.cpp.
void writeToFile | ( | const std::string & | path | ) | const |
Write the XML of this tree document to a file.
path | Path to the output file. |
Definition at line 1095 of file tree_document.cpp.
TreeDocument & reset | ( | ) |
Clear this document and reset it to its initial state.
Definition at line 1106 of file tree_document.cpp.