bt_factory.cpp
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 
15 
16 namespace BT
17 {
19 {
20  registerNodeType<FallbackNode>("Fallback");
21  registerNodeType<FallbackStarNode>("FallbackStar");
22  registerNodeType<SequenceNode>("Sequence");
23  registerNodeType<SequenceStarNode>("SequenceStar");
24  registerNodeType<ParallelNode>("ParallelNode");
25 
26  registerNodeType<InverterNode>("Inverter");
27  registerNodeType<RetryNode>("RetryUntilSuccesful");
28  registerNodeType<RepeatNode>("Repeat");
29  registerNodeType<TimeoutNode>("Timeout");
30 
31  registerNodeType<ForceSuccessDecorator>("ForceSuccess");
32  registerNodeType<ForceFailureDecorator>("ForceFailure");
33 
34  registerNodeType<AlwaysSuccess>("AlwaysSuccess");
35  registerNodeType<AlwaysFailure>("AlwaysFailure");
36  registerNodeType<SetBlackboard>("SetBlackboard");
37 
38  registerNodeType<DecoratorSubtreeNode>("SubTree");
39 
40  registerNodeType<BlackboardPreconditionNode<int>>("BlackboardCheckInt");
41  registerNodeType<BlackboardPreconditionNode<double>>("BlackboardCheckDouble");
42  registerNodeType<BlackboardPreconditionNode<std::string>>("BlackboardCheckString");
43 
44  for( const auto& it: builders_)
45  {
46  builtin_IDs_.insert( it.first );
47  }
48 }
49 
50 bool BehaviorTreeFactory::unregisterBuilder(const std::string& ID)
51 {
52  auto it = builders_.find(ID);
53  if (it == builders_.end())
54  {
55  return false;
56  }
57  builders_.erase(ID);
58  return true;
59 }
60 
62 {
63  auto it = builders_.find( manifest.registration_ID);
64  if (it != builders_.end())
65  {
66  throw BehaviorTreeException("ID '" + manifest.registration_ID + "' already registered");
67  }
68 
69  builders_.insert(std::make_pair(manifest.registration_ID, builder));
70  manifests_.push_back(manifest);
72 }
73 
75  const std::string& ID, const SimpleConditionNode::TickFunctor& tick_functor)
76 {
77  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeParameters& params) {
78  return std::unique_ptr<TreeNode>(new SimpleConditionNode(name, tick_functor, params));
79  };
80 
82  registerBuilder(manifest, builder);
83 }
84 
85 void BehaviorTreeFactory::registerSimpleAction(const std::string& ID,
86  const SimpleActionNode::TickFunctor& tick_functor)
87 {
88  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeParameters& params) {
89  return std::unique_ptr<TreeNode>(new SimpleActionNode(name, tick_functor, params));
90  };
91 
93  registerBuilder(manifest, builder);
94 }
95 
97  const std::string& ID, const SimpleDecoratorNode::TickFunctor& tick_functor)
98 {
99  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeParameters& params) {
100  return std::unique_ptr<TreeNode>(new SimpleDecoratorNode(name, tick_functor, params));
101  };
102 
104  registerBuilder(manifest, builder);
105 }
106 
107 void BehaviorTreeFactory::registerFromPlugin(const std::string file_path)
108 {
109  BT::SharedLibrary loader;
110  loader.load(file_path);
111  typedef void (*Func)(BehaviorTreeFactory&);
112 
113  if (loader.hasSymbol(PLUGIN_SYMBOL))
114  {
115  Func func = (Func)loader.getSymbol(PLUGIN_SYMBOL);
116  func(*this);
117  }
118  else
119  {
120  std::cout << "ERROR loading library [" << file_path << "]: can't find symbol ["
121  << PLUGIN_SYMBOL << "]" << std::endl;
122  }
123 }
124 
126  const std::string& ID, const std::string& name,
127  const NodeParameters& params,
128  const Blackboard::Ptr& blackboard) const
129 {
130  auto it = builders_.find(ID);
131  if (it == builders_.end())
132  {
133  std::cerr << ID << " not included in this list:" << std::endl;
134  for (const auto& it: builders_)
135  {
136  std::cerr << it.first << std::endl;
137  }
138  throw std::invalid_argument("ID '" + ID + "' not registered");
139  }
140  std::unique_ptr<TreeNode> node = it->second(name, params);
141  node->setRegistrationName(ID);
142  node->setBlackboard(blackboard);
143  node->initializeOnce();
144 
145  return node;
146 }
147 
148 const std::map<std::string, NodeBuilder>& BehaviorTreeFactory::builders() const
149 {
150  return builders_;
151 }
152 
153 const std::vector<TreeNodeManifest>& BehaviorTreeFactory::manifests() const
154 {
155  return manifests_;
156 }
157 
158 const std::set<std::string> &BehaviorTreeFactory::builtinNodes() const
159 {
160  return builtin_IDs_;
161 }
162 
164 {
165  std::sort(manifests_.begin(), manifests_.end(),
166  [](const TreeNodeManifest& a, const TreeNodeManifest& b) {
167  int comp = std::strcmp(toStr(a.type), toStr(b.type));
168  if (comp == 0)
169  {
170  return a.registration_ID < b.registration_ID;
171  }
172  return comp < 0;
173  });
174 }
175 
176 } // end namespace
std::map< std::string, NodeBuilder > builders_
Definition: bt_factory.h:143
std::vector< TreeNodeManifest > manifests_
Definition: bt_factory.h:144
manifest
The SimpleConditionNode provides an easy to use ConditionNode. The user should simply provide a callb...
std::function< NodeStatus(NodeStatus, TreeNode &)> TickFunctor
std::set< std::string > builtin_IDs_
Definition: bt_factory.h:145
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:40
const std::set< std::string > & builtinNodes() const
Definition: bt_factory.cpp:158
This information is used mostly by the XMLParser.
Definition: bt_factory.h:33
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor)
Register a SimpleConditionNode.
Definition: bt_factory.cpp:74
bool hasSymbol(const std::string &name)
Returns true iff a library has been loaded.
The SimpleDecoratorNode provides an easy to use DecoratorNode. The user should simply provide a callb...
const char * toStr(const BT::NodeStatus &status, bool colored=false)
toStr converts NodeStatus to string. Optionally colored.
Definition: basic_types.cpp:7
void registerBuilder(const TreeNodeManifest &manifest, NodeBuilder builder)
Definition: bt_factory.cpp:61
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
std::unique_ptr< TreeNode > instantiateTreeNode(const std::string &ID, const std::string &name, const NodeParameters &params, const Blackboard::Ptr &blackboard) const
instantiateTreeNode creates a TreeNode
Definition: bt_factory.cpp:125
void * getSymbol(const std::string &name)
std::string registration_ID
Definition: bt_factory.h:36
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor)
Register a SimpleActionNode.
Definition: bt_factory.cpp:85
builder
Definition: build.py:70
const char PLUGIN_SYMBOL[]
Definition: bt_factory.h:40
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:80
void load(const std::string &path, int flags=0)
void registerFromPlugin(const std::string file_path)
registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro)...
Definition: bt_factory.cpp:107
The SimpleActionNode provides an easy to use ActionNode. The user should simply provide a callback wi...
Definition: action_node.h:77
const std::map< std::string, NodeBuilder > & builders() const
All the builders. Made available mostly for debug purposes.
Definition: bt_factory.cpp:148
void registerSimpleDecorator(const std::string &ID, const SimpleDecoratorNode::TickFunctor &tick_functor)
Register a SimpleDecoratorNode.
Definition: bt_factory.cpp:96
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeParameters &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
Definition: bt_factory.h:30
bool unregisterBuilder(const std::string &ID)
Definition: bt_factory.cpp:50
const std::vector< TreeNodeManifest > & manifests() const
Manifests of all the registered TreeNodes.
Definition: bt_factory.cpp:153
std::function< NodeStatus(TreeNode &)> TickFunctor


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