/src/behaviortreecpp/src/controls/while_do_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/while_do_else_node.h" |
14 | | |
15 | | namespace BT |
16 | | { |
17 | | WhileDoElseNode::WhileDoElseNode(const std::string& name) |
18 | 148 | : ControlNode::ControlNode(name, {}) |
19 | 148 | { |
20 | 148 | setRegistrationID("WhileDoElse"); |
21 | 148 | } |
22 | | |
23 | | void WhileDoElseNode::halt() |
24 | 180 | { |
25 | 180 | ControlNode::halt(); |
26 | 180 | } |
27 | | |
28 | | NodeStatus WhileDoElseNode::tick() |
29 | 0 | { |
30 | 0 | const size_t children_count = children_nodes_.size(); |
31 | |
|
32 | 0 | if(children_count != 2 && children_count != 3) |
33 | 0 | { |
34 | 0 | throw std::logic_error("WhileDoElseNode must have either 2 or 3 children"); |
35 | 0 | } |
36 | | |
37 | 0 | setStatus(NodeStatus::RUNNING); |
38 | |
|
39 | 0 | const NodeStatus condition_status = children_nodes_[0]->executeTick(); |
40 | |
|
41 | 0 | if(condition_status == NodeStatus::RUNNING) |
42 | 0 | { |
43 | 0 | return condition_status; |
44 | 0 | } |
45 | | |
46 | 0 | NodeStatus status = NodeStatus::IDLE; |
47 | |
|
48 | 0 | if(condition_status == NodeStatus::SUCCESS) |
49 | 0 | { |
50 | 0 | if(children_count == 3) |
51 | 0 | { |
52 | 0 | haltChild(2); |
53 | 0 | } |
54 | 0 | status = children_nodes_[1]->executeTick(); |
55 | 0 | } |
56 | 0 | else if(condition_status == NodeStatus::FAILURE) |
57 | 0 | { |
58 | 0 | if(children_count == 3) |
59 | 0 | { |
60 | 0 | haltChild(1); |
61 | 0 | status = children_nodes_[2]->executeTick(); |
62 | 0 | } |
63 | 0 | else if(children_count == 2) |
64 | 0 | { |
65 | 0 | status = NodeStatus::FAILURE; |
66 | 0 | } |
67 | 0 | } |
68 | |
|
69 | 0 | if(status == NodeStatus::RUNNING) |
70 | 0 | { |
71 | 0 | return NodeStatus::RUNNING; |
72 | 0 | } |
73 | 0 | resetChildren(); |
74 | 0 | return status; |
75 | 0 | } |
76 | | |
77 | | } // namespace BT |