MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / instantiateTreeNode

Method instantiateTreeNode

src/bt_factory.cpp:306–400  ·  view source on GitHub ↗

NOLINTNEXTLINE(readability-function-cognitive-complexity)

Source from the content-addressed store, hash-verified

304
305// NOLINTNEXTLINE(readability-function-cognitive-complexity)
306std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
307 const std::string& name, const std::string& ID, const NodeConfig& config) const
308{
309 auto idNotFound = [this, ID] {
310 std::cerr << ID << " not included in this list:" << std::endl;
311 for(const auto& builder_it : _p->builders)
312 {
313 std::cerr << builder_it.first << std::endl;
314 }
315 throw RuntimeError("BehaviorTreeFactory: ID [", ID, "] not registered");
316 };
317
318 auto it_manifest = _p->manifests.find(ID);
319 if(it_manifest == _p->manifests.end())
320 {
321 idNotFound();
322 }
323
324 std::unique_ptr<TreeNode> node;
325
326 bool substituted = false;
327 for(const auto& [filter, rule] : _p->substitution_rules)
328 {
329 if(filter == name || filter == ID || wildcards_match(config.path, filter))
330 {
331 // first case: the rule is simply a string with the name of the
332 // node to create instead
333 if(const auto* const substituted_ID = std::get_if<std::string>(&rule))
334 {
335 auto it_builder = _p->builders.find(*substituted_ID);
336 if(it_builder != _p->builders.end())
337 {
338 auto& builder = it_builder->second;
339 node = builder(name, config);
340 }
341 else
342 {
343 throw RuntimeError("Substituted Node ID [", *substituted_ID, "] not found");
344 }
345 substituted = true;
346 break;
347 }
348
349 if(const auto* const test_config = std::get_if<TestNodeConfig>(&rule))
350 {
351 node = std::make_unique<TestNode>(name, config,
352 std::make_shared<TestNodeConfig>(*test_config));
353 substituted = true;
354 break;
355 }
356
357 if(const auto* const test_config =
358 std::get_if<std::shared_ptr<TestNodeConfig>>(&rule))
359 {
360 node = std::make_unique<TestNode>(name, config, *test_config);
361 substituted = true;
362 break;
363 }

Callers 1

createNodeFromXMLMethod · 0.80

Calls 7

RuntimeErrorClass · 0.85
wildcards_matchFunction · 0.85
LogicErrorClass · 0.85
ParseScriptFunction · 0.85
setRegistrationIDMethod · 0.80
endMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected