script_precondition.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 #pragma once
14 
17 #include <type_traits>
18 
19 namespace BT
20 {
22 {
23 public:
24  PreconditionNode(const std::string& name, const NodeConfig& config)
26  {
27  loadExecutor();
28  }
29 
30  virtual ~PreconditionNode() override = default;
31 
33  {
34  return { InputPort<std::string>("if"),
35  InputPort<NodeStatus>("else", NodeStatus::FAILURE,
36  "Return status if condition is "
37  "false") };
38  }
39 
40 private:
41  virtual BT::NodeStatus tick() override
42  {
43  loadExecutor();
44 
45  BT::NodeStatus else_return;
46  if(!getInput("else", else_return))
47  {
48  throw RuntimeError("Missing parameter [else] in Precondition");
49  }
50 
52  if(_executor(env).cast<bool>())
53  {
54  auto const child_status = child_node_->executeTick();
55  if(isStatusCompleted(child_status))
56  {
57  resetChild();
58  }
59  return child_status;
60  }
61  else
62  {
63  return else_return;
64  }
65  }
66 
67  void loadExecutor()
68  {
69  std::string script;
70  if(!getInput("if", script))
71  {
72  throw RuntimeError("Missing parameter [if] in Precondition");
73  }
74  if(script == _script)
75  {
76  return;
77  }
78  auto executor = ParseScript(script);
79  if(!executor)
80  {
81  throw RuntimeError(executor.error());
82  }
83  else
84  {
85  _executor = executor.value();
86  _script = script;
87  }
88  }
89 
90  std::string _script;
92 };
93 
94 } // namespace BT
BT::PreconditionNode::_executor
ScriptFunction _executor
Definition: script_precondition.h:91
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ParseScript
Expected< ScriptFunction > ParseScript(const std::string &script)
Definition: script_parser.cpp:14
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:71
BT::PreconditionNode::_script
std::string _script
Definition: script_precondition.h:90
BT::ScriptFunction
std::function< Any(Ast::Environment &env)> ScriptFunction
Definition: script_parser.hpp:43
BT::PreconditionNode::providedPorts
static PortsList providedPorts()
Definition: script_precondition.h:32
BT::DecoratorNode
Definition: decorator_node.h:8
BT::PreconditionNode::loadExecutor
void loadExecutor()
Definition: script_precondition.h:67
BT::NodeConfig::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:79
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
BT::PreconditionNode
Definition: script_precondition.h:21
BT::PreconditionNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: script_precondition.h:41
BT::RuntimeError
Definition: exceptions.h:58
BT::PreconditionNode::~PreconditionNode
virtual ~PreconditionNode() override=default
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::isStatusCompleted
bool isStatusCompleted(const NodeStatus &status)
Definition: basic_types.h:47
BT::PreconditionNode::PreconditionNode
PreconditionNode(const std::string &name, const NodeConfig &config)
Definition: script_precondition.h:24
BT::NodeConfig
Definition: tree_node.h:73
BT::DecoratorNode::resetChild
void resetChild()
Definition: decorator_node.cpp:53
decorator_node.h
BT::NodeConfig::enums
std::shared_ptr< ScriptingEnumsRegistry > enums
Definition: tree_node.h:81
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
script_parser.hpp
BT::DecoratorNode::child_node_
TreeNode * child_node_
Definition: decorator_node.h:11
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 Dec 13 2024 03:19:17