/src/behaviortreecpp/src/controls/if_then_else_node.cpp
Line | Count | Source |
1 | | /* Copyright (C) 2020-2025 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 "behaviortree_cpp/controls/if_then_else_node.h" |
14 | | |
15 | | namespace BT |
16 | | { |
17 | | IfThenElseNode::IfThenElseNode(const std::string& name) |
18 | 248 | : ControlNode::ControlNode(name, {}), child_idx_(0) |
19 | 248 | { |
20 | 248 | setRegistrationID("IfThenElse"); |
21 | 248 | } |
22 | | |
23 | | void IfThenElseNode::halt() |
24 | 298 | { |
25 | 298 | child_idx_ = 0; |
26 | 298 | ControlNode::halt(); |
27 | 298 | } |
28 | | |
29 | | NodeStatus IfThenElseNode::tick() |
30 | 0 | { |
31 | 0 | const size_t children_count = children_nodes_.size(); |
32 | |
|
33 | 0 | if(children_count != 2 && children_count != 3) |
34 | 0 | { |
35 | 0 | throw std::logic_error("IfThenElseNode must have either 2 or 3 children"); |
36 | 0 | } |
37 | | |
38 | 0 | setStatus(NodeStatus::RUNNING); |
39 | |
|
40 | 0 | if(child_idx_ == 0) |
41 | 0 | { |
42 | 0 | const NodeStatus condition_status = children_nodes_[0]->executeTick(); |
43 | |
|
44 | 0 | if(condition_status == NodeStatus::RUNNING) |
45 | 0 | { |
46 | 0 | return condition_status; |
47 | 0 | } |
48 | 0 | if(condition_status == NodeStatus::SUCCESS) |
49 | 0 | { |
50 | 0 | child_idx_ = 1; |
51 | 0 | } |
52 | 0 | else if(condition_status == NodeStatus::FAILURE) |
53 | 0 | { |
54 | 0 | if(children_count == 3) |
55 | 0 | { |
56 | 0 | child_idx_ = 2; |
57 | 0 | } |
58 | 0 | else |
59 | 0 | { |
60 | 0 | return condition_status; |
61 | 0 | } |
62 | 0 | } |
63 | 0 | } |
64 | | // not an else |
65 | 0 | if(child_idx_ > 0) |
66 | 0 | { |
67 | 0 | const NodeStatus status = children_nodes_[child_idx_]->executeTick(); |
68 | 0 | if(status == NodeStatus::RUNNING) |
69 | 0 | { |
70 | 0 | return NodeStatus::RUNNING; |
71 | 0 | } |
72 | 0 | resetChildren(); |
73 | 0 | child_idx_ = 0; |
74 | 0 | return status; |
75 | 0 | } |
76 | | |
77 | 0 | throw std::logic_error("Something unexpected happened in IfThenElseNode"); |
78 | 0 | } |
79 | | |
80 | | } // namespace BT |