switch_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 #ifndef SWITCH_NODE_H
14 #define SWITCH_NODE_H
15 
17 
18 namespace BT
19 {
43 template <size_t NUM_CASES>
44 class SwitchNode : public ControlNode
45 {
46  public:
47  SwitchNode(const std::string& name, const BT::NodeConfiguration& config)
48  : ControlNode::ControlNode(name, config ),
49  running_child_(-1)
50  {
51  setRegistrationID("Switch");
52  }
53 
54  virtual ~SwitchNode() override = default;
55 
56  void halt() override
57  {
58  running_child_ = -1;
60  }
61 
63  {
64  PortsList ports;
65  ports.insert( BT::InputPort<std::string>("variable") );
66  for(unsigned i=0; i < NUM_CASES; i++)
67  {
68  char case_str[20];
69  sprintf(case_str, "case_%d", i+1);
70  ports.insert( BT::InputPort<std::string>(case_str) );
71  }
72  return ports;
73  }
74 
75  private:
77  virtual BT::NodeStatus tick() override;
78 };
79 
80 template<size_t NUM_CASES> inline
82 {
83  constexpr const char * case_port_names[9] = {
84  "case_1", "case_2", "case_3", "case_4", "case_5", "case_6", "case_7", "case_8", "case_9"};
85 
86  if( childrenCount() != NUM_CASES+1)
87  {
88  throw LogicError("Wrong number of children in SwitchNode; "
89  "must be (num_cases + default)");
90  }
91 
92  std::string variable;
93  std::string value;
94  int child_index = NUM_CASES; // default index;
95 
96  if (getInput("variable", variable)) // no variable? jump to default
97  {
98  // check each case until you find a match
99  for (unsigned index = 0; index < NUM_CASES; ++index)
100  {
101  bool found = false;
102  if( index < 9 )
103  {
104  found = (bool)getInput(case_port_names[index], value);
105  }
106  else{
107  char case_str[20];
108  sprintf(case_str, "case_%d", index+1);
109  found = (bool)getInput(case_str, value);
110  }
111 
112  if (found && variable == value)
113  {
114  child_index = index;
115  break;
116  }
117  }
118  }
119 
120  // if another one was running earlier, halt it
121  if( running_child_ != -1 && running_child_ != child_index)
122  {
124  }
125 
126  auto& selected_child = children_nodes_[child_index];
127  NodeStatus ret = selected_child->executeTick();
128  if( ret == NodeStatus::RUNNING )
129  {
130  running_child_ = child_index;
131  }
132  else{
133  haltChildren();
134  running_child_ = -1;
135  }
136  return ret;
137 }
138 
139 }
140 
141 #endif // SWITCH_NODE_H
size_t childrenCount() const
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:25
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
virtual void halt() override
SwitchNode(const std::string &name, const BT::NodeConfiguration &config)
Definition: switch_node.h:47
void halt() override
Definition: switch_node.h:56
The SwitchNode is equivalent to a switch statement, where a certain branch (child) is executed accord...
Definition: switch_node.h:44
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: switch_node.h:81
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:192
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:317
static PortsList providedPorts()
Definition: switch_node.h:62
virtual ~SwitchNode() override=default
void setRegistrationID(StringView ID)
Definition: tree_node.h:163
void haltChild(size_t i)
NodeStatus
Definition: basic_types.h:35


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25