|
59 | 59 | #endif |
60 | 60 | #endif |
61 | 61 |
|
| 62 | +#include "schematron_template.gen.h" |
| 63 | + |
62 | 64 | #include "behaviortree_cpp/blackboard.h" |
63 | 65 | #include "behaviortree_cpp/tree_node.h" |
64 | 66 | #include "behaviortree_cpp/utils/demangle_util.h" |
@@ -1560,17 +1562,24 @@ std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory, |
1560 | 1562 | } |
1561 | 1563 |
|
1562 | 1564 | std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
| 1565 | +{ |
| 1566 | + return writeTreeXSD(factory, false); |
| 1567 | +} |
| 1568 | + |
| 1569 | +std::string writeTreeXSD(const BehaviorTreeFactory& factory, bool generic) |
1563 | 1570 | { |
1564 | 1571 | // There are 2 forms of representation for a node: |
1565 | 1572 | // compact: <Sequence .../> and explicit: <Control ID="Sequence" ... /> |
1566 | 1573 | // Only the compact form is supported because the explicit form doesn't |
1567 | 1574 | // make sense with XSD since we would need to allow any attribute. |
1568 | 1575 | // Prepare the data |
1569 | 1576 |
|
| 1577 | + const auto& builtin_set = factory.builtinNodes(); |
1570 | 1578 | std::map<std::string, const TreeNodeManifest*> ordered_models; |
1571 | 1579 | for(const auto& [registration_id, model] : factory.manifests()) |
1572 | 1580 | { |
1573 | | - ordered_models.insert({ registration_id, &model }); |
| 1581 | + if(!generic || builtin_set.count(registration_id) != 0) |
| 1582 | + ordered_models.insert({ registration_id, &model }); |
1574 | 1583 | } |
1575 | 1584 |
|
1576 | 1585 | XMLDocument doc; |
@@ -1651,6 +1660,15 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
1651 | 1660 | </xs:extension> |
1652 | 1661 | </xs:simpleContent> |
1653 | 1662 | </xs:complexType> |
| 1663 | + <xs:complexType name="inoutPortType"> |
| 1664 | + <xs:simpleContent> |
| 1665 | + <xs:extension base="xs:string"> |
| 1666 | + <xs:attribute name="name" type="xs:string" use="required"/> |
| 1667 | + <xs:attribute name="type" type="xs:string" use="optional"/> |
| 1668 | + <xs:attribute name="default" type="xs:string" use="optional"/> |
| 1669 | + </xs:extension> |
| 1670 | + </xs:simpleContent> |
| 1671 | + </xs:complexType> |
1654 | 1672 | <xs:attributeGroup name="preconditionAttributeGroup"> |
1655 | 1673 | <xs:attribute name="_failureIf" type="xs:string" use="optional"/> |
1656 | 1674 | <xs:attribute name="_onlyIf" type="xs:string" use="optional"/> |
@@ -1686,6 +1704,7 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
1686 | 1704 | <xs:choice minOccurs="0" maxOccurs="unbounded"> |
1687 | 1705 | <xs:element name="input_port" type="inputPortType"/> |
1688 | 1706 | <xs:element name="output_port" type="outputPortType"/> |
| 1707 | + <xs:element name="inout_port" type="inoutPortType"/> |
1689 | 1708 | </xs:choice> |
1690 | 1709 | <xs:element name="description" type="descriptionType" minOccurs="0" maxOccurs="1"/> |
1691 | 1710 | </xs:sequence> |
@@ -1724,15 +1743,27 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
1724 | 1743 | XMLElement* one_node_group = doc.NewElement("xs:group"); |
1725 | 1744 | { |
1726 | 1745 | one_node_group->SetAttribute("name", "oneNodeGroup"); |
1727 | | - std::ostringstream xsd; |
1728 | | - xsd << "<xs:choice>"; |
1729 | | - for(const auto& [registration_id, model] : ordered_models) |
| 1746 | + if(generic) |
1730 | 1747 | { |
1731 | | - xsd << "<xs:element name=\"" << registration_id << "\" type=\"" << registration_id |
1732 | | - << "Type\"/>"; |
| 1748 | + // Generic: accept any element; lax processing validates known built-ins |
| 1749 | + // via the top-level xs:element declarations emitted at the end of this function. |
| 1750 | + parse_and_insert(one_node_group, "<xs:choice>" |
| 1751 | + "<xs:any namespace=\"##any\" " |
| 1752 | + "processContents=\"lax\"/>" |
| 1753 | + "</xs:choice>"); |
| 1754 | + } |
| 1755 | + else |
| 1756 | + { |
| 1757 | + std::ostringstream xsd; |
| 1758 | + xsd << "<xs:choice>"; |
| 1759 | + for(const auto& [registration_id, model] : ordered_models) |
| 1760 | + { |
| 1761 | + xsd << "<xs:element name=\"" << registration_id << "\" type=\"" << registration_id |
| 1762 | + << "Type\"/>"; |
| 1763 | + } |
| 1764 | + xsd << "</xs:choice>"; |
| 1765 | + parse_and_insert(one_node_group, xsd.str().c_str()); |
1733 | 1766 | } |
1734 | | - xsd << "</xs:choice>"; |
1735 | | - parse_and_insert(one_node_group, xsd.str().c_str()); |
1736 | 1767 | schema_element->InsertEndChild(one_node_group); |
1737 | 1768 | } |
1738 | 1769 |
|
@@ -1799,8 +1830,12 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
1799 | 1830 | XMLElement* common_attr_group = doc.NewElement("xs:attributeGroup"); |
1800 | 1831 | common_attr_group->SetAttribute("ref", "commonAttributeGroup"); |
1801 | 1832 | type->InsertEndChild(common_attr_group); |
| 1833 | + std::map<std::string, const BT::PortInfo*> ordered_ports; |
1802 | 1834 | for(const auto& [port_name, port_info] : model->ports) |
| 1835 | + ordered_ports.insert({ port_name, &port_info }); |
| 1836 | + for(const auto& [port_name, port_info_ptr] : ordered_ports) |
1803 | 1837 | { |
| 1838 | + const auto& port_info = *port_info_ptr; |
1804 | 1839 | XMLElement* attr = doc.NewElement("xs:attribute"); |
1805 | 1840 | attr->SetAttribute("name", port_name.c_str()); |
1806 | 1841 | const auto xsd_attribute_type = xsdAttributeType(port_info); |
@@ -1828,11 +1863,54 @@ std::string writeTreeXSD(const BehaviorTreeFactory& factory) |
1828 | 1863 | schema_element->InsertEndChild(type); |
1829 | 1864 | } |
1830 | 1865 |
|
| 1866 | + // Generic mode: emit top-level xs:element declarations so that |
| 1867 | + // processContents="lax" in oneNodeGroup can resolve and validate |
| 1868 | + // known built-in node elements by name. |
| 1869 | + if(generic) |
| 1870 | + { |
| 1871 | + for(const auto& [registration_id, model] : ordered_models) |
| 1872 | + { |
| 1873 | + XMLElement* elem = doc.NewElement("xs:element"); |
| 1874 | + elem->SetAttribute("name", registration_id.c_str()); |
| 1875 | + elem->SetAttribute("type", (registration_id + "Type").c_str()); |
| 1876 | + schema_element->InsertEndChild(elem); |
| 1877 | + } |
| 1878 | + } |
| 1879 | + |
1831 | 1880 | XMLPrinter printer; |
1832 | 1881 | doc.Print(&printer); |
1833 | 1882 | return std::string(printer.CStr(), size_t(printer.CStrSize() - 1)); |
1834 | 1883 | } |
1835 | 1884 |
|
| 1885 | +std::string writeTreeSchematron(const BehaviorTreeFactory& factory) |
| 1886 | +{ |
| 1887 | + // Build the pipe-delimited builtin node list, e.g. "|AlwaysFailure|Fallback|...|", |
| 1888 | + // then substitute the ##BUILTIN_PIPE## placeholder in the embedded template. |
| 1889 | + // The template itself lives in src/btcpp4_schematron.sch (human-readable); |
| 1890 | + // schematron_template.gen.h is generated from it by CMake. |
| 1891 | + std::ostringstream builtin_pipe_ss; |
| 1892 | + builtin_pipe_ss << "|"; |
| 1893 | + for(const auto& name : factory.builtinNodes()) |
| 1894 | + builtin_pipe_ss << name << "|"; |
| 1895 | + |
| 1896 | + std::string result = BT::detail::schematron_template; |
| 1897 | + const std::string placeholder = "##BUILTIN_PIPE##"; |
| 1898 | + const std::string builtin_pipe = builtin_pipe_ss.str(); |
| 1899 | + int replacements = 0; |
| 1900 | + |
| 1901 | + for(std::size_t pos = result.find(placeholder); pos != std::string::npos; |
| 1902 | + pos = result.find(placeholder, pos)) |
| 1903 | + { |
| 1904 | + result.replace(pos, placeholder.size(), builtin_pipe); |
| 1905 | + pos += builtin_pipe.size(); |
| 1906 | + ++replacements; |
| 1907 | + } |
| 1908 | + if(replacements == 0) |
| 1909 | + throw RuntimeError("writeTreeSchematron: placeholder '", placeholder, |
| 1910 | + "' not found in schematron template"); |
| 1911 | + return result; |
| 1912 | +} |
| 1913 | + |
1836 | 1914 | std::string WriteTreeToXML(const Tree& tree, bool add_metadata, bool add_builtin_models) |
1837 | 1915 | { |
1838 | 1916 | XMLDocument doc; |
|
0 commit comments