Skip to content

Commit 6337feb

Browse files
committed
handle enums conversions is assignment
1 parent ec2e696 commit 6337feb

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <memory>
1717
#include <string>
1818
#include <vector>
19-
#include <utility>
2019

2120
#include "behaviortree_cpp/scripting/any_types.hpp"
2221
#include "behaviortree_cpp/scripting/script_parser.hpp"
@@ -555,11 +554,16 @@ struct ExprAssignment : ExprBase
555554
{
556555
*dst_ptr = converter(str);
557556
}
558-
else
557+
else if(dst_ptr->isNumber())
559558
{
560-
auto msg = StrCat(errorPrefix(), "\nThe right operand is a string but"
561-
"can't find the corresponding "
562-
"convertFromString<T>().");
559+
auto num_value = StringToDouble(value, env);
560+
*dst_ptr = Any(num_value);
561+
}
562+
else {
563+
auto msg = StrCat(errorPrefix(),
564+
"\nThe right operand is a string, "
565+
"can't convert to ",
566+
demangle(dst_ptr->type()));
563567
throw RuntimeError(msg);
564568
}
565569
}

tests/gtest_postconditions.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,59 @@ TEST(PostConditions, Issue601)
8686
ASSERT_EQ(tree.rootBlackboard()->get<std::string>("test"), "halted");
8787
}
8888

89+
enum BatteryStatus
90+
{
91+
BATTERY_OK,
92+
LOW_BATTERY
93+
};
94+
95+
class BatteryCheck : public BT::SyncActionNode
96+
{
97+
public:
98+
BatteryCheck(const std::string& name, const BT::NodeConfig& config) :
99+
BT::SyncActionNode(name, config){}
100+
101+
static BT::PortsList providedPorts() {
102+
return { InputPort<int>("health")};
103+
}
104+
105+
BT::NodeStatus tick() override {
106+
int health = getInput<int>("health").value();
107+
return health > 10 ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
108+
}
109+
};
110+
111+
TEST(PostConditions, OnFailure)
112+
{
113+
const std::string xml_text = R"(
114+
<root BTCPP_format="4" >
115+
<BehaviorTree ID="Main">
116+
<Sequence>
117+
<Script code="battery_voltage := 7"/>
118+
<Script code="battery_status := BATTERY_OK"/>
119+
<SubTree ID="Sub" _autoremap="true"
120+
fault_status="LOW_BATTERY"
121+
health="{battery_voltage}" />
122+
</Sequence>
123+
</BehaviorTree>
124+
125+
<BehaviorTree ID="Sub">
126+
<BatteryCheck health="{health}"
127+
_onFailure="battery_status := fault_status" />
128+
</BehaviorTree>
129+
</root>)";
130+
131+
BehaviorTreeFactory factory;
132+
factory.registerNodeType<BatteryCheck>("BatteryCheck");
133+
factory.registerScriptingEnums<BatteryStatus>();
134+
135+
factory.registerBehaviorTreeFromText(xml_text);
136+
137+
auto tree = factory.createTree("Main");
138+
const auto status = tree.tickWhileRunning();
139+
140+
ASSERT_EQ(status, NodeStatus::FAILURE);
141+
ASSERT_EQ(tree.rootBlackboard()->get<BatteryStatus>("battery_status"), LOW_BATTERY);
142+
}
143+
89144

tests/gtest_preconditions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ class SimpleOutput : public BT::SyncActionNode
314314
setOutput("output", true);
315315
return BT::NodeStatus::SUCCESS;
316316
}
317-
318317
};
319318

320319
TEST(Preconditions, Remapping)

tests/script_parser_test.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,38 @@ TEST(ParserTest, NotInitializedComparison)
254254
EXPECT_ANY_THROW(GetResult("x += 1"));
255255
}
256256

257-
TEST(ParserTest, Enums)
257+
TEST(ParserTest, EnumsBasic)
258+
{
259+
BT::Ast::Environment environment = {BT::Blackboard::create(), {}};
260+
261+
auto GetResult = [&environment](const char* text) -> BT::Any {
262+
return GetScriptResult(environment, text);
263+
};
264+
265+
enum Color
266+
{
267+
RED = 1,
268+
BLUE = 3,
269+
GREEN = 5
270+
};
271+
272+
environment.enums = std::make_shared<BT::EnumsTable>();
273+
environment.enums->insert( {"RED", RED} );
274+
environment.enums->insert( {"BLUE", BLUE} );
275+
environment.enums->insert( {"GREEN", GREEN} );
276+
GetResult("A:=RED");
277+
GetResult("B:=RED");
278+
GetResult("C:=BLUE");
279+
280+
EXPECT_EQ(GetResult("A==B").cast<int>(), 1);
281+
EXPECT_EQ(GetResult("A!=C").cast<int>(), 1);
282+
283+
EXPECT_EQ(GetResult("A").cast<Color>(), RED);
284+
EXPECT_EQ(GetResult("B").cast<Color>(), RED);
285+
EXPECT_EQ(GetResult("C").cast<Color>(), BLUE);
286+
}
287+
288+
TEST(ParserTest, EnumsXML)
258289
{
259290
BT::BehaviorTreeFactory factory;
260291

0 commit comments

Comments
 (0)