Skip to content

Commit d974b6b

Browse files
committed
bugfix: string to enum/integer/boolean in scripts
1 parent 61589d1 commit d974b6b

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#pragma once
1414

1515
#include <cmath>
16-
#include <cstdio>
17-
#include <unordered_map>
1816
#include <memory>
1917
#include <string>
2018
#include <vector>
@@ -30,6 +28,27 @@ using SimpleString = SafeAny::SimpleString;
3028

3129
using expr_ptr = std::shared_ptr<struct ExprBase>;
3230

31+
// extended strin to number that consider enums and booleans
32+
double StringToDouble(const Any& value, const Environment& env)
33+
{
34+
const auto str = value.cast<std::string>();
35+
if(str == "true") {
36+
return 1.0;
37+
}
38+
if(str == "false") {
39+
return 0.0;
40+
}
41+
if(env.enums)
42+
{
43+
auto it = env.enums->find(str);
44+
if(it != env.enums->end())
45+
{
46+
return it->second;
47+
}
48+
}
49+
return value.cast<double>();
50+
}
51+
3352
struct ExprBase
3453
{
3554
using Ptr = std::shared_ptr<ExprBase>;
@@ -380,16 +399,24 @@ struct ExprComparison : ExprBase
380399
return False;
381400
}
382401
}
383-
else if ((lhs_v.isString() && rhs_v.isNumber()) ||
384-
(lhs_v.isNumber() && rhs_v.isString()))
402+
else if (lhs_v.isString() && rhs_v.isNumber())
385403
{
386-
auto lv = lhs_v.cast<double>();
404+
auto lv = StringToDouble(lhs_v, env);
387405
auto rv = rhs_v.cast<double>();
388406
if (!SwitchImpl(lv, rv, ops[i]))
389407
{
390408
return False;
391409
}
392410
}
411+
else if (lhs_v.isNumber() && rhs_v.isString())
412+
{
413+
auto lv = lhs_v.cast<double>();
414+
auto rv = StringToDouble(rhs_v, env);
415+
if (!SwitchImpl(lv, rv, ops[i]))
416+
{
417+
return False;
418+
}
419+
}
393420
else
394421
{
395422
throw RuntimeError(StrCat("Can't mix different types in Comparison. "

tests/gtest_preconditions.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ TEST(Preconditions, Issue615_NoSkipWhenRunning_B)
300300
}
301301

302302

303+
class SimpleOutput : public BT::SyncActionNode
304+
{
305+
public:
306+
SimpleOutput(const std::string& name, const BT::NodeConfig& config) :
307+
BT::SyncActionNode(name, config){}
308+
309+
static BT::PortsList providedPorts() {
310+
return { OutputPort<bool>("output")};
311+
}
312+
313+
BT::NodeStatus tick() override {
314+
setOutput("output", true);
315+
return BT::NodeStatus::SUCCESS;
316+
}
317+
318+
};
303319

304320
TEST(Preconditions, Remapping)
305321
{
@@ -308,23 +324,32 @@ TEST(Preconditions, Remapping)
308324
309325
<BehaviorTree ID="Main">
310326
<Sequence>
311-
<Script code="value:=1" />
327+
<SimpleOutput output="{param}" />
328+
<Script code="value:=true" />
329+
330+
<SubTree ID="Sub1" param="{param}"/>
312331
<SubTree ID="Sub1" param="{value}"/>
313-
<TestA _skipIf="value!=1" />
332+
<SubTree ID="Sub1" param="true"/>
333+
<TestA/>
314334
</Sequence>
315335
</BehaviorTree>
316336
317337
<BehaviorTree ID="Sub1">
318338
<Sequence>
319-
<TestB _skipIf="param!=1" />
339+
<SubTree ID="Sub2" _skipIf="param != true" />
320340
</Sequence>
321341
</BehaviorTree>
342+
343+
<BehaviorTree ID="Sub2">
344+
<TestB/>
345+
</BehaviorTree>
322346
</root>
323347
)";
324348

325349
BehaviorTreeFactory factory;
326350

327351
std::array<int, 2> counters;
352+
factory.registerNodeType<SimpleOutput>("SimpleOutput");
328353
RegisterTestTick(factory, "Test", counters);
329354

330355
factory.registerBehaviorTreeFromText(xml_text);
@@ -334,5 +359,5 @@ TEST(Preconditions, Remapping)
334359

335360
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
336361
ASSERT_EQ( counters[0], 1 );
337-
ASSERT_EQ( counters[1], 1 );
362+
ASSERT_EQ( counters[1], 3 );
338363
}

0 commit comments

Comments
 (0)