00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "StatementProcessor.hpp"
00041 #include "Parser.hpp"
00042 #include "parse_exception.hpp"
00043
00044 #include "TaskContext.hpp"
00045 #include "DispatchInterface.hpp"
00046 #include "ConditionInterface.hpp"
00047 #include <MultiVector.hpp>
00048 #include <TypeStream.hpp>
00049 #include "Logger.hpp"
00050
00051 #include <vector>
00052 #include <boost/tuple/tuple.hpp>
00053 #include <iostream>
00054
00055
00056 using namespace boost;
00057
00058 namespace RTT
00059 {
00060 using namespace detail;
00061 struct StatementProcessor::D
00062 {
00063 TaskContext* tc;
00064 std::vector<boost::tuple<int, DispatchInterface::shared_ptr, ConditionInterface*, std::string> > comms;
00065 int seq;
00066 D() : seq(0) {}
00067
00068 void checkFinished()
00069 {
00070 std::vector<boost::tuple<int, DispatchInterface::shared_ptr, ConditionInterface*, std::string> >::iterator it = comms.begin();
00071 while( it != comms.end() )
00072 {
00073 if ( it->get<1>()->valid() && it->get<2>()->evaluate() == true ) {
00074 Logger::log() <<Logger::Info<<"Command ("<<it->get<0>()<<") '"<< it->get<3>() << "' is done." <<Logger::endl;
00075 comms.erase(it);
00076 it = comms.begin();
00077 continue;
00078 }
00079 if ( it->get<1>()->accepted() == false ) {
00080 Logger::log() <<Logger::Error<<"Command ("<<it->get<0>()<<") '"<< it->get<3>() << "' was not accepted." <<Logger::endl;
00081 comms.erase(it);
00082 it = comms.begin();
00083 continue;
00084 }
00085 if ( it->get<1>()->executed() == true ) {
00086 if ( it->get<1>()->valid() == false ) {
00087 Logger::log() <<Logger::Error<<"Command ("<<it->get<0>()<<") '"<< it->get<3>() << "' was executed but rejected." <<Logger::endl;
00088 comms.erase(it);
00089 it = comms.begin();
00090 continue;
00091 }
00092 Logger::log() <<Logger::Info<<"Command ("<<it->get<0>()<<") '"<< it->get<3>() << "' is busy." <<Logger::endl;
00093 }
00094 ++it;
00095 }
00096 }
00097
00098 DispatchInterface::shared_ptr getCommand(int cnr)
00099 {
00100 std::vector<boost::tuple<int, DispatchInterface::shared_ptr, ConditionInterface*, std::string> >::iterator it = comms.begin();
00101 while( it != comms.end() )
00102 if ( it->get<0>() == cnr )
00103 return it->get<1>();
00104 return DispatchInterface::shared_ptr();
00105 }
00106
00107 int add(DispatchInterface* command, ConditionInterface* cond, std::string code)
00108 {
00109 comms.push_back( boost::tuple<int, DispatchInterface::shared_ptr, ConditionInterface*,std::string>(seq, DispatchInterface::shared_ptr(command), cond, code) );
00110 Logger::log() <<Logger::Info<<"Executing Command ("<<seq<<") '"<< code << "'..." <<Logger::endl;
00111 command->dispatch();
00112 ++seq;
00113 return seq -1;
00114 }
00115
00116 void printResult( DataSourceBase* ds, bool recurse) {
00117 std::string prompt(" = ");
00118
00119 Logger::log() << Logger::Info <<prompt;
00120 doPrint( ds, recurse );
00121 Logger::log() << Logger::endl;
00122 }
00123
00124 void doPrint( DataSourceBase* ds, bool recurse) {
00125
00126
00127
00128 ds->reset();
00133
00134 DataSource<bool>* dsb = DataSource<bool>::narrow(ds);
00135 if (dsb) {
00136 Logger::log() << dsb->get();
00137 return;
00138 }
00139 DataSource<int>* dsi = DataSource<int>::narrow(ds);
00140 if (dsi) {
00141 Logger::log() << dsi->get() ;
00142 return;
00143 }
00144 #if 0
00145
00146 DataSource<long>* dsl = DataSource<long>::narrow(ds);
00147 if (dsl) {
00148 Logger::log() << dsl->get() ;
00149 return;
00150 }
00151 #endif
00152 DataSource<unsigned int>* dsui = DataSource<unsigned int>::narrow(ds);
00153 if (dsui) {
00154 Logger::log() << dsui->get() ;
00155 return;
00156 }
00157 DataSource<std::string>* dss = DataSource<std::string>::narrow(ds);
00158 if (dss) {
00159 Logger::log() <<'"'<< dss->get() << '"' ;
00160 return;
00161 }
00162 DataSource<const std::string&>* dscs = DataSource<const std::string&>::narrow(ds);
00163 if (dscs) {
00164 Logger::log() <<'"'<< dscs->get() <<'"' ;
00165 return;
00166 }
00167 DataSource<std::vector<double> >* dsvval = DataSource< std::vector<double> >::narrow(ds);
00168 if (dsvval) {
00169 Logger::log() << dsvval->get() ;
00170 return;
00171 }
00172 DataSource<const std::vector<double>& >* dsv = DataSource<const std::vector<double>& >::narrow(ds);
00173 if (dsv) {
00174 Logger::log() << dsv->get() ;
00175 return;
00176 }
00177 DataSource< Double6D >* ds6d = DataSource<Double6D>::narrow(ds);
00178 if (ds6d) {
00179 Logger::log() << ds6d->get() ;
00180 return;
00181 }
00182 DataSource<double>* dsd = DataSource<double>::narrow(ds);
00183 if (dsd) {
00184 Logger::log() << dsd->get() ;
00185 return;
00186 }
00187 DataSource<char>* dsc = DataSource<char>::narrow(ds);
00188 if (dsc) {
00189 Logger::log() <<'\''<< dsc->get()<<'\'' ;
00190 return;
00191 }
00192
00193 DataSource<PropertyBag>* dspbag = DataSource<PropertyBag>::narrow(ds);
00194 if (dspbag) {
00195 PropertyBag bag( dspbag->get() );
00196 if (!recurse) {
00197 int siz = bag.getProperties().size();
00198 Logger::log() << siz <<" Properties";
00199 } else {
00200 if ( ! bag.empty() ) {
00201 Logger::log() <<Logger::nl;
00202 for( PropertyBag::iterator it= bag.getProperties().begin(); it!=bag.getProperties().end(); ++it) {
00203 Logger::log() <<(*it)->getType()<<" "<< (*it)->getName();
00204 DataSourceBase::shared_ptr propds = (*it)->getDataSource();
00205 this->printResult( propds.get(), false );
00206 Logger::log() <<" ("<<(*it)->getDescription()<<')' << Logger::nl;
00207 }
00208 } else {
00209 Logger::log() <<"(empty PropertyBag)";
00210 }
00211 }
00212 return;
00213 }
00214
00215
00216 DataSource<void>* dsvd = DataSource<void>::narrow(ds);
00217 if (dsvd) {
00218 dsvd->get();
00219 Logger::log() << "(void)" ;
00220 return;
00221 }
00222
00223 if (ds) {
00224 ds->evaluate();
00225 Logger::log() << "( result type '"+ds->getType()+"' not known to TaskBrowser )" ;
00226 }
00227
00228 }
00229
00230 };
00231
00232
00233 StatementProcessor::StatementProcessor(TaskContext* tc)
00234 : d ( new D() )
00235 {
00236 d->tc = tc;
00237 }
00238
00239 StatementProcessor::~StatementProcessor() {
00240 delete d;
00241 }
00242
00243 void StatementProcessor::checkFinished() {
00244 Logger::In in("StatementProcessor");
00245 d->checkFinished();
00246 }
00247
00248 DispatchInterface::shared_ptr StatementProcessor::getCommand(int cnr) {
00249 Logger::In in("StatementProcessor");
00250 return d->getCommand(cnr);
00251 }
00252
00253 int StatementProcessor::execute(const std::string& comm)
00254 {
00255 Logger::In in("StatementProcessor");
00256
00257 d->checkFinished();
00258
00259 TaskContext* taskcontext = d->tc;
00260
00261
00262
00263
00264
00265 if ( taskcontext->attributes()->getValue( comm ) ) {
00266 d->printResult( taskcontext->attributes()->getValue( comm )->getDataSource().get(), true );
00267 return 0;
00268 }
00269
00270 Parser _parser;
00271 std::pair< CommandInterface*, ConditionInterface*> comcon;
00272 DispatchInterface* command;
00273 ConditionInterface* condition;
00274
00275 Logger::log() <<Logger::Debug << "Trying ValueChange...";
00276 try {
00277
00278 DataSourceBase::shared_ptr ds = _parser.parseValueChange( comm, taskcontext );
00279
00280 if ( ds.get() != 0 ) {
00281 Logger::log() << "ok" << Logger::endl;
00282 d->printResult( ds.get(), false );
00283 return 0;
00284 } else
00285 Logger::log() <<Logger::Debug << "no"<<Logger::endl;
00286 } catch ( fatal_semantic_parse_exception& pe ) {
00287
00288 Logger::log() << Logger::Debug << "fatal_semantic_parse_exception: ";
00289 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00290 return -1;
00291 } catch ( syntactic_parse_exception& pe ) {
00292
00293 Logger::log() << Logger::Error << "syntactic_parse_exception: ";
00294 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00295 return -1;
00296 } catch ( parse_exception_parser_fail &pe )
00297 {
00298
00299 Logger::log() << Logger::Debug << "Ignoring ValueChange exception :"<<Logger::nl;
00300 Logger::log() << Logger::Debug << pe.what() <<Logger::nl;
00301 } catch ( parse_exception& pe ) {
00302
00303 Logger::log() << Logger::Error << "parse_exception :";
00304 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00305 return -1;
00306 }
00307 Logger::log() << Logger::Debug << "Trying Expression..."<<Logger::nl;
00308 try {
00309
00310 DataSourceBase::shared_ptr ds = _parser.parseExpression( comm, taskcontext );
00311
00312 if ( ds.get() != 0 ) {
00313 d->printResult( ds.get(), true );
00314 return 0;
00315 } else
00316 Logger::log() << Logger::Error << "returned zero !"<<Logger::nl;
00317 } catch ( syntactic_parse_exception& pe ) {
00318
00319 Logger::log() << Logger::Error << "syntactic_parse_exception :";
00320 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00321 return -1;
00322 } catch ( fatal_semantic_parse_exception& pe ) {
00323
00324 Logger::log() << Logger::Error << "fatal_semantic_parse_exception :";
00325 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00326 return -1;
00327 } catch ( parse_exception_parser_fail &pe ) {
00328
00329 Logger::log() << Logger::Debug << "Ignoring Expression exception :"<<Logger::nl;
00330 Logger::log() << Logger::Debug << pe.what() <<Logger::nl;
00331 } catch ( parse_exception& pe ) {
00332
00333 Logger::log() << Logger::Debug << "Ignoring Expression parse_exception :"<<Logger::nl;
00334 Logger::log() << Logger::Debug << pe.what() <<Logger::nl;
00335 }
00336 Logger::log() << Logger::Debug << "Trying Command...";
00337 try {
00338 comcon = _parser.parseCommand( comm, taskcontext, true );
00339 assert( dynamic_cast<DispatchInterface*>(comcon.first) );
00340 command = dynamic_cast<DispatchInterface*>(comcon.first);
00341 condition = comcon.second;
00342 } catch ( parse_exception& pe ) {
00343 Logger::log() << Logger::Debug << "CommandParser parse_exception :"<<Logger::nl;
00344 Logger::log() << Logger::Error << pe.what() <<Logger::nl;
00345 return -1;
00346 } catch (...) {
00347 Logger::log() << Logger::Error << "Illegal Input."<<Logger::nl;
00348 return -1;
00349 }
00350
00351 if ( command == 0 ) {
00352 Logger::log() << Logger::Error << "Uncaught : Illegal command."<<Logger::nl;
00353 return -1;
00354 }
00355
00356 return d->add( command, condition, comm);
00357 }
00358
00359 }
00360