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
+43 -3
View File
@@ -24,7 +24,9 @@ _REPO_ROOT = os.path.dirname(os.path.dirname(_SCRIPT_DIR))
sys.path.insert(0, os.path.join(_REPO_ROOT, 'doc', 'generate'))
import generate_api_header
import generate_functions
import generate_mjcf_table
import generate_schema
import mjcf_schema
# Functions in headers that are intentionally not in functions.rst.
_FUNCTIONS_TO_SKIP = set()
@@ -81,6 +83,14 @@ class DocTest(googletest.TestCase):
if source != file.read():
self.fail("The file 'references.h' needs to be updated.")
def test_mjcf_table(self):
"""Checks that mjcf_table.inc matches the schema-generated output."""
table_file = os.path.join(_REPO_ROOT, 'src', 'xml', 'mjcf_table.inc')
source = generate_mjcf_table.generate()
with open(table_file, 'r', encoding='utf-8') as file:
if source != file.read():
self.fail("The file 'mjcf_table.inc' needs to be updated.")
def test_schema(self):
"""Checks that XMLschema.rst matches the generated output."""
schema_file = os.path.join(_REPO_ROOT, 'doc', 'XMLschema.rst')
@@ -91,7 +101,9 @@ class DocTest(googletest.TestCase):
def test_functions(self):
"""Checks that functions.rst matches the generated output."""
functions_file = os.path.join(_REPO_ROOT, 'doc', 'APIreference', 'functions.rst')
functions_file = os.path.join(
_REPO_ROOT, 'doc', 'APIreference', 'functions.rst'
)
source = generate_functions.generate()
with open(functions_file, 'r', encoding='utf-8') as file:
if source != file.read():
@@ -100,7 +112,9 @@ class DocTest(googletest.TestCase):
def test_all_functions_included(self):
"""Checks that every public C function has an entry in functions.rst."""
functions_file = os.path.join(_REPO_ROOT, 'doc', 'APIreference', 'functions.rst')
functions_file = os.path.join(
_REPO_ROOT, 'doc', 'APIreference', 'functions.rst'
)
with open(functions_file, 'r', encoding='utf-8') as file:
content = file.read()
@@ -125,7 +139,9 @@ class DocTest(googletest.TestCase):
def test_all_types_included(self):
"""Checks that every public struct and enum has an entry in APItypes.rst."""
types_file = os.path.join(_REPO_ROOT, 'doc', 'APIreference', 'APItypes.rst')
types_file = os.path.join(
_REPO_ROOT, 'doc', 'APIreference', 'APItypes.rst'
)
with open(types_file, 'r', encoding='utf-8') as file:
content = file.read()
@@ -151,6 +167,30 @@ class DocTest(googletest.TestCase):
msg = 'APItypes.rst mismatches:\n' + '\n'.join(errors)
self.fail(msg)
def test_element_constraints_diamond_inheritance(self):
con = mjcf_schema.Constraint(
kind='exclusive', bundles=(('a',), ('b',)), doc=None, line=1)
common_group = mjcf_schema.Group(
name='common', variant=False, members=[con], doc=None, line=1)
group1 = mjcf_schema.Group(
name='group1', variant=False,
members=[mjcf_schema.Use(group='common', line=1)], doc=None, line=1)
group2 = mjcf_schema.Group(
name='group2', variant=False,
members=[mjcf_schema.Use(group='common', line=1)], doc=None, line=1)
elem = mjcf_schema.Element(
name='elem', spec=None, facets={},
members=[mjcf_schema.Use(group='group1', line=1),
mjcf_schema.Use(group='group2', line=1)],
doc=None, line=1)
schema = mjcf_schema.Schema(
enums={},
groups={'common': common_group, 'group1': group1, 'group2': group2},
elements={'elem': elem},
path='<test>')
cons = generate_mjcf_table._element_constraints(schema, elem)
self.assertEqual(len(cons), 1) # pylint: disable=g-generic-assert
if __name__ == '__main__':
googletest.main()
+2 -2
View File
@@ -698,9 +698,9 @@ TEST_F(SensorTest, BadContact) {
{"geom1='sphere1' geom2='sphere2' num='-3'",
"'num' must be positive in sensor"},
{"geom1='sphere1' geom2='sphere2' site='site'",
"at most one of (geom1, body1, subtree1, site) can be specified"},
"at most one of 'geom1', 'body1', 'subtree1', 'site' can be specified"},
{"geom2='sphere1' body2='body'",
"at most one of (geom2, body2, subtree2) can be specified"},
"at most one of 'geom2', 'body2', 'subtree2' can be specified"},
};
for (const auto& test : test_cases) {
+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
+16 -8
View File
@@ -2024,7 +2024,7 @@ TEST_F(XMLReaderTest, CameraInvalidFovyAndSensorsize) {
std::array<char, 1024> error;
MjModelPtr m = LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(m.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("either 'fovy' or 'sensorsize'"));
EXPECT_THAT(error.data(), HasSubstr("at most one of 'fovy', 'sensorsize'"));
EXPECT_THAT(error.data(), HasSubstr("line 6"));
}
@@ -2081,8 +2081,8 @@ TEST_F(XMLReaderTest, InvalidInertialOrientation) {
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(
error.data(),
HasSubstr(
"fullinertia and inertial orientation cannot both be specified"));
HasSubstr("at most one of 'fullinertia', 'quat', 'axisangle', "
"'xyaxes', 'zaxis', 'euler' can be specified"));
}
TEST_F(XMLReaderTest, ReadShellParameter) {
@@ -2137,7 +2137,7 @@ TEST_F(XMLReaderTest, BuiltinAndFile) {
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(),
HasSubstr("builtin mesh cannot be used with user vertex data"));
HasSubstr("at most one of 'builtin', 'vertex' can be specified"));
}
TEST_F(XMLReaderTest, MakePlateNoParameters) {
@@ -2422,7 +2422,9 @@ TEST_F(RelativeFrameSensorParsingTest, RefNameButNoType) {
)";
std::array<char, 1024> error;
LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(error.data(), HasSubstr("but reftype is missing"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'reftype', 'refname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 8"));
}
@@ -2440,7 +2442,9 @@ TEST_F(RelativeFrameSensorParsingTest, RefTypeButNoName) {
)";
std::array<char, 1024> error;
LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(error.data(), HasSubstr("attribute missing: 'refname'"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'reftype', 'refname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 8"));
}
@@ -3563,7 +3567,9 @@ TEST_F(SensorParseTest, UserObjTypeNoName) {
std::array<char, 1024> error;
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("objtype 'site' given but"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'objtype', 'objname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 4"));
}
@@ -3578,7 +3584,9 @@ TEST_F(SensorParseTest, UserObjNameNoType) {
std::array<char, 1024> error;
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("objname 'kevin' given but"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'objtype', 'objname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 4"));
}