comp_mgmt.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Functions for component management.
00018 
00019 '''
00020 
00021 
00022 import OpenRTM_aist
00023 import re
00024 import RTC
00025 import rtctree.tree
00026 import rtctree.utils
00027 import sys
00028 
00029 import gen_comp
00030 import rts_exceptions
00031 
00032 
00033 def find_comp_in_mgr(name, mgr):
00034     '''Find a component in a manager.
00035 
00036     @param name The type name of the component to search for.
00037     @param mgr The manager to which the component is registered.
00038 
00039     '''
00040     for c in mgr.getComponents():
00041         if c.getTypeName() == name:
00042             return c
00043     raise rts_exceptions.MissingCompError(name)
00044 
00045 
00046 def get_comp(rtc, tree=None, orb=None):
00047     '''Get a rtctree.Component object from an rtctree.RTCTree.
00048 
00049     Get the component object by searching the RTCTree for the specified RTC.
00050 
00051     @param rtc Path to the component. This should be in
00052                the format used by rtctree, i.e. a list of path entries, with
00053                the first being a /. e.g. ['/', 'localhost', 'comp0.rtc'].
00054     @param tree An already-populated rtctree.RTCTree object, or None if one
00055                 should be created.
00056     @param orb An ORB to use if the tree must be created, or None to make one.
00057 
00058     '''
00059     if not tree:
00060         tree = rtctree.tree.RTCTree(paths=rtc, orb=orb, filter=[rtc])
00061 
00062     if not tree.has_path(rtc):
00063         raise rts_exceptions.NoSuchObjectError(rtc)
00064     comp = tree.get_node(rtc)
00065     if not comp.is_component:
00066         raise rts_exceptions.NotAComponentError(rtc)
00067     return comp
00068 
00069 
00070 def find_port(rtc, port, tree=None, orb=None):
00071     '''Get a rtctree.Port object from an rtctree.RTCTree.
00072 
00073     Get the port object by searching the RTCTree for the specified RTC, then
00074     looking for the specified port on that component.
00075 
00076     @param rtc Path to the component that should have a port. This should be in
00077                the format used by rtctree, i.e. a list of path entries, with
00078                the first being a /. e.g. ['/', 'localhost', 'comp0.rtc'].
00079     @param port Name of the port.
00080     @param tree An already-populated rtctree.RTCTree object, or None if one
00081                 should be created.
00082     @param orb An ORB to use if the tree must be created, or None to make one.
00083 
00084     '''
00085     comp = get_comp(rtc, tree=tree, orb=orb)
00086     port_obj = comp.get_port_by_name(port)
00087     if not port_obj:
00088         raise rts_exceptions.PortNotFoundError(rtc, port)
00089     return port_obj
00090 
00091 
00092 def choose_name(base, tree):
00093     '''Choose a name for the component from a given base.
00094 
00095     The name is chosen such that it does not conflict with other possible
00096     instances of the base name by appending an index number.
00097 
00098     @param base The base name to append the index to.
00099     @param tree A populated RTCTree to search for other instances of the
00100                 same type of component.
00101 
00102     '''
00103     def get_result(node, args):
00104         return int(regex.match(node.name).group(1))
00105     def is_gen_comp(node):
00106         if regex.match(node.name):
00107             return True
00108         return False
00109     regex = re.compile('{0}(\d+)0.rtc'.format(base))
00110     matches = tree.iterate(get_result, filter=['is_component', is_gen_comp])
00111     if not matches:
00112         return base + '0'
00113     matches.sort()
00114     return base + '{0}'.format(matches[-1] + 1)
00115 
00116 
00117 def make_comp(name_base, tree, cons, port_specs, event=None, rate=1.0,
00118         max=-1, **kwargs):
00119     name = choose_name(name_base, tree)
00120     mgr = OpenRTM_aist.Manager.init(1, [sys.argv[0]])
00121     mgr.setModuleInitProc(gen_comp.make_init(name, cons, port_specs,
00122         event=event, rate=rate, max=max, **kwargs))
00123     mgr.activateManager()
00124     mgr.runManager(True)
00125     return name, mgr
00126 
00127 
00128 def delete_comp(mgr, comp):
00129     '''Delete the component from the manager.'''
00130     mgr.deleteComponent(comp=comp)
00131 
00132 
00133 def shutdown(mgr):
00134     '''Shut down the manager.'''
00135     mgr.shutdown()
00136     mgr.join()
00137 
00138 
00139 def connect(comp, port_specs, tree):
00140     def find_local_port(name, ports):
00141         for p in ports:
00142             if p.get_port_profile().name.split('.')[-1] == name:
00143                 return p
00144         raise rts_exceptions.PortNotFoundError(comp.getTypeName(), name)
00145 
00146     props = {'dataport.dataflow_type':'push',
00147             'dataport.interface_type':'corba_cdr',
00148             'dataport.subscription_type':'flush'}
00149     ports = comp.get_ports()
00150     conns = []
00151     for p in port_specs:
00152         local_port = find_local_port(p.name, ports)
00153         for t in p.targets:
00154             dest_port = find_port(t[0], t[1], tree)
00155             props['dataport.data_type'] = \
00156                     dest_port.properties['dataport.data_type']
00157             prof = RTC.ConnectorProfile(p.name + '_' + t[1],
00158                     '', [local_port, dest_port.object],
00159                     rtctree.utils.dict_to_nvlist(props))
00160             res, connector = local_port.connect(prof)
00161             if res != RTC.RTC_OK:
00162                 raise rts_exceptions.ConnectFailedError(t[0], t[1])
00163             conns.append(connector)
00164     return conns
00165 
00166 
00167 def disconnect(comp):
00168     '''Disconnect all connections to @ref comp.
00169 
00170     @param comp An RTObject.
00171 
00172     '''
00173     ports = comp.get_ports()
00174     for p in ports:
00175         p.disconnect_all()
00176 
00177 
00178 def activate(comp):
00179     for ec in comp.get_owned_contexts():
00180         if ec.activate_component(comp.getObjRef()) != RTC.RTC_OK:
00181             raise rts_exceptions.ActivateError(comp.getTypeName())
00182     for ec in comp.get_participating_contexts():
00183         if ec.activate_component(comp.getObjRef()) != RTC.RTC_OK:
00184             raise rts_exceptions.ActivateError(comp.getTypeName())
00185 
00186 
00187 def deactivate(comp):
00188     for ec in comp.get_owned_contexts():
00189         if ec.deactivate_component(comp.getObjRef()) != RTC.RTC_OK:
00190             raise rts_exceptions.DeactivateError(comp.getTypeName())
00191     for ec in comp.get_participating_contexts():
00192         if ec.deactivate_component(comp.getObjRef()) != RTC.RTC_OK:
00193             raise rts_exceptions.DeactivateError(comp.getTypeName())
00194 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12