script_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 {
21 class ScriptNode : public SyncActionNode
22 {
23 public:
24  ScriptNode(const std::string& name, const NodeConfig& config)
26  {
27  setRegistrationID("ScriptNode");
28 
29  loadExecutor();
30  }
31 
33  {
34  return { InputPort<std::string>("code", "Piece of code that can be parsed") };
35  }
36 
37 private:
38  virtual BT::NodeStatus tick() override
39  {
40  loadExecutor();
41  if(_executor)
42  {
44  _executor(env);
45  }
46  return NodeStatus::SUCCESS;
47  }
48 
49  void loadExecutor()
50  {
51  std::string script;
52  if(!getInput("code", script))
53  {
54  throw RuntimeError("Missing port [code] in Script");
55  }
56  if(script == _script)
57  {
58  return;
59  }
60  auto executor = ParseScript(script);
61  if(!executor)
62  {
63  throw RuntimeError(executor.error());
64  }
65  else
66  {
67  _executor = executor.value();
68  _script = script;
69  }
70  }
71 
72  std::string _script;
74 };
75 
76 } // 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::ScriptNode::_script
std::string _script
Definition: script_node.h:72
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::ScriptNode::ScriptNode
ScriptNode(const std::string &name, const NodeConfig &config)
Definition: script_node.h:24
BT::ScriptFunction
std::function< Any(Ast::Environment &env)> ScriptFunction
Definition: script_parser.hpp:43
BT::ScriptNode::_executor
ScriptFunction _executor
Definition: script_node.h:73
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
action_node.h
BT::ScriptNode::loadExecutor
void loadExecutor()
Definition: script_node.h:49
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
BT::ScriptNode
Definition: script_node.h:21
BT::ScriptNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: script_node.h:38
BT::ScriptNode::providedPorts
static PortsList providedPorts()
Definition: script_node.h:32
BT::NodeConfig
Definition: tree_node.h:73
BT::NodeConfig::enums
std::shared_ptr< ScriptingEnumsRegistry > enums
Definition: tree_node.h:81
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
script_parser.hpp
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