Skip to content

Commit 4a0e337

Browse files
committed
add script condition
1 parent 834775a commit 4a0e337

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

include/behaviortree_cpp/actions/script_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ScriptNode : public SyncActionNode
5151
std::string script;
5252
if (!getInput("code", script))
5353
{
54-
throw RuntimeError("Missing parameter [script] in Script");
54+
throw RuntimeError("Missing port [code] in Script");
5555
}
5656
if (script == _script)
5757
{

include/behaviortree_cpp/behavior_tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "behaviortree_cpp/actions/always_success_node.h"
3737
#include "behaviortree_cpp/actions/always_failure_node.h"
38+
#include "behaviortree_cpp/actions/script_condition.h"
3839
#include "behaviortree_cpp/actions/script_node.h"
3940
#include "behaviortree_cpp/actions/set_blackboard_node.h"
4041
#include "behaviortree_cpp/actions/test_node.h"

src/bt_factory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ BehaviorTreeFactory::BehaviorTreeFactory():
7676
registerNodeType<AlwaysSuccessNode>("AlwaysSuccess");
7777
registerNodeType<AlwaysFailureNode>("AlwaysFailure");
7878
registerNodeType<ScriptNode>("Script");
79+
registerNodeType<ScriptCondition>("ScriptCondition");
7980
registerNodeType<SetBlackboard>("SetBlackboard");
8081
registerNodeType<SleepNode>("Sleep");
8182

0 commit comments

Comments
 (0)