Generate the MJCF grammar table and enforce its constraints.

The hand-written MJCF[] table in xml_native_reader.cc is replaced by
mjcf_table.inc, emitted from mjcf.schema by generate_mjcf_table.py and
checked for freshness by doc_test. nMJCF is now self-sizing. The two
tables are identical as trees of (tag, cardinality, attribute-set);
within-row attribute order changes where the schema factors shared
groups and projects default-context rows, and top-level rows follow
the schema's dependency order -- neither affects validation, which is
set-based, nor XMLschema.rst, whose generator orders sections itself
(regenerated here, reading the .inc instead of the reader source).

The schema's constraint declarations become enforcement: the emitter
writes a companion MJCF_constraints[] array (row-indexed into MJCF[]),
and mjXSchema::Check evaluates each element's constraints after its
attribute check, with uniform messages derived from the declaration:
"at most one of 'fovy', 'sensorsize' can be specified", "attributes
'reftype', 'refname' must be specified together", and so on.
Multi-attribute bundles render as ('site1', 'site2').

Fifteen hand-written co-occurrence checks across fourteen elements are
deleted -- connect/weld semantics mixing and completeness, the actuator
transmission mutex, camera fovy/sensorsize, light directional/type,
inertial fullinertia-versus-orientation, rangefinder and the distance
family, contact's matching criteria, user-sensor pairing, the frame
family's reftype/refname, size memory exclusivities, mesh builtin
exclusions, and attach body/frame (newly declared). Tests assert the
uniform messages.

Two findings along the way: sensorsize-requires-resolution is a
value-level compiler rule (positive resolution), not a presence rule --
a presence constraint would be wrong and is not declared; and Size()'s
nstack/njmax range checks tested the spec value before assignment, so
they never validated the parsed value -- now they do.

Verified by compiling all 81 models in the model/ corpus.

PiperOrigin-RevId: 958064622
Change-Id: I802cf5c0aee08a62926e36a281320ff9e34c0668
This commit is contained in:
Yuval Tassa
2026-08-02 16:29:32 -07:00
committed by Copybara-Service
parent 3f8db4c17a
commit 790f8fac30
12 changed files with 1600 additions and 1261 deletions
+8 -7
View File
@@ -113,18 +113,18 @@ def generate_dropdown(
def generate() -> str:
"""Generates XMLschema.rst by parsing xml_native_reader.cc.
"""Generates XMLschema.rst by parsing mjcf_table.inc.
The schema is defined in xml_native_reader.cc as a nested structure called
MJCF[nMJCF]. This function parses that structure and generates nested
dropdown directives with list-tables for attributes.
The schema is defined in mjcf_table.inc (generated from mjcf.schema) as a
nested structure called MJCF[]. This function parses that structure and
generates nested dropdown directives with list-tables for attributes.
Returns:
RST content with nested dropdown directives for the MJCF schema.
"""
script_dir = os.path.dirname(os.path.abspath(__file__))
repo_root = os.path.dirname(os.path.dirname(script_dir))
filepath = os.path.join(repo_root, 'src', 'xml', 'xml_native_reader.cc')
filepath = os.path.join(repo_root, 'src', 'xml', 'mjcf_table.inc')
xmlfile = os.path.join(repo_root, 'doc', 'XMLreference.rst')
# Collect all link targets from XMLreference.rst for validation.
@@ -159,7 +159,7 @@ def generate() -> str:
# Skip to the MJCF schema definition in the C++ source.
for line in file:
if 'std::vector<const char*> MJCF[nMJCF] = {' in line.strip():
if 'std::vector<const char*> MJCF[] = {' in line.strip():
break
# Parse the schema structure.
@@ -224,7 +224,8 @@ def generate() -> str:
parent[level + 1] = element[0]
element = []
return output
# single newline at end of file
return output.rstrip('\n') + '\n'
def main() -> int: