00001
00002
00003
00004 '''rtsprofile
00005
00006 Copyright (C) 2009-2010
00007 Geoffrey Biggs
00008 RT-Synthesis Research Group
00009 Intelligent Systems Research Institute,
00010 National Institute of Advanced Industrial Science and Technology (AIST),
00011 Japan
00012 All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015
00016 File: targets.py
00017
00018 Objects representing targets. These can be target components, execution
00019 contexts or ports.
00020
00021 '''
00022
00023 __version__ = '$Revision: $'
00024
00025
00026
00027 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
00028 RTS_EXT_NS_YAML
00029 from rtsprofile.utils import get_direct_child_elements_xml, \
00030 parse_properties_xml, properties_to_xml, \
00031 validate_attribute
00032
00033
00034
00035
00036
00037 class TargetComponent(object):
00038 '''Stores enough information to uniquely identify a component in the RT
00039 system. Used to specify target components, for example the components
00040 participating in a group or running in an execution context, or the
00041 execution order of components.
00042
00043 '''
00044
00045 def __init__(self, component_id='', instance_name=''):
00046 '''Constructor.
00047
00048 @param component_id The ID of the target component.
00049 @type component_id str
00050 @param instance_name The instance name of the target component.
00051 @type instance_name str
00052
00053 '''
00054 validate_attribute(component_id, 'target_component.componentID',
00055 expected_type=[str, unicode], required=False)
00056 self._component_id = component_id
00057 validate_attribute(instance_name, 'target_component.instanceName',
00058 expected_type=[str, unicode], required=False)
00059 self._instance_name = instance_name
00060 self._properties = {}
00061
00062 def __str__(self):
00063 result = 'Component ID: {0}\nInstance name: {1}\n'.format(\
00064 self.component_id, self.instance_name)
00065 if self.properties:
00066 result += 'Properties:\n'
00067 for p in self.properties:
00068 result += ' {0}: {1}\n'.format(p, self.properties[p])
00069 return result[:-1]
00070
00071 @property
00072 def component_id(self):
00073 '''The ID of the target component.
00074
00075 RT components can be uniquely identified using the component ID and the
00076 instance name.
00077
00078 '''
00079 return self._component_id
00080
00081 @component_id.setter
00082 def component_id(self, component_id):
00083 validate_attribute(component_id, 'target_component.componentID',
00084 expected_type=[str, unicode], required=True)
00085 self._component_id = component_id
00086
00087 @property
00088 def instance_name(self):
00089 '''The instance name of the target component.
00090
00091 RT components can be uniquely identified using the component ID and the
00092 instance name.
00093
00094 '''
00095 return self._instance_name
00096
00097 @instance_name.setter
00098 def instance_name(self, instance_name):
00099 validate_attribute(instance_name, 'target_component.instanceName',
00100 expected_type=[str, unicode], required=True)
00101 self._instance_name = instance_name
00102
00103 @property
00104 def properties(self):
00105 '''Miscellaneous properties.
00106
00107 Stores key/value pair properties.
00108
00109 Part of the extended profile.
00110
00111 '''
00112 return self._properties
00113
00114 @properties.setter
00115 def properties(self, properties):
00116 validate_attribute(properties, 'target_component.ext.Properties',
00117 expected_type=dict, required=False)
00118 self._properties = properties
00119
00120 def parse_xml_node(self, node):
00121 '''Parse an xml.dom Node object representing a target component into
00122 this object.
00123
00124 '''
00125 self.component_id = node.getAttributeNS(RTS_NS, 'componentId')
00126 self.instance_name = node.getAttributeNS(RTS_NS, 'instanceName')
00127 for c in node.getElementsByTagNameNS(RTS_EXT_NS, 'Properties'):
00128 name, value = parse_properties_xml(c)
00129 self._properties[name] = value
00130 return self
00131
00132 def parse_yaml(self, y):
00133 '''Parse a YAML specification of a target component into this
00134 object.
00135
00136 '''
00137 self.component_id = y['componentId']
00138 self.instance_name = y['instanceName']
00139 if RTS_EXT_NS_YAML + 'properties' in y:
00140 for p in y.get(RTS_EXT_NS_YAML + 'properties'):
00141 if 'value' in p:
00142 value = p['value']
00143 else:
00144 value = None
00145 self._properties[p['name']] = value
00146 return self
00147
00148 def save_xml(self, doc, element):
00149 '''Save this target component into an xml.dom.Element object.'''
00150 element.setAttributeNS(RTS_NS, RTS_NS_S + 'componentId',
00151 self.component_id)
00152 element.setAttributeNS(RTS_NS, RTS_NS_S + 'instanceName',
00153 self.instance_name)
00154 for p in self.properties:
00155 new_prop_element = doc.createElementNS(RTS_EXT_NS,
00156 RTS_EXT_NS_S + 'Properties')
00157 properties_to_xml(new_prop_element, p, self.properties[p])
00158 element.appendChild(new_prop_element)
00159
00160 def to_dict(self):
00161 '''Save this target component into a dictionary.'''
00162 d = {'componentId': self.component_id,
00163 'instanceName': self.instance_name}
00164 props = []
00165 for name in self.properties:
00166 p = {'name': name}
00167 if self.properties[name]:
00168 p['value'] = str(self.properties[name])
00169 props.append(p)
00170 if props:
00171 d[RTS_EXT_NS_YAML + 'properties'] = props
00172 return d
00173
00174
00175
00176
00177
00178 class TargetPort(TargetComponent):
00179 '''Stores enough information to uniquely identify a port on a component in
00180 the RT system. Used to specify target ports in connections.
00181
00182 '''
00183
00184 def __init__(self, component_id='', instance_name='', port_name=''):
00185 '''Constructor. See also the @ref TargetComponent constructor.
00186
00187 @param port_name The name of the target port.
00188 @type port_name str
00189
00190 '''
00191 super(TargetPort, self).__init__(component_id, instance_name)
00192 validate_attribute(port_name, 'target_port.portName',
00193 expected_type=[str, unicode], required=False)
00194 self._port_name = port_name
00195
00196 def __str__(self):
00197 return TargetComponent.__str__(self) + '\nPort name: {0}'.format(\
00198 self.port_name)
00199
00200 @property
00201 def port_name(self):
00202 '''The ID of the target port.'''
00203 return self._port_name
00204
00205 @port_name.setter
00206 def port_name(self, port_name):
00207 validate_attribute(port_name, 'target_port.portName',
00208 expected_type=[str, unicode], required=True)
00209 self._port_name = port_name
00210
00211 def parse_xml_node(self, node):
00212 '''Parse an xml.dom Node object representing a target port into this
00213 object.
00214
00215 '''
00216 super(TargetPort, self).parse_xml_node(node)
00217 self.port_name = node.getAttributeNS(RTS_NS, 'portName')
00218 return self
00219
00220 def parse_yaml(self, y):
00221 '''Parse a YAML specification of a target port into this object.'''
00222 super(TargetPort, self).parse_yaml(y)
00223 self.port_name = y['portName']
00224 return self
00225
00226 def save_xml(self, doc, element):
00227 '''Save this target port into an xml.dom.Element object.'''
00228 super(TargetPort, self).save_xml(doc, element)
00229 element.setAttributeNS(RTS_NS, RTS_NS_S + 'portName', self.port_name)
00230
00231 def to_dict(self):
00232 '''Save this target port into a dictionary.'''
00233 d = super(TargetPort, self).to_dict()
00234 d['portName'] = self.port_name
00235 return d
00236
00237
00238
00239
00240
00241 class TargetExecutionContext(TargetComponent):
00242 '''Stores enough information to uniquely identify an execution context in
00243 the RT system. Used to specify target execution contexts of components that
00244 are used to manage execution order, execution conditions, and so on. An
00245 RT component may hold multiple execution contexts. The execution context
00246 concerned must be specified when activating, shutting down, etc an RT
00247 component.
00248
00249 '''
00250
00251 def __init__(self, component_id='', instance_name='', id=''):
00252 '''Constructor. See also the @ref TargetComponent constructor.
00253
00254 @param id The ID of the target execution context.
00255 @type id str
00256
00257 '''
00258 super(TargetExecutionContext, self).__init__(component_id,
00259 instance_name)
00260 validate_attribute(id, 'target_executioncontext.id',
00261 expected_type=[str, unicode], required=False)
00262 self._id = id
00263 self._properties = {}
00264
00265 def __str__(self):
00266 result = TargetComponent.__str__(self) + '\nID: {0}\n'.format(self.id)
00267 if self.properties:
00268 result += 'Properties:\n'
00269 for p in self.properties:
00270 result += ' {0}: {1}\n'.format(p, self.properties[p])
00271 return result[:-1]
00272
00273 @property
00274 def id(self):
00275 '''The ID of the target execution context.'''
00276 return self._id
00277
00278 @id.setter
00279 def id(self, id):
00280 validate_attribute(id, 'target_executioncontext.id',
00281 expected_type=[str, unicode], required=True)
00282 self._id = id
00283
00284 @property
00285 def properties(self):
00286 '''Miscellaneous properties.
00287
00288 Stores key/value pair properties.
00289
00290 Part of the extended profile.
00291
00292 '''
00293 return self._properties
00294
00295 @properties.setter
00296 def properties(self, properties):
00297 validate_attribute(properties,
00298 'target_executioncontext.ext.Properties',
00299 expected_type=dict, required=False)
00300 self._properties = properties
00301
00302 def parse_xml_node(self, node):
00303 '''Parse an xml.dom Node object representing a target execution context
00304 into this object.
00305
00306 '''
00307 super(TargetExecutionContext, self).parse_xml_node(node)
00308 if node.hasAttributeNS(RTS_NS, 'id'):
00309 self.id = node.getAttributeNS(RTS_NS, 'id')
00310 else:
00311 self.id = ''
00312 return self
00313
00314 def parse_yaml(self, y):
00315 '''Parse a YAML specification of a target execution context into this
00316 object.
00317
00318 '''
00319 super(TargetExecutionContext, self).parse_yaml(y)
00320 if 'id' in y:
00321 self.id = y['id']
00322 else:
00323 self.id = ''
00324 return self
00325
00326 def save_xml(self, doc, element):
00327 '''Save this target execution context into an xml.dom.Element
00328 object.
00329
00330 '''
00331 super(TargetExecutionContext, self).save_xml(doc, element)
00332 element.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
00333
00334 def to_dict(self):
00335 '''Save this target execution context into a dictionary.'''
00336 d = super(TargetExecutionContext, self).to_dict()
00337 d['id'] = self.id
00338 return d
00339
00340
00341
00342