|
| 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 | + |
| 16 | +#include "behaviortree_cpp/condition_node.h" |
| 17 | +#include "behaviortree_cpp/scripting/script_parser.hpp" |
| 18 | + |
| 19 | +namespace BT |
| 20 | +{ |
| 21 | +/** |
| 22 | + * @brief Execute a script, and if the result is true, return |
| 23 | + * SUCCESS, FAILURE otherwise. |
| 24 | + */ |
| 25 | +class ScriptCondition : public ConditionNode |
| 26 | +{ |
| 27 | +public: |
| 28 | + ScriptCondition(const std::string& name, const NodeConfig& config) : |
| 29 | + ConditionNode(name, config) |
| 30 | + { |
| 31 | + setRegistrationID("ScriptCondition"); |
| 32 | + loadExecutor(); |
| 33 | + } |
| 34 | + |
| 35 | + static PortsList providedPorts() |
| 36 | + { |
| 37 | + return {InputPort("code", "Piece of code that can be parsed. Must return false or true")}; |
| 38 | + } |
| 39 | + |
| 40 | +private: |
| 41 | + virtual BT::NodeStatus tick() override |
| 42 | + { |
| 43 | + loadExecutor(); |
| 44 | + |
| 45 | + Ast::Environment env = {config().blackboard, config().enums}; |
| 46 | + auto result = _executor(env); |
| 47 | + return (result.cast<bool>()) ? |
| 48 | + 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; |
| 75 | + ScriptFunction _executor; |
| 76 | +}; |
| 77 | + |
| 78 | +} // namespace BT |
0 commit comments