NOLINTNEXTLINE(readability-function-cognitive-complexity)
| 304 | |
| 305 | // NOLINTNEXTLINE(readability-function-cognitive-complexity) |
| 306 | std::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 | } |
no test coverage detected