MCPcopy Create free account
hub / github.com/BehaviorTree/Groot / BuildTreeFromXML

Function BuildTreeFromXML

bt_editor/utils.cpp:314–375  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

312}
313
314AbsBehaviorTree BuildTreeFromXML(const QDomElement& bt_root, const NodeModels& models )
315{
316 AbsBehaviorTree tree;
317
318 if( bt_root.tagName() != "BehaviorTree" )
319 {
320 throw std::runtime_error( "Expecting a node called <BehaviorTree>");
321 }
322
323 //-------------------------------------
324 std::function<void(AbstractTreeNode* parent, QDomElement)> recursiveStep;
325 recursiveStep = [&](AbstractTreeNode* parent, QDomElement xml_node)
326 {
327 // The nodes with a ID used that QString to insert into the registry()
328 QString modelID = xml_node.tagName();
329 if( xml_node.hasAttribute("ID") )
330 {
331 modelID = xml_node.attribute("ID");
332 }
333
334 AbstractTreeNode tree_node;
335
336 auto model_it = models.find(modelID);
337 if( model_it == models.end() )
338 {
339 throw std::runtime_error( (QString("This model has not been registered: ") + modelID).toStdString() );
340 }
341 tree_node.model = model_it->second;
342
343 if( xml_node.hasAttribute("name") )
344 {
345 tree_node.instance_name = ( xml_node.attribute("name") );
346 }
347 else{
348 tree_node.instance_name = modelID;
349 }
350
351 auto attributes = xml_node.attributes();
352 for( int attr=0; attr < attributes.size(); attr++ )
353 {
354 auto attribute = attributes.item(attr).toAttr();
355 if( attribute.name() != "ID" && attribute.name() != "name")
356 {
357 tree_node.ports_mapping.insert( { attribute.name(), attribute.value() } );
358 }
359 }
360
361 auto added_node = tree.addNode(parent, std::move(tree_node));
362
363 for (QDomElement child = xml_node.firstChildElement( ) ;
364 !child.isNull();
365 child = child.nextSiblingElement( ) )
366 {
367 recursiveStep( added_node, child );
368 }
369 };
370
371 // start recursion

Callers 1

loadFromXMLMethod · 0.85

Calls 2

nameMethod · 0.80
addNodeMethod · 0.80

Tested by

no test coverage detected