-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathxml_validation_test.py
More file actions
72 lines (54 loc) · 2.31 KB
/
xml_validation_test.py
File metadata and controls
72 lines (54 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright 2018 The dm_control Authors.
#
# 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.
# ============================================================================
"""Tests that generated XML string is valid."""
import os
from absl.testing import absltest
from dm_control.mjcf import parser
from dm_control.mujoco import wrapper
ASSETS_DIR = os.path.join(os.path.dirname(__file__), 'test_assets')
_ARENA_XML = os.path.join(ASSETS_DIR, 'arena.xml')
_LEGO_BRICK_XML = os.path.join(ASSETS_DIR, 'lego_brick.xml')
_ROBOT_XML = os.path.join(ASSETS_DIR, 'robot_arm.xml')
_ZIPPED_MODEL = os.path.join(ASSETS_DIR, 'model_with_assetdir.zip')
def validate(xml_string, assets=None):
"""Validates that an XML string is a valid MJCF.
Validation is performed by constructing Mujoco model from the string.
The construction process contains compilation and validation phases by Mujoco
engine, the best validation tool we have access to.
Args:
xml_string: XML string to validate
assets: Optional dict of assets to use for the model.
"""
mjmodel = wrapper.MjModel.from_xml_string(xml_string, assets)
wrapper.MjData(mjmodel)
class XMLValidationTest(absltest.TestCase):
def testXmlAttach(self):
robot_arm = parser.from_file(_ROBOT_XML)
arena = parser.from_file(_ARENA_XML)
lego = parser.from_file(_LEGO_BRICK_XML)
# validate MJCF strings before changing them
validate(robot_arm.to_xml_string())
validate(arena.to_xml_string())
validate(lego.to_xml_string())
# combine objects in complex scene
robot_arm.find('site', 'fingertip1').attach(lego)
arena.worldbody.attach(robot_arm)
# validate
validate(arena.to_xml_string())
def testXmlFromZip(self):
model = parser.from_zip(_ZIPPED_MODEL)
validate(model.to_xml_string(), model.get_assets())
if __name__ == '__main__':
absltest.main()