exec_context.py
Go to the documentation of this file.
1 # -*- Python -*-
2 # -*- coding: utf-8 -*-
3 
4 '''rtctree
5 
6 Copyright (C) 2009-2014
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 Object representing an execution context.
17 
18 '''
19 
20 
21 import RTC
22 import threading
23 
24 from rtctree.utils import build_attr_string, nvlist_to_dict
25 
26 
27 ##############################################################################
28 ## Execution context object
29 
30 class ExecutionContext(object):
31  '''An execution context, within which components may be executing.'''
32  def __init__(self, ec_obj=None, handle=None, *args, **kwargs):
33  '''Constructor.
34 
35  @param ec_obj The CORBA ExecutionContext object to wrap.
36  @param handle The handle of this execution context, which can be used
37  to uniquely identify it.
38 
39  '''
40  super(ExecutionContext, self).__init__(*args, **kwargs)
41  self._is_service = True
42  self._obj = ec_obj._narrow(RTC.ExecutionContextService)
43  if not self._obj:
44  # EC does not implement the ExecutionContextService interface
45  self._is_service = False
46  self._obj = ec_obj
47  self._handle = handle
48  self._mutex = threading.RLock()
49  self._parse()
50 
51  def activate_component(self, comp_ref):
52  '''Activate a component within this context.
53 
54  @param comp_ref The CORBA LightweightRTObject to activate.
55 
56  '''
57  with self._mutex:
58  self._obj.activate_component(comp_ref)
59 
60  def deactivate_component(self, comp_ref):
61  '''Deactivate a component within this context.
62 
63  @param comp_ref The CORBA LightweightRTObject to deactivate.
64 
65  '''
66  with self._mutex:
67  self._obj.deactivate_component(comp_ref)
68 
69  def reset_component(self, comp_ref):
70  '''Reset a component within this context.
71 
72  @param comp_ref The CORBA LightweightRTObject to reset.
73 
74  '''
75  with self._mutex:
76  self._obj.reset_component(comp_ref)
77 
78  def get_component_state(self, comp):
79  '''Get the state of a component within this context.
80 
81  @param comp The CORBA LightweightRTObject to get the state of.
82  @return The component state, as a LifeCycleState value.
83 
84  '''
85  with self._mutex:
86  return self._obj.get_component_state(comp)
87 
88  def kind_as_string(self, add_colour=True):
89  '''Get the type of this context as an optionally coloured string.
90 
91  @param add_colour If True, ANSI colour codes will be added.
92  @return A string describing the kind of execution context this is.
93 
94  '''
95  with self._mutex:
96  if self.kind == self.PERIODIC:
97  result = 'Periodic', ['reset']
98  elif self.kind == self.EVENT_DRIVEN:
99  result = 'Event-driven', ['reset']
100  elif self.kind == self.OTHER:
101  result = 'Other', ['reset']
102  if add_colour:
103  return build_attr_string(result[1], supported=add_colour) + \
104  result[0] + build_attr_string('reset', supported=add_colour)
105  else:
106  return result[0]
107 
108  def reparse(self):
109  '''Reparse this execution context.
110 
111  This causes the execution context's state, profile and other
112  information to be reloaded from the remote object.
113 
114  '''
115  self._parse()
116 
117  def running_as_string(self, add_colour=True):
118  '''Get the state of this context as an optionally coloured string.
119 
120  @param add_colour If True, ANSI colour codes will be added.
121  @return A string describing this context's running state.
122 
123  '''
124  with self._mutex:
125  if self.running:
126  result = 'Running', ['bold', 'green']
127  else:
128  result = 'Stopped', ['reset']
129  if add_colour:
130  return build_attr_string(result[1], supported=add_colour) + \
131  result[0] + build_attr_string('reset', supported=add_colour)
132  else:
133  return result[0]
134 
135  def start(self):
136  '''Start the context.'''
137  with self._mutex:
138  self._obj.start()
139 
140  def stop(self):
141  '''Stop the context.'''
142  with self._mutex:
143  self._obj.stop()
144 
145  @property
146  def handle(self):
147  '''The handle of this execution context.'''
148  with self._mutex:
149  return self._handle
150 
151  @property
152  def kind(self):
153  '''The kind of this execution context.'''
154  with self._mutex:
155  kind = self._obj.get_kind()
156  if kind == RTC.PERIODIC:
157  return self.PERIODIC
158  elif kind == RTC.EVENT_DRIVEN:
159  return self.EVENT_DRIVEN
160  else:
161  return self.OTHER
162 
163  @property
164  def kind_string(self):
165  '''The kind of this execution context as a coloured string.'''
166  return self.kind_as_string()
167 
168  @property
169  def owner(self):
170  '''The RTObject that owns this context.'''
171  with self._mutex:
172  return self._owner
173 
174  @property
175  def owner_name(self):
176  '''The name of the RTObject that owns this context.'''
177  with self._mutex:
178  if self._owner:
179  return self._owner.get_component_profile().instance_name
180  else:
181  return ''
182 
183  @property
184  def participants(self):
185  '''The list of RTObjects participating in this context.'''
186  with self._mutex:
187  return self._participants
188 
189  @property
190  def participant_names(self):
191  '''The names of the RTObjects participating in this context.'''
192  with self._mutex:
193  return [obj.get_component_profile().instance_name \
194  for obj in self._participants]
195 
196  @property
197  def properties(self):
198  '''The execution context's extra properties dictionary.'''
199  with self._mutex:
200  return self._properties
201 
202  @property
203  def rate(self):
204  '''The execution rate of this execution context.'''
205  with self._mutex:
206  return self._obj.get_rate()
207 
208  @rate.setter
209  def rate(self, new_rate):
210  with self._mutex:
211  self._obj.set_rate(new_rate)
212 
213  @property
214  def running(self):
215  '''Is this execution context running?'''
216  with self._mutex:
217  return self._obj.is_running()
218 
219  @property
220  def running_string(self):
221  '''The state of this execution context as a coloured string.'''
222  return self.running_as_string()
223 
224  def _parse(self):
225  # Parse the ExecutionContext object.
226  with self._mutex:
227  if self._is_service:
228  profile = self._obj.get_profile()
229  self._owner = profile.owner
230  self._participants = profile.participants
231  self._properties = nvlist_to_dict(profile.properties)
232  else:
233  self._owner = None
234  self._participants = []
235  self._properties = []
236 
237  ## Constant for a periodic execution context.
238  PERIODIC = 1
239  ## Constant for an event driven execution context.
240  EVENT_DRIVEN = 2
241  ## Constant for an execution context of some other type.
242  OTHER = 3
243 
244 
245 # vim: tw=79
246 
Execution context object.
Definition: exec_context.py:30
int EVENT_DRIVEN
Constant for an event driven execution context.
int PERIODIC
Constant for a periodic execution context.
def build_attr_string(attrs, supported=True)
Definition: utils.py:62
def __init__(self, ec_obj=None, handle=None, args, kwargs)
Definition: exec_context.py:32
def activate_component(self, comp_ref)
Definition: exec_context.py:51
def nvlist_to_dict(nvlist)
Definition: utils.py:169
def running_as_string(self, add_colour=True)
def deactivate_component(self, comp_ref)
Definition: exec_context.py:60
def kind_as_string(self, add_colour=True)
Definition: exec_context.py:88
int OTHER
Constant for an execution context of some other type.


rtctree
Author(s): Geoffrey Biggs
autogenerated on Fri Jun 7 2019 21:56:24