blackboard_precondition.h
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 DECORATOR_BLACKBOARD_PRECONDITION_NODE_H
14 #define DECORATOR_BLACKBOARD_PRECONDITION_NODE_H
15 
17 
18 namespace BT
19 {
20 template <typename T>
22 {
23  public:
24  BlackboardPreconditionNode(const std::string& name, const NodeParameters& params)
25  : DecoratorNode(name, params)
26  {
27  if( std::is_same<T,int>::value)
28  setRegistrationName("BlackboardCheckInt");
29  else if( std::is_same<T,double>::value)
30  setRegistrationName("BlackboardCheckDouble");
31  else if( std::is_same<T,std::string>::value)
32  setRegistrationName("BlackboardCheckString");
33  }
34 
35  virtual ~BlackboardPreconditionNode() override = default;
36 
38  {
39  static NodeParameters params = {{"key", ""}, {"expected", "*"}};
40  return params;
41  }
42 
43  private:
44  virtual BT::NodeStatus tick() override;
45 };
46 
47 //----------------------------------------------------
48 
49 template<typename T> inline
51 {
52  std::string key;
53  T expected_value;
54  T current_value;
55 
56  getParam("key", key);
58 
59  // check if the key is present in the blackboard
60  if ( !blackboard() || !(blackboard()->contains(key)) )
61  {
62  return NodeStatus::FAILURE;
63  }
64 
65  if( initializationParameters().at("expected") == "*" )
66  {
67  return child_node_->executeTick();
68  }
69 
70  bool same = ( getParam("expected", expected_value) &&
71  blackboard()->get(key, current_value) &&
72  current_value == expected_value ) ;
73  if(same)
74  {
75  return child_node_->executeTick();
76  }
77  else{
78  if( child()->status() == NodeStatus::RUNNING)
79  {
80  haltChild();
81  }
82  return NodeStatus::FAILURE;
83  }
84 
85 }
86 
87 }
88 
89 #endif
BT::optional< T > getParam(const std::string &key) const
Definition: tree_node.h:117
virtual ~BlackboardPreconditionNode() override=default
const TreeNode * child() const
const std::string & name() const
Definition: tree_node.cpp:92
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
const Blackboard::Ptr & blackboard() const
Definition: tree_node.cpp:64
void setRegistrationName(const std::string &registration_name)
registrationName() is set by the BehaviorTreeFactory
Definition: tree_node.cpp:113
const NodeParameters & initializationParameters() const
Definition: tree_node.cpp:137
BlackboardPreconditionNode(const std::string &name, const NodeParameters &params)
static const NodeParameters & requiredNodeParameters()
TreeNode * child_node_
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
NodeStatus status() const
Definition: tree_node.cpp:75
NodeStatus
Definition: basic_types.h:28
virtual BT::NodeStatus executeTick()
The method that will be executed to invoke tick(); and setStatus();.
Definition: tree_node.cpp:35
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32