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
+9 -9
View File
@@ -2461,8 +2461,8 @@ TEST_F(UserObjectsTest, BadConnect) {
EXPECT_THAT(AsVector(m->eq_data, 6), ElementsAre(0, 0, 0, 0, 0, 0));
char error_missing[] =
"either both body1 and anchor must be defined,"
" or both site1 and site2 must be defined\nElement 'connect', line 12";
"one of ('site1', 'site2'), ('body1', 'anchor') must be specified"
"\nElement 'connect', line 12";
// bad model (missing anchor)
xml = base.replace(pos, len, "<connect body1='1'/>");
@@ -2471,8 +2471,8 @@ TEST_F(UserObjectsTest, BadConnect) {
EXPECT_THAT(error, HasSubstr(error_missing));
char error_mixed[] =
"body and site semantics cannot be mixed"
"\nElement 'connect', line 12";
"at most one of ('site1', 'site2'), ('body1', 'body2', 'anchor')"
" can be specified\nElement 'connect', line 12";
// bad model (mixing body and site)
xml = base.replace(pos, len, "<connect body1='1' site1='1'/>");
@@ -2540,8 +2540,8 @@ TEST_F(UserObjectsTest, BadWeld) {
ElementsAre(0, 1, 0, 0, 0, 0, 0, 0, 0, 0));
char error_mixed[] =
"body and site semantics cannot be mixed"
"\nElement 'weld', line 12";
"at most one of ('site1', 'site2'), ('body1', 'body2', 'anchor', "
"'relpose') can be specified\nElement 'weld', line 12";
// bad model (mixing body and site)
xml = base.replace(pos, len, "<weld body1='1' site1='1'/>");
@@ -2568,8 +2568,8 @@ TEST_F(UserObjectsTest, BadWeld) {
EXPECT_THAT(error, HasSubstr(error_mixed));
char error_underspecified[] =
"either body1 must be defined and optionally {body2, anchor, "
"relpose}, or site1 and site2 must be defined\nElement 'weld', line 12";
"one of ('site1', 'site2'), 'body1' must be specified"
"\nElement 'weld', line 12";
// bad model (underspecified body semantics)
xml = base.replace(pos, len, "<weld anchor='0 0 1'/>");
@@ -2663,7 +2663,7 @@ TEST_F(UserObjectsTest, Inertial) {
)";
m = LoadModelFromString(bad_xml2.c_str(), error, sizeof(error));
ASSERT_THAT(m.get(), IsNull());
EXPECT_THAT(error, HasSubstr("fullinertia and inertial orientation cannot"));
EXPECT_THAT(error, HasSubstr("at most one of 'fullinertia', 'quat'"));
}
// Merged COM must be correct when a fused-static child has a non-identity