00001 #!/usr/bin/env python 00002 # -*- coding: euc-jp -*- 00003 00004 ## 00005 # @file PeriodicExecutionContext.py 00006 # @brief PeriodicExecutionContext class 00007 # @date $Date: 2007/08/29$ 00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara 00009 # 00010 # Copyright (C) 2006-2008 00011 # Task-intelligence Research Group, 00012 # Intelligent Systems Research Institute, 00013 # National Institute of 00014 # Advanced Industrial Science and Technology (AIST), Japan 00015 # All rights reserved. 00016 00017 00018 import copy 00019 import threading 00020 import time 00021 from omniORB import CORBA, PortableServer 00022 00023 import OpenRTM 00024 import RTC, RTC__POA 00025 00026 ## 00027 # @if jp 00028 # @class PeriodicExecutionContext 00029 # @brief PeriodicExecutionContext クラス 00030 # 00031 # Periodic Sampled Data Processing(周期実行用)ExecutionContextクラス。 00032 # 00033 # @since 0.4.0 00034 # 00035 # @else 00036 # @class PeriodicExecutionContext 00037 # @brief PeriodicExecutionContext class 00038 # @endif 00039 class PeriodicExecutionContext(OpenRTM.ExecutionContextBase): 00040 """ 00041 """ 00042 00043 00044 00045 ## 00046 # @if jp 00047 # @class DFP 00048 # @brief DFP クラス 00049 # 00050 # 参加者リストに登録された DataFlowParticipant の関数を起動するための 00051 # 内部クラス。 00052 # 00053 # @param Object 管理対象コンポーネントの型 00054 # 00055 # @else 00056 # 00057 # @endif 00058 class DFP: 00059 00060 00061 00062 ## 00063 # @if jp 00064 # @brief デフォルトコンストラクタ 00065 # 00066 # デフォルトコンストラクタ 00067 # 00068 # @param self 00069 # @param obj 管理対象コンポーネント 00070 # @param id_ 所属する ExecutionContext のID 00071 # 00072 # @else 00073 # @brief Constructor 00074 # @endif 00075 def __init__(self, obj, id_): 00076 self._obj = obj 00077 self._active = True 00078 self.ec_id = id_ 00079 self._sm = OpenRTM.StateMachine(3) 00080 self._sm.setListener(self) 00081 self._sm.setEntryAction (RTC.ACTIVE_STATE, 00082 self.on_activated) 00083 self._sm.setDoAction (RTC.ACTIVE_STATE, 00084 self.on_execute) 00085 self._sm.setPostDoAction(RTC.ACTIVE_STATE, 00086 self.on_state_update) 00087 self._sm.setExitAction (RTC.ACTIVE_STATE, 00088 self.on_deactivated) 00089 self._sm.setEntryAction (RTC.ERROR_STATE, 00090 self.on_aborting) 00091 self._sm.setDoAction (RTC.ERROR_STATE, 00092 self.on_error) 00093 self._sm.setExitAction (RTC.ERROR_STATE, 00094 self.on_reset) 00095 st = OpenRTM.StateHolder() 00096 st.prev = RTC.INACTIVE_STATE 00097 st.curr = RTC.INACTIVE_STATE 00098 st.next = RTC.INACTIVE_STATE 00099 self._sm.setStartState(st) 00100 self._sm.goTo(RTC.INACTIVE_STATE) 00101 00102 00103 ## 00104 # @if jp 00105 # @brief ExecutionContext 実行開始時に呼ばれる関数 00106 # 00107 # 参加している ExecutionContext が実行を開始する時(Running状態へ遷移時) 00108 # に、管理対象コンポーネントの on_startup を呼びだす。 00109 # 00110 # @param self 00111 # 00112 # @else 00113 # 00114 # @brief 00115 # 00116 # @endif 00117 def on_startup(self): 00118 return self._obj.on_startup(self.ec_id) 00119 00120 00121 ## 00122 # @if jp 00123 # @brief ExecutionContext 停止時に呼ばれる関数 00124 # 00125 # 参加している ExecutionContext が実行を停止する時(Stopped状態へ遷移時) 00126 # に、管理対象コンポーネントの on_shutdown を呼びだす。 00127 # 00128 # @param self 00129 # 00130 # @else 00131 # 00132 # @endif 00133 def on_shutdown(self): 00134 return self._obj.on_shutdown(self.ec_id) 00135 00136 00137 ## 00138 # @if jp 00139 # @brief RTコンポーネントがアクティブ化された時に呼ばれる関数 00140 # 00141 # 管理対象のRTコンポーネントがアクティブ化された時(Active状態へ遷移時) 00142 # に、管理対象コンポーネントの on_activated を呼びだす。 00143 # 管理対象コンポーネントのアクティブ化が失敗した場合には、ステートマシン 00144 # を Error 状態に遷移させる。 00145 # 00146 # @param self 00147 # @param st 対象RTコンポーネントの現在の状態 00148 # 00149 # @else 00150 # 00151 # @endif 00152 def on_activated(self, st): 00153 if self._obj.on_activated(self.ec_id) != RTC.RTC_OK: 00154 self._sm.goTo(RTC.ERROR_STATE) 00155 return 00156 return 00157 00158 00159 ## 00160 # @if jp 00161 # @brief RTコンポーネントが非アクティブ化された時に呼ばれる関数 00162 # 00163 # 管理対象のRTコンポーネントが非アクティブ化された時 00164 # (Deactive状態へ遷移時)に、管理対象コンポーネントの on_deactivated を 00165 # 呼びだす。 00166 # 00167 # @param self 00168 # @param st 対象RTコンポーネントの現在の状態 00169 # 00170 # @else 00171 # 00172 # @endif 00173 def on_deactivated(self, st): 00174 self._obj.on_deactivated(self.ec_id) 00175 00176 00177 ## 00178 # @if jp 00179 # @brief RTコンポーネントでエラーが発生した時に呼ばれる関数 00180 # 00181 # 管理対象のRTコンポーネントにエラーが発生した時(Error状態へ遷移時) 00182 # に管理対象コンポーネントの on_aborting を呼びだす。 00183 # 00184 # @param self 00185 # @param st 対象RTコンポーネントの現在の状態 00186 # 00187 # @else 00188 # 00189 # @brief 00190 # 00191 # @endif 00192 def on_aborting(self, st): 00193 self._obj.on_aborting(self.ec_id) 00194 00195 00196 ## 00197 # @if jp 00198 # @brief RTコンポーネントがエラー状態の時に呼ばれる関数 00199 # 00200 # 管理対象のRTコンポーネントがエラー状態にいる間、 00201 # 管理対象コンポーネントの on_aborting を定期的に呼びだす。 00202 # 00203 # @param self 00204 # @param st 対象RTコンポーネントの現在の状態 00205 # 00206 # @else 00207 # 00208 # @brief 00209 # 00210 # @endif 00211 def on_error(self, st): 00212 self._obj.on_error(self.ec_id) 00213 00214 00215 ## 00216 # @if jp 00217 # @brief RTコンポーネントをリセットする時に呼ばれる関数 00218 # 00219 # 管理対象のRTコンポーネントをリセットする際に、管理対象コンポーネント 00220 # の on_reset を呼びだす。 00221 # 00222 # @param self 00223 # @param st 対象RTコンポーネントの現在の状態 00224 # 00225 # @else 00226 # 00227 # @endif 00228 def on_reset(self, st): 00229 if self._obj.on_reset(self.ec_id) != RTC.RTC_OK: 00230 self._sm.goTo(RTC.ERROR_STATE) 00231 return 00232 return 00233 00234 00235 ## 00236 # @if jp 00237 # @brief RTコンポーネント実行時に定期的に呼ばれる関数 00238 # 00239 # 管理対象のRTコンポーネントが Active 状態であるとともに、 00240 # ExecutionContext が Running 状態の場合に、設定された動作周期で定期的に 00241 # 管理対象コンポーネントの on_execute を呼びだす。 00242 # 関数の実行に失敗した場合(返値が RTC_OK 以外)、管理対象コンポーネントの 00243 # 状態を Error 状態に遷移させる。 00244 # 00245 # @param self 00246 # @param st 対象RTコンポーネントの現在の状態 00247 # 00248 # @else 00249 # 00250 # @endif 00251 def on_execute(self, st): 00252 if self._obj.on_execute(self.ec_id) != RTC.RTC_OK: 00253 self._sm.goTo(RTC.ERROR_STATE) 00254 return 00255 return 00256 00257 00258 ## 00259 # @if jp 00260 # @brief RTコンポーネント実行時に定期的に呼ばれる関数 00261 # 00262 # 管理対象のRTコンポーネントが Active 状態であるとともに、 00263 # ExecutionContext が Running 状態の場合に、設定された動作周期で定期的に 00264 # 管理対象コンポーネントの on_state_update を呼びだす。 00265 # 関数の実行に失敗した場合(返値が RTC_OK 以外)、管理対象コンポーネントの 00266 # 状態を Error 状態に遷移させる。 00267 # 00268 # @param self 00269 # @param st 対象RTコンポーネントの現在の状態 00270 # 00271 # @else 00272 # 00273 # @endif 00274 def on_state_update(self, st): 00275 if self._obj.on_state_update(self.ec_id) != RTC.RTC_OK: 00276 self._sm.goTo(RTC.ERROR_STATE) 00277 return 00278 return 00279 00280 00281 ## 00282 # @if jp 00283 # @brief ExecutionContext の実行周期変更時に呼ばれる関数 00284 # 00285 # 参加している ExecutionContext の実行周期が変更となった場合に、 00286 # 管理対象コンポーネントの on_rate_changed を呼びだす。 00287 # 00288 # @param self 00289 # 00290 # @else 00291 # 00292 # @endif 00293 def on_rate_changed(self): 00294 self._obj.on_rate_changed(self.ec_id) 00295 00296 00297 ## 00298 # @if jp 00299 # @brief 状態遷移を実行するワーカーを取得する 00300 # 00301 # 管理対象RTコンポーネントの状態遷移を実行するワーカーを取得する。 00302 # 00303 # @param self 00304 # 00305 # @return ワーカー 00306 # 00307 # @else 00308 # 00309 # @brief 00310 # 00311 # @endif 00312 def worker(self): 00313 return self._sm.worker() 00314 00315 00316 ## 00317 # @if jp 00318 # @brief 現在の状態を取得する 00319 # 00320 # 管理対象RTコンポーネントの現在の状態を取得する。 00321 # 00322 # @param self 00323 # 00324 # @return 現在状態 00325 # 00326 # @else 00327 # 00328 # @brief 00329 # 00330 # @endif 00331 def get_state(self): 00332 return self._sm.getState() 00333 00334 00335 ## 00336 # @if jp 00337 # @brief コンストラクタ 00338 # 00339 # コンストラクタ 00340 # 設定された値をプロファイルに設定する。 00341 # 00342 # @param self 00343 # @param owner 当該 Executioncontext の owner(デフォルト値:None) 00344 # @param rate 動作周期(Hz)(デフォルト値:None) 00345 # 00346 # @else 00347 # @brief Constructor 00348 # @endif 00349 def __init__(self, owner=None, rate=None): 00350 self._nowait = False 00351 self._running = False 00352 00353 if rate is None: 00354 self._rate = 1000.0 00355 rate_ = 0.0 00356 self._usec = long(0) 00357 else: 00358 self._rate = rate 00359 rate_ = rate 00360 if rate == 0: 00361 rate_ = 0.0000001 00362 self._usec = long(1000000/rate_) 00363 if self._usec == 0: 00364 self._nowait = True 00365 self._comps = [] 00366 self._profile = RTC.ExecutionContextProfile(RTC.PERIODIC, rate_, None, [], []) 00367 self._ref = self._this() 00368 self._thread = threading.Thread(target=self.run) 00369 00370 00371 ## 00372 # @if jp 00373 # @brief CORBA オブジェクト参照の取得 00374 # 00375 # 本オブジェクトの ExecutioncontextService としての CORBA オブジェクト参照 00376 # を取得する。 00377 # 00378 # @return CORBA オブジェクト参照 00379 # 00380 # @param self 00381 # 00382 # @else 00383 # 00384 # @endif 00385 def getRef(self): 00386 return self._ref 00387 00388 00389 ## 00390 # @if jp 00391 # @brief コンポーネントのアクティビティスレッド関数 00392 # 00393 # コンポーネントの内部アクティビティスレッドの実行関数。 00394 # ACE_Task サービスクラスメソッドのオーバーライド。 00395 # 00396 # @else 00397 # 00398 # @brief Create internal activity thread 00399 # 00400 # Run by a daemon thread to handle deferred processing. 00401 # ACE_Task class method override. 00402 # 00403 # @endif 00404 def run(self): 00405 flag = True 00406 00407 while flag: 00408 sec_ = float(self._usec)/1000000.0 00409 for comp in self._comps: 00410 comp._sm.worker() 00411 00412 #while not self._running: 00413 #time.sleep(sec_) 00414 if not self._nowait: 00415 time.sleep(sec_) 00416 00417 flag = self._running 00418 00419 return 0 00420 00421 00422 ## 00423 # @if jp 00424 # @brief ExecutionContext 用のスレッド実行関数 00425 # 00426 # ExecutionContext 用のスレッド終了時に呼ばれる。 00427 # コンポーネントオブジェクトの非アクティブ化、マネージャへの通知を行う。 00428 # これは ACE_Task サービスクラスメソッドのオーバーライド。 00429 # 00430 # @param self 00431 # @param flags 終了処理フラグ 00432 # 00433 # @return 終了処理結果 00434 # 00435 # @else 00436 # 00437 # @brief Close activity thread 00438 # 00439 # close() method is called when activity thread svc() is returned. 00440 # This method deactivate this object and notify it to manager. 00441 # ACE_Task class method override. 00442 # 00443 # @endif 00444 def close(self, flags): 00445 return 0 00446 00447 00448 ## 00449 # @if jp 00450 # @brief ExecutionContext 実行状態確認関数 00451 # 00452 # この操作は ExecutionContext が Runnning 状態の場合に true を返す。 00453 # Executioncontext が Running の間、当該 Executioncontext に参加している 00454 # 全てのアクティブRTコンポーネントが、 ExecutionContext の実行種類に応じて 00455 # 実行される。 00456 # 00457 # @param self 00458 # 00459 # @return 状態確認関数(動作中:true、停止中:false) 00460 # 00461 # @else 00462 # 00463 # @brief Check for ExecutionContext running state 00464 # 00465 # This operation shall return true if the context is in the Running state. 00466 # While the context is Running, all Active RTCs participating 00467 # in the context shall be executed according to the context’s execution 00468 # kind. 00469 # 00470 # @endif 00471 def is_running(self): 00472 return self._running 00473 00474 00475 ## 00476 # @if jp 00477 # @brief ExecutionContext の実行を開始 00478 # 00479 # ExecutionContext の実行状態を Runnning とするためのリクエストを発行する。 00480 # ExecutionContext の状態が遷移すると ComponentAction::on_startup が 00481 # 呼び出される。 00482 # 参加しているRTコンポーネントが、初期化されるまで ExecutionContext を開始 00483 # することはできない。 00484 # ExecutionContext は複数回開始/停止を繰り返すことができる。 00485 # 00486 # @param self 00487 # 00488 # @return ReturnCode_t 型のリターンコード 00489 # 00490 # @else 00491 # 00492 # @brief Start ExecutionContext 00493 # 00494 # Request that the context enter the Running state. 00495 # Once the state transition occurs, the ComponentAction::on_startup 00496 # operation will be invoked. 00497 # An execution context may not be started until the RT components that 00498 # participate in it have been initialized. 00499 # An execution context may be started and stopped multiple times. 00500 # 00501 # @endif 00502 def start(self): 00503 if self._running: 00504 return RTC.PRECONDITION_NOT_MET 00505 00506 for comp in self._comps: 00507 comp._sm.on_startup() 00508 00509 self._running = True 00510 self._thread.start() 00511 00512 return RTC.RTC_OK 00513 00514 00515 ## 00516 # @if jp 00517 # @brief ExecutionContext の実行を停止 00518 # 00519 # ExecutionContext の状態を Stopped とするためのリクエストを発行する。 00520 # 遷移が発生した場合は、 ComponentAction::on_shutdown が呼び出される。 00521 # 参加しているRTコンポーネントが終了する前に ExecutionContext を停止する 00522 # 必要がある。 00523 # ExecutionContext は複数回開始/停止を繰り返すことができる。 00524 # 00525 # @param self 00526 # 00527 # @return ReturnCode_t 型のリターンコード 00528 # 00529 # @else 00530 # 00531 # @brief Stop ExecutionContext 00532 # 00533 # Request that the context enter the Stopped state. 00534 # Once the transition occurs, the ComponentAction::on_shutdown operation 00535 # will be invoked. 00536 # An execution context must be stopped before the RT components that 00537 # participate in it are finalized. 00538 # An execution context may be started and stopped multiple times. 00539 # 00540 # @endif 00541 def stop(self): 00542 if not self._running: 00543 return RTC.PRECONDITION_NOT_MET 00544 00545 for comp in self._comps: 00546 comp._sm.on_shutdown() 00547 00548 self._running = False 00549 00550 return RTC.RTC_OK 00551 00552 00553 ## 00554 # @if jp 00555 # @brief ExecutionContext の実行周期(Hz)を取得する 00556 # 00557 # Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を取得する。 00558 # 00559 # @param self 00560 # 00561 # @return 処理周期(単位:Hz) 00562 # 00563 # @else 00564 # 00565 # @brief Get ExecutionRate 00566 # 00567 # This operation shall return the rate (in hertz) at which its Active 00568 # participating RTCs are being invoked. 00569 # 00570 # @endif 00571 def get_rate(self): 00572 return self._profile.rate 00573 00574 00575 ## 00576 # @if jp 00577 # @brief ExecutionContext の実行周期(Hz)を設定する 00578 # 00579 # Active 状態にてRTコンポーネントが実行される周期(単位:Hz)を設定する。 00580 # 実行周期の変更は、 DataFlowComponentAction の on_rate_changed によって 00581 # 各RTコンポーネントに伝達される。 00582 # 00583 # @param self 00584 # @param rate 処理周期(単位:Hz) 00585 # 00586 # @return ReturnCode_t 型のリターンコード 00587 # 00588 # @else 00589 # 00590 # @brief Set ExecutionRate 00591 # 00592 # This operation shall set the rate (in hertz) at which this context’s 00593 # Active participating RTCs are being called. 00594 # If the execution kind of the context is PERIODIC, a rate change shall 00595 # result in the invocation of on_rate_changed on any RTCs realizing 00596 # DataFlowComponentAction that are registered with any RTCs participating 00597 # in the context. 00598 # 00599 # @endif 00600 def set_rate(self, rate): 00601 if rate > 0.0: 00602 self._profile.rate = rate 00603 self._usec = long(1000000/rate) 00604 for comp in self._comps: 00605 comp._sm.on_rate_changed() 00606 return RTC.RTC_OK 00607 return RTC.BAD_PARAMETER 00608 00609 00610 ## 00611 # @if jp 00612 # @brief RTコンポーネントをアクティブ化する 00613 # 00614 # Inactive 状態にあるRTコンポーネントをActive に遷移させ、アクティブ化する。 00615 # この操作が呼ばれた結果、 on_activate が呼び出される。 00616 # 指定したRTコンポーネントが参加者リストに含まれない場合は、 BAD_PARAMETER 00617 # が返される。 00618 # 指定したRTコンポーネントの状態が Inactive 以外の場合は、 00619 # PRECONDITION_NOT_MET が返される。 00620 # 00621 # @param self 00622 # @param comp アクティブ化対象RTコンポーネント 00623 # 00624 # @return ReturnCode_t 型のリターンコード 00625 # 00626 # @else 00627 # 00628 # @brief Activate a RT-component 00629 # 00630 # The given participant RTC is Inactive and is therefore not being invoked 00631 # according to the execution context’s execution kind. This operation 00632 # shall cause the RTC to transition to the Active state such that it may 00633 # subsequently be invoked in this execution context. 00634 # The callback on_activate shall be called as a result of calling this 00635 # operation. This operation shall not return until the callback has 00636 # returned, and shall result in an error if the callback does. 00637 # 00638 # @endif 00639 def activate_component(self, comp): 00640 for compIn in self._comps: 00641 if compIn._ref._is_equivalent(comp): 00642 if not compIn._sm._sm.isIn(RTC.INACTIVE_STATE): 00643 return RTC.PRECONDITION_NOT_MET 00644 compIn._sm._sm.goTo(RTC.ACTIVE_STATE) 00645 return RTC.RTC_OK 00646 00647 return RTC.BAD_PARAMETER 00648 00649 00650 ## 00651 # @if jp 00652 # @brief RTコンポーネントを非アクティブ化する 00653 # 00654 # Inactive 状態にあるRTコンポーネントを非アクティブ化し、 00655 # Inactive に遷移させる。 00656 # この操作が呼ばれた結果、 on_deactivate が呼び出される。 00657 # 指定したRTコンポーネントが参加者リストに含まれない場合は、 BAD_PARAMETER 00658 # が返される。 00659 # 指定したRTコンポーネントの状態が Active 以外の場合は、 00660 # PRECONDITION_NOT_MET が返される。 00661 # 00662 # @param self 00663 # @param comp 非アクティブ化対象RTコンポーネント 00664 # 00665 # @return ReturnCode_t 型のリターンコード 00666 # 00667 # @else 00668 # 00669 # @brief Deactivate a RT-component 00670 # 00671 # The given RTC is Active in the execution context. Cause it to transition 00672 # to the Inactive state such that it will not be subsequently invoked from 00673 # the context unless and until it is activated again. 00674 # The callback on_deactivate shall be called as a result of calling this 00675 # operation. This operation shall not return until the callback has 00676 # returned, and shall result in an error if the callback does. 00677 # 00678 # @endif 00679 def deactivate_component(self, comp): 00680 for compIn in self._comps: 00681 if compIn._ref._is_equivalent(comp): 00682 if not compIn._sm._sm.isIn(RTC.ACTIVE_STATE): 00683 return RTC.PRECONDITION_NOT_MET 00684 compIn._sm._sm.goTo(RTC.INACTIVE_STATE) 00685 return RTC.RTC_OK 00686 00687 return RTC.BAD_PARAMETER 00688 00689 00690 ## 00691 # @if jp 00692 # @brief RTコンポーネントをリセットする 00693 # 00694 # Error 状態のRTコンポーネントの復帰を試みる。 00695 # この操作が呼ばれた結果、 on_reset が呼び出される。 00696 # 指定したRTコンポーネントが参加者リストに含まれない場合は、 BAD_PARAMETER 00697 # が返される。 00698 # 指定したRTコンポーネントの状態が Error 以外の場合は、 PRECONDITION_NOT_MET 00699 # が返される。 00700 # 00701 # @param self 00702 # @param comp リセット対象RTコンポーネント 00703 # 00704 # @return ReturnCode_t 型のリターンコード 00705 # 00706 # @else 00707 # 00708 # @brief Reset a RT-component 00709 # 00710 # Attempt to recover the RTC when it is in Error. 00711 # The ComponentAction::on_reset callback shall be invoked. This operation 00712 # shall not return until the callback has returned, and shall result in an 00713 # error if the callback does. If possible, the RTC developer should 00714 # implement that callback such that the RTC may be returned to a valid 00715 # state. 00716 # 00717 # @endif 00718 def reset_component(self, comp): 00719 for compIn in self._comps: 00720 if compIn._ref._is_equivalent(comp): 00721 if not compIn._sm._sm.isIn(RTC.ERROR_STATE): 00722 return RTC.PRECONDITION_NOT_MET 00723 compIn._sm._sm.goTo(RTC.INACTIVE_STATE) 00724 return RTC.RTC_OK 00725 00726 return RTC.BAD_PARAMETER 00727 00728 00729 ## 00730 # @if jp 00731 # @brief RTコンポーネントの状態を取得する 00732 # 00733 # 指定したRTコンポーネントの状態(LifeCycleState)を取得する。 00734 # 指定したRTコンポーネントが参加者リストに含まれない場合は、 UNKNOWN_STATE 00735 # が返される。 00736 # 00737 # @param self 00738 # @param comp 状態取得対象RTコンポーネント 00739 # 00740 # @return 現在の状態(LifeCycleState) 00741 # 00742 # @else 00743 # 00744 # @brief Get RT-component's state 00745 # 00746 # This operation shall report the LifeCycleState of the given participant 00747 # RTC. 00748 # 00749 # @endif 00750 def get_component_state(self, comp): 00751 for compIn in self._comps: 00752 if compIn._ref._is_equivalent(comp): 00753 return compIn._sm._sm.getState() 00754 00755 return RTC.UNKNOWN_STATE 00756 00757 00758 ## 00759 # @if jp 00760 # @brief ExecutionKind を取得する 00761 # 00762 # 本 ExecutionContext の ExecutionKind を取得する 00763 # 00764 # @param self 00765 # 00766 # @return ExecutionKind 00767 # 00768 # @else 00769 # 00770 # @brief Get the ExecutionKind 00771 # 00772 # This operation shall report the execution kind of the execution context. 00773 # 00774 # @endif 00775 def get_kind(self): 00776 return self._profile.kind 00777 00778 00779 ## 00780 # @if jp 00781 # @brief RTコンポーネントを追加する 00782 # 00783 # 指定したRTコンポーネントを参加者リストに追加する。 00784 # 追加されたRTコンポーネントは attach_context が呼ばれ、Inactive 状態に遷移 00785 # する。 00786 # 指定されたRTコンポーネントがnullの場合は、BAD_PARAMETER が返される。 00787 # 指定されたRTコンポーネントが DataFlowComponent 以外の場合は、 00788 # BAD_PARAMETER が返される。 00789 # 00790 # @param self 00791 # @param comp 追加対象RTコンポーネント 00792 # 00793 # @return ReturnCode_t 型のリターンコード 00794 # 00795 # @else 00796 # 00797 # @brief Add a RT-component 00798 # 00799 # The operation causes the given RTC to begin participating in the 00800 # execution context. 00801 # The newly added RTC will receive a call to 00802 # LightweightRTComponent::attach_context and then enter the Inactive state. 00803 # 00804 # @endif 00805 def add(self, comp): 00806 if CORBA.is_nil(comp): 00807 return RTC.BAD_PARAMETER 00808 try: 00809 dfp_ = comp._narrow(RTC.DataFlowComponent) 00810 id_ = dfp_.attach_executioncontext(self._ref) 00811 comp_ = self.Comp(ref=comp, dfp=dfp_, id=id_) 00812 self._comps.append(comp_) 00813 return RTC.RTC_OK 00814 except CORBA.Exception: 00815 return RTC.BAD_PARAMETER 00816 00817 return RTC.RTC_OK 00818 00819 00820 ## 00821 # @if jp 00822 # @brief RTコンポーネントを参加者リストから削除する 00823 # 00824 # 指定したRTコンポーネントを参加者リストから削除する。 00825 # 削除されたRTコンポーネントは detach_context が呼ばれる。 00826 # 指定されたRTコンポーネントが参加者リストに登録されていない場合は、 00827 # BAD_PARAMETER が返される。 00828 # 00829 # @param self 00830 # @param comp 削除対象RTコンポーネント 00831 # 00832 # @return ReturnCode_t 型のリターンコード 00833 # 00834 # @else 00835 # 00836 # @brief Remove the RT-component from participant list 00837 # 00838 # This operation causes a participant RTC to stop participating in the 00839 # execution context. 00840 # The removed RTC will receive a call to 00841 # LightweightRTComponent::detach_context. 00842 # 00843 # @endif 00844 def remove(self, comp): 00845 len_ = len(self._comps) 00846 for i in range(len_): 00847 idx = (len_ - 1) - i 00848 if self._comps[idx]._ref._is_equivalent(comp): 00849 self._comps[idx]._ref.detach_executioncontext(self._comps[idx]._sm.ec_id) 00850 del self._comps[idx] 00851 return RTC.RTC_OK 00852 00853 return RTC.BAD_PARAMETER 00854 00855 00856 ## 00857 # @if jp 00858 # @brief ExecutionContextProfile を取得する 00859 # 00860 # 本 ExecutionContext のプロファイルを取得する。 00861 # 00862 # @param self 00863 # 00864 # @return ExecutionContextProfile 00865 # 00866 # @else 00867 # 00868 # @brief Get the ExecutionContextProfile 00869 # 00870 # This operation provides a profile “descriptor” for the execution 00871 # context. 00872 # 00873 # @endif 00874 def get_profile(self): 00875 p = RTC.ExecutionContextProfile(self._profile.kind, 00876 self._profile.rate, 00877 self._profile.owner, 00878 self._profile.participants, 00879 self._profile.properties) 00880 return p 00881 00882 00883 ## 00884 # @if jp 00885 # @class Comp 00886 # @brief コンポーネント管理用内部クラス 00887 # @else 00888 # @endif 00889 class Comp: 00890 def __init__(self, ref=None, dfp=None, id=None, comp=None): 00891 if comp is None: 00892 self._ref = ref 00893 self._sm = PeriodicExecutionContext.DFP(dfp,id) 00894 else: 00895 self._ref = comp._ref 00896 self._sm = PeriodicExecutionContext.DFP(comp._sm._obj,comp._sm.ec_id) 00897 00898 00899 ## 00900 # @if jp 00901 # @brief ExecutionContext を初期化する 00902 # 00903 # ExecutionContext 起動用ファクトリを登録する。 00904 # 00905 # @param manager マネージャオブジェクト 00906 # 00907 # @else 00908 # 00909 # @endif 00910 def PeriodicExecutionContextInit(manager): 00911 manager.registerECFactory("PeriodicExecutionContext", 00912 OpenRTM.PeriodicExecutionContext, 00913 OpenRTM.ECDelete)