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

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Command/CommandTranslator.cpp

    00001
    00002 //
    00003 // Copyright (c) 2005           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 //
    00010
    00011 #include "CommandTranslator.h"
    00012
    00013 #include "CommandDispatcherEntry.h"
    00014 #include <Mers/Interfaces/Task.h>
    00015
    00017
    00018 using namespace Mers::Executives::DeductiveController::Command;
    00019 using Mers::Utils::Containers::DynamicArrayTemplate;
    00020 using Mers::Utils::Lang::String;
    00021 using Mers::Interfaces::TaskFactory;
    00022 using Mers::Interfaces::Task;
    00023 using std::vector;
    00024 using std::ostream;
    00025
    00027
    00028 log4cplus::Logger CommandTranslator::logger( log4cplus::Logger::getInstance
    00029         ("Mers.Executives.DeductiveController.Command.CommandTranslator" ));
    00030
    00032
    00033 CommandTranslator::CommandTranslator(ExecutionInterfaceBase * execInt,
    00034         DynamicArrayTemplate< CommandDispatcherEntry > &entries)
    00035         : ExecutionInterfaceBase( NULL ), m_execInt( execInt ), m_DispatcherEntries( true )
    00036 {
    00037         // Take the dispatcher entries.
    00038         for( unsigned int i = 0; i < entries.size(); ++i ) {
    00039                 LOG4CPLUS_TRACE( logger, "Task Trans Entry: " << *(entries[i]) );
    00040                 m_DispatcherEntries.pushBack( entries[i] );
    00041         }
    00042         // Remove them from the entries object, we own them now.
    00043         for( unsigned int i = 0; i < entries.size(); ++i ) {
    00044                 entries.popBack();
    00045         }
    00046 }
    00047
    00049
    00050 CommandTranslator::~CommandTranslator()
    00051 {
    00052 }
    00053
    00055
    00056 bool CommandTranslator::start( CommandTranslator::TaskIdentifier task )
    00057 {
    00058         return m_execInt != NULL && m_execInt->start( task );
    00059 }
    00060
    00062
    00063 bool CommandTranslator::stop()
    00064 {
    00065         return m_execInt != NULL && m_execInt->stop();
    00066 }
    00067
    00069
    00070 bool CommandTranslator::stop( CommandTranslator::TaskIdentifier task )
    00071 {
    00072         return m_execInt != NULL && m_execInt->stop( task );
    00073 }
    00074
    00076
    00077 int CommandTranslator::step( bool block )
    00078 {
    00079         if( m_execInt == NULL )
    00080                 return 1;
    00081         return m_execInt->step( block );
    00082 }
    00083
    00085
    00086 bool CommandTranslator::isDone()
    00087 {
    00088         return m_execInt == NULL || m_execInt->isDone();
    00089 }
    00090
    00092
    00093 bool CommandTranslator::isDone( CommandTranslator::TaskIdentifier task )
    00094 {
    00095         return m_execInt == NULL || m_execInt->isDone( task );
    00096 }
    00097
    00099
    00100 String CommandTranslator::getActiveTasksString() const
    00101 {
    00102         if( m_execInt == NULL )
    00103                 return String();
    00104         return m_execInt->getActiveTasksString();
    00105 }
    00106
    00108
    00109 CommandTranslator::TaskIdentifier
    00110 CommandTranslator::createTask( const String &taskName )
    00111 {
    00112         // Fake having a set of parameters.  Our parametered function is
    00113         // equiped to handle either case.
    00114         const vector< const String * > fromParams;
    00115
    00116         return createTask( taskName, fromParams );
    00117 }
    00118
    00120
    00121 CommandTranslator::TaskIdentifier
    00122 CommandTranslator::createTask( const String &taskName,
    00123         const vector< const String * > &parameters )
    00124 {
    00125         if( m_execInt == NULL )
    00126                 return TaskIdentifier();
    00127
    00128         LOG4CPLUS_TRACE(logger, "Creating task " << taskName << "( param x " <<
    00129                                                                         parameters.size() << ")");
    00130
    00131         // For each entry, see if we have a match, and if so, use it.
    00132         for( unsigned int i = 0; i < m_DispatcherEntries.size(); ++i ) {
    00133                 const CommandDispatcherEntry * entry = m_DispatcherEntries[i];
    00134
    00135                 // See that we match.
    00136                 if( entry->matches( taskName, parameters ) ) {
    00137                         // Found a match - convert the parameters and pass it on.  Note
    00138                         // that we don't have any from parameters naturally.
    00139                         vector< const String * > toParams;
    00140                         entry->convertParameters( parameters, toParams );
    00141
    00142                         // Create the task.  Note that we could continue looking to see
    00143                         // if there are more matches - indicating an error - but we
    00144                         // shouldn't need to.
    00145                         LOG4CPLUS_TRACE(logger, "Converted to task " <<
    00146                                 entry->getToFunctionName() << "( param x " << toParams.size() << ")");
    00147                         if( toParams.size() == 0 )
    00148                                 return m_execInt->createTask( entry->getToFunctionName() );
    00149                         else
    00150                                 return m_execInt->createTask( entry->getToFunctionName(), toParams );
    00151                 }
    00152         }
    00153         return TaskIdentifier();
    00154 }
    00155
    00157
    00158 bool CommandTranslator::isValid( CommandTranslator::TaskIdentifier task )
    00159 {
    00160         return m_execInt != NULL && m_execInt->isValid( task );
    00161 }
    00162
    00164
    00165 void CommandTranslator::deleteTask( CommandTranslator::TaskIdentifier task )
    00166 {
    00167         if( m_execInt != NULL )
    00168                 m_execInt->deleteTask( task );
    00169 }
    00170
    00172
    00173 TaskFactory * CommandTranslator::getTaskFactory()
    00174 {
    00175         if( m_execInt != NULL )
    00176                 return m_execInt->getTaskFactory();
    00177         return NULL;
    00178 }
    00179
    00181
    00182 CommandTranslator::t_TaskSharedPointer
    00183 CommandTranslator::getTask( CommandTranslator::TaskIdentifier task ) const
    00184 {
    00185         LOG4CPLUS_TRACE( logger, "CommandTranslator: Looking for task " <<
    00186                 task->toString() );
    00187         if( m_execInt != NULL ) {
    00188                 t_TaskSharedPointer pTask( m_execInt->getTask( task ) );
    00189                 LOG4CPLUS_DEBUG( logger, ( pTask ?
    00190                         "CommandTranslator: Found the task " + pTask->toString() :
    00191                         "CommandTranslator: Task was not found" ) );
    00192                 return pTask;
    00193         }
    00194         return t_TaskSharedPointer();
    00195 }
    00196
    00198
    00199 #ifndef EMBEDDED
    00200
    00201 void CommandTranslator::print( ostream &out ) const
    00202 {
    00203         out << "CommandTranslator has " << m_DispatcherEntries.size() << " entries:";
    00204         m_DispatcherEntries.printArray(out, "\n");
    00205 }
    00206
    00207 #endif
    00208
    00210