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.recordLog(*pEngine);
00091 if(profiler.profilerWriteMode == GTXabsl2Profiler::writeProfiles && profiler.log.size()){
00092 profiler.writeLogEntriesToFileAndRemove();
00093 }
00094 else if(profiler.profilerWriteMode == GTXabsl2Profiler::writeCompleteProfiles && profiler.log.size()){
00095 profiler.writeCompleteLogToFileAndClear();
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 DEBUG_RESPONSE("automated requests:xabsl:debugMessages", sendDebugMessage(););
00114 break;
00115 case SolutionRequest::headControl:
00116 if (getDebugKeyTable().isActive(DebugKeyTable::sendXabsl2DebugMessagesForHeadControl))
00117 sendDebugMessage();
00118 DEBUG_RESPONSE("automated requests:xabsl:HeadControl:debugMessages", sendDebugMessage(););
00119 break;
00120 default:
00121 ASSERT(false);
00122
00123 }
00124 }
00125
00126 void GTXabsl2EngineExecutor::sendDebugMessage()
00127 {
00128 int i, j;
00129
00130 Out& out = getDebugOut().bin;
00131
00132
00133 out << (char)id;
00134
00135
00136 out << pEngine->getSelectedAgentName();
00137
00138
00139 out << watchedDecimalInputSymbols.getSize();
00140 for (i = 0; i < watchedDecimalInputSymbols.getSize(); i++)
00141 out << watchedDecimalInputSymbols[i]->n << watchedDecimalInputSymbols[i]->getValue();
00142
00143
00144 out << watchedBooleanInputSymbols.getSize();
00145 for (i = 0; i < watchedBooleanInputSymbols.getSize(); i++)
00146 out << watchedBooleanInputSymbols[i]->n << (char)watchedBooleanInputSymbols[i]->getValue();
00147
00148
00149 out << watchedEnumeratedInputSymbols.getSize();
00150 for (i = 0; i < watchedEnumeratedInputSymbols.getSize(); i++)
00151 {
00152 Xabsl2EnumeratedInputSymbol* s = watchedEnumeratedInputSymbols[i];
00153 out << s->n;
00154 int v = s->getValue();
00155 for (j = 0; j < s->enumElements.getSize();j++)
00156 {
00157 if (s->enumElements[j]->v == v) break;
00158 }
00159 if (j==s->enumElements.getSize())
00160 {
00161 out << " ";
00162 }
00163 else
00164 {
00165 out << s->enumElements[j]->n;
00166 }
00167 }
00168
00169
00170 out << watchedEnumeratedOutputSymbols.getSize();
00171 for (i=0; i<watchedEnumeratedOutputSymbols.getSize(); i++)
00172 {
00173 Xabsl2EnumeratedOutputSymbol* s = watchedEnumeratedOutputSymbols[i];
00174 out << s->n;
00175 for (j = 0; j < s->enumElements.getSize();j++)
00176 {
00177 if (s->enumElements[j]->v == s->activeValue) break;
00178 }
00179 if (j==s->enumElements.getSize())
00180 {
00181 out << " ";
00182 }
00183 else
00184 {
00185 out << s->enumElements[j]->n;
00186 }
00187 }
00188
00189
00190 int numberOfActiveOptions = 0;
00191 const Xabsl2Option* option;
00192 if (debugMode == executeOption || debugMode == executeRootOption)
00193
00194 {
00195 option = pEngine->getRootOption();
00196
00197 while (option!=0)
00198 {
00199 option = option->activeState->subsequentOption;
00200 numberOfActiveOptions++;
00201 }
00202 }
00203 out << numberOfActiveOptions;
00204
00205
00206
00207 if (debugMode == executeOption || debugMode == executeRootOption)
00208 {
00209 option = pEngine->getRootOption();
00210
00211 while (option!=0)
00212 {
00213 out << option->n << option->timeOfOptionExecution
00214 << option->activeState->n << option->activeState->timeOfStateExecution;
00215
00216
00217 int numberOfOptionParameters = option->parameters.getSize();
00218 out << (char)numberOfOptionParameters;
00219
00220 for (i = 0; i < numberOfOptionParameters; i++)
00221 {
00222 out << option->parameters.getName(i);
00223 out << option->parameters[i];
00224 }
00225
00226 option = option->activeState->subsequentOption;
00227 }
00228 }
00229
00230
00231 const Xabsl2BasicBehavior* bb = pEngine->getSelectedBasicBehavior();
00232 out << bb->n;
00233
00234
00235 char numberOfBasicBehaviorParameters = bb->parameters.getSize();
00236 out << numberOfBasicBehaviorParameters;
00237
00238
00239 for (i=0;i<numberOfBasicBehaviorParameters;i++)
00240 {
00241 out << bb->parameters.getName(i);
00242 out << bb->parameters[i];
00243 }
00244
00245
00246 char buf[200];
00247 printGeneratedMainActionToString(buf);
00248 out << buf;
00249
00250 getDebugOut().finishMessage(idXabsl2DebugMessage);
00251 }
00252
00253 bool GTXabsl2EngineExecutor::handleMessage(InMessage& message)
00254 {
00255 switch (message.getMessageID())
00256 {
00257 case idXabsl2DebugRequest:
00258 {
00259
00260 char buf[100];
00261 char buf2[100];
00262 double valueDouble;
00263 char c;
00264 int i, numberOfWatchedInputSymbols, numberOfWatchedEnumeratedOutputSymbols;
00265
00266 message.bin >> c;
00267
00268 if ((SolutionRequest::xabsl2EngineID)c != this->id)
00269 {
00270
00271 return true;
00272 }
00273
00274
00275 watchedDecimalInputSymbols.clear();
00276 watchedBooleanInputSymbols.clear();
00277 watchedEnumeratedInputSymbols.clear();
00278 watchedEnumeratedOutputSymbols.clear();
00279 setEnumeratedOutputSymbols.clear();
00280 setEnumeratedOutputSymbolValues.clear();
00281
00282
00283 message.bin >> numberOfWatchedInputSymbols;
00284
00285 for (i=0; i< numberOfWatchedInputSymbols; i++)
00286 {
00287 message.bin >> c;
00288 message.bin >> buf;
00289 switch(c)
00290 {
00291 case 'd':
00292 if (pEngine->existsDecimalInputSymbol(buf))
00293 watchedDecimalInputSymbols.append(buf,pEngine->getDecimalInputSymbol(buf));
00294 break;
00295 case 'b':
00296 if (pEngine->existsBooleanInputSymbol(buf))
00297 watchedBooleanInputSymbols.append(buf,pEngine->getBooleanInputSymbol(buf));
00298 break;
00299 case 'e':
00300 if (pEngine->existsEnumeratedInputSymbol(buf))
00301 watchedEnumeratedInputSymbols.append(buf,pEngine->getEnumeratedInputSymbol(buf));
00302 break;
00303 }
00304 }
00305
00306
00307 message.bin >> numberOfWatchedEnumeratedOutputSymbols;
00308 for (i=0; i< numberOfWatchedEnumeratedOutputSymbols; i++)
00309 {
00310 message.bin >> buf;
00311 if (pEngine->existsEnumeratedOutputSymbol(buf))
00312 watchedEnumeratedOutputSymbols.append(buf,pEngine->getEnumeratedOutputSymbol(buf));
00313 }
00314
00315 char mode;
00316 char numberOfParameters;
00317 message.bin >> mode;
00318 switch (mode)
00319 {
00320 case 'b':
00321 debugMode = executeBasicBehavior;
00322 message.bin >> buf;
00323 if(!pEngine->setSelectedBasicBehavior(buf))
00324 {
00325 OUTPUT(idText, text, "BasicBehavior " << buf << " does not exist");
00326 }
00327
00328 message.bin >> numberOfParameters;
00329 for(i = 0; i < numberOfParameters; i++)
00330 {
00331 message.bin >> buf2;
00332 message.bin >> valueDouble;
00333 pEngine->setBasicBehaviorParameter(buf, buf2, valueDouble);
00334 }
00335 break;
00336 case 'o':
00337 debugMode = executeOption;
00338 message.bin >> buf;
00339 if(!pEngine->setRootOption(buf))
00340 {
00341 OUTPUT(idText, text, "Option " << buf << " does not exist");
00342 }
00343 message.bin >> numberOfParameters;
00344 for(i = 0; i < numberOfParameters; i++)
00345 {
00346 message.bin >> buf2;
00347 message.bin >> valueDouble;
00348 pEngine->setOptionParameter(buf, buf2, valueDouble);
00349 }
00350 break;
00351 case 'd':
00352 debugMode = executeRootOption;
00353 break;
00354 }
00355
00356
00357 char numberOfChangedOutputSymbols;
00358 message.bin >> numberOfChangedOutputSymbols;
00359 for(i = 0; i < numberOfChangedOutputSymbols; i++)
00360 {
00361 message.bin >> buf;
00362 message.bin >> buf2;
00363 if (!pEngine->existsEnumeratedOutputSymbol(buf))
00364 {
00365 OUTPUT(idText,text,"Enumerated output symbol " << buf << " does not exist");
00366 }
00367 else
00368 {
00369 Xabsl2EnumeratedOutputSymbol* pSymbol = pEngine->getEnumeratedOutputSymbol(buf);
00370 int value = 0;
00371
00372 setEnumeratedOutputSymbols.append(buf,pSymbol);
00373
00374 if (!pSymbol->enumElements.exists(buf2))
00375 {
00376 OUTPUT(idText,text,"Enum element " << buf2 << " for symbol " << buf
00377 << " does not exist");
00378 }
00379 else
00380 {
00381 value = pSymbol->enumElements.getElement(buf2)->v;
00382 }
00383 setEnumeratedOutputSymbolValues.append(buf,value);
00384 }
00385 }
00386
00387 return true;
00388 }
00389 case idXabsl2IntermediateCode:
00390 {
00391 char c;
00392 message.bin >> c;
00393
00394 if ((SolutionRequest::xabsl2EngineID)c != this->id)
00395 {
00396
00397 return true;
00398 }
00399
00400 Xabsl2MessageInputSource input(message.config);
00401 init(input);
00402 return true;
00403 }
00404 default:
00405 return false;
00406 }
00407 }
00408
00409 void GTXabsl2EngineExecutor::setSelectedAgent(const char* name)
00410 {
00411 if (pEngine != 0)
00412 pEngine->setSelectedAgent(name);
00413 }
00414
00415 GTXabsl2ErrorHandler::GTXabsl2ErrorHandler(SolutionRequest::xabsl2EngineID id)
00416 : id (id)
00417 {
00418 }
00419
00420 void GTXabsl2ErrorHandler::printError(const char* text)
00421 {
00422 OUTPUT(idText, text, "("
00423 << SolutionRequest::getXabsl2EngineIDName(id)
00424 << " Xabsl2Engine) error: " << text);
00425 }
00426
00427 void GTXabsl2ErrorHandler::printMessage(const char* text)
00428 {
00429 OUTPUT(idText, text, "("
00430 << SolutionRequest::getXabsl2EngineIDName(id)
00431 << " Xabsl2Engine): " << text);
00432 }
00433
00434 Xabsl2FileInputSource::Xabsl2FileInputSource(const char* fileName)
00435 : Xabsl2NamedItem(fileName), file(0)
00436 {
00437 }
00438
00439 Xabsl2FileInputSource::~Xabsl2FileInputSource()
00440 {
00441 if (file!=0) delete file;
00442 }
00443
00444 bool Xabsl2FileInputSource::open()
00445 {
00446 if (file==0) file=new InConfigFile(n);
00447 return (file!=0 && file->exists());
00448 }
00449
00450
00451 void Xabsl2FileInputSource::close()
00452 {
00453 if (file!=0) delete file;
00454 file = 0;
00455 }
00456
00457 double Xabsl2FileInputSource::readValue()
00458 {
00459 double d=0;
00460 if (file!=0) *file >> d;
00461 return d;
00462 }
00463
00464 bool Xabsl2FileInputSource::readString(char* destination, int maxLength)
00465 {
00466 if (file!=0) *file >> destination;
00467 return true;
00468 }
00469
00470 Xabsl2MessageInputSource::Xabsl2MessageInputSource(InConfigMessage& message)
00471 : message(message)
00472 {
00473 }
00474
00475 double Xabsl2MessageInputSource::readValue()
00476 {
00477 double d;
00478 message >> d;
00479 return d;
00480 }
00481
00482 bool Xabsl2MessageInputSource::readString(char* destination, int maxLength)
00483 {
00484 message >> destination;
00485 return true;
00486 }
00487