exec_context.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: exec_context.py
17 
18 Object representing a component's execution context.
19 
20 '''
21 
22 __version__ = '$Revision: $'
23 # $Source$
24 
25 
26 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
27  RTS_EXT_NS_YAML
28 from rtsprofile.participant import Participant
29 from rtsprofile.utils import get_direct_child_elements_xml, \
30  indent_string, parse_properties_xml, \
31  properties_to_xml, validate_attribute
32 
33 
34 ##############################################################################
35 ## ExecutionContext object
36 
37 class ExecutionContext(object):
38  '''Represents an execution context being used in the RT system.'''
39 
40  def __init__(self, id='', kind='', rate=0.0):
41  '''Constructor.
42 
43  @param id The ID of this execution context.
44  @type id str
45  @param kind The action execution type used by this context.
46  @type kind str
47  @param rate The execution rate of this context, if it is periodic.
48  @type float
49 
50  '''
51  validate_attribute(id, 'execution_context.id',
52  expected_type=[str, unicode], required=False)
53  self._id = id
54  validate_attribute(kind, 'execution_context.kind',
55  expected_type=[str, unicode], required=False)
56  self._kind = kind
57  validate_attribute(rate, 'execution_context.rate',
58  expected_type=[int, float], required=False)
59  self._rate = rate
60  self._participants = []
61  self._properties = {}
62 
63  def __str__(self):
64  result = 'ID: {0}\nKind: {1}\nRate: {2}\n'.format(self.id, self.kind,
65  self.rate)
66  if self.participants:
67  result += 'Participants:\n'
68  for p in self.participants:
69  result += '{0}\n'.format(indent_str(str(p)))
70  if self.properties:
71  result += 'Properties:\n'
72  for p in self.properties:
73  result += ' {0}: {1}\n'.format(p, self.properties[p])
74  return result[:-1] # Lop off the last new line
75 
76  @property
77  def id(self):
78  '''The ID used to identify this execution context.'''
79  return self._id
80 
81  @id.setter
82  def id(self, id):
83  validate_attribute(id, 'execution_context.id',
84  expected_type=[str, unicode], required=True)
85  self._id = id
86 
87  @property
88  def kind(self):
89  '''The action execution type used by this context.
90 
91  Valid values are supposed to be in the specification appendix, but they
92  aren't. The best way to find them is to create a system with
93  RTSystemEditor and look at the XML. A common valid value is
94  PeriodicExecutionContext.
95 
96  '''
97  return self._kind
98 
99  @kind.setter
100  def kind(self, kind):
101  validate_attribute(kind, 'execution_context.kind',
102  expected_type=[str, unicode], required=True)
103  self._kind = kind
104 
105  @property
106  def participants(self):
107  '''The components participating in this execution context.
108 
109  An ordered list. May be an empty list if no components are
110  participating in this context.
111 
112  '''
113  return self._participants
114 
115  @participants.setter
116  def participants(self, participants):
117  validate_attribute(participants, 'execution_context.participants',
118  expected_type = list)
119  self._participants = participants
120 
121  @property
122  def rate(self):
123  '''The execution rate of this context, if it has one, in Hertz.
124 
125  This value is only used if the execution context is periodic for a data
126  flow component.
127 
128  '''
129  return self._rate
130 
131  @rate.setter
132  def rate(self, rate):
133  validate_attribute(rate, 'execution_context.rate',
134  expected_type=[int, float], required=False)
135  self._rate = rate
136 
137  @property
138  def properties(self):
139  '''Miscellaneous properties.
140 
141  Stores key/value pair properties.
142 
143  Part of the extended profile.
144 
145  '''
146  return self._properties
147 
148  @properties.setter
149  def properties(self, properties):
150  validate_attribute(properties, 'execution_context.ext.Properties',
151  expected_type=dict, required=False)
152  self._properties = properties
153 
154  def parse_xml_node(self, node):
155  '''Parse an xml.dom Node object representing an execution context into
156  this object.
157 
158  '''
159  self.id = node.getAttributeNS(RTS_NS, 'id')
160  self.kind = node.getAttributeNS(RTS_NS, 'kind')
161  if node.hasAttributeNS(RTS_NS, 'rate'):
162  self.rate = float(node.getAttributeNS(RTS_NS, 'rate'))
163  else:
164  self.rate = 0.0
165  self._participants = []
166  for c in node.getElementsByTagNameNS(RTS_NS, 'Participants'):
167  self._participants.append(TargetComponent().parse_xml_node(c))
168  for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
169  local_name='Properties'):
170  name, value = parse_properties_xml(c)
171  self._properties[name] = value
172  return self
173 
174  def parse_yaml(self, y):
175  '''Parse a YAML spefication of an execution context into this
176  object.
177 
178  '''
179  self.id = y['id']
180  self.kind = y['kind']
181  if 'rate' in y:
182  self.rate = float(y['rate'])
183  else:
184  self.rate = 0.0
185  self._participants = []
186  if 'participants' in y:
187  for p in y.get('participants'):
188  self._participants.append(TargetComponent().parse_yaml(p))
189  if RTS_EXT_NS_YAML + 'properties' in y:
190  for p in y.get(RTS_EXT_NS_YAML + 'properties'):
191  if 'value' in p:
192  value = p['value']
193  else:
194  value = None
195  self._properties[p['name']] = value
196  return self
197 
198  def save_xml(self, doc, element):
199  '''Save this execution context into an xml.dom.Element object.'''
200  element.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
201  element.setAttributeNS(RTS_NS, RTS_NS_S + 'kind', self.kind)
202  element.setAttributeNS(RTS_NS, RTS_NS_S + 'rate', str(self.rate))
203  for p in self.participants:
204  new_element = doc.createElementNS(RTS_NS,
205  RTS_NS_S + 'Participants')
206  p.save_xml(doc, new_element)
207  element.appendChild(new_element)
208  for p in self.properties:
209  new_prop_element = doc.createElementNS(RTS_EXT_NS,
210  RTS_EXT_NS_S + 'Properties')
211  properties_to_xml(new_prop_element, p, self.properties[p])
212  element.appendChild(new_prop_element)
213 
214  def to_dict(self):
215  '''Save this execution context into a dictionary.'''
216  d = {'id': self.id,
217  'kind': self.kind}
218  if self.rate != 0.0:
219  d['rate'] = self.rate
220  participants = []
221  for p in self.participants:
222  participants.append(p.to_dict())
223  if participants:
224  d['participants'] = participants
225  props = []
226  for name in self.properties:
227  p = {'name': name}
228  if self.properties[name]:
229  p['value'] = str(self.properties[name])
230  props.append(p)
231  if props:
232  d[RTS_EXT_NS_YAML + 'properties'] = props
233  return d
234 
235 
236 # vim: tw=79
237 
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 get_direct_child_elements_xml(node, prefix=None, local_name=None)
Definition: utils.py:50
def __init__(self, id='', kind='', rate=0.0)
Definition: exec_context.py:40


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