forked from google-deepmind/dm_control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamescope.py
More file actions
215 lines (180 loc) · 6.78 KB
/
namescope.py
File metadata and controls
215 lines (180 loc) · 6.78 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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.
# ============================================================================
"""An object to manage the scoping of identifiers in MJCF models."""
import collections
from dm_control.mjcf import constants
class NameScope:
"""A name scoping context for an MJCF model.
This object maintains the uniqueness of identifiers within each MJCF
namespace. Examples of MJCF namespaces include 'body', 'joint', and 'geom'.
Each namescope also carries a name, and can have a parent namescope.
When MJCF models are merged, all identifiers gain a hierarchical prefix
separated by '/', which is the concatenation of all scope names up to
the root namescope.
"""
def __init__(self, name, mjcf_model, model_dir='', assets=None):
"""Initializes a scope with the given name.
Args:
name: The scope's name
mjcf_model: The RootElement of the MJCF model associated with this scope.
model_dir: (optional) Path to the directory containing the model XML file.
This is used to prefix the paths of all asset files.
assets: (optional) A dictionary of pre-loaded assets, of the form
`{filename: bytestring}`. If present, PyMJCF will search for assets in
this dictionary before attempting to load them from the filesystem.
"""
self._parent = None
self._name = name
self._mjcf_model = mjcf_model
self._namespaces = collections.defaultdict(dict)
self._model_dir = model_dir
self._files = set()
self._assets = assets or {}
self._revision = 0
@property
def revision(self):
return self._revision
def increment_revision(self):
self._revision += 1
for namescope in self._namespaces['namescope'].values():
namescope.increment_revision()
@property
def name(self):
"""This scope's name."""
return self._name
@property
def files(self):
"""A set containing the `File` attributes registered in this scope."""
return self._files
@property
def assets(self):
"""A dictionary containing pre-loaded assets."""
return self._assets
@property
def model_dir(self):
"""Path to the directory containing the model XML file."""
return self._model_dir
@name.setter
def name(self, new_name):
if self._parent:
self._parent.add('namescope', new_name, self)
self._parent.remove('namescope', self._name)
self._name = new_name
self.increment_revision()
@property
def mjcf_model(self):
return self._mjcf_model
@property
def parent(self):
"""This parent `NameScope`, or `None` if this is a root scope."""
return self._parent
@parent.setter
def parent(self, new_parent):
if self._parent:
self._parent.remove('namescope', self._name)
self._parent = new_parent
if self._parent:
self._parent.add('namescope', self._name, self)
self.increment_revision()
@property
def root(self):
if self._parent is None:
return self
else:
return self._parent.root
def full_prefix(self, prefix_root=None, as_list=False):
"""The prefix for identifiers belonging to this scope.
Args:
prefix_root: (optional) A `NameScope` object to be treated as root
for the purpose of calculating the prefix. If `None` then no prefix
is produced.
as_list: (optional) A boolean, if `True` return the list of prefix
components. If `False`, return the full prefix string separated by
`mjcf.constants.PREFIX_SEPARATOR`.
Returns:
The prefix string.
"""
prefix_root = prefix_root or self
if prefix_root != self and self._parent:
prefix_list = self._parent.full_prefix(prefix_root, as_list=True)
prefix_list.append(self._name)
else:
prefix_list = []
if as_list:
return prefix_list
else:
if prefix_list:
prefix_list.append('')
return constants.PREFIX_SEPARATOR.join(prefix_list)
def _assign(self, namespace, identifier, obj):
"""Checks a proposed identifier's validity before assigning to an object."""
namespace_dict = self._namespaces[namespace]
if not isinstance(identifier, str):
raise ValueError(
'Identifier must be a string: got {}'.format(type(identifier)))
elif constants.PREFIX_SEPARATOR in identifier:
raise ValueError(
'Identifier cannot contain {!r}: got {}'
.format(constants.PREFIX_SEPARATOR, identifier))
else:
namespace_dict[identifier] = obj
def add(self, namespace, identifier, obj):
"""Add an identifier to this name scope.
Args:
namespace: A string specifying the namespace to which the
identifier belongs.
identifier: The identifier string.
obj: The object referred to by the identifier.
Raises:
ValueError: If `identifier` not valid.
"""
namespace_dict = self._namespaces[namespace]
if identifier in namespace_dict:
raise ValueError('Duplicated identifier {!r} in namespace <{}>'
.format(identifier, namespace))
else:
self._assign(namespace, identifier, obj)
self.increment_revision()
def replace(self, namespace, identifier, obj):
"""Reassociates an identifier with a different object.
Args:
namespace: A string specifying the namespace to which the
identifier belongs.
identifier: The identifier string.
obj: The object referred to by the identifier.
Raises:
ValueError: If `identifier` not valid.
"""
self._assign(namespace, identifier, obj)
self.increment_revision()
def remove(self, namespace, identifier):
"""Removes an identifier from this name scope.
Args:
namespace: A string specifying the namespace to which the
identifier belongs.
identifier: The identifier string.
Raises:
KeyError: If `identifier` does not exist in this scope.
"""
del self._namespaces[namespace][identifier]
self.increment_revision()
def rename(self, namespace, old_identifier, new_identifier):
obj = self.get(namespace, old_identifier)
self.add(namespace, new_identifier, obj)
self.remove(namespace, old_identifier)
def get(self, namespace, identifier):
return self._namespaces[namespace][identifier]
def has_identifier(self, namespace, identifier):
return identifier in self._namespaces[namespace]