00001 #!/usr/bin/env python 00002 # -*- coding: euc-jp -*- 00003 00004 ## 00005 # @file OutPortBase.py 00006 # @brief OutPortBase base class 00007 # @date $Date: 2007/09/19 $ 00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara 00009 # 00010 # Copyright (C) 2003-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 00019 ## 00020 # @if jp 00021 # 00022 # @class OutPortBase 00023 # @brief OutPort 基底クラス 00024 # 00025 # OutPort の実装である OutPort<T> の基底クラス。 00026 # 00027 # OutPortBase と PublisherBase は一種の Observer パターンを形成している。 00028 # OutPortBase の attach(), detach(), notify() および 00029 # PublisherBase の push() は Observer パターンに関連したメソッドである。 00030 # 00031 # @since 0.2.0 00032 # 00033 # @else 00034 # 00035 # @class OutPortBase 00036 # @brief Output port base class. 00037 # 00038 # The base class of OutPort<T> s which are implementations of OutPort 00039 # 00040 # @endif 00041 class OutPortBase: 00042 """ 00043 """ 00044 00045 00046 00047 ## 00048 # @if jp 00049 # @brief コンストラクタ 00050 # 00051 # コンストラクタ。 00052 # 00053 # @param self 00054 # @param name ポート名 00055 # 00056 # @else 00057 # 00058 # @brief A constructor of OutPortBase class. 00059 # 00060 # Constructor of OutPortBase. 00061 # 00062 # @endif 00063 def __init__(self, name): 00064 self._name = name 00065 self._publishers = [] 00066 00067 00068 ## 00069 # @if jp 00070 # @brief デストラクタ 00071 # 00072 # デストラクタ。 00073 # 登録された全ての Publisher を削除する。 00074 # 00075 # @param self 00076 # 00077 # @else 00078 # 00079 # @brief destructor 00080 # 00081 # Destructor 00082 # 00083 # @endif 00084 def __del__(self): 00085 for pub in self._publishers: 00086 del(pub) 00087 00088 00089 ## 00090 # @if jp 00091 # @brief OutPort名称の取得 00092 # 00093 # OutPortの名称を取得する。 00094 # 00095 # @param self 00096 # 00097 # @return ポート名称 00098 # 00099 # @else 00100 # 00101 # @brief OutPort's name 00102 # 00103 # This operation returns OutPort's name 00104 # 00105 # @endif 00106 def name(self): 00107 return self._name 00108 00109 00110 ## 00111 # @if jp 00112 # @brief Publisherの追加 00113 # 00114 # 指定したPublisherをデータ更新通知先としてリストの最後尾に追加する。 00115 # attach_back() と同様な機能。 00116 # 00117 # @param self 00118 # @param id_ 指定されたPublisherに割り当てるID 00119 # @param publisher 登録対象Publisherオブジェクト 00120 # 00121 # @else 00122 # 00123 # @brief Attach a publisher 00124 # 00125 # Attach a publisher to observe OutPort. 00126 # 00127 # @endif 00128 def attach(self, id_, publisher): 00129 self.attach_back(id_, publisher) 00130 00131 00132 ## 00133 # @if jp 00134 # @brief リスト先頭へのPublisherの追加 00135 # 00136 # Publisherをリストの先頭に追加する。 00137 # 00138 # @param self 00139 # @param id_ 指定されたPublisherに割り当てるID 00140 # @param publisher 登録対象Publisherオブジェクト 00141 # 00142 # @else 00143 # 00144 # @brief Attach a publisher 00145 # 00146 # Attach a publisher to the head of the Publisher list. 00147 # 00148 # @endif 00149 def attach_front(self, id_, publisher): 00150 self._publishers.insert(0, self.Publisher(id_, publisher)) 00151 00152 00153 ## 00154 # @if jp 00155 # @brief リスト最後尾へのPublisherの追加 00156 # 00157 # Publisherをリストの最後尾に追加する。 00158 # 00159 # @param self 00160 # @param id_ 指定されたPublisherに割り当てるID 00161 # @param publisher 登録対象Publisherオブジェクト 00162 # 00163 # @else 00164 # 00165 # @brief Attach a publisher 00166 # 00167 # Attach a publisher to the taile of the Publisher list. 00168 # 00169 # @endif 00170 def attach_back(self, id_, publisher): 00171 self._publishers.append(self.Publisher(id_, publisher)) 00172 00173 00174 ## 00175 # @if jp 00176 # @brief Publisherの削除 00177 # 00178 # 指定された Publisher をデータ更新通知先リストから削除する。 00179 # 00180 # @param self 00181 # @param id_削除対象 Publisher のID 00182 # 00183 # @return 削除に成功した場合は、削除した Publisher オブジェクト 00184 # 指定した Publisher が存在しない場合は null 00185 # 00186 # @else 00187 # 00188 # @brief Detach a publisher 00189 # 00190 # Detach a publisher to observe OutPort. 00191 # 00192 # @endif 00193 def detach(self, id_): 00194 index = -1 00195 00196 for i in range(len(self._publishers)): 00197 if id_ == self._publishers[i].id: 00198 index = i 00199 break 00200 if index < 0: 00201 return None 00202 00203 pub = self._publishers[index].publisher 00204 del self._publishers[index] 00205 return pub 00206 00207 00208 ## 00209 # @if jp 00210 # @brief 更新の通知 00211 # 00212 # 登録されている全ての Publisher にデータ更新を通知する。 00213 # 00214 # @param self 00215 # 00216 # @else 00217 # 00218 # @brief Notify data update 00219 # 00220 # This operation notify data update to Publishers 00221 # 00222 # @endif 00223 def notify(self): 00224 for pub in self._publishers: 00225 pub.publisher.update() 00226 00227 00228 ## 00229 # @if jp 00230 # @brief Publisher 管理用内部クラス 00231 # @else 00232 # 00233 # @endif 00234 class Publisher: 00235 def __init__(self, id_, publisher_): 00236 self.id = id_ 00237 self.publisher = publisher_ 00238 00239