Skip to content

Commit b629e8b

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 b629e8b

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/xml_parsing.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <functional>
1818
#include <iostream>
1919
#include <list>
20+
#include <charconv>
2021
#include <sstream>
2122
#include <string>
2223
#include <tuple>
@@ -1147,10 +1148,49 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(
11471148
}
11481149
else
11491150
{
1150-
// constant string: just set that constant value into the BB
1151+
// constant value: set it into the BB with appropriate type
11511152
// IMPORTANT: this must not be autoremapped!!!
11521153
new_bb->enableAutoRemapping(false);
1153-
new_bb->set(attr_name, static_cast<std::string>(attr_value));
1154+
const std::string str_value(attr_value);
1155+
1156+
// Try to preserve numeric types so that Script expressions
1157+
// can perform arithmetic without type-mismatch errors.
1158+
// Use std::from_chars with strict full-string validation to avoid
1159+
// false positives on compound strings like "1;2;3" or "2.2;2.4".
1160+
bool stored = false;
1161+
if(!str_value.empty())
1162+
{
1163+
const char* begin = str_value.data();
1164+
const char* end = begin + str_value.size();
1165+
// Try integer first (no decimal point, no exponent notation)
1166+
if(str_value.find('.') == std::string::npos &&
1167+
str_value.find('e') == std::string::npos &&
1168+
str_value.find('E') == std::string::npos)
1169+
{
1170+
int64_t int_val = 0;
1171+
auto [ptr, ec] = std::from_chars(begin, end, int_val);
1172+
if(ec == std::errc() && ptr == end)
1173+
{
1174+
new_bb->set(attr_name, int_val);
1175+
stored = true;
1176+
}
1177+
}
1178+
// Try double
1179+
if(!stored)
1180+
{
1181+
double dbl_val = 0;
1182+
auto [ptr, ec] = std::from_chars(begin, end, dbl_val);
1183+
if(ec == std::errc() && ptr == end)
1184+
{
1185+
new_bb->set(attr_name, dbl_val);
1186+
stored = true;
1187+
}
1188+
}
1189+
}
1190+
if(!stored)
1191+
{
1192+
new_bb->set(attr_name, str_value);
1193+
}
11541194
new_bb->enableAutoRemapping(do_autoremap);
11551195
}
11561196
}

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)