switch_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2020-2022 Davide Faconti - 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 #pragma once
14 
16 
17 namespace BT
18 {
42 namespace details
43 {
44 
45 bool CheckStringEquality(const std::string& v1, const std::string& v2,
46  const ScriptingEnumsRegistry* enums);
47 }
48 
49 template <size_t NUM_CASES>
50 class SwitchNode : public ControlNode
51 {
52 public:
53  SwitchNode(const std::string& name, const BT::NodeConfig& config);
54 
55  virtual ~SwitchNode() override = default;
56 
57  void halt() override;
58 
59  static PortsList providedPorts();
60 
61 private:
63  std::vector<std::string> case_keys_;
64  virtual BT::NodeStatus tick() override;
65 };
66 
67 //-----------------------------------------------
68 //-----------------------------------------------
69 
70 template <size_t NUM_CASES>
71 inline SwitchNode<NUM_CASES>::SwitchNode(const std::string& name,
72  const NodeConfig& config)
73  : ControlNode::ControlNode(name, config), running_child_(-1)
74 {
75  setRegistrationID("Switch");
76  for(unsigned i = 1; i <= NUM_CASES; i++)
77  {
78  case_keys_.push_back(std::string("case_") + std::to_string(i));
79  }
80 }
81 
82 template <size_t NUM_CASES>
84 {
85  running_child_ = -1;
87 }
88 
89 template <size_t NUM_CASES>
91 {
92  static PortsList provided_ports = []() {
93  PortsList ports;
94  ports.insert(BT::InputPort<std::string>("variable"));
95  for(unsigned i = 1; i <= NUM_CASES; i++)
96  {
97  auto key = std::string("case_") + std::to_string(i);
98  ports.insert(BT::InputPort<std::string>(key));
99  }
100  return ports;
101  }();
102 
103  return provided_ports;
104 }
105 
106 template <size_t NUM_CASES>
108 {
109  if(childrenCount() != NUM_CASES + 1)
110  {
111  throw LogicError("Wrong number of children in SwitchNode; "
112  "must be (num_cases + default)");
113  }
114 
115  std::string variable;
116  std::string value;
117  int match_index = int(NUM_CASES); // default index;
118 
119  // no variable? jump to default
120  if(getInput("variable", variable))
121  {
122  // check each case until you find a match
123  for(int index = 0; index < int(NUM_CASES); ++index)
124  {
125  const std::string& case_key = case_keys_[index];
126  if(getInput(case_key, value))
127  {
128  if(details::CheckStringEquality(variable, value, this->config().enums.get()))
129  {
130  match_index = index;
131  break;
132  }
133  }
134  }
135  }
136 
137  // if another one was running earlier, halt it
138  if(running_child_ != -1 && running_child_ != match_index)
139  {
140  haltChild(running_child_);
141  }
142 
143  auto& selected_child = children_nodes_[match_index];
144  NodeStatus ret = selected_child->executeTick();
145  if(ret == NodeStatus::SKIPPED)
146  {
147  // if the matching child is SKIPPED, should I jump to default or
148  // be SKIPPED myself? Going with the former, for the time being.
149  running_child_ = -1;
150  return NodeStatus::SKIPPED;
151  }
152  else if(ret == NodeStatus::RUNNING)
153  {
154  running_child_ = match_index;
155  }
156  else
157  {
158  resetChildren();
159  running_child_ = -1;
160  }
161  return ret;
162 }
163 
164 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::SwitchNode::halt
void halt() override
Definition: switch_node.h:83
BT::details::CheckStringEquality
bool CheckStringEquality(const std::string &v1, const std::string &v2, const ScriptingEnumsRegistry *enums)
Definition: switch_node.cpp:22
BT::LogicError
Definition: exceptions.h:45
BT::SwitchNode::running_child_
int running_child_
Definition: switch_node.h:62
BT::SwitchNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: switch_node.h:107
BT::SwitchNode::~SwitchNode
virtual ~SwitchNode() override=default
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::SwitchNode::providedPorts
static PortsList providedPorts()
Definition: switch_node.h:90
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::ScriptingEnumsRegistry
std::unordered_map< std::string, int > ScriptingEnumsRegistry
Definition: tree_node.h:71
to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:24456
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
control_node.h
BT::SwitchNode
Definition: switch_node.h:50
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::RUNNING
@ RUNNING
BT::SwitchNode::SwitchNode
SwitchNode(const std::string &name, const BT::NodeConfig &config)
Definition: switch_node.h:71
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::NodeConfig
Definition: tree_node.h:73
BT::ControlNode
Definition: control_node.h:21
BT::SwitchNode::case_keys_
std::vector< std::string > case_keys_
Definition: switch_node.h:63
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


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