Skip to content

Commit 97e5b11

Browse files
committed
Refactor VerifyXML to clarify logic
- Reduces duplication in VerifyXML by handling the ID check for built-in node types up front so they can then be definitively looked up in the registered nodes. - Enhances error messaging in VerifyXML by using *either* the node name *or* the ID, depending on which is appropriate, instead of leaving users guessing "which Decorator is wrong" - Fixes custom Action and Condition nodes using shorthand syntax not being properly verified - Fixes `<Control ID="ReactiveSequence"/>` not being verified with the same logic as `<ReactiveSequence/>`
1 parent 4ede94d commit 97e5b11

File tree

1 file changed

+53
-80
lines changed

1 file changed

+53
-80
lines changed

src/xml_parsing.cpp

Lines changed: 53 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -429,119 +429,76 @@ void VerifyXML(const std::string& xml_text,
429429
const std::string ID = node->Attribute("ID") ? node->Attribute("ID") : "";
430430
const int line_number = node->GetLineNum();
431431

432-
if(name == "Decorator")
432+
// Precondition: built-in XML element types must define attribute [ID]
433+
const bool is_builtin =
434+
(name == "Decorator" || name == "Action" || name == "Condition" ||
435+
name == "Control" || name == "SubTree");
436+
if(is_builtin && ID.empty())
433437
{
434-
if(children_count != 1)
435-
{
436-
ThrowError(line_number, "The tag <Decorator> must have exactly 1 "
437-
"child");
438-
}
439-
if(ID.empty())
440-
{
441-
ThrowError(line_number, "The tag <Decorator> must have the "
442-
"attribute [ID]");
443-
}
444-
}
445-
else if(name == "Action")
446-
{
447-
if(children_count != 0)
448-
{
449-
ThrowError(line_number, "The tag <Action> must not have any "
450-
"child");
451-
}
452-
if(ID.empty())
453-
{
454-
ThrowError(line_number, "The tag <Action> must have the "
455-
"attribute [ID]");
456-
}
438+
ThrowError(line_number,
439+
std::string("The tag <") + name + "> must have the attribute [ID]");
457440
}
458-
else if(name == "Condition")
441+
442+
if(name == "BehaviorTree")
459443
{
460-
if(children_count != 0)
461-
{
462-
ThrowError(line_number, "The tag <Condition> must not have any "
463-
"child");
464-
}
465-
if(ID.empty())
444+
if(ID.empty() && behavior_tree_count > 1)
466445
{
467-
ThrowError(line_number, "The tag <Condition> must have the "
468-
"attribute [ID]");
446+
ThrowError(line_number, "The tag <BehaviorTree> must have the attribute [ID]");
469447
}
470-
}
471-
else if(name == "Control")
472-
{
473-
if(children_count == 0)
448+
if(registered_nodes.count(ID) != 0)
474449
{
475-
ThrowError(line_number, "The tag <Control> must have at least 1 "
476-
"child");
450+
ThrowError(line_number, "The attribute [ID] of tag <BehaviorTree> must not use "
451+
"the name of a registered Node");
477452
}
478-
if(ID.empty())
453+
if(children_count != 1)
479454
{
480-
ThrowError(line_number, "The tag <Control> must have the "
481-
"attribute [ID]");
455+
ThrowError(line_number, "The tag <BehaviorTree> with ID '" + ID +
456+
"' must have exactly 1 child");
482457
}
483458
}
484459
else if(name == "SubTree")
485460
{
486461
if(children_count != 0)
487462
{
488-
ThrowError(line_number, "<SubTree> should not have any child");
489-
}
490-
if(ID.empty())
491-
{
492-
ThrowError(line_number, "The tag <SubTree> must have the "
493-
"attribute [ID]");
463+
ThrowError(line_number,
464+
"<SubTree> with ID '" + ID + "' should not have any child");
494465
}
495466
if(registered_nodes.count(ID) != 0)
496467
{
497-
ThrowError(line_number, "The attribute [ID] of tag <SubTree> must "
498-
"not use the name of a registered Node");
499-
}
500-
}
501-
else if(name == "BehaviorTree")
502-
{
503-
if(ID.empty() && behavior_tree_count > 1)
504-
{
505-
ThrowError(line_number, "The tag <BehaviorTree> must have the "
506-
"attribute [ID]");
507-
}
508-
if(children_count != 1)
509-
{
510-
ThrowError(line_number, "The tag <BehaviorTree> must have exactly 1 "
511-
"child");
512-
}
513-
if(registered_nodes.count(ID) != 0)
514-
{
515-
ThrowError(line_number, "The attribute [ID] of tag <BehaviorTree> "
516-
"must not use the name of a registered Node");
468+
ThrowError(line_number, "The attribute [ID] of tag <SubTree> must not use the "
469+
"name of a registered Node");
517470
}
518471
}
519472
else
520473
{
521-
// search in the factory and the list of subtrees
522-
const auto search = registered_nodes.find(name);
474+
// use ID for builtin node types, otherwise use the element name
475+
const auto lookup_name = is_builtin ? ID : name;
476+
const auto search = registered_nodes.find(lookup_name);
523477
bool found = (search != registered_nodes.end());
524478
if(!found)
525479
{
526-
ThrowError(line_number, std::string("Node not recognized: ") + name);
480+
ThrowError(line_number, std::string("Node not recognized: ") + lookup_name);
527481
}
528482

529-
if(search->second == NodeType::DECORATOR)
483+
const auto node_type = search->second;
484+
const std::string& registered_name = search->first;
485+
486+
if(node_type == NodeType::DECORATOR)
530487
{
531488
if(children_count != 1)
532489
{
533-
ThrowError(line_number,
534-
std::string("The node <") + name + "> must have exactly 1 child");
490+
ThrowError(line_number, std::string("The node '") + registered_name +
491+
"' must have exactly 1 child");
535492
}
536493
}
537-
else if(search->second == NodeType::CONTROL)
494+
else if(node_type == NodeType::CONTROL)
538495
{
539496
if(children_count == 0)
540497
{
541-
ThrowError(line_number,
542-
std::string("The node <") + name + "> must have 1 or more children");
498+
ThrowError(line_number, std::string("The node '") + registered_name +
499+
"' must have 1 or more children");
543500
}
544-
if(name == "ReactiveSequence")
501+
if(registered_name == "ReactiveSequence")
545502
{
546503
size_t async_count = 0;
547504
for(auto child = node->FirstChildElement(); child != nullptr;
@@ -563,13 +520,29 @@ void VerifyXML(const std::string& xml_text,
563520
++async_count;
564521
if(async_count > 1)
565522
{
566-
ThrowError(line_number, std::string("A ReactiveSequence cannot have more "
567-
"than one async child."));
523+
ThrowError(line_number, std::string("A ReactiveSequence cannot have "
524+
"more than one async child."));
568525
}
569526
}
570527
}
571528
}
572529
}
530+
else if(node_type == NodeType::ACTION)
531+
{
532+
if(children_count != 0)
533+
{
534+
ThrowError(line_number, std::string("The node '") + registered_name +
535+
"' must not have any child");
536+
}
537+
}
538+
else if(node_type == NodeType::CONDITION)
539+
{
540+
if(children_count != 0)
541+
{
542+
ThrowError(line_number, std::string("The node '") + registered_name +
543+
"' must not have any child");
544+
}
545+
}
573546
}
574547
//recursion
575548
for(auto child = node->FirstChildElement(); child != nullptr;

0 commit comments

Comments
 (0)