exec_context.py
Go to the documentation of this file.
00001 # -*- Python -*-
00002 # -*- coding: utf-8 -*-
00003 
00004 '''rtctree
00005 
00006 Copyright (C) 2009-2014
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 Object representing an execution context.
00017 
00018 '''
00019 
00020 
00021 import RTC
00022 import threading
00023 
00024 from rtctree.utils import build_attr_string, nvlist_to_dict
00025 
00026 
00027 ##############################################################################
00028 ## Execution context object
00029 
00030 class ExecutionContext(object):
00031     '''An execution context, within which components may be executing.'''
00032     def __init__(self, ec_obj=None, handle=None, *args, **kwargs):
00033         '''Constructor.
00034 
00035         @param ec_obj The CORBA ExecutionContext object to wrap.
00036         @param handle The handle of this execution context, which can be used
00037                       to uniquely identify it.
00038 
00039         '''
00040         super(ExecutionContext, self).__init__(*args, **kwargs)
00041         self._is_service = True
00042         self._obj = ec_obj._narrow(RTC.ExecutionContextService)
00043         if not self._obj:
00044             # EC does not implement the ExecutionContextService interface
00045             self._is_service = False
00046             self._obj = ec_obj
00047         self._handle = handle
00048         self._mutex = threading.RLock()
00049         self._parse()
00050 
00051     def activate_component(self, comp_ref):
00052         '''Activate a component within this context.
00053 
00054         @param comp_ref The CORBA LightweightRTObject to activate.
00055 
00056         '''
00057         with self._mutex:
00058             self._obj.activate_component(comp_ref)
00059 
00060     def deactivate_component(self, comp_ref):
00061         '''Deactivate a component within this context.
00062 
00063         @param comp_ref The CORBA LightweightRTObject to deactivate.
00064 
00065         '''
00066         with self._mutex:
00067             self._obj.deactivate_component(comp_ref)
00068 
00069     def reset_component(self, comp_ref):
00070         '''Reset a component within this context.
00071 
00072         @param comp_ref The CORBA LightweightRTObject to reset.
00073 
00074         '''
00075         with self._mutex:
00076             self._obj.reset_component(comp_ref)
00077 
00078     def get_component_state(self, comp):
00079         '''Get the state of a component within this context.
00080 
00081         @param comp The CORBA LightweightRTObject to get the state of.
00082         @return The component state, as a LifeCycleState value.
00083 
00084         '''
00085         with self._mutex:
00086             return self._obj.get_component_state(comp)
00087 
00088     def kind_as_string(self, add_colour=True):
00089         '''Get the type of this context as an optionally coloured string.
00090 
00091         @param add_colour If True, ANSI colour codes will be added.
00092         @return A string describing the kind of execution context this is.
00093 
00094         '''
00095         with self._mutex:
00096             if self.kind == self.PERIODIC:
00097                 result = 'Periodic', ['reset']
00098             elif self.kind == self.EVENT_DRIVEN:
00099                 result = 'Event-driven', ['reset']
00100             elif self.kind == self.OTHER:
00101                 result = 'Other', ['reset']
00102         if add_colour:
00103             return build_attr_string(result[1], supported=add_colour) + \
00104                     result[0] + build_attr_string('reset', supported=add_colour)
00105         else:
00106             return result[0]
00107 
00108     def reparse(self):
00109         '''Reparse this execution context.
00110 
00111         This causes the execution context's state, profile and other
00112         information to be reloaded from the remote object.
00113 
00114         '''
00115         self._parse()
00116 
00117     def running_as_string(self, add_colour=True):
00118         '''Get the state of this context as an optionally coloured string.
00119 
00120         @param add_colour If True, ANSI colour codes will be added.
00121         @return A string describing this context's running state.
00122 
00123         '''
00124         with self._mutex:
00125             if self.running:
00126                 result = 'Running', ['bold', 'green']
00127             else:
00128                 result = 'Stopped', ['reset']
00129         if add_colour:
00130             return build_attr_string(result[1], supported=add_colour) + \
00131                     result[0] + build_attr_string('reset', supported=add_colour)
00132         else:
00133             return result[0]
00134 
00135     def start(self):
00136         '''Start the context.'''
00137         with self._mutex:
00138             self._obj.start()
00139 
00140     def stop(self):
00141         '''Stop the context.'''
00142         with self._mutex:
00143             self._obj.stop()
00144 
00145     @property
00146     def handle(self):
00147         '''The handle of this execution context.'''
00148         with self._mutex:
00149             return self._handle
00150 
00151     @property
00152     def kind(self):
00153         '''The kind of this execution context.'''
00154         with self._mutex:
00155             kind = self._obj.get_kind()
00156             if kind == RTC.PERIODIC:
00157                 return self.PERIODIC
00158             elif kind == RTC.EVENT_DRIVEN:
00159                 return self.EVENT_DRIVEN
00160             else:
00161                 return self.OTHER
00162 
00163     @property
00164     def kind_string(self):
00165         '''The kind of this execution context as a coloured string.'''
00166         return self.kind_as_string()
00167 
00168     @property
00169     def owner(self):
00170         '''The RTObject that owns this context.'''
00171         with self._mutex:
00172             return self._owner
00173 
00174     @property
00175     def owner_name(self):
00176         '''The name of the RTObject that owns this context.'''
00177         with self._mutex:
00178             if self._owner:
00179                 return self._owner.get_component_profile().instance_name
00180             else:
00181                 return ''
00182 
00183     @property
00184     def participants(self):
00185         '''The list of RTObjects participating in this context.'''
00186         with self._mutex:
00187             return self._participants
00188 
00189     @property
00190     def participant_names(self):
00191         '''The names of the RTObjects participating in this context.'''
00192         with self._mutex:
00193             return [obj.get_component_profile().instance_name \
00194                     for obj in self._participants]
00195 
00196     @property
00197     def properties(self):
00198         '''The execution context's extra properties dictionary.'''
00199         with self._mutex:
00200             return self._properties
00201 
00202     @property
00203     def rate(self):
00204         '''The execution rate of this execution context.'''
00205         with self._mutex:
00206             return self._obj.get_rate()
00207 
00208     @rate.setter
00209     def rate(self, new_rate):
00210         with self._mutex:
00211             self._obj.set_rate(new_rate)
00212 
00213     @property
00214     def running(self):
00215         '''Is this execution context running?'''
00216         with self._mutex:
00217             return self._obj.is_running()
00218 
00219     @property
00220     def running_string(self):
00221         '''The state of this execution context as a coloured string.'''
00222         return self.running_as_string()
00223 
00224     def _parse(self):
00225         # Parse the ExecutionContext object.
00226         with self._mutex:
00227             if self._is_service:
00228                 profile = self._obj.get_profile()
00229                 self._owner = profile.owner
00230                 self._participants = profile.participants
00231                 self._properties = nvlist_to_dict(profile.properties)
00232             else:
00233                 self._owner = None
00234                 self._participants = []
00235                 self._properties = []
00236 
00237     ## Constant for a periodic execution context.
00238     PERIODIC = 1
00239     ## Constant for an event driven execution context.
00240     EVENT_DRIVEN = 2
00241     ## Constant for an execution context of some other type.
00242     OTHER = 3
00243 
00244 
00245 # vim: tw=79
00246 


rtctree
Author(s): Geoffrey Biggs
autogenerated on Wed Aug 26 2015 16:13:08