Repository navigation
Expand file tree
/
Copy pathxml_validation_test.py
More file actions
71 lines (53 loc) · 2.17 KB
/
Copy pathxml_validation_test.py
File metadata and controls
71 lines (53 loc) · 2.17 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
# 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."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
# Internal dependencies.
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')
def validate(xml_string):
"""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
"""
mjmodel = wrapper.MjModel.from_xml_string(xml_string)
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())
if __name__ == '__main__':
absltest.main()