AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
node_model_type.cpp
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#include "auto_apms_behavior_tree_core/node/node_model_type.hpp"
16
17#include "auto_apms_behavior_tree_core/builder.hpp"
18#include "auto_apms_behavior_tree_core/exceptions.hpp"
19#include "behaviortree_cpp/tree_node.h"
20
22{
23namespace core
24{
25
27
28} // namespace core
29
30namespace model
31{
32
33BT::NodeType SubTree::type() { return BT::NodeType::SUBTREE; }
34
35std::string SubTree::name() { return core::TreeDocument::SUBTREE_ELEMENT_NAME; }
36
37std::string SubTree::getRegistrationName() const { return name(); }
38
39SubTree & SubTree::setBlackboardRemapping(const std::map<std::string, std::string> & remapping)
40{
41 for (const auto & [key, val] : remapping) {
42 if (!BT::TreeNode::isBlackboardPointer(val)) {
43 throw exceptions::TreeDocumentError(
44 "When setting the blackboard remapping for a subtree, you must refer to the parent blackboard's entry to remap "
45 "to using curly brackets (Got: '" +
46 val + "').");
47 }
48 ele_ptr_->SetAttribute(key.c_str(), val.c_str());
49 }
50 return *this;
51}
52
53std::map<std::string, std::string> SubTree::getBlackboardRemapping() const
54{
55 std::map<std::string, std::string> remapping;
56 for (const tinyxml2::XMLAttribute * attr = ele_ptr_->FirstAttribute(); attr != nullptr; attr = attr->Next()) {
57 if (BT::TreeNode::isBlackboardPointer(attr->Value())) {
58 remapping[attr->Name()] = attr->Value();
59 }
60 }
61 return remapping;
62}
63
64SubTree & SubTree::set_auto_remap(bool val) { return setPorts({{"_autoremap", BT::toStr(val)}}); }
65
66bool SubTree::get_auto_remap() const { return BT::convertFromString<bool>(getPorts().at("_autoremap")); }
67
68} // namespace model
69} // namespace auto_apms_behavior_tree
NodeElement toNodeElement()
Convert to a lower level NodeElement object.
Handle for a single node of a TreeDocument.
NodeElement & setPorts(const PortValues &port_values)
Populate the the node's data ports.
NodeElement(TreeDocument *doc_ptr, XMLElement *ele_ptr)
Protected constructor intended for internal use only.
PortValues getPorts() const
Assemble the values given to each data port implemented by this node.
tinyxml2::XMLElement * ele_ptr_
Pointer to the corresponding XMLElement of the base document.
Subtree behavior tree node model.
static BT::NodeType type()
Type of the behavior tree node.
bool get_auto_remap() const
Get automatic blackboard remapping.
SubTree & setBlackboardRemapping(const std::map< std::string, std::string > &remapping)
Configure which blackboard entries of the subtree node's parent tree should be also available for the...
std::string getRegistrationName() const override final
Get the type specific name under which all subtree nodes are registered with the behavior tree factor...
std::map< std::string, std::string > getBlackboardRemapping() const
Get the currently configured blackboard remapping.
SubTree & set_auto_remap(bool val=false)
Set automatic blackboard remapping.
static std::string name()
Static method that provides the hard coded registration name of subtree nodes.
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32
Models for all available behavior tree nodes.
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32