script_condition.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 Davide Faconti - All Rights Reserved
2  *
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
14 #pragma once
15 
18 
19 namespace BT
20 {
26 {
27 public:
28  ScriptCondition(const std::string& name, const NodeConfig& config)
30  {
31  setRegistrationID("ScriptCondition");
32  loadExecutor();
33  }
34 
36  {
37  return { InputPort("code", "Piece of code that can be parsed. Must return false or "
38  "true") };
39  }
40 
41 private:
42  virtual BT::NodeStatus tick() override
43  {
44  loadExecutor();
45 
47  auto result = _executor(env);
48  return (result.cast<bool>()) ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
49  }
50 
51  void loadExecutor()
52  {
53  std::string script;
54  if(!getInput("code", script))
55  {
56  throw RuntimeError("Missing port [code] in ScriptCondition");
57  }
58  if(script == _script)
59  {
60  return;
61  }
62  auto executor = ParseScript(script);
63  if(!executor)
64  {
65  throw RuntimeError(executor.error());
66  }
67  else
68  {
69  _executor = executor.value();
70  _script = script;
71  }
72  }
73 
74  std::string _script;
76 };
77 
78 } // namespace BT
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::ConditionNode
Definition: condition_node.h:21
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::ScriptCondition::_executor
ScriptFunction _executor
Definition: script_condition.h:75
BT::InputPort
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
Definition: basic_types.h:470
BT::ScriptFunction
std::function< Any(Ast::Environment &env)> ScriptFunction
Definition: script_parser.hpp:43
BT::ScriptCondition::ScriptCondition
ScriptCondition(const std::string &name, const NodeConfig &config)
Definition: script_condition.h:28
BT::ScriptCondition::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: script_condition.h:42
BT::ScriptCondition::loadExecutor
void loadExecutor()
Definition: script_condition.h:51
BT::ScriptCondition
Execute a script, and if the result is true, return SUCCESS, FAILURE otherwise.
Definition: script_condition.h:25
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::ScriptCondition::_script
std::string _script
Definition: script_condition.h:74
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
condition_node.h
BT::NodeConfig
Definition: tree_node.h:73
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::ScriptCondition::providedPorts
static PortsList providedPorts()
Definition: script_condition.h:35
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