/src/behaviortreecpp/src/actions/test_node.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "behaviortree_cpp/actions/test_node.h" |
2 | | |
3 | | namespace BT |
4 | | { |
5 | | |
6 | | TestNode::TestNode(const std::string& name, const NodeConfig& config, |
7 | | TestNodeConfig test_config) |
8 | 0 | : TestNode(name, config, std::make_shared<TestNodeConfig>(std::move(test_config))) |
9 | 0 | {} |
10 | | |
11 | | TestNode::TestNode(const std::string& name, const NodeConfig& config, |
12 | | std::shared_ptr<TestNodeConfig> test_config) |
13 | 0 | : StatefulActionNode(name, config), _config(std::move(test_config)) |
14 | 0 | { |
15 | 0 | setRegistrationID("TestNode"); |
16 | |
|
17 | 0 | if(_config->return_status == NodeStatus::IDLE) |
18 | 0 | { |
19 | 0 | throw RuntimeError("TestNode can not return IDLE"); |
20 | 0 | } |
21 | | |
22 | 0 | auto prepareScript = [](const std::string& script, auto& executor) { |
23 | 0 | if(!script.empty()) |
24 | 0 | { |
25 | 0 | auto result = ParseScript(script); |
26 | 0 | if(!result) |
27 | 0 | { |
28 | 0 | throw RuntimeError(result.error()); |
29 | 0 | } |
30 | 0 | executor = result.value(); |
31 | 0 | } |
32 | 0 | }; |
33 | 0 | prepareScript(_config->success_script, _success_executor); |
34 | 0 | prepareScript(_config->failure_script, _failure_executor); |
35 | 0 | prepareScript(_config->post_script, _post_executor); |
36 | 0 | } |
37 | | |
38 | | NodeStatus TestNode::onStart() |
39 | 0 | { |
40 | 0 | if(_config->async_delay <= std::chrono::milliseconds(0)) |
41 | 0 | { |
42 | 0 | return onCompleted(); |
43 | 0 | } |
44 | | // convert this in an asynchronous operation. Use another thread to count |
45 | | // a certain amount of time. |
46 | 0 | _completed = false; |
47 | 0 | _timer.add(std::chrono::milliseconds(_config->async_delay), [this](bool aborted) { |
48 | 0 | if(!aborted) |
49 | 0 | { |
50 | 0 | _completed.store(true); |
51 | 0 | this->emitWakeUpSignal(); |
52 | 0 | } |
53 | 0 | else |
54 | 0 | { |
55 | 0 | _completed.store(false); |
56 | 0 | } |
57 | 0 | }); |
58 | 0 | return NodeStatus::RUNNING; |
59 | 0 | } |
60 | | |
61 | | NodeStatus TestNode::onRunning() |
62 | 0 | { |
63 | 0 | if(_completed) |
64 | 0 | { |
65 | 0 | return onCompleted(); |
66 | 0 | } |
67 | 0 | return NodeStatus::RUNNING; |
68 | 0 | } |
69 | | |
70 | | void TestNode::onHalted() |
71 | 0 | { |
72 | 0 | _timer.cancelAll(); |
73 | 0 | } |
74 | | |
75 | | NodeStatus TestNode::onCompleted() |
76 | 0 | { |
77 | 0 | Ast::Environment env = { config().blackboard, config().enums }; |
78 | |
|
79 | 0 | auto status = |
80 | 0 | (_config->complete_func) ? _config->complete_func() : _config->return_status; |
81 | |
|
82 | 0 | if(status == NodeStatus::SUCCESS && _success_executor) |
83 | 0 | { |
84 | 0 | _success_executor(env); |
85 | 0 | } |
86 | 0 | else if(status == NodeStatus::FAILURE && _failure_executor) |
87 | 0 | { |
88 | 0 | _failure_executor(env); |
89 | 0 | } |
90 | 0 | if(_post_executor) |
91 | 0 | { |
92 | 0 | _post_executor(env); |
93 | 0 | } |
94 | 0 | return status; |
95 | 0 | } |
96 | | |
97 | | } // namespace BT |