00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 package cast.architecture;
00027
00028 import java.util.HashMap;
00029
00030 import Ice.Current;
00031 import cast.cdl.TaskManagementDecision;
00032 import cast.cdl.TaskOutcome;
00033 import cast.core.Pair;
00034 import cast.core.QueuedDataRunnable;
00035 import cast.interfaces.TaskManagerPrx;
00036 import cast.interfaces._ManagedComponentOperations;
00037
00045 public abstract class ManagedComponent extends WorkingMemoryReaderComponent
00046 implements _ManagedComponentOperations {
00047
00048 private static class TaskManagementResult {
00049 public String m_id;
00050 public TaskManagementDecision m_decision;
00051
00052 public TaskManagementResult(String _id, TaskManagementDecision _decision) {
00053 m_id = _id;
00054 m_decision = _decision;
00055 }
00056 }
00057
00058 private TaskManagerPrx m_taskManager;
00059 private final HashMap<String, Pair<String, String>> m_proposedTasks;
00060
00061 public ManagedComponent() {
00062 m_proposedTasks = new HashMap<String, Pair<String, String>>();
00063 m_taskCounter = 0;
00064 m_taskNotificationLock = new Object();
00065 m_notifications = 0;
00066 m_responseRunnable = null;
00067 }
00068
00069 public void setTaskManager(TaskManagerPrx _tm, Current __current) {
00070 m_taskManager = _tm;
00071 }
00072
00079 private static class GMResponseHandler extends
00080 QueuedDataRunnable<TaskManagementResult> {
00081
00082 private ManagedComponent m_gdp;
00083
00089 public GMResponseHandler(ManagedComponent _gdp) {
00090 super();
00091 m_gdp = _gdp;
00092 }
00093
00094 @Override
00095 protected void nextInQueue(TaskManagementResult _data) {
00096 m_gdp.lockComponent();
00097 m_gdp.handleTaskManagerResponse(_data.m_id, _data.m_decision);
00098 m_gdp.unlockComponent();
00099 }
00100
00101 }
00102
00103 private final Object m_taskNotificationLock;
00104
00108 int m_notifications;
00109
00113 protected int m_taskCounter;
00114
00115 private GMResponseHandler m_responseRunnable;
00116
00126 private void handleTaskManagerResponse(String _taskID,
00127 TaskManagementDecision _decision) {
00128
00129
00130 if (_decision == TaskManagementDecision.TaskAdopted) {
00131 taskAdopted(_taskID);
00132 synchronized (m_taskNotificationLock) {
00133 m_notifications++;
00134 m_taskNotificationLock.notifyAll();
00135 }
00136
00137 } else if (_decision == TaskManagementDecision.TaskRejected
00138 ) {
00139 taskRejected(_taskID);
00140 synchronized (m_taskNotificationLock) {
00141 m_notifications++;
00142 m_taskNotificationLock.notifyAll();
00143 }
00144
00145 } else if (_decision == TaskManagementDecision.TaskWaiting) {
00146 debug("TaskWaiting received for: " + _taskID);
00147 }
00148
00149 }
00150
00156 protected String newTaskID() {
00157 StringBuffer sb = new StringBuffer("task:");
00158 sb.append(getComponentID());
00159 sb.append(m_taskCounter++);
00160 return sb.toString();
00161 }
00162
00171 protected void proposeInformationProcessingTask(String _taskID,
00172 String _taskName) {
00173
00174 assert (_taskID != null);
00175 assert (_taskName != null);
00176 assert (m_taskManager != null);
00177
00178
00179 m_proposedTasks.put(_taskID, new Pair<String, String>(_taskID,
00180 _taskName));
00181
00182 m_taskManager.proposeTask(getComponentID(), _taskID, _taskName);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00214 protected void retractInformationProcessingTask(String _taskID) {
00215 assert (m_taskManager != null);
00216 m_proposedTasks.remove(_taskID);
00217 m_taskManager.retractTask(getComponentID(),_taskID);
00218 }
00219
00225 protected void taskAdopted(String _taskID) {
00226
00227 }
00228
00240 protected void taskComplete(String _taskID, TaskOutcome _outcome) {
00241
00242 assert (_taskID != null);
00243 assert (_outcome != null);
00244 assert (m_taskManager != null);
00245
00246
00247 m_proposedTasks.remove(_taskID);
00248
00249 m_taskManager.taskComplete(getComponentID(),_taskID, _outcome);
00250 }
00251
00257 protected void taskRejected(String _taskID) {
00258
00259 }
00260
00261
00262
00263
00275 protected void waitForNotifications() {
00276
00277 synchronized (m_taskNotificationLock) {
00278 if (m_notifications == 0) {
00279
00280 try {
00281 m_taskNotificationLock.wait();
00282 } catch (InterruptedException e) {
00283 e.printStackTrace();
00284 }
00285
00286
00287 m_notifications--;
00288 assert (m_notifications >= 0);
00289
00290 }
00291
00292 else {
00293 m_notifications--;
00294 assert (m_notifications >= 0);
00295 }
00296 }
00297
00298 }
00299
00300 public void taskDecision(String _id, TaskManagementDecision _decision, Current __current) {
00301
00302 debug("received goal management decision: " + _id);
00303
00304
00305
00306 if (m_proposedTasks.containsKey(_id)) {
00307
00308 if (m_responseRunnable == null) {
00309 m_responseRunnable = new GMResponseHandler(this);
00310 m_responseRunnable.start();
00311 new Thread(m_responseRunnable).start();
00312 }
00313
00314 m_responseRunnable.queue(new TaskManagementResult(_id, _decision));
00315 }
00316 }
00317
00318 @Override
00319 public void stopInternal() {
00320
00321 if (m_responseRunnable != null) {
00322 m_responseRunnable.stop();
00323 }
00324
00325 synchronized (m_taskNotificationLock) {
00326 m_taskNotificationLock.notifyAll();
00327 }
00328 }
00329
00330 }