Files
Mujoco_WASM/doc/generate/generate_default_table.py
T
Yuval Tassa 4278c7b0cd Table-driven attribute reading: rebase the reader on generated rows.
Every mechanical attribute read in MJCF now derives from mjcf.schema.
generate_read_table.py emits typed mjXAttr rows (mjcf_read_table.inc,
doc_test-gated) binding each attribute to its spec struct field; field
offsets are offsetof() expressions, so binding mistakes are compile
errors, and the field's C type -- parsed from the headers -- selects the
row kind, so mjtNum-versus-double is decided by the struct, not the
schema. mjXReader::ReadAttrTable is the generic loop; its static core
also serves the section parsers and records XML-authored fields via
mjs_setAuthored for attach conflict resolution. Row kinds cover
strings, string lists, numeric scalars and vectors (exact and ranged),
enums (int- and byte-width), bitwise flag sets, bools, unbounded typed
vectors, fixed char arrays, and identity constants declared by 'set'.
The rows are inline variables, and carry the writing=custom flag,
because the writer will share them.

The keyword maps the rows reference are generated too: the ~48
hand-written mjMap tables become mjcf_map.h, one map and size constant
per enum as C++17 inline variables, retiring the hand-maintained
extern block in xml_base.h. Map names follow the schema enum names
(fluid->fluidshape, TFAuto->FalseTrueAuto, FAuto->FalseAuto,
joint->jointtype, geom->geomtype, jac->jacobian); all maps are
key-order- and value-identical to the hand tables they replace, and
bool_map is hand-emitted (the bool type is built in, not a schema
enum).

The OneX() parsers reduce to genuine irregulars, schema-marked as
reading=custom: orientation alternatives, file attributes (VFS and
asset-dir context), the actuator shorthand remappings and per-type
input maps, springlength's one-value copy, mesh builtin construction,
hfield elevation, texture cube files, flexcomp seeding, the memory
suffix parse, and the flag bit families. All 41 sensors that are pure
identity-plus-references -- including the frame family and insidesite
-- dispatch through a generated tag table; frame-sensor
objtype/reftype vocabulary tightens from the full mju_str2Type
namespace to the documented body/xbody/geom/site/camera subset, so an
invalid keyword now fails at parse time instead of compile time. The
equality family and both tendon types read shared group rows; the
twelve actuator shorthands share the general rows, with per-tag
legality enforced by the schema check. Sections bind non-mjs structs,
the visual sub-sections reaching their anonymous sub-structs through
member paths declared by an element-level field= facet.

Latent irregularities surfaced by the migration and preserved via
schema declarations or remnants: key's name is set even when absent,
eulerseq and gridlayout are fixed char arrays (chars[n], arity in
characters), gridlayout's length-must-match-gridsize stays a
value-conditional remnant, and constructor-style elements (tendon
wraps, asset model, replicate, attach) are annotated as such -- their
attributes are arguments, not field writes.

Two coherence tests guard the schema against the C sources: every
schema enum constant must be a member of the C enum it claims, and
every C member must be a keyword, a count sentinel, or a documented
exemption; and generate_default_table.py emits one row per defaulted
attribute (mjcf_default_table.inc), compared by SchemaDefaultsTest
against a freshly-constructed spec -- the schema cannot disagree with
the C default-constructors without failing the suite.

Verified: doc_test regenerates and diffs every artifact; the full
suite; and an A/B harness compiling the model corpus against the
pre-migration reader -- saved XML and binary models are byte-identical.
PiperOrigin-RevId: 958075724
Change-Id: I9715fe4deeb438eec988fd5084d74ba8b466b10b
2026-08-02 17:24:45 -07:00

187 lines
6.8 KiB
Python

