test_node.cpp
Go to the documentation of this file.
2 
3 BT::TestNode::TestNode(const std::string& name, const NodeConfig& config,
4  TestNodeConfig test_config)
5  : StatefulActionNode(name, config), _test_config(std::move(test_config))
6 {
7  setRegistrationID("TestNode");
8 
10  {
11  throw RuntimeError("TestNode can not return IDLE");
12  }
13 
14  auto prepareScript = [](const std::string& script, auto& executor) {
15  if(!script.empty())
16  {
17  auto result = ParseScript(script);
18  if(!result)
19  {
20  throw RuntimeError(result.error());
21  }
22  executor = result.value();
23  }
24  };
27  prepareScript(_test_config.post_script, _post_executor);
28 }
29 
31 {
32  if(_test_config.async_delay <= std::chrono::milliseconds(0))
33  {
34  return onCompleted();
35  }
36  // convert this in an asynchronous operation. Use another thread to count
37  // a certain amount of time.
38  _completed = false;
39  _timer.add(std::chrono::milliseconds(_test_config.async_delay), [this](bool aborted) {
40  if(!aborted)
41  {
42  _completed.store(true);
43  this->emitWakeUpSignal();
44  }
45  else
46  {
47  _completed.store(false);
48  }
49  });
50  return NodeStatus::RUNNING;
51 }
52 
54 {
55  if(_completed)
56  {
57  return onCompleted();
58  }
59  return NodeStatus::RUNNING;
60 }
61 
63 {
64  _timer.cancelAll();
65 }
66 
68 {
69  Ast::Environment env = { config().blackboard, config().enums };
70 
71  auto status = _test_config.complete_func();
72  if(status == NodeStatus::SUCCESS && _success_executor)
73  {
74  _success_executor(env);
75  }
76  else if(status == NodeStatus::FAILURE && _failure_executor)
77  {
78  _failure_executor(env);
79  }
80  if(_post_executor)
81  {
82  _post_executor(env);
83  }
84  return status;
85 }
BT::TestNodeConfig::failure_script
std::string failure_script
script to execute when complete_func() returns FAILURE
Definition: test_node.h:32
BT::ParseScript
Expected< ScriptFunction > ParseScript(const std::string &script)
Definition: script_parser.cpp:14
BT::Ast::Environment::enums
EnumsTablePtr enums
Definition: script_parser.hpp:34
BT::TestNode::onHalted
virtual void onHalted() override
Definition: test_node.cpp:62
BT::TestNode::_success_executor
ScriptFunction _success_executor
Definition: test_node.h:84
test_node.h
BT::TestNodeConfig
Definition: test_node.h:23
BT::NodeStatus::FAILURE
@ FAILURE
BT::TestNodeConfig::return_status
NodeStatus return_status
status to return when the action is completed.
Definition: test_node.h:26
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:433
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::TestNodeConfig::success_script
std::string success_script
script to execute when complete_func() returns SUCCESS
Definition: test_node.h:29
BT::TestNode::_test_config
TestNodeConfig _test_config
Definition: test_node.h:83
BT::NodeStatus::RUNNING
@ RUNNING
BT::TestNode::_post_executor
ScriptFunction _post_executor
Definition: test_node.h:86
std
Definition: std.hpp:30
BT::TestNodeConfig::post_script
std::string post_script
script to execute when actions is completed
Definition: test_node.h:35
BT::NodeStatus::IDLE
@ IDLE
BT::TestNode::_failure_executor
ScriptFunction _failure_executor
Definition: test_node.h:85
BT::TestNode::onRunning
virtual NodeStatus onRunning() override
method invoked when the action is already in the RUNNING state.
Definition: test_node.cpp:53
BT::NodeConfig
Definition: tree_node.h:73
BT::TestNode::onCompleted
NodeStatus onCompleted()
Definition: test_node.cpp:67
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::TestNode::TestNode
TestNode(const std::string &name, const NodeConfig &config, TestNodeConfig test_config)
Definition: test_node.cpp:3
BT::StatefulActionNode
The StatefulActionNode is the preferred way to implement asynchronous Actions. It is actually easier ...
Definition: action_node.h:159
BT::TestNode::onStart
virtual NodeStatus onStart() override
Definition: test_node.cpp:30
BT::Ast::Environment
The Environment class is used to encapsulate the information and states needed by the scripting langu...
Definition: script_parser.hpp:31


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08