targets.py
Go to the documentation of this file.
1 # -*- Python -*-
2 # -*- coding: utf-8 -*-
3 
4 '''rtsprofile
5 
6 Copyright (C) 2009-2010
7  Geoffrey Biggs
8  RT-Synthesis Research Group
9  Intelligent Systems Research Institute,
10  National Institute of Advanced Industrial Science and Technology (AIST),
11  Japan
12  All rights reserved.
13 Licensed under the Eclipse Public License -v 1.0 (EPL)
14 http://www.opensource.org/licenses/eclipse-1.0.txt
15 
16 File: targets.py
17 
18 Objects representing targets. These can be target components, execution
19 contexts or ports.
20 
21 '''
22 
23 __version__ = '$Revision: $'
24 # $Source$
25 
26 
27 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
28  RTS_EXT_NS_YAML
29 from rtsprofile.utils import get_direct_child_elements_xml, \
30  parse_properties_xml, properties_to_xml, \
31  validate_attribute
32 
33 
34 ##############################################################################
35 ## TargetComponent object
36 
37 class TargetComponent(object):
38  '''Stores enough information to uniquely identify a component in the RT
39  system. Used to specify target components, for example the components
40  participating in a group or running in an execution context, or the
41  execution order of components.
42 
43  '''
44 
45  def __init__(self, component_id='', instance_name=''):
46  '''Constructor.
47 
48  @param component_id The ID of the target component.
49  @type component_id str
50  @param instance_name The instance name of the target component.
51  @type instance_name str
52 
53  '''
54  validate_attribute(component_id, 'target_component.componentID',
55  expected_type=[str, unicode], required=False)
56  self._component_id = component_id
57  validate_attribute(instance_name, 'target_component.instanceName',
58  expected_type=[str, unicode], required=False)
59  self._instance_name = instance_name
60  self._properties = {}
61 
62  def __str__(self):
63  result = 'Component ID: {0}\nInstance name: {1}\n'.format(\
64  self.component_id, self.instance_name)
65  if self.properties:
66  result += 'Properties:\n'
67  for p in self.properties:
68  result += ' {0}: {1}\n'.format(p, self.properties[p])
69  return result[:-1] # Lop off the last new line
70 
71  @property
72  def component_id(self):
73  '''The ID of the target component.
74 
75  RT components can be uniquely identified using the component ID and the
76  instance name.
77 
78  '''
79  return self._component_id
80 
81  @component_id.setter
82  def component_id(self, component_id):
83  validate_attribute(component_id, 'target_component.componentID',
84  expected_type=[str, unicode], required=True)
85  self._component_id = component_id
86 
87  @property
88  def instance_name(self):
89  '''The instance name of the target component.
90 
91  RT components can be uniquely identified using the component ID and the
92  instance name.
93 
94  '''
95  return self._instance_name
96 
97  @instance_name.setter
98  def instance_name(self, instance_name):
99  validate_attribute(instance_name, 'target_component.instanceName',
100  expected_type=[str, unicode], required=True)
101  self._instance_name = instance_name
102 
103  @property
104  def properties(self):
105  '''Miscellaneous properties.
106 
107  Stores key/value pair properties.
108 
109  Part of the extended profile.
110 
111  '''
112  return self._properties
113 
114  @properties.setter
115  def properties(self, properties):
116  validate_attribute(properties, 'target_component.ext.Properties',
117  expected_type=dict, required=False)
118  self._properties = properties
119 
120  def parse_xml_node(self, node):
121  '''Parse an xml.dom Node object representing a target component into
122  this object.
123 
124  '''
125  self.component_id = node.getAttributeNS(RTS_NS, 'componentId')
126  self.instance_name = node.getAttributeNS(RTS_NS, 'instanceName')
127  for c in node.getElementsByTagNameNS(RTS_EXT_NS, 'Properties'):
128  name, value = parse_properties_xml(c)
129  self._properties[name] = value
130  return self
131 
132  def parse_yaml(self, y):
133  '''Parse a YAML specification of a target component into this
134  object.
135 
136  '''
137  self.component_id = y['componentId']
138  self.instance_name = y['instanceName']
139  if RTS_EXT_NS_YAML + 'properties' in y:
140  for p in y.get(RTS_EXT_NS_YAML + 'properties'):
141  if 'value' in p:
142  value = p['value']
143  else:
144  value = None
145  self._properties[p['name']] = value
146  return self
147 
148  def save_xml(self, doc, element):
149  '''Save this target component into an xml.dom.Element object.'''
150  element.setAttributeNS(RTS_NS, RTS_NS_S + 'componentId',
151  self.component_id)
152  element.setAttributeNS(RTS_NS, RTS_NS_S + 'instanceName',
153  self.instance_name)
154  for p in self.properties:
155  new_prop_element = doc.createElementNS(RTS_EXT_NS,
156  RTS_EXT_NS_S + 'Properties')
157  properties_to_xml(new_prop_element, p, self.properties[p])
158  element.appendChild(new_prop_element)
159 
160  def to_dict(self):
161  '''Save this target component into a dictionary.'''
162  d = {'componentId': self.component_id,
163  'instanceName': self.instance_name}
164  props = []
165  for name in self.properties:
166  p = {'name': name}
167  if self.properties[name]:
168  p['value'] = str(self.properties[name])
169  props.append(p)
170  if props:
171  d[RTS_EXT_NS_YAML + 'properties'] = props
172  return d
173 
174 
175 ##############################################################################
176 ## TargetPort object
177 
179  '''Stores enough information to uniquely identify a port on a component in
180  the RT system. Used to specify target ports in connections.
181 
182  '''
183 
184  def __init__(self, component_id='', instance_name='', port_name=''):
185  '''Constructor. See also the @ref TargetComponent constructor.
186 
187  @param port_name The name of the target port.
188  @type port_name str
189 
190  '''
191  super(TargetPort, self).__init__(component_id, instance_name)
192  validate_attribute(port_name, 'target_port.portName',
193  expected_type=[str, unicode], required=False)
194  self._port_name = port_name
195 
196  def __str__(self):
197  return TargetComponent.__str__(self) + '\nPort name: {0}'.format(\
198  self.port_name)
199 
200  @property
201  def port_name(self):
202  '''The ID of the target port.'''
203  return self._port_name
204 
205  @port_name.setter
206  def port_name(self, port_name):
207  validate_attribute(port_name, 'target_port.portName',
208  expected_type=[str, unicode], required=True)
209  self._port_name = port_name
210 
211  def parse_xml_node(self, node):
212  '''Parse an xml.dom Node object representing a target port into this
213  object.
214 
215  '''
216  super(TargetPort, self).parse_xml_node(node)
217  self.port_name = node.getAttributeNS(RTS_NS, 'portName')
218  return self
219 
220  def parse_yaml(self, y):
221  '''Parse a YAML specification of a target port into this object.'''
222  super(TargetPort, self).parse_yaml(y)
223  self.port_name = y['portName']
224  return self
225 
226  def save_xml(self, doc, element):
227  '''Save this target port into an xml.dom.Element object.'''
228  super(TargetPort, self).save_xml(doc, element)
229  element.setAttributeNS(RTS_NS, RTS_NS_S + 'portName', self.port_name)
230 
231  def to_dict(self):
232  '''Save this target port into a dictionary.'''
233  d = super(TargetPort, self).to_dict()
234  d['portName'] = self.port_name
235  return d
236 
237 
238 ##############################################################################
239 ## TargetExecutionContext object
240 
242  '''Stores enough information to uniquely identify an execution context in
243  the RT system. Used to specify target execution contexts of components that
244  are used to manage execution order, execution conditions, and so on. An
245  RT component may hold multiple execution contexts. The execution context
246  concerned must be specified when activating, shutting down, etc an RT
247  component.
248 
249  '''
250 
251  def __init__(self, component_id='', instance_name='', id=''):
252  '''Constructor. See also the @ref TargetComponent constructor.
253 
254  @param id The ID of the target execution context.
255  @type id str
256 
257  '''
258  super(TargetExecutionContext, self).__init__(component_id,
259  instance_name)
260  validate_attribute(id, 'target_executioncontext.id',
261  expected_type=[str, unicode], required=False)
262  self._id = id
263  self._properties = {}
264 
265  def __str__(self):
266  result = TargetComponent.__str__(self) + '\nID: {0}\n'.format(self.id)
267  if self.properties:
268  result += 'Properties:\n'
269  for p in self.properties:
270  result += ' {0}: {1}\n'.format(p, self.properties[p])
271  return result[:-1] # Lop off the last new line
272 
273  @property
274  def id(self):
275  '''The ID of the target execution context.'''
276  return self._id
277 
278  @id.setter
279  def id(self, id):
280  validate_attribute(id, 'target_executioncontext.id',
281  expected_type=[str, unicode], required=True)
282  self._id = id
283 
284  @property
285  def properties(self):
286  '''Miscellaneous properties.
287 
288  Stores key/value pair properties.
289 
290  Part of the extended profile.
291 
292  '''
293  return self._properties
294 
295  @properties.setter
296  def properties(self, properties):
297  validate_attribute(properties,
298  'target_executioncontext.ext.Properties',
299  expected_type=dict, required=False)
300  self._properties = properties
301 
302  def parse_xml_node(self, node):
303  '''Parse an xml.dom Node object representing a target execution context
304  into this object.
305 
306  '''
307  super(TargetExecutionContext, self).parse_xml_node(node)
308  if node.hasAttributeNS(RTS_NS, 'id'):
309  self.id = node.getAttributeNS(RTS_NS, 'id')
310  else:
311  self.id = ''
312  return self
313 
314  def parse_yaml(self, y):
315  '''Parse a YAML specification of a target execution context into this
316  object.
317 
318  '''
319  super(TargetExecutionContext, self).parse_yaml(y)
320  if 'id' in y:
321  self.id = y['id']
322  else:
323  self.id = ''
324  return self
325 
326  def save_xml(self, doc, element):
327  '''Save this target execution context into an xml.dom.Element
328  object.
329 
330  '''
331  super(TargetExecutionContext, self).save_xml(doc, element)
332  element.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
333 
334  def to_dict(self):
335  '''Save this target execution context into a dictionary.'''
336  d = super(TargetExecutionContext, self).to_dict()
337  d['id'] = self.id
338  return d
339 
340 
341 # vim: tw=79
342 
def __init__(self, component_id='', instance_name='', id='')
Definition: targets.py:251
TargetComponent object.
Definition: targets.py:37
def save_xml(self, doc, element)
Definition: targets.py:326
def parse_xml_node(self, node)
Definition: targets.py:211
def save_xml(self, doc, element)
Definition: targets.py:226
def parse_xml_node(self, node)
Definition: targets.py:120
def save_xml(self, doc, element)
Definition: targets.py:148
def properties_to_xml(element, name, value=None)
Definition: utils.py:86
def parse_properties_xml(node)
Definition: utils.py:77
def validate_attribute(attr, name, expected_type=None, required=False)
Definition: utils.py:92
def __init__(self, component_id='', instance_name='')
Definition: targets.py:45
TargetPort object.
Definition: targets.py:178
TargetExecutionContext object.
Definition: targets.py:241
def __init__(self, component_id='', instance_name='', port_name='')
Definition: targets.py:184


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Fri Jun 7 2019 21:52:35