PeriodicECSharedComposite.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
20 
21 import string
22 import sys
23 import time
24 
25 from omniORB import CORBA
26 import OpenRTM
27 import RTC
28 import OpenRTM_aist
29 
30 
31 periodicecsharedcomposite_spec = ["implementation_id", "PeriodicECSharedComposite",
32  "type_name", "PeriodicECSharedComposite",
33  "description", "PeriodicECSharedComposite",
34  "version", "1.0",
35  "vendor", "jp.go.aist",
36  "category", "composite.PeriodicECShared",
37  "activity_type", "DataFlowComponent",
38  "max_instance", "0",
39  "language", "Python",
40  "lang_type", "script",
41  "exported_ports", "",
42  "conf.default.members", "",
43  "conf.default.exported_ports", "",
44  ""]
45 
46 
47 def stringToStrVec(v, _is):
48  str = [_is]
49  OpenRTM_aist.eraseBlank(str)
50  v[0] = str[0].split(",")
51  return True
52 
53 
55  def __init__(self, org):
56  self._org = org
57  pass
58 
59  def __call__(self, config_set):
60  self._org.updateDelegatedPorts()
61 
62 
63 
65  def __init__(self, org):
66  self._org = org
67  pass
68 
69  def __call__(self, config_set):
70  self._org.updateDelegatedPorts()
71  return
72 
73 
74 
89 
90 
91 
108  def __init__(self, rtobj):
109  OpenRTM_aist.Organization_impl.__init__(self,rtobj.getObjRef())
110  self._rtobj = rtobj
111  self._ec = None
112  self._rtcMembers = []
113  self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.PeriodicECOrganization")
114  self._expPorts = []
115  return
116 
117 
118 
141  def add_members(self, sdo_list):
142  self._rtcout.RTC_DEBUG("add_members()")
144  for sdo in sdo_list:
145  dfc = [None]
146  if not self.sdoToDFC(sdo, dfc):
147  continue
148  member = self.Member(dfc[0])
149  self.stopOwnedEC(member)
150  self.addOrganizationToTarget(member)
151  self.addParticipantToEC(member)
152  self.addPort(member, self._expPorts)
153  self._rtcMembers.append(member)
154 
155  result = OpenRTM_aist.Organization_impl.add_members(self,sdo_list)
156 
157  return result
158 
159 
160 
184  def set_members(self, sdo_list):
185  self._rtcout.RTC_DEBUG("set_members()")
186  # self._rtcMembers = []
187  self.removeAllMembers()
189 
190  for sdo in sdo_list:
191  dfc = [None]
192  if not self.sdoToDFC(sdo, dfc):
193  continue
194 
195  member = self.Member(dfc[0])
196  self.stopOwnedEC(member)
197  self.addOrganizationToTarget(member)
198  self.addParticipantToEC(member)
199  self.addPort(member, self._expPorts)
200  self._rtcMembers.append(member)
201 
202  result = OpenRTM_aist.Organization_impl.set_members(self, sdo_list)
203 
204  return result
205 
206 
207 
229  def remove_member(self, id):
230  self._rtcout.RTC_DEBUG("remove_member(id = %s)", id)
231  rm_rtc = []
232  for member in self._rtcMembers:
233  if str(id) != str(member._profile.instance_name):
234  continue
235  self.removePort(member, self._expPorts)
236  self._rtobj.getProperties().setProperty("conf.default.exported_ports", OpenRTM_aist.flatten(self._expPorts))
237  self.removeParticipantFromEC(member)
238  self.removeOrganizationFromTarget(member)
239  self.startOwnedEC(member)
240  rm_rtc.append(member)
241 
242  for m in rm_rtc:
243  self._rtcMembers.remove(m)
244 
245  result = OpenRTM_aist.Organization_impl.remove_member(self, id)
246  return result
247 
248 
249 
256  def removeAllMembers(self):
257  self._rtcout.RTC_DEBUG("removeAllMembers()")
259  for member in self._rtcMembers:
260  self.removePort(member, self._expPorts)
261  self.removeParticipantFromEC(member)
262  self.removeOrganizationFromTarget(member)
263  self.startOwnedEC(member)
264  OpenRTM_aist.Organization_impl.remove_member(self, member._profile.instance_name)
265 
266  self._rtcMembers = []
267  self._expPorts = []
268  return
269 
270 
271 
279  def sdoToDFC(self, sdo, dfc):
280  if CORBA.is_nil(sdo):
281  return False
282 
283  dfc[0] = sdo._narrow(OpenRTM.DataFlowComponent)
284  if CORBA.is_nil(dfc[0]):
285  return False
286 
287  return True
288 
289 
290 
298  def stopOwnedEC(self, member):
299  ecs = member._eclist
300  for ec in ecs:
301  ret = ec.stop()
302 
303  return
304 
305 
306 
313  def startOwnedEC(self, member):
314  ecs = member._eclist
315  for ec in ecs:
316  ret = ec.start()
317 
318  return
319 
320 
321 
329  def addOrganizationToTarget(self, member):
330  conf = member._config
331  if CORBA.is_nil(conf):
332  return
333 
334  conf.add_organization(self._objref)
335  return
336 
337 
338 
346  def removeOrganizationFromTarget(self, member):
347  # get given RTC's configuration object
348  if CORBA.is_nil(member._config):
349  return
350 
351  # set organization to target RTC's conf
352  ret = member._config.remove_organization(self._pId)
353  return
354 
355 
356 
364  def addParticipantToEC(self, member):
365  if CORBA.is_nil(self._ec) or self._ec is None:
366  ecs = self._rtobj.get_owned_contexts()
367  if len(ecs) > 0:
368  self._ec = ecs[0]
369  else:
370  return
371  # set ec to target RTC
372  ret = self._ec.add_component(member._rtobj)
373 
374  orglist = member._rtobj.get_organizations()
375  for org in orglist:
376  sdos = org.get_members()
377  for sdo in sdos:
378  dfc = [None]
379  if not self.sdoToDFC(sdo, dfc):
380  continue
381  self._ec.add_component(dfc[0])
382  return
383 
384 
385 
393  def removeParticipantFromEC(self, member):
394  if CORBA.is_nil(self._ec) or self._ec is None:
395  ecs = self._rtobj.get_owned_contexts()
396  if len(ecs) > 0:
397  self._ec = ecs[0]
398  else:
399  self._rtcout.RTC_FATAL("no owned EC")
400  return
401  self._ec.remove_component(member._rtobj)
402 
403  orglist = member._rtobj.get_organizations()
404 
405  for org in orglist:
406  sdos = org.get_members()
407  for sdo in sdos:
408  dfc = [None]
409  if not self.sdoToDFC(sdo, dfc):
410  continue
411  self._ec.remove_component(dfc[0])
412  return
413 
414 
415 
435 
436 
444  def addPort(self, member, portlist):
445  self._rtcout.RTC_TRACE("addPort(%s)", OpenRTM_aist.flatten(portlist))
446  if len(portlist) == 0:
447  return
448 
449  comp_name = member._profile.instance_name
450  plist = member._profile.port_profiles
451 
452  # port delegation
453  for prof in plist:
454  # port name -> comp_name.port_name
455  port_name = prof.name
456 
457  self._rtcout.RTC_DEBUG("port_name: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
458  if not port_name in portlist:
459  self._rtcout.RTC_DEBUG("Not found: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
460  continue
461 
462  self._rtcout.RTC_DEBUG("Found: %s is in %s", (port_name,OpenRTM_aist.flatten(portlist)))
463  self._rtobj.addPort(prof.port_ref)
464  self._rtcout.RTC_DEBUG("Port %s was delegated.", port_name)
465 
466  return
467 
468 
469 
477  def removePort(self, member, portlist):
478  self._rtcout.RTC_DEBUG("removePort()")
479  if len(portlist) == 0:
480  return
481 
482  comp_name = member._profile.instance_name
483  plist = member._profile.port_profiles
484 
485  # port delegation
486  for prof in plist:
487  # port name -> comp_name.port_name
488  port_name = prof.name
489 
490  self._rtcout.RTC_DEBUG("port_name: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
491  if not port_name in portlist:
492  self._rtcout.RTC_DEBUG("Not found: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
493  continue
494 
495  self._rtcout.RTC_DEBUG("Found: %s is in %s", (port_name,OpenRTM_aist.flatten(portlist)))
496  self._rtobj.removePort(prof.port_ref)
497  portlist.remove(port_name)
498  self._rtcout.RTC_DEBUG("Port %s was deleted.", port_name)
499 
500  return
501 
502 
503 
511  plist = self._rtobj.getProperties().getProperty("conf.default.exported_ports")
512  if plist:
513  p = [plist]
514  OpenRTM_aist.eraseBlank(p)
515  self._expPorts = p[0].split(",")
516 
517  return
518 
519 
527  oldPorts = self._expPorts
528  ports = self._rtobj.getProperties().getProperty("conf.default.exported_ports")
529  newPorts = ports.split(",")
530 
531 
532  removedPorts = list(set(oldPorts).difference(set(newPorts)))
533  createdPorts = list(set(newPorts).difference(set(oldPorts)))
534 
535  self._rtcout.RTC_VERBOSE("old ports: %s", OpenRTM_aist.flatten(oldPorts))
536  self._rtcout.RTC_VERBOSE("new ports: %s", OpenRTM_aist.flatten(newPorts))
537  self._rtcout.RTC_VERBOSE("remove ports: %s", OpenRTM_aist.flatten(removedPorts))
538  self._rtcout.RTC_VERBOSE("add ports: %s", OpenRTM_aist.flatten(createdPorts))
539 
540  for member in self._rtcMembers:
541  self.removePort(member, removedPorts)
542  self.addPort(member, createdPorts)
543 
544  self._expPorts = newPorts
545  return
546 
547 
548  class Member:
549  def __init__(self, rtobj):
550  self._rtobj = rtobj
551  self._profile = rtobj.get_component_profile()
552  self._eclist = rtobj.get_owned_contexts()
553  self._config = rtobj.get_configuration()
554  return
555 
556  def __call__(self, x):
557  tmp = x
558  tmp.swap(self)
559  return self
560 
561 
562  def swap(self, x):
563  rtobj = x._rtobj
564  profile = x._profile
565  eclist = x._eclist
566  config = x._config
567 
568  x._rtobj = self._rtobj
569  x._profile = self._profile
570  x._eclist = self._eclist
571  x._config = self._config
572 
573  self._rtobj = rtobj
574  self._profile = profile
575  self._eclist = eclist
576  self._config = config
577  return
578 
579 
580 
594 
595 
618 
619 
620 
637  def __init__(self, manager):
638  OpenRTM_aist.RTObject_impl.__init__(self,manager)
639  self._ref = self._this()
640  self._objref = self._ref
643  self._org.getObjRef())
644 
645  self._members = [[]]
646  self.bindParameter("members", self._members, "", stringToStrVec)
647  self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.periodic_ec_shared")
648  self._configsets.addConfigurationSetListener(\
649  OpenRTM_aist.ConfigurationSetListenerType.ON_SET_CONFIG_SET,
650  setCallback(self._org))
651 
652  self._configsets.addConfigurationSetListener(\
653  OpenRTM_aist.ConfigurationSetListenerType.ON_ADD_CONFIG_SET,
654  addCallback(self._org))
655 
656  return
657 
658 
659 
672  def __del__(self):
673  self._rtcout.RTC_TRACE("destructor of PeriodicECSharedComposite")
674  pass
675 
676 
677 
692  def onInitialize(self):
693  self._rtcout.RTC_TRACE("onInitialize()")
694 
695  active_set = self._properties.getProperty("configuration.active_config",
696  "default")
697  if self._configsets.haveConfig(active_set):
698  self._configsets.update(active_set)
699  else:
700  self._configsets.update("default")
701 
702  mgr = OpenRTM_aist.Manager.instance()
703  sdos = []
704  for member in self._members[0]:
705  if member == "":
706  continue
707 
708  rtc = mgr.getComponent(member)
709 
710  if rtc is None:
711  print "no RTC found: ", member
712  continue
713 
714  sdo = rtc.getObjRef()
715  if CORBA.is_nil(sdo):
716  continue
717 
719 
720  try:
721  self._org.set_members(sdos)
722  except:
723  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
724 
725  return RTC.RTC_OK
726 
727 
728 
759  def onActivated(self, exec_handle):
760  self._rtcout.RTC_TRACE("onActivated(%d)", exec_handle)
761  ecs = self.get_owned_contexts()
762  sdos = self._org.get_members()
763 
764  for sdo in sdos:
765  rtc = sdo._narrow(RTC.RTObject)
766  ecs[0].activate_component(rtc)
767 
768  len_ = len(self._members[0])
769 
770  # since Python 2.5
771  # self._rtcout.RTC_DEBUG("%d member RTC%s activated.", (len_,(lambda x: if x > 1 else "was")(len_)))
772  if len_ > 1:
773  str_ = "s were"
774  else:
775  str_ = "was"
776 
777  self._rtcout.RTC_DEBUG("%d member RTC%s activated.", (len_, str_))
778 
779  return RTC.RTC_OK
780 
781 
782 
813  def onDeactivated(self, exec_handle):
814  self._rtcout.RTC_TRACE("onDeactivated(%d)", exec_handle)
815  ecs = self.get_owned_contexts()
816  sdos = self._org.get_members()
817 
818  for sdo in sdos:
819  rtc = sdo._narrow(RTC.RTObject)
820  ecs[0].deactivate_component(rtc)
821 
822  return RTC.RTC_OK
823 
824 
825 
855  def onReset(self, exec_handle):
856  self._rtcout.RTC_TRACE("onReset(%d)", exec_handle)
857  ecs = self.get_owned_contexts()
858  sdos = self._org.get_members()
859 
860  for sdo in sdos:
861  rtc = sdo._narrow(RTC.RTObject)
862  ecs[0].reset_component(rtc)
863 
864  return RTC.RTC_OK
865 
866 
867 
891  def onFinalize(self):
892  self._rtcout.RTC_TRACE("onFinalize()")
893  self._org.removeAllMembers()
894  self._rtcout.RTC_PARANOID("onFinalize() done")
895  return RTC.RTC_OK
896 
897 
898 
900  profile = OpenRTM_aist.Properties(defaults_str=periodicecsharedcomposite_spec)
901  manager.registerFactory(profile,
903  OpenRTM_aist.Delete)
904 
def addPort(self, member, portlist)
Set CompositeRTC's ExecutionContext to the given DFCvoid setCompositeECToTarget(::OpenRTM::DataFlowCo...
def removeParticipantFromEC(self, member)
Remove participant DFC from CompositeRTC's ExecutionContextvoid PeriodicECOrganization::removePartici...
def split(input, delimiter)
Split string by delimiter.
Definition: StringUtil.py:323
def push_back(seq, elem)
Push the new element back to the CORBA sequence.
def bindParameter(self, param_name, var, def_val, trans=None)
template <typename vartype>=""> bool bindParameter(const char* param_name, VarType& var...
Definition: RTObject.py:2588
def add_members(self, sdo_list)
[CORBA interface] Add Organization member
def onFinalize(self)
[ComponentAction CORBA interface] Finalize RTC
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def stopOwnedEC(self, member)
Stop Owned ExecutionContextsvoid stopOwnedEC(Member& member);.
def updateDelegatedPorts(self)
Update/Remove a member of Organization.
def onDeactivated(self, exec_handle)
Callback function to deactivate.
def onActivated(self, exec_handle)
Callback function to activate.
def removePort(self, member, portlist)
Remove delegated participatns&#39;s ports from the compositevoid removePort(Member& member, PortList& portlist)
def remove_member(self, id)
[CORBA interface] Remove a member of Organization
def startOwnedEC(self, member)
Start Owned ExecutionContexts.
def addOrganizationToTarget(self, member)
Set Organization object to target DFCvoid addOrganizationToTarget(Member& member);.
def set_members(self, sdo_list)
[CORBA interface] Set Organization member
Organization implementation class.
def get_owned_contexts(self)
[CORBA interface] Get ExecutionContextList.
Definition: RTObject.py:803
def sdoToDFC(self, sdo, dfc)
Conversion from SDO to DFCbool sdoToDFC(const SDO_ptr sdo, ::OpenRTM::DataFlowComponent_ptr& dfc);...
def removeOrganizationFromTarget(self, member)
Remove Organization object from a target DFCvoid removeOrganizationFromTarget(Member& member) ...
def onReset(self, exec_handle)
Callback function to reset.
def addParticipantToEC(self, member)
Set CompositeRTC&#39;s ExecutionContext to the given DFCvoid addParticipantToEC(Member& member) ...


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34