00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "GTXabsl2EngineExecutor.h"
00010 #include "Tools/Debugging/Debugging.h"
00011 #include "Platform/GTAssert.h"
00012 #include "Platform/SystemCall.h"
00013
00014 GTXabsl2EngineExecutor::GTXabsl2EngineExecutor (SolutionRequest::xabsl2EngineID id,
00015 SolutionRequest::ModuleID module,
00016 const unsigned long& frameNumber)
00017 : pEngine(0), errorHandler(id), id(id), debugMode(executeOption),
00018 module(module), profiler(id, &frameNumber)
00019 {
00020 }
00021
00022 GTXabsl2EngineExecutor::~GTXabsl2EngineExecutor()
00023 {
00024 if (pEngine!=0) delete pEngine;
00025 }
00026
00027 void GTXabsl2EngineExecutor::init(Xabsl2InputSource& input)
00028 {
00029 if (pEngine != 0) delete pEngine;
00030 watchedBooleanInputSymbols.clear();
00031 watchedDecimalInputSymbols.clear();
00032 watchedEnumeratedInputSymbols.clear();
00033 watchedEnumeratedOutputSymbols.clear();
00034 setEnumeratedOutputSymbols.clear();
00035 setEnumeratedOutputSymbolValues.clear();
00036
00037 this->debugMode = executeRootOption;
00038
00039 errorHandler.errorsOccurred = false;
00040
00041 unsigned long startTime = SystemCall::getCurrentSystemTime();
00042 unsigned long freeMemoryBefore = SystemCall::getFreeMem();
00043
00044 pEngine = new Xabsl2Engine(errorHandler,&SystemCall::getCurrentSystemTime);
00045
00046 registerSymbolsAndBasicBehaviors();
00047 profiler.registerSymbols(*pEngine);
00048
00049 pEngine->createOptionGraph(input);
00050
00051 if (!errorHandler.errorsOccurred)
00052 {
00053 errorHandler.message("created a new Engine (%li ms, %li bytes)",
00054 SystemCall::getCurrentSystemTime() - startTime,
00055 freeMemoryBefore - SystemCall::getFreeMem());
00056 profiler.init(*pEngine);
00057
00058 }
00059 }
00060
00061 void GTXabsl2EngineExecutor::executeEngine()
00062 {
00063 if (errorHandler.errorsOccurred)
00064 {
00065 executeIfEngineCouldNotBeCreated();
00066 return;
00067 }
00068
00069 switch (debugMode)
00070 {
00071 case executeRootOption:
00072 pEngine->setRootOption();
00073 pEngine->execute();
00074 break;
00075 case executeOption:
00076
00077
00078 pEngine->execute();
00079 break;
00080 case executeBasicBehavior:
00081
00082 pEngine->executeSelectedBasicBehavior();
00083 break;
00084 }
00085
00086
00087 if(debugMode == executeRootOption || debugMode == executeOption){
00088
00089 if(profiler.profilerCollectMode == GTXabsl2Profiler::collectProfiles)
00090 profiler.doProfiling(*pEngine);
00091 if(profiler.profilerWriteMode == GTXabsl2Profiler::writeProfiles && profiler.size()){
00092 profiler.recordCollectedLogs();
00093 }
00094 else if(profiler.profilerWriteMode == GTXabsl2Profiler::writeCompleteProfiles && profiler.size()){
00095 profiler.recordCompleteLog();
00096 }
00097 profiler.profilerWriteMode = GTXabsl2Profiler::dontWriteProfiles;
00098 }
00099
00100 for (int i=0; i<setEnumeratedOutputSymbols.getSize(); i++)
00101 {
00102 setEnumeratedOutputSymbols[i]->activeValue = setEnumeratedOutputSymbolValues[i];
00103 setEnumeratedOutputSymbols[i]->activeValueWasSet = true;
00104 }
00105 pEngine->setOutputSymbols();
00106
00107
00108 switch(module)
00109 {
00110 case SolutionRequest::behaviorControl:
00111 if (getDebugKeyTable().isActive(DebugKeyTable::sendXabsl2DebugMessagesForBehaviorControl))
00112 sendDebugMessage();
00113 break;
00114 case SolutionRequest::headControl:
00115 if (getDebugKeyTable().isActive(DebugKeyTable::sendXabsl2DebugMessagesForHeadControl))
00116 sendDebugMessage();
00117 break;
00118 default:
00119 ASSERT(false);
00120
00121 }
00122 }
00123
00124 void GTXabsl2EngineExecutor::sendDebugMessage()
00125 {
00126 int i, j;
00127
00128 Out& out = getDebugOut().bin;
00129
00130
00131 out << (char)id;
00132
00133
00134 out << pEngine->getSelectedAgentName();
00135
00136
00137 out << watchedDecimalInputSymbols.getSize();
00138 for (i = 0; i < watchedDecimalInputSymbols.getSize(); i++)
00139 out << watchedDecimalInputSymbols[i]->n << watchedDecimalInputSymbols[i]->getValue();
00140
00141
00142 out << watchedBooleanInputSymbols.getSize();
00143 for (i = 0; i < watchedBooleanInputSymbols.getSize(); i++)
00144 out << watchedBooleanInputSymbols[i]->n << (char)watchedBooleanInputSymbols[i]->getValue();
00145
00146
00147 out << watchedEnumeratedInputSymbols.getSize();
00148 for (i = 0; i < watchedEnumeratedInputSymbols.getSize(); i++)
00149 {
00150 Xabsl2EnumeratedInputSymbol* s = watchedEnumeratedInputSymbols[i];
00151 out << s->n;
00152 int v = s->getValue();
00153 for (j = 0; j < s->enumElements.getSize();j++)
00154 {
00155 if (s->enumElements[j]->v == v) break;
00156 }
00157 if (j==s->enumElements.getSize())
00158 {
00159 out << " ";
00160 }
00161 else
00162 {
00163 out << s->enumElements[j]->n;
00164 }
00165 }
00166
00167
00168 out << watchedEnumeratedOutputSymbols.getSize();
00169 for (i=0; i<watchedEnumeratedOutputSymbols.getSize(); i++)
00170 {
00171 Xabsl2EnumeratedOutputSymbol* s = watchedEnumeratedOutputSymbols[i];
00172 out << s->n;
00173 for (j = 0; j < s->enumElements.getSize();j++)
00174 {
00175 if (s->enumElements[j]->v == s->activeValue) break;
00176 }
00177 if (j==s->enumElements.getSize())
00178 {
00179 out << " ";
00180 }
00181 else
00182 {
00183 out << s->enumElements[j]->n;
00184 }
00185 }
00186
00187
00188 int numberOfActiveOptions = 0;
00189 const Xabsl2Option* option;
00190 if (debugMode == executeOption || debugMode == executeRootOption)
00191
00192 {
00193 option = pEngine->getRootOption();
00194
00195 while (option!=0)
00196 {
00197 option = option->activeState->subsequentOption;
00198 numberOfActiveOptions++;
00199 }
00200 }
00201 out << numberOfActiveOptions;
00202
00203
00204
00205 if (debugMode == executeOption || debugMode == executeRootOption)
00206 {
00207 option = pEngine->getRootOption();
00208
00209 while (option!=0)
00210 {
00211 out << option->n << option->timeOfOptionExecution
00212 << option->activeState->n << option->activeState->timeOfStateExecution;
00213
00214
00215 int numberOfOptionParameters = option->parameters.getSize();
00216 out << (char)numberOfOptionParameters;
00217
00218 for (i = 0; i < numberOfOptionParameters; i++)
00219 {
00220 out << option->parameters.getName(i);
00221 out << option->parameters[i];
00222 }
00223
00224 option = option->activeState->subsequentOption;
00225 }
00226 }
00227
00228
00229 const Xabsl2BasicBehavior* bb = pEngine->getSelectedBasicBehavior();
00230 out << bb->n;
00231
00232
00233 char numberOfBasicBehaviorParameters = bb->parameters.getSize();
00234 out << numberOfBasicBehaviorParameters;
00235
00236
00237 for (i=0;i<numberOfBasicBehaviorParameters;i++)
00238 {
00239 out << bb->parameters.getName(i);
00240 out << bb->parameters[i];
00241 }
00242
00243
00244 char buf[200];
00245 printGeneratedMainActionToString(buf);
00246 out << buf;
00247
00248 getDebugOut().finishMessage(idXabsl2DebugMessage);
00249 }
00250
00251 bool GTXabsl2EngineExecutor::handleMessage(InMessage& message)
00252 {
00253 switch (message.getMessageID())
00254 {
00255 case idXabsl2DebugRequest:
00256 {
00257
00258 char buf[100];
00259 char buf2[100];
00260 double valueDouble;
00261 char c;
00262 int i, numberOfWatchedInputSymbols, numberOfWatchedEnumeratedOutputSymbols;
00263
00264 message.bin >> c;
00265
00266 if ((SolutionRequest::xabsl2EngineID)c != this->id)
00267 {
00268
00269 return true;
00270 }
00271
00272
00273 watchedDecimalInputSymbols.clear();
00274 watchedBooleanInputSymbols.clear();
00275 watchedEnumeratedInputSymbols.clear();
00276 watchedEnumeratedOutputSymbols.clear();
00277 setEnumeratedOutputSymbols.clear();
00278 setEnumeratedOutputSymbolValues.clear();
00279
00280
00281 message.bin >> numberOfWatchedInputSymbols;
00282
00283 for (i=0; i< numberOfWatchedInputSymbols; i++)
00284 {
00285 message.bin >> c;
00286 message.bin >> buf;
00287 switch(c)
00288 {
00289 case 'd':
00290 if (pEngine->existsDecimalInputSymbol(buf))
00291 watchedDecimalInputSymbols.append(buf,pEngine->getDecimalInputSymbol(buf));
00292 break;
00293 case 'b':
00294 if (pEngine->existsBooleanInputSymbol(buf))
00295 watchedBooleanInputSymbols.append(buf,pEngine->getBooleanInputSymbol(buf));
00296 break;
00297 case 'e':
00298 if (pEngine->existsEnumeratedInputSymbol(buf))
00299 watchedEnumeratedInputSymbols.append(buf,pEngine->getEnumeratedInputSymbol(buf));
00300 break;
00301 }
00302 }
00303
00304
00305 message.bin >> numberOfWatchedEnumeratedOutputSymbols;
00306 for (i=0; i< numberOfWatchedEnumeratedOutputSymbols; i++)
00307 {
00308 message.bin >> buf;
00309 if (pEngine->existsEnumeratedOutputSymbol(buf))
00310 watchedEnumeratedOutputSymbols.append(buf,pEngine->getEnumeratedOutputSymbol(buf));
00311 }
00312
00313 char mode;
00314 char numberOfParameters;
00315 message.bin >> mode;
00316 switch (mode)
00317 {
00318 case 'b':
00319 debugMode = executeBasicBehavior;
00320 message.bin >> buf;
00321 if(!pEngine->setSelectedBasicBehavior(buf))
00322 {
00323 OUTPUT(idText, text, "BasicBehavior " << buf << " does not exist");
00324 }
00325
00326 message.bin >> numberOfParameters;
00327 for(i = 0; i < numberOfParameters; i++)
00328 {
00329 message.bin >> buf2;
00330 message.bin >> valueDouble;
00331 pEngine->setBasicBehaviorParameter(buf, buf2, valueDouble);
00332 }
00333 break;
00334 case 'o':
00335 debugMode = executeOption;
00336 message.bin >> buf;
00337 if(!pEngine->setRootOption(buf))
00338 {
00339 OUTPUT(idText, text, "Option " << buf << " does not exist");
00340 }
00341 message.bin >> numberOfParameters;
00342 for(i = 0; i < numberOfParameters; i++)
00343 {
00344 message.bin >> buf2;
00345 message.bin >> valueDouble;
00346 pEngine->setOptionParameter(buf, buf2, valueDouble);
00347 }
00348 break;
00349 case 'd':
00350 debugMode = executeRootOption;
00351 break;
00352 }
00353
00354
00355 char numberOfChangedOutputSymbols;
00356 message.bin >> numberOfChangedOutputSymbols;
00357 for(i = 0; i < numberOfChangedOutputSymbols; i++)
00358 {
00359 message.bin >> buf;
00360 message.bin >> buf2;
00361 if (!pEngine->existsEnumeratedOutputSymbol(buf))
00362 {
00363 OUTPUT(idText,text,"Enumerated output symbol " << buf << " does not exist");
00364 }
00365 else
00366 {
00367 Xabsl2EnumeratedOutputSymbol* pSymbol = pEngine->getEnumeratedOutputSymbol(buf);
00368 int value = 0;
00369
00370 setEnumeratedOutputSymbols.append(buf,pSymbol);
00371
00372 if (!pSymbol->enumElements.exists(buf2))
00373 {
00374 OUTPUT(idText,text,"Enum element " << buf2 << " for symbol " << buf
00375 << " does not exist");
00376 }
00377 else
00378 {
00379 value = pSymbol->enumElements.getElement(buf2)->v;
00380 }
00381 setEnumeratedOutputSymbolValues.append(buf,value);
00382 }
00383 }
00384
00385 return true;
00386 }
00387 case idXabsl2IntermediateCode:
00388 {
00389 char c;
00390 message.bin >> c;
00391
00392 if ((SolutionRequest::xabsl2EngineID)c != this->id)
00393 {
00394
00395 return true;
00396 }
00397
00398 Xabsl2MessageInputSource input(message.config);
00399 init(input);
00400 return true;
00401 }
00402 default:
00403 return false;
00404 }
00405 }
00406
00407 void GTXabsl2EngineExecutor::setSelectedAgent(const char* name)
00408 {
00409 if (pEngine != 0)
00410 pEngine->setSelectedAgent(name);
00411 }
00412
00413 GTXabsl2ErrorHandler::GTXabsl2ErrorHandler(SolutionRequest::xabsl2EngineID id)
00414 : id (id)
00415 {
00416 }
00417
00418 void GTXabsl2ErrorHandler::printError(const char* text)
00419 {
00420 OUTPUT(idText, text, "("
00421 << SolutionRequest::getXabsl2EngineIDName(id)
00422 << " Xabsl2Engine) error: " << text);
00423 }
00424
00425 void GTXabsl2ErrorHandler::printMessage(const char* text)
00426 {
00427 OUTPUT(idText, text, "("
00428 << SolutionRequest::getXabsl2EngineIDName(id)
00429 << " Xabsl2Engine): " << text);
00430 }
00431
00432 Xabsl2FileInputSource::Xabsl2FileInputSource(const char* fileName)
00433 : Xabsl2NamedItem(fileName), file(0)
00434 {
00435 }
00436
00437 Xabsl2FileInputSource::~Xabsl2FileInputSource()
00438 {
00439 if (file!=0) delete file;
00440 }
00441
00442 bool Xabsl2FileInputSource::open()
00443 {
00444 if (file==0) file=new InConfigFile(n);
00445 return (file!=0 && file->exists());
00446 }
00447
00448
00449 void Xabsl2FileInputSource::close()
00450 {
00451 if (file!=0) delete file;
00452 file = 0;
00453 }
00454
00455 double Xabsl2FileInputSource::readValue()
00456 {
00457 double d=0;
00458 if (file!=0) *file >> d;
00459 return d;
00460 }
00461
00462 bool Xabsl2FileInputSource::readString(char* destination, int maxLength)
00463 {
00464 if (file!=0) *file >> destination;
00465 return true;
00466 }
00467
00468 Xabsl2MessageInputSource::Xabsl2MessageInputSource(InConfigMessage& message)
00469 : message(message)
00470 {
00471 }
00472
00473 double Xabsl2MessageInputSource::readValue()
00474 {
00475 double d;
00476 message >> d;
00477 return d;
00478 }
00479
00480 bool Xabsl2MessageInputSource::readString(char* destination, int maxLength)
00481 {
00482 message >> destination;
00483 return true;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518