set_blackboard_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #ifndef ACTION_SETBLACKBOARD_NODE_H
14 #define ACTION_SETBLACKBOARD_NODE_H
15 
17 
18 namespace BT
19 {
37 {
38 public:
39  SetBlackboardNode(const std::string& name, const NodeConfig& config)
41  {
42  setRegistrationID("SetBlackboard");
43  }
44 
46  {
47  return { InputPort("value", "Value to be written int othe output_key"),
48  BidirectionalPort("output_key", "Name of the blackboard entry where the "
49  "value should be written") };
50  }
51 
52 private:
53  virtual BT::NodeStatus tick() override
54  {
55  std::string output_key;
56  if(!getInput("output_key", output_key))
57  {
58  throw RuntimeError("missing port [output_key]");
59  }
60 
61  const std::string value_str = config().input_ports.at("value");
62 
63  StringView stripped_key;
64  if(isBlackboardPointer(value_str, &stripped_key))
65  {
66  const auto input_key = std::string(stripped_key);
67  std::shared_ptr<Blackboard::Entry> src_entry =
68  config().blackboard->getEntry(input_key);
69  std::shared_ptr<Blackboard::Entry> dst_entry =
70  config().blackboard->getEntry(output_key);
71 
72  if(!src_entry)
73  {
74  throw RuntimeError("Can't find the port referred by [value]");
75  }
76  if(!dst_entry)
77  {
78  config().blackboard->createEntry(output_key, src_entry->info);
79  dst_entry = config().blackboard->getEntry(output_key);
80  }
81  dst_entry->value = src_entry->value;
82  }
83  else
84  {
85  config().blackboard->set(output_key, value_str);
86  }
87 
88  return NodeStatus::SUCCESS;
89  }
90 };
91 } // namespace BT
92 
93 #endif
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
BT::SetBlackboardNode::SetBlackboardNode
SetBlackboardNode(const std::string &name, const NodeConfig &config)
Definition: set_blackboard_node.h:39
BT::InputPort
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
Definition: basic_types.h:470
BT::NodeConfig::input_ports
PortsRemapping input_ports
Definition: tree_node.h:83
BT::SetBlackboardNode
The SetBlackboard is action used to store a string into an entry of the Blackboard specified in "outp...
Definition: set_blackboard_node.h:36
BT::BidirectionalPort
std::pair< std::string, PortInfo > BidirectionalPort(StringView name, StringView description={})
Definition: basic_types.h:494
BT::NodeConfig::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:79
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::SetBlackboardNode::providedPorts
static PortsList providedPorts()
Definition: set_blackboard_node.h:45
BT::SetBlackboardNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: set_blackboard_node.h:53
action_node.h
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeConfig
Definition: tree_node.h:73
BT::TreeNode::isBlackboardPointer
static bool isBlackboardPointer(StringView str, StringView *stripped_pointer=nullptr)
Check a string and return true if it matches the pattern: {...}.
Definition: tree_node.cpp:375
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17