Skip to content

Commit 5e5f65b

Browse files
L4co77claude
andcommitted
Preserve numeric types for literal subtree port values
When literal values are passed to SubTree ports (not blackboard remapping), detect numeric types (int64_t, double) before storing them in the child blackboard. Previously all literals were stored as std::string, which caused type-mismatch errors in Script expressions that tried to do arithmetic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3633e27 commit 5e5f65b

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

src/xml_parsing.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
#include "behaviortree_cpp/basic_types.h"
1414

15+
#include <charconv>
1516
#include <cstdio>
1617
#include <cstring>
1718
#include <functional>
1819
#include <iostream>
20+
#include <limits>
1921
#include <list>
2022
#include <sstream>
2123
#include <string>
@@ -1147,10 +1149,59 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(
11471149
}
11481150
else
11491151
{
1150-
// constant string: just set that constant value into the BB
1152+
// constant value: set it into the BB with appropriate type
11511153
// IMPORTANT: this must not be autoremapped!!!
11521154
new_bb->enableAutoRemapping(false);
1153-
new_bb->set(attr_name, static_cast<std::string>(attr_value));
1155+
const std::string str_value(attr_value);
1156+
1157+
// Try to preserve numeric types so that Script expressions
1158+
// can perform arithmetic without type-mismatch errors.
1159+
// Use std::from_chars with strict full-string validation to avoid
1160+
// false positives on compound strings like "1;2;3" or "2.2;2.4".
1161+
bool stored = false;
1162+
if(!str_value.empty())
1163+
{
1164+
const char* begin = str_value.data();
1165+
const char* end = begin + str_value.size();
1166+
// Try integer first (no decimal point, no exponent notation).
1167+
// Use int when the value fits, to match the most common port
1168+
// declarations. Fall back to int64_t for larger values.
1169+
if(str_value.find('.') == std::string::npos &&
1170+
str_value.find('e') == std::string::npos &&
1171+
str_value.find('E') == std::string::npos)
1172+
{
1173+
int64_t int_val = 0;
1174+
auto [ptr, ec] = std::from_chars(begin, end, int_val);
1175+
if(ec == std::errc() && ptr == end)
1176+
{
1177+
if(int_val >= std::numeric_limits<int>::min() &&
1178+
int_val <= std::numeric_limits<int>::max())
1179+
{
1180+
new_bb->set(attr_name, static_cast<int>(int_val));
1181+
}
1182+
else
1183+
{
1184+
new_bb->set(attr_name, int_val);
1185+
}
1186+
stored = true;
1187+
}
1188+
}
1189+
// Try double
1190+
if(!stored)
1191+
{
1192+
double dbl_val = 0;
1193+
auto [ptr, ec] = std::from_chars(begin, end, dbl_val);
1194+
if(ec == std::errc() && ptr == end)
1195+
{
1196+
new_bb->set(attr_name, dbl_val);
1197+
stored = true;
1198+
}
1199+
}
1200+
}
1201+
if(!stored)
1202+
{
1203+
new_bb->set(attr_name, str_value);
1204+
}
11541205
new_bb->enableAutoRemapping(do_autoremap);
11551206
}
11561207
}

tests/gtest_subtree.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,41 @@ TEST(SubTree, NestedDuplicateNames_ShouldFail)
974974
// Should throw RuntimeError because of duplicate SubTree names
975975
ASSERT_THROW((void)factory.createTreeFromText(xml_text), RuntimeError);
976976
}
977+
978+
// Regression test: literal numeric values passed to subtrees should preserve
979+
// their numeric type so that Script expressions can do arithmetic.
980+
TEST(SubTree, LiteralNumericPortsPreserveType)
981+
{
982+
// clang-format off
983+
static const char* xml_text = R"(
984+
<root BTCPP_format="4" main_tree_to_execute="MainTree">
985+
986+
<BehaviorTree ID="MainTree">
987+
<Sequence>
988+
<SubTree ID="DoMath" int_val="42" dbl_val="3.14" str_val="hello"
989+
remapped_val="{from_parent}" />
990+
</Sequence>
991+
</BehaviorTree>
992+
993+
<BehaviorTree ID="DoMath">
994+
<Sequence>
995+
<ScriptCondition code=" int_val + 1 == 43 " />
996+
<ScriptCondition code=" dbl_val > 3.0 " />
997+
<ScriptCondition code=" remapped_val + 1 == 101 " />
998+
</Sequence>
999+
</BehaviorTree>
1000+
1001+
</root>
1002+
)";
1003+
// clang-format on
1004+
1005+
BehaviorTreeFactory factory;
1006+
1007+
auto tree = factory.createTreeFromText(xml_text);
1008+
1009+
// Set the remapped parent value as an integer
1010+
tree.rootBlackboard()->set("from_parent", 100);
1011+
1012+
const auto status = tree.tickWhileRunning();
1013+
ASSERT_EQ(status, NodeStatus::SUCCESS);
1014+
}

0 commit comments

Comments
 (0)