- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Interfaces/ExecutionInterfaceBase.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "ExecutionInterfaceBase.h"
    00015 #include "Task.h"
    00016 #include "TaskFactory.h"
    00017
    00018 #include <Mers/Utils/Logging/logger.h>
    00019
    00021
    00022 using Mers::Interfaces::ExecutionInterfaceBase;
    00023 using Mers::Interfaces::TaskFactory;
    00024 using Mers::Utils::Lang::String;
    00025 using std::set;
    00026 using std::vector;
    00027 using std::set;
    00028
    00030
    00031 log4cplus::Logger ExecutionInterfaceBase::m_Logger
    00032 ( log4cplus::Logger::getInstance( "Mers.Interfaces.ExecutionInterfaceBase" ) );
    00033
    00035
    00036 ExecutionInterfaceBase::ExecutionInterfaceBase( TaskFactory *pTaskFactory )
    00037   : mTasks(), mFailedTasks(), mTaskFactory( NULL )
    00038 {
    00039         // We need to handle properly the case where the pointer to the new task factory is NULL.
    00040         // It may be necessary to pass a NULL pointer to this constructor.
    00041         // For example, if a class derives from ExecutionInterface base and owns as a member variable the task factory it will use,
    00042         // it needs to set it as the task factory for the execution interface. However, at the time when this constructor is called,
    00043         // the task factory member variable will not yet be initialized (base classes are initialized before member variables). In
    00044         // this case, we must pass a NULL pointer to this constructor then immediately set the task factory once it has been initialized.
    00045         setTaskFactory( pTaskFactory );
    00046 }
    00047
    00049
    00050 ExecutionInterfaceBase::~ExecutionInterfaceBase()
    00051 {
    00052         if( mTaskFactory != NULL ) {
    00053                 LOG4CPLUS_TRACE( m_Logger, "Removing Reference to task factory" );
    00054                 mTaskFactory->removeReference();
    00055         }
    00056 }
    00057
    00059
    00060 int
    00061 ExecutionInterfaceBase::executeTask( TaskIdentifier task )
    00062 {
    00063   int ret;
    00064
    00065   // See if we can start this task.
    00066   if(!start(task))
    00067   {
    00068     return -1;
    00069   }
    00070   // Run this procedure until completion
    00071   while(!isDone(task))
    00072   {
    00073     // Take a step
    00074     ret = step(true);
    00075     // See if we got an error.
    00076     if(ret > 0)
    00077     {
    00078       // See what the error was.
    00079       if(ret == 1)
    00080       {
    00081         // The current set of tasks failed to execute together.
    00082         LOG4CPLUS_TRACE( m_Logger, "Execution failure for one step, terminating." );
    00083
    00084         // could opt to just reset the procedure and run it again, but
    00085         // we aren't going to try something different next time, so
    00086         // just fail.
    00087         stop(task);
    00088         return 1;
    00089       }else if(ret == 2){
    00090         // The task indicated its own failure.  Return the appropriate
    00091         // failure.
    00092         LOG4CPLUS_TRACE( m_Logger, "The procedure failed, terminating." );
    00093         stop(task);
    00094         return 2;
    00095       }else{
    00096         // Unknown error type.
    00097         LOG4CPLUS_TRACE( m_Logger, "Sequencer returned the error code: " << ret );
    00098         return ret;
    00099       }
    00100     }
    00101   }
    00102   return 0;
    00103 }
    00104
    00105
    00107 bool
    00108 ExecutionInterfaceBase::isFailure()
    00109 {
    00110         if( mFailedTasks.size() > 0 )
    00111                 return true;
    00112         else
    00113                 return false;
    00114 }
    00115
    00116
    00118 set< ExecutionInterfaceBase::TaskIdentifier >
    00119 ExecutionInterfaceBase::getFailedTasks()
    00120 {
    00121         return mFailedTasks;
    00122 }
    00123
    00124
    00126
    00127
    00128 bool
    00129 ExecutionInterfaceBase::isReady( TaskIdentifier task )
    00130 {
    00131   // you should implement this function for your specific hardware
    00132   return isValid( task );
    00133 }
    00134
    00135
    00136 String
    00137 ExecutionInterfaceBase::getActiveTasksString() const
    00138 {
    00139   // Do nothing
    00140   return String();
    00141 }
    00142
    00144
    00145 ExecutionInterfaceBase::TaskIdentifier
    00146 ExecutionInterfaceBase::createTask( const String &taskName )
    00147 {
    00148   t_TaskSharedPointer task = mTaskFactory->createTask( taskName );
    00149         if( task ) {
    00150                 mTasks.insert( getTaskID( task ) );
    00151         } else {
    00152                 LOG4CPLUS_DEBUG( m_Logger, "Task factory failed to create task with name " << taskName );
    00153         }
    00154   return getTaskID( task );
    00155 }
    00156
    00158
    00159 ExecutionInterfaceBase::TaskIdentifier
    00160 ExecutionInterfaceBase::createTask( const String &taskName,
    00161                                 vector<const String *> const &parameters )
    00162 {
    00163   t_TaskSharedPointer task = mTaskFactory->createTask(taskName, parameters);
    00164         if( task ) {
    00165                 mTasks.insert( getTaskID( task ) );
    00166         } else {
    00167                 LOG4CPLUS_DEBUG( m_Logger, "Task factory failed to create task with name " << taskName << " (" << parameters.size() << " params)" );
    00168         }
    00169   return getTaskID( task );
    00170 }
    00171
    00173
    00174 bool
    00175 ExecutionInterfaceBase::isValid(TaskIdentifier taskID)
    00176 {
    00177   return getTask(taskID);
    00178   // return mTasks.find(task) != mTasks.end();
    00179 }
    00180
    00182
    00183 void
    00184 ExecutionInterfaceBase::deleteTask(TaskIdentifier taskID)
    00185 {
    00186   set<TaskIdentifier>::iterator iSet = mTasks.find(taskID);
    00187   if(iSet != mTasks.end())
    00188   {
    00189     mTasks.erase(iSet);
    00190   }
    00191 }
    00192
    00194
    00195 TaskFactory*
    00196 ExecutionInterfaceBase::getTaskFactory()
    00197 {
    00198   return mTaskFactory;
    00199 }
    00200
    00202 ExecutionInterfaceBase::t_TaskSharedPointer
    00203 ExecutionInterfaceBase::getFailedTask(TaskIdentifier taskID) const
    00204 {
    00205         set<TaskIdentifier>::const_iterator iSet = mFailedTasks.find(taskID);
    00206         if(iSet != mFailedTasks.end() ) {
    00207                 return *iSet;
    00208         } else {
    00209                 return t_TaskSharedPointer();
    00210         }
    00211 }
    00212
    00214
    00215 ExecutionInterfaceBase::t_TaskSharedPointer
    00216 ExecutionInterfaceBase::getTask(TaskIdentifier taskID) const
    00217 {
    00218   set<TaskIdentifier>::const_iterator iSet = mTasks.find(taskID);
    00219   if(iSet != mTasks.end()) {
    00220     return *iSet;
    00221   } else {
    00222                 return t_TaskSharedPointer();
    00223   }
    00224 }
    00225
    00227
    00228 void
    00229 ExecutionInterfaceBase::setTaskFactory(TaskFactory * tFactory)
    00230 {
    00231   if( tFactory == NULL ) {
    00232                 LOG4CPLUS_INFO( m_Logger, "setTaskFactory(...) : Warning : Task factory is null!" );
    00233         }
    00234         else {
    00235    tFactory->addReference();
    00236                 if( mTaskFactory != NULL ) {
    00237             mTaskFactory->removeReference();
    00238                 }
    00239     mTaskFactory = tFactory;
    00240   }
    00241 }
    00242
    00244
    00245 ExecutionInterfaceBase::TaskIdentifier
    00246 ExecutionInterfaceBase::getTaskID( t_TaskSharedPointer pTask )
    00247 {
    00248         // In this implementation, the unique ID for a task is simply its shared pointer
    00249         // So TaskIdentifier and t_TaskSharedPointer are the same type
    00250         return pTask;
    00251 }
    00252
    00253 #include <sstream>
    00254 using std::stringstream;
    00255 using std::endl;
    00256
    00257 String ExecutionInterfaceBase::PrintTasks( void )
    00258 {
    00259         stringstream ss;
    00260         ss << "Execution interface has " << mTasks.size() << " tasks ..." << endl;
    00261         for( set<TaskIdentifier>::iterator iter=mTasks.begin(); iter!=mTasks.end(); iter++ ) {
    00262                 ss << (*iter)->toString();
    00263         }
    00264         return ss.str();
    00265 }
    00266