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

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Command/CommandTranslatorTest.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 //
    00010
    00011 #include "TaskTransConfigParser.hpp"
    00012 #include "TaskTransConfigLexer.hpp"
    00013 #include "CommandTranslator.h"
    00014 #include <Mers/Interfaces/ExecutionInterface.h>
    00015 #include <fstream>
    00016 #include <antlr/RecognitionException.hpp>
    00017 #include <antlr/TokenStreamException.hpp>
    00018
    00020
    00021 using namespace Mers::Executives::DeductiveController::Command;
    00022 using Mers::Interfaces::ExecutionInterface;
    00023 using Mers::Utils::Containers::DynamicArrayTemplate;
    00024 using Mers::Utils::Lang::String;
    00025 using Mers::Interfaces::ExecutionInterface;
    00026 using std::fprintf;
    00027 using std::cerr;
    00028 using std::ifstream;
    00029 using std::vector;
    00030 using std::endl;
    00031
    00033
    00037 void printUse(char const * progName)
    00038 {
    00039         fprintf(stderr, "Use: %s <Functions> <TT Config File> [<log4cplus Config File>]\n"
    00040                 "<Functions>: Contains list of functions, one per line, comma deliminated:"
    00041                 "\n    <function name>,<param 1>,<param 2>,...\n"
    00042                 "<TT Config File>: Contains the Task Translation configuration\n"
    00043                 "<log4cplus Config File>: Contains logging settings for this test\n",
    00044                 progName);
    00045 }
    00046
    00050 int main(int argc, char * argv[])
    00051 {
    00052         if(argc != 3 && argc != 4){
    00053                 printUse(argv[0]);
    00054                 return 1;
    00055         }
    00056
    00057         // load our config file
    00058         if(argc == 3) {
    00059                 log4cplus::BasicConfigurator::doConfigure();
    00060         } else {
    00061                 log4cplus::PropertyConfigurator::doConfigure(argv[3]);
    00062         }
    00063
    00064         // Load our task translation.
    00065         DynamicArrayTemplate< CommandDispatcherEntry > entries(false);
    00066         try {
    00067                 cerr << "Loading Task Translation Configuration." << endl;
    00068                 ifstream ifs(argv[2]);
    00069                 TaskTransConfigLexer lex(ifs);
    00070                 lex.setFilename( argv[2] );
    00071                 TaskTransConfigParser parser(lex);
    00072                 parser.setFilename( argv[2] );
    00073
    00074                 parser.loadConfigFile( entries );
    00075         } catch(ANTLR_USE_NAMESPACE(antlr)TokenStreamException * e) {
    00076                 cerr << "Problem with stream: " << e->toString() << endl;
    00077                 return 1;
    00078         } catch(ANTLR_USE_NAMESPACE(antlr)RecognitionException * re) {
    00079                 cerr << "Bad input: " << re->toString() << endl;
    00080                 return 1;
    00081         } catch(ANTLR_USE_NAMESPACE(antlr)TokenStreamException &er) {
    00082                 cerr << "Problem with stream: " << er.toString() << endl;
    00083                 return 1;
    00084         } catch(ANTLR_USE_NAMESPACE(antlr)RecognitionException &rer) {
    00085                 cerr << "Bad input: " << rer.toString() << endl;
    00086                 return 1;
    00087         } catch( ::Mers::Utils::Exceptions::Exception * me ) {
    00088                 cerr << "Exception: " << me->what() << endl;
    00089                 return 1;
    00090         } catch( ::std::exception * se ) {
    00091                 cerr << "Exception: " << se->what() << endl;
    00092                 return 1;
    00093         } catch( ::Mers::Utils::Exceptions::Exception &mer ) {
    00094                 cerr << "Exception: " << mer.what() << endl;
    00095                 return 1;
    00096         } catch( ::std::exception &ser ) {
    00097                 cerr << "Exception: " << ser.what() << endl;
    00098                 return 1;
    00099         } catch(...) {
    00100                 cerr << "Unknown Task Translation exception." << endl;
    00101                 return 1;
    00102         }
    00103
    00104         cerr << "Constructing execution interface stub." << endl;
    00105         ExecutionInterface dispCon( NULL );
    00106
    00107         cerr << "Constructing Command Translator." << endl;
    00108         CommandTranslator cDisp( &dispCon, entries );
    00109
    00110         cerr << "Loading commands." << endl;
    00111         // Run our series of commands.
    00112         ifstream ifs( argv[1] );
    00113         char buf[1024];
    00114
    00115         while( ifs.good() && !ifs.eof() ) {
    00116                 ifs.getline( buf, sizeof( buf ) / sizeof( char ) );
    00117                 if( strlen( buf ) > 0 ) {
    00118                         cerr << "Trying: " << buf << endl;
    00119                         // Got a non-empty line - use it.
    00120                         String funcName;
    00121                         vector< const String * > params;
    00122                         char * cp = buf;
    00123
    00124                         int index = -1;
    00125                         for( ; *cp != '\0'; ++cp ) {
    00126                                 if(*cp == ',') {
    00127                                         ++index;
    00128                                         params.push_back( new String() );
    00129                                 } else {
    00130                                         if( index < 0 )
    00131                                                 funcName += *cp;
    00132                                         else
    00133                                                 *((String *) params[index]) += *cp;
    00134                                 }
    00135                         }
    00136
    00137                         // Run this example.
    00138                         ExecutionInterface::TaskIdentifier taskID =
    00139                                 cDisp.createTask( funcName, params );
    00140                         // Start the task, if valid.
    00141                         if( cDisp.isValid( taskID ) ) {
    00142                                 // start a task.
    00143                                 cDisp.start( taskID );
    00144                                 // step once.
    00145                                 cDisp.step( true );
    00146                                 // Stop it.
    00147                                 cDisp.stop( taskID );
    00148                                 // Delete it.
    00149                                 cDisp.deleteTask( taskID );
    00150                         }
    00151
    00152                         // Cleanup the memory.
    00153                         for( unsigned int i = 0; i < params.size(); ++i )
    00154                                 delete params[i];
    00155                 }
    00156         }
    00157
    00158         cerr << "All Done" << endl;
    00159
    00160         return 0;
    00161 }
    00162