bt_factory.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018-2019 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 
16 
17 namespace BT
18 {
20 {
21  registerNodeType<FallbackNode>("Fallback");
22  registerNodeType<SequenceNode>("Sequence");
23  registerNodeType<SequenceStarNode>("SequenceStar");
24  registerNodeType<ParallelNode>("Parallel");
25  registerNodeType<ReactiveSequence>("ReactiveSequence");
26  registerNodeType<ReactiveFallback>("ReactiveFallback");
27 
28  registerNodeType<InverterNode>("Inverter");
29  registerNodeType<RetryNode>("RetryUntilSuccesful");
30  registerNodeType<RepeatNode>("Repeat");
31  registerNodeType<TimeoutNode>("Timeout");
32 
33  registerNodeType<ForceSuccessNode>("ForceSuccess");
34  registerNodeType<ForceFailureNode>("ForceFailure");
35 
36  registerNodeType<AlwaysSuccessNode>("AlwaysSuccess");
37  registerNodeType<AlwaysFailureNode>("AlwaysFailure");
38  registerNodeType<SetBlackboard>("SetBlackboard");
39 
40  registerNodeType<DecoratorSubtreeNode>("SubTree");
41 
42  registerNodeType<BlackboardPreconditionNode<int>>("BlackboardCheckInt");
43  registerNodeType<BlackboardPreconditionNode<double>>("BlackboardCheckDouble");
44  registerNodeType<BlackboardPreconditionNode<std::string>>("BlackboardCheckString");
45 
46  for( const auto& it: builders_)
47  {
48  builtin_IDs_.insert( it.first );
49  }
50 }
51 
52 bool BehaviorTreeFactory::unregisterBuilder(const std::string& ID)
53 {
54  if( builtinNodes().count(ID) )
55  {
56  throw LogicError("You can not remove the builtin registration ID [", ID, "]");
57  }
58  auto it = builders_.find(ID);
59  if (it == builders_.end())
60  {
61  return false;
62  }
63  builders_.erase(ID);
64  manifests_.erase(ID);
65  return true;
66 }
67 
69 {
70  auto it = builders_.find( manifest.registration_ID);
71  if (it != builders_.end())
72  {
73  throw BehaviorTreeException("ID [", manifest.registration_ID, "] already registered");
74  }
75 
76  builders_.insert( {manifest.registration_ID, builder} );
77  manifests_.insert( {manifest.registration_ID, manifest} );
78 }
79 
81  const SimpleConditionNode::TickFunctor& tick_functor,
82  PortsList ports)
83 {
84  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeConfiguration& config) {
85  return std::make_unique<SimpleConditionNode>(name, tick_functor, config);
86  };
87 
88  TreeNodeManifest manifest = { NodeType::CONDITION, ID, std::move(ports) };
89  registerBuilder(manifest, builder);
90 }
91 
92 void BehaviorTreeFactory::registerSimpleAction(const std::string& ID,
93  const SimpleActionNode::TickFunctor& tick_functor,
94  PortsList ports)
95 {
96  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeConfiguration& config) {
97  return std::make_unique<SimpleActionNode>(name, tick_functor, config);
98  };
99 
100  TreeNodeManifest manifest = { NodeType::ACTION, ID, std::move(ports) };
101  registerBuilder(manifest, builder);
102 }
103 
105  const SimpleDecoratorNode::TickFunctor& tick_functor,
106  PortsList ports)
107 {
108  NodeBuilder builder = [tick_functor, ID](const std::string& name, const NodeConfiguration& config) {
109  return std::make_unique<SimpleDecoratorNode>(name, tick_functor, config);
110  };
111 
112  TreeNodeManifest manifest = { NodeType::DECORATOR, ID, std::move(ports) };
113  registerBuilder(manifest, builder);
114 }
115 
116 void BehaviorTreeFactory::registerFromPlugin(const std::string& file_path)
117 {
118  BT::SharedLibrary loader;
119  loader.load(file_path);
120  typedef void (*Func)(BehaviorTreeFactory&);
121 
122  if (loader.hasSymbol(PLUGIN_SYMBOL))
123  {
124  Func func = (Func)loader.getSymbol(PLUGIN_SYMBOL);
125  func(*this);
126  }
127  else
128  {
129  std::cout << "ERROR loading library [" << file_path << "]: can't find symbol ["
130  << PLUGIN_SYMBOL << "]" << std::endl;
131  }
132 }
133 
135  const std::string& name,
136  const std::string& ID,
137  const NodeConfiguration& config) const
138 {
139  auto it = builders_.find(ID);
140  if (it == builders_.end())
141  {
142  std::cerr << ID << " not included in this list:" << std::endl;
143  for (const auto& builder_it: builders_)
144  {
145  std::cerr << builder_it.first << std::endl;
146  }
147  throw RuntimeError("BehaviorTreeFactory: ID [", ID, "] not registered");
148  }
149 
150  std::unique_ptr<TreeNode> node = it->second(name, config);
151  node->setRegistrationID( ID );
152  return node;
153 }
154 
155 const std::unordered_map<std::string, NodeBuilder> &BehaviorTreeFactory::builders() const
156 {
157  return builders_;
158 }
159 
160 const std::unordered_map<std::string,TreeNodeManifest>& BehaviorTreeFactory::manifests() const
161 {
162  return manifests_;
163 }
164 
165 const std::set<std::string> &BehaviorTreeFactory::builtinNodes() const
166 {
167  return builtin_IDs_;
168 }
169 
171  Blackboard::Ptr blackboard)
172 {
173  XMLParser parser(*this);
174  parser.loadFromText(text);
175  auto tree = parser.instantiateTree(blackboard);
176  tree.manifests = this->manifests();
177  return tree;
178 }
179 
180 Tree BehaviorTreeFactory::createTreeFromFile(const std::string &file_path,
181  Blackboard::Ptr blackboard)
182 {
183  XMLParser parser(*this);
184  parser.loadFromFile(file_path);
185  auto tree = parser.instantiateTree(blackboard);
186  tree.manifests = this->manifests();
187  return tree;
188 }
189 
191 {
192  if (root_node) {
193  haltAllActions(root_node);
194  }
195 }
196 
198 {
199  if( blackboard_stack.size() > 0)
200  {
201  return blackboard_stack.front();
202  }
203  return {};
204 }
205 
206 
207 } // end namespace
constexpr const char * PLUGIN_SYMBOL
Definition: bt_factory.h:35
manifest
std::function< NodeStatus(NodeStatus, TreeNode &)> TickFunctor
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeConfiguration &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
Definition: bt_factory.h:33
std::unordered_map< std::string, NodeBuilder > builders_
Definition: bt_factory.h:232
std::set< std::string > builtin_IDs_
Definition: bt_factory.h:234
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:26
const std::set< std::string > & builtinNodes() const
List of builtin IDs.
Definition: bt_factory.cpp:165
This information is used mostly by the XMLParser.
Definition: tree_node.h:32
const std::unordered_map< std::string, NodeBuilder > & builders() const
All the builders. Made available mostly for debug purposes.
Definition: bt_factory.cpp:155
std::unordered_map< std::string, TreeNodeManifest > manifests_
Definition: bt_factory.h:233
bool hasSymbol(const std::string &name)
Returns true iff a library has been loaded.
std::unique_ptr< TreeNode > instantiateTreeNode(const std::string &name, const std::string &ID, const NodeConfiguration &config) const
instantiateTreeNode creates an instance of a previously registered TreeNode.
Definition: bt_factory.cpp:134
void haltAllActions(TreeNode *root_node)
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:98
void * getSymbol(const std::string &name)
std::string registration_ID
Definition: tree_node.h:35
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:170
builder
Definition: build.py:70
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:116
Tree instantiateTree(const Blackboard::Ptr &root_blackboard) override
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
Definition: bt_factory.cpp:92
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:84
void load(const std::string &path, int flags=0)
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:316
static volatile int count
Definition: minitrace.cpp:55
const std::unordered_map< std::string, TreeNodeManifest > & manifests() const
Manifests of all the registered TreeNodes.
Definition: bt_factory.cpp:160
The XMLParser is a class used to read the model of a BehaviorTree from file or text and instantiate t...
Definition: xml_parsing.h:14
void loadFromText(const std::string &xml_text) override
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
The most generic way to register your own builder.
Definition: bt_factory.cpp:68
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:78
std::unordered_map< std::string, TreeNodeManifest > manifests
Definition: bt_factory.h:83
bool unregisterBuilder(const std::string &ID)
Remove a registered ID.
Definition: bt_factory.cpp:52
void loadFromFile(const std::string &filename) override
Definition: xml_parsing.cpp:92
Tree createTreeFromFile(const std::string &file_path, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:180
void registerSimpleDecorator(const std::string &ID, const SimpleDecoratorNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleDecorator help you register nodes of type SimpleDecoratorNode.
Definition: bt_factory.cpp:104
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
Definition: bt_factory.cpp:80
std::function< NodeStatus(TreeNode &)> TickFunctor
Blackboard::Ptr rootBlackboard()
Definition: bt_factory.cpp:197


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04