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
+113 -2
View File
@@ -333,11 +333,20 @@ XMLElement* NextSiblingElement(XMLElement* e, const char* name) {
}
// constructor
mjXSchema::mjXSchema(std::vector<const char*> schema[], unsigned nrow) {
mjXSchema::mjXSchema(std::vector<const char*> schema[], unsigned nrow,
const mjXConstraintDef* constraints, int nconstraint,
int first_row) {
// set name and type
name_ = schema[0][0];
type_ = schema[0][1][0];
// adopt the presence constraints declared for this row
for (int i = 0; i < nconstraint; i++) {
if (constraints[i].row == first_row) {
constraints_.push_back(&constraints[i]);
}
}
// set attributes
int nattr = schema[0].size() - 2;
for (int i = 0; i < nattr; i++) {
@@ -370,7 +379,8 @@ mjXSchema::mjXSchema(std::vector<const char*> schema[], unsigned nrow) {
}
// add child element
subschema_.emplace_back(schema+start, end-start+1);
subschema_.emplace_back(schema+start, end-start+1, constraints,
nconstraint, first_row+start);
// proceed with next subelement
start = end+1;
@@ -380,6 +390,102 @@ mjXSchema::mjXSchema(std::vector<const char*> schema[], unsigned nrow) {
// quoted list of constraint bundles: 'a' or ('a', 'b'), comma-joined
static std::string BundleList(const std::vector<std::vector<std::string>>& bundles) {
std::string out;
for (size_t i = 0; i < bundles.size(); i++) {
if (i) {
out += ", ";
}
if (bundles[i].size() == 1) {
out += "'" + bundles[i][0] + "'";
} else {
out += '(';
for (size_t j = 0; j < bundles[i].size(); j++) {
out += (j ? ", '" : "'") + bundles[i][j] + "'";
}
out += ')';
}
}
return out;
}
// enforce the presence constraints declared for this element
XMLElement* mjXSchema::CheckConstraints(XMLElement* elem) {
for (const mjXConstraintDef* con : constraints_) {
// split the spec into bundles of attribute names
std::vector<std::vector<std::string>> bundles(1);
std::string token;
for (const char* c = con->spec;; c++) {
if (*c == ' ' || *c == '|' || *c == '\0') {
if (!token.empty()) {
bundles.back().push_back(token);
token.clear();
}
if (*c == '|') {
bundles.emplace_back();
}
if (*c == '\0') {
break;
}
} else {
token += *c;
}
}
// per-bundle presence: any member present / all members present
int n_any = 0, n_all = 0, n_attr = 0, n_present = 0;
for (const auto& bundle : bundles) {
bool any = false, all = true;
for (const std::string& attr : bundle) {
bool present = elem->Attribute(attr.c_str()) != nullptr;
any |= present;
all &= present;
n_attr++;
n_present += present;
}
n_any += any;
n_all += all;
}
switch (con->kind) {
case 'e': // at most one bundle may be present
if (n_any > 1) {
error = "at most one of " + BundleList(bundles) +
" can be specified";
return elem;
}
break;
case 't': // all listed attributes appear together or not at all
if (n_present != 0 && n_present != n_attr) {
error = "attributes " + BundleList(bundles) +
" must be specified together";
return elem;
}
break;
case 'r': // first attribute requires the second
if (n_any && elem->Attribute(bundles[0][0].c_str()) &&
!elem->Attribute(bundles[1][0].c_str())) {
error = "attribute '" + bundles[0][0] + "' requires attribute '" +
bundles[1][0] + "'";
return elem;
}
break;
case 'o': // at least one bundle must be complete
if (n_all == 0) {
error = "one of " + BundleList(bundles) + " must be specified";
return elem;
}
break;
}
}
return nullptr;
}
// get pointer to error message
std::string mjXSchema::GetError() {
return error;
@@ -525,6 +631,11 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
}
}
// check presence constraints
if ((bad = CheckConstraints(elem))) {
return bad;
}
// handle recursion
if (type_ == 'R') {
// check child elements with same name