Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,14 @@ void addTreeToXML(const Tree& tree, XMLDocument& doc, XMLElement* rootXML,
addNodeModelToXML(*model, doc, model_root);
}
}

// "major.minor" of this library, written in the [BTCPP_version] attribute of <root>.
// It is informative only (e.g. for Groot2): the parser ignores it.
std::string libraryMajorMinorVersion()
{
const std::string version = LibraryVersionString();
return version.substr(0, version.rfind('.'));
}
} // namespace

std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory,
Expand All @@ -1463,6 +1471,7 @@ std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory,

XMLElement* rootXML = doc.NewElement("root");
rootXML->SetAttribute("BTCPP_format", "4");
rootXML->SetAttribute("BTCPP_version", libraryMajorMinorVersion().c_str());
doc.InsertFirstChild(rootXML);

XMLElement* model_root = doc.NewElement("TreeNodesModel");
Expand Down Expand Up @@ -1641,6 +1650,7 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory)
</xs:sequence>
<xs:attribute name="BTCPP_format" type="xs:string" use="required"/>
<xs:attribute name="main_tree_to_execute" type="xs:string" use="optional"/>
<xs:attribute name="BTCPP_version" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
)";
Expand Down Expand Up @@ -1765,6 +1775,7 @@ std::string WriteTreeToXML(const Tree& tree, bool add_metadata, bool add_builtin

XMLElement* rootXML = doc.NewElement("root");
rootXML->SetAttribute("BTCPP_format", 4);
rootXML->SetAttribute("BTCPP_version", libraryMajorMinorVersion().c_str());
doc.InsertFirstChild(rootXML);

addTreeToXML(tree, doc, rootXML, add_metadata, add_builtin_models);
Expand Down
24 changes: 24 additions & 0 deletions tests/gtest_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,30 @@ TEST(BehaviorTreeFactory, ManifestMethod)
EXPECT_NE(xml.find(expectedXML), std::string::npos);
}

TEST(BehaviorTreeFactory, WrittenXMLHasLibraryVersion)
{
// "4.11.2" -> "4.11"
const std::string version = LibraryVersionString();
const std::string major_minor = version.substr(0, version.rfind('.'));
const std::string expected = "BTCPP_version=\"" + major_minor + "\"";

BehaviorTreeFactory factory;
EXPECT_NE(writeTreeNodesModelXML(factory).find(expected), std::string::npos);

auto tree = factory.createTreeFromText(R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<AlwaysSuccess/>
</BehaviorTree>
</root>)");
const auto xml = WriteTreeToXML(tree, false, false);
EXPECT_NE(xml.find(expected), std::string::npos);

// the attribute is ignored by the parser
auto reloaded = factory.createTreeFromText(xml);
EXPECT_EQ(reloaded.tickWhileRunning(), NodeStatus::SUCCESS);
}

TEST(BehaviorTreeFactory, ReturnByValue)
{
// Issue #937: BehaviorTreeFactory cannot be returned by value from
Expand Down
Loading