# Copyright 2026 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Generates the default-value check table from src/xml/mjcf.schema.
The schema declares attribute defaults, but the defaults that act live in
the C default-constructors (mjs_default*, mj_defaultOption, ...). This
emits src/xml/mjcf_default_table.inc: one row per defaulted attribute,
binding the declared values to the field they describe, consumed by
SchemaDefaultsTest, which compares every row against a freshly-constructed
spec -- so a schema default that disagrees with the C defaults is a test
failure, not documentation drift. Checked in and gated by
test/doc/doc_test.py.
"""
import os
import sys
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _SCRIPT_DIR)
import generate_read_table
import mjcf_schema
_REPO_ROOT = os.path.dirname(os.path.dirname(_SCRIPT_DIR))
SCHEMA_PATH = os.path.join(_REPO_ROOT, 'src', 'xml', 'mjcf.schema')
# kind codes shared with the test
KIND_BY_CTYPE = {'double': 0, 'float': 1, 'int': 2,
'mjtByte': 3, 'mjtBool': 3, 'mjtNum': 4}
_HEADER = '''\
// Copyright 2026 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// GENERATED FILE, DO NOT EDIT. Generated from src/xml/mjcf.schema by
// doc/generate/generate_default_table.py; test/doc/doc_test.py checks
// freshness.
//
// One row per schema attribute with a declared default, binding the declared
// values to the bound field. SchemaDefaultsTest compares every row against a
// freshly-constructed spec: the schema's defaults must agree with the C
// default-constructors. Rows are {attr, offset, kind, len, ndecl, values};
// values beyond ndecl are expected to be zero; kind: 0=double 1=float 2=int
// 3=byte 4=mjtNum.
// clang-format off
struct mjXDefaultEntry {
const char* attr;
int offset;
int kind;
int len;
int ndecl;
double value[8];
};
struct mjXDefaultTable {
const char* structname;
const mjXDefaultEntry* entries;
int n;
};
'''
def _values(schema, attr, ctype):
"""(ndecl, [C value expressions]) for an attribute's declared default."""
default = attr.default
if attr.type == 'enum':
constants = dict(schema.enums[attr.target].items)
return 1, [f'(double){constants[default]}']
if attr.type == 'bool':
return 1, ['1' if default == 'true' else '0']
values = default if isinstance(default, tuple) else (default,)
return len(values), [repr(v) for v in values]
def collect(schema, structs):
"""struct key -> list of row tuples, deduplicated across elements."""
tables = {}
seen = {}
for element in schema.elements.values():
if not element.spec:
continue
sub = element.facets.get('field')
key = f'{element.spec}.{sub}' if sub else element.spec
fields = structs.get(key)
if fields is None:
continue
prefix = f'{sub}.' if sub else ''
for attr in schema.expanded_attrs(element):
if attr.default is None or attr.type in ('string', 'file', 'chars',
'ref', 'id', 'flags'):
continue
field = attr.facets.get('field', attr.name)
entry = fields.get(field)
if entry is None:
if 'reading' in attr.facets:
continue # custom lowering with no direct binding
raise ValueError(f'{element.name}.{attr.name}: no field '
f'{element.spec}.{field}')
ctype, dim = entry
if ctype not in KIND_BY_CTYPE and not ctype.startswith('mjt'):
raise ValueError(f'{element.name}.{attr.name}: default bound to '
f'field {field} ({ctype})')
kind = KIND_BY_CTYPE.get(ctype, 2) # other mjt enums are int-sized
length = dim if dim is not None else '1'
ndecl, values = _values(schema, attr, ctype)
if ndecl > 8:
raise ValueError(f'{element.name}.{attr.name}: {ndecl} default '
'values exceed the row capacity')
row = (attr.name, f'(int)offsetof({element.spec}, {prefix}{field})',
kind, str(length), ndecl, values)
prior = seen.get((key, field))
if prior is not None:
if prior != (ndecl, values):
raise ValueError(f'{element.name}.{attr.name}: conflicting '
f'defaults for {key}.{field}')
continue
seen[(key, field)] = (ndecl, values)
tables.setdefault(key, []).append(row)
return tables
def generate() -> str:
schema = mjcf_schema.parse_file(SCHEMA_PATH)
structs = generate_read_table.parse_spec_structs(
generate_read_table.SPEC_H_PATH, generate_read_table.MODEL_H_PATH)
tables = collect(schema, structs)
out = [_HEADER]
for key in sorted(tables):
array = 'kDefaults_' + key.replace('.', '_')
out.append(f'static const mjXDefaultEntry {array}[] = {{')
for attr, offset, kind, length, ndecl, values in tables[key]:
vals = ', '.join(values)
out.append(f' {{"{attr}", {offset}, {kind}, {length}, {ndecl}, '
f'{{{vals}}}}},')
out.append('};')
out.append('')
out.append('static const mjXDefaultTable kDefaultTables[] = {')
for key in sorted(tables):
array = 'kDefaults_' + key.replace('.', '_')
root = key.split('.')[0]
out.append(f' {{"{root}", {array}, '
f'(int)(sizeof({array}) / sizeof({array}[0]))}},')
out.append('};')
out.append('static const int kDefaultTablesN = '
'(int)(sizeof(kDefaultTables) / sizeof(kDefaultTables[0]));')
out.append('// clang-format on')
return '\n'.join(out) + '\n'
def main() -> int:
if len(sys.argv) > 2:
sys.exit('usage: generate_default_table.py [output.inc]')
text = generate()
if len(sys.argv) == 2:
with open(sys.argv[1], 'w', encoding='utf-8') as file:
file.write(text)
else:
sys.stdout.write(text)
return 0
if __name__ == '__main__':
sys.exit(main())