|
| 1 | +/* Copyright (C) 2023 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 | +#include <algorithm> |
| 14 | +#include <cstddef> |
| 15 | + |
| 16 | +#include "behaviortree_cpp/controls/parallel_all_node.h" |
| 17 | + |
| 18 | +namespace BT |
| 19 | +{ |
| 20 | + |
| 21 | +ParallelAllNode::ParallelAllNode(const std::string& name, const NodeConfig& config) : |
| 22 | + ControlNode::ControlNode(name, config), |
| 23 | + failure_threshold_(1) |
| 24 | +{} |
| 25 | + |
| 26 | +NodeStatus ParallelAllNode::tick() |
| 27 | +{ |
| 28 | + int max_failures = 0; |
| 29 | + if (!getInput("max_failures", max_failures)) |
| 30 | + { |
| 31 | + throw RuntimeError("Missing parameter [max_failures] in ParallelNode"); |
| 32 | + } |
| 33 | + const size_t children_count = children_nodes_.size(); |
| 34 | + setFailureThreshold(max_failures); |
| 35 | + |
| 36 | + size_t skipped_count = 0; |
| 37 | + |
| 38 | + if (children_count < failure_threshold_) |
| 39 | + { |
| 40 | + throw LogicError("Number of children is less than threshold. Can never fail."); |
| 41 | + } |
| 42 | + |
| 43 | + setStatus(NodeStatus::RUNNING); |
| 44 | + |
| 45 | + // Routing the tree according to the sequence node's logic: |
| 46 | + for (size_t index = 0; index < children_count; index++) |
| 47 | + { |
| 48 | + TreeNode* child_node = children_nodes_[index]; |
| 49 | + |
| 50 | + // already completed |
| 51 | + if(completed_list_.count(index) != 0) |
| 52 | + { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + NodeStatus const child_status = child_node->executeTick(); |
| 57 | + |
| 58 | + switch (child_status) |
| 59 | + { |
| 60 | + case NodeStatus::SUCCESS: { |
| 61 | + completed_list_.insert(index); |
| 62 | + } |
| 63 | + break; |
| 64 | + |
| 65 | + case NodeStatus::FAILURE: { |
| 66 | + completed_list_.insert(index); |
| 67 | + failure_count_++; |
| 68 | + } |
| 69 | + break; |
| 70 | + |
| 71 | + case NodeStatus::RUNNING: { |
| 72 | + // Still working. Check the next |
| 73 | + } |
| 74 | + break; |
| 75 | + |
| 76 | + case NodeStatus::SKIPPED: { |
| 77 | + skipped_count++; |
| 78 | + } |
| 79 | + break; |
| 80 | + |
| 81 | + case NodeStatus::IDLE: { |
| 82 | + throw LogicError("[", name(), "]: A children should not return IDLE"); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if(skipped_count == children_count) |
| 88 | + { |
| 89 | + return NodeStatus::SKIPPED; |
| 90 | + } |
| 91 | + if( skipped_count + completed_list_.size() >= children_count) |
| 92 | + { |
| 93 | + // DONE |
| 94 | + haltChildren(); |
| 95 | + completed_list_.clear(); |
| 96 | + failure_count_ = 0; |
| 97 | + return (failure_count_ >= failure_threshold_) ? NodeStatus::FAILURE : |
| 98 | + NodeStatus::SUCCESS; |
| 99 | + } |
| 100 | + |
| 101 | + // Some children haven't finished, yet. |
| 102 | + return NodeStatus::RUNNING; |
| 103 | +} |
| 104 | + |
| 105 | +void ParallelAllNode::halt() |
| 106 | +{ |
| 107 | + completed_list_.clear(); |
| 108 | + failure_count_ = 0; |
| 109 | + ControlNode::halt(); |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +size_t ParallelAllNode::failureThreshold() const |
| 114 | +{ |
| 115 | + return failure_threshold_; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +void ParallelAllNode::setFailureThreshold(int threshold) |
| 120 | +{ |
| 121 | + if (threshold < 0) |
| 122 | + { |
| 123 | + failure_threshold_ = size_t(std::max(int(children_nodes_.size()) + threshold + 1, 0)); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + failure_threshold_ = size_t(threshold); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace BT |
0 commit comments