Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Xabsl2/Xabsl2Engine/Xabsl2Engine.cpp

Go to the documentation of this file.
00001 /** 
00002 * @file Xabsl2Engine.cpp
00003 *
00004 * Implementation of class Xabsl2Engine
00005 *
00006 * @author Matthias Jüngel
00007 * @author Martin Lötzsch
00008 */
00009 
00010 #include "Xabsl2Engine.h"
00011 
00012 Xabsl2Engine::Xabsl2Engine(Xabsl2ErrorHandler& e,unsigned long (*pTimeFunction)())
00013 : Xabsl2Symbols(e), selectedAgent(0), rootOption(0),
00014 selectedBasicBehavior(0), errorHandler(e), initialized(false), 
00015 pTimeFunction(pTimeFunction)
00016 {
00017 }
00018 
00019 Xabsl2Engine::~Xabsl2Engine()
00020 {
00021   int i;
00022   for (i=0; i< options.getSize(); i++)
00023     if (options[i]!=0) 
00024       delete options[i];
00025   for (i=0; i< agents.getSize(); i++)
00026     if (agents[i]!=0) 
00027       delete agents[i];
00028 }
00029 
00030 void Xabsl2Engine::execute()
00031 {
00032   Xabsl2Option* currentOption = rootOption;
00033 
00034   int i;
00035 
00036   while (currentOption!=0)
00037   {
00038     currentOption->execute();
00039     
00040     Xabsl2Option* nextOption = currentOption->activeState->subsequentOption;
00041     if (nextOption == 0)
00042     {
00043       selectedBasicBehavior = currentOption->activeState->subsequentBasicBehavior;
00044     }
00045     currentOption = nextOption;
00046   }
00047 
00048   for (i=0; i< options.getSize(); i++)
00049   {
00050     Xabsl2Option* option = options[i];
00051     option->optionWasActive = option->optionIsActive;
00052     option->optionIsActive = false;
00053   }
00054 
00055   setOutputSymbols();
00056 
00057   executeSelectedBasicBehavior();
00058 }
00059 
00060 void Xabsl2Engine::executeSelectedBasicBehavior()
00061 {
00062 
00063   selectedBasicBehavior->execute();
00064 
00065   for (int i=0;i<basicBehaviors.getSize();i++)
00066   {
00067     basicBehaviors[i].basicBehaviorWasActiveDuringLastExecutionOfEngine = false; 
00068   }
00069   
00070   selectedBasicBehavior->basicBehaviorWasActiveDuringLastExecutionOfEngine = true;
00071 }
00072 
00073 void Xabsl2Engine::createOptionGraph(Xabsl2InputSource& input)
00074 {
00075   int i;
00076   char buf1[100],buf2[100];
00077 
00078   if (initialized)
00079   {
00080     errorHandler.error("createOptionGraph(): Don't call this function twice");
00081     return;
00082   }
00083   if (!input.open()) 
00084   {
00085     errorHandler.error("createOptionGraph(): Can't open input source");
00086     return;
00087   }
00088 
00089   // the total number of options in the intermediate code
00090   int numberOfOptions = (int)input.readValue(); 
00091   
00092   // create empty options
00093   for (i=0; i< numberOfOptions; i++)
00094   {
00095     input.readString(buf1,99);
00096     options.append(buf1,new Xabsl2Option(buf1,input,errorHandler,pTimeFunction));
00097   }
00098   XABSL2_DEBUG_INIT(errorHandler.message("registered %i options",numberOfOptions));
00099 
00100   // create the options and its states
00101   for (i=0; i< numberOfOptions; i++)
00102   {
00103     XABSL2_DEBUG_INIT(errorHandler.message("creating option \"%s\"",options[i]->n));
00104     options[i]->create(input,options,basicBehaviors,*this);
00105     if (errorHandler.errorsOccurred)
00106     {
00107       errorHandler.error("Xabsl2Engine::createOptionGraph(): could not create option \"%s\"",options[i]->n);
00108       return;
00109     }
00110   }
00111 
00112   // create the agents
00113   int numberOfAgents = (int)input.readValue();
00114   for (i=0; i< numberOfAgents; i++)
00115   {
00116     input.readString(buf1,99);
00117     input.readString(buf2,99);
00118     agents.append(buf1,new Xabsl2Agent(buf1,options.getElement(buf2),errorHandler));
00119   }
00120 
00121   // check for loops in the option graph
00122   for (i=0;i<agents.getSize();i++)
00123   {
00124     Xabsl2Option* currentOptionPath[1000];
00125     
00126     currentOptionPath[0] = agents[i]->getRootOption();
00127 
00128     // call recursively the checkForLoops function
00129     if (checkForLoops(currentOptionPath,0)) 
00130     {
00131       errorHandler.error("createOptionGraph(): The created option graph contains loops");
00132     };
00133   }
00134 
00135 
00136   selectedAgent = agents[0];
00137   selectedBasicBehavior = &basicBehaviors[0];
00138   rootOption = selectedAgent->getRootOption();
00139 
00140   XABSL2_DEBUG_INIT(errorHandler.message("selected agent: \"%s\"",selectedAgent->n));
00141   input.close();
00142   initialized = true;
00143 }
00144 
00145 
00146 bool Xabsl2Engine::checkForLoops(Xabsl2Option* currentOptionPath[],int currentDepth)
00147 {
00148   int i,j;
00149   Xabsl2Option* currentOption = currentOptionPath[currentDepth];
00150 
00151   for (i=0; i<currentOption->states.getSize(); i++)
00152   {
00153     if (currentOption->states[i]->subsequentOption != 0)
00154     {
00155       for(j=0; j<=currentDepth; j++)
00156       {
00157         // check for the subsequent option of each state whether the referenced 
00158         // option is contained in the current option path
00159         if (currentOption->states[i]->subsequentOption == currentOptionPath[j])
00160         {
00161           errorHandler.error("checkForLoops() : state \"%s\" in option \"%s\" references subsequent option \"%s\". But option \"%s\" is also directly or indirectly referenced by option \"%s\".",
00162             currentOption->states[i]->n, 
00163             currentOption->n,
00164             currentOption->states[i]->subsequentOption->n,
00165             currentOption->n,
00166             currentOption->states[i]->subsequentOption->n);
00167 
00168           return true;
00169         }
00170       }
00171       
00172       // recursion
00173       currentOptionPath[currentDepth+1] = currentOption->states[i]->subsequentOption;
00174       if (checkForLoops(currentOptionPath,currentDepth+1))
00175       {
00176         return true;
00177       }
00178     }
00179   }
00180 
00181   return false;
00182 }
00183 
00184 
00185 void Xabsl2Engine::registerBasicBehavior(Xabsl2BasicBehavior& basicBehavior)
00186 {
00187   XABSL2_DEBUG_INIT(errorHandler.message("registering basic behavior \"%s\"",basicBehavior.n));
00188 
00189   if (basicBehaviors.exists(basicBehavior.n))
00190   {
00191     errorHandler.error("registerBasicBehavior(): basic behavior \"%s\" was already registered",basicBehavior.n);
00192     return;
00193   }
00194   basicBehaviors.append(basicBehavior.n,basicBehavior);
00195 }
00196 
00197 bool Xabsl2Engine::setSelectedBasicBehavior(const char* name)
00198 {
00199   if (!basicBehaviors.exists(name))
00200     return false;
00201   else
00202   {
00203     selectedBasicBehavior = &basicBehaviors.getElement(name);
00204     return true;
00205   }
00206 }
00207 
00208 bool Xabsl2Engine::setRootOption(const char* name)
00209 {
00210   // check if the option exists
00211   if (!options.exists(name)) return false;
00212 
00213   // set the curren root option to the requested option
00214   rootOption = options.getElement(name);
00215 
00216   return true;
00217 }
00218 
00219 void Xabsl2Engine::setRootOption()
00220 {
00221   rootOption = selectedAgent->getRootOption();
00222 }
00223 
00224 const Xabsl2Option* Xabsl2Engine::getRootOption() const
00225 {
00226   return rootOption;
00227 }
00228 
00229 bool Xabsl2Engine::setBasicBehaviorParameter(const char* name, const char* param, double value)
00230 {
00231   if (!basicBehaviors.exists(name)) 
00232   {
00233     return false;
00234   }
00235   else if (!basicBehaviors.getElement(name).parameters.exists(param))
00236   {
00237     return false;
00238   }
00239   else 
00240   { 
00241     basicBehaviors.getElement(name).parameters.setElement(param,value);
00242     return false;
00243   }
00244 }
00245 
00246 bool Xabsl2Engine::setOptionParameter(const char* name, const char* param, double value)
00247 {
00248   if (!options.exists(name)) 
00249   {
00250     return false;
00251   }
00252   else if (!options.getElement(name)->parameters.exists(param))
00253   {
00254     return false;
00255   }
00256   else 
00257   { 
00258     options.getElement(name)->parameters.setElement(param,value);
00259     return false;
00260   }
00261 }
00262 
00263 const char* Xabsl2Engine::getSelectedAgentName()
00264 {
00265   return selectedAgent->n;
00266 }
00267 
00268 const Xabsl2BasicBehavior* Xabsl2Engine::getSelectedBasicBehavior()
00269 {
00270   return selectedBasicBehavior;
00271 }
00272 
00273 bool Xabsl2Engine::setSelectedAgent(const char* name)
00274 {
00275   if (!agents.exists(name)) 
00276   {
00277     return false;
00278   }
00279 
00280   Xabsl2Agent* newAgent = agents.getElement(name);
00281 
00282   if (selectedAgent != newAgent)
00283   {
00284     selectedAgent = newAgent;
00285     rootOption = selectedAgent->getRootOption();
00286   }
00287 
00288   return true;
00289 }
00290 
00291 /*
00292 * Change Log
00293 * 
00294 * $Log: Xabsl2Engine.cpp,v $
00295 * Revision 1.3  2004/05/26 09:31:52  juengel
00296 * Warnings removed.
00297 *
00298 * Revision 1.2  2004/05/25 20:26:34  loetzsch
00299 * improved
00300 *
00301 * Revision 1.1.1.1  2004/05/22 17:37:56  cvsadm
00302 * created new repository GT2004_WM
00303 *
00304 * Revision 1.4  2004/04/09 14:31:47  tim
00305 * basic behaviors are now executed after the computation of the output symbols
00306 *
00307 * Revision 1.3  2003/12/16 18:53:22  loetzsch
00308 * The XabslEngine now checks for loops
00309 *
00310 * Revision 1.2  2003/10/08 11:50:09  loetzsch
00311 * made the Xabsl2Engine really platform independent
00312 * (removed inclusion of Platform/SystemCall.h)
00313 * A time function is given to the engine by parameter.
00314 *
00315 * Revision 1.1  2003/10/07 10:13:25  cvsadm
00316 * Created GT2004 (M.J.)
00317 *
00318 * Revision 1.5  2003/09/30 10:51:11  dueffert
00319 * typos fixed
00320 *
00321 * Revision 1.4  2003/09/20 16:34:16  loetzsch
00322 * renamed "following-option-..." to "subsequent-option-.." and
00323 * "following-basic-behavior-.." to "subsequent-basic-behavior-.."
00324 * for consistency with publications
00325 *
00326 * Revision 1.3  2003/09/16 13:27:21  loetzsch
00327 * changed all occurrences of "option tree" to "option graph"
00328 *
00329 * Revision 1.2  2003/08/04 16:02:50  loetzsch
00330 * ::setSelectedAgent doesn't throw error messages when anymore
00331 *
00332 * Revision 1.1.1.1  2003/07/02 09:40:29  cvsadm
00333 * created new repository for the competitions in Padova from the 
00334 * tamara CVS (Tuesday 2:00 pm)
00335 *
00336 * removed unused solutions
00337 *
00338 * Revision 1.17  2003/04/14 16:18:51  loetzsch
00339 * ATH after GermanOpen CVS merge
00340 * added basicBehaviorWasActiveDuringLastExecutionOfEngine
00341 *
00342 * Revision 1.2  2003/04/09 17:01:12  loetzsch
00343 * added the boolean basicBehaviorWasActiveDuringLastExecutionOfEngine
00344 *
00345 * Revision 1.1.1.1  2003/04/09 14:23:22  loetzsch
00346 * started Aibo Team Humboldt's GermanOpen CVS
00347 *
00348 * Revision 1.16  2003/03/06 11:45:04  dueffert
00349 * re-order warning removed
00350 *
00351 * Revision 1.15  2003/01/28 18:07:47  loetzsch
00352 * no message
00353 *
00354 * Revision 1.14  2003/01/28 17:51:35  loetzsch
00355 * added function setOptionParameter()
00356 *
00357 * Revision 1.13  2003/01/19 13:04:52  loetzsch
00358 * xabsl2 agents now can be changed by using the Module and SolutionRequest
00359 * mechanism
00360 *
00361 * Revision 1.12  2003/01/14 16:28:32  loetzsch
00362 * creation of references to output symbols added
00363 * setting of output symbols added
00364 *
00365 * Revision 1.11  2003/01/13 22:39:38  loetzsch
00366 * implemented the execution of the engine.
00367 *
00368 * Revision 1.10  2003/01/13 13:18:18  loetzsch
00369 * Creation of boolean and decimal expressions finished.
00370 *
00371 * Revision 1.9  2003/01/12 14:54:04  loetzsch
00372 * continued creation of option tree: Xabsl2Statement and derivates added
00373 *
00374 * Revision 1.8  2003/01/11 14:41:42  loetzsch
00375 * continued creation of the option tree
00376 *
00377 * Revision 1.7  2003/01/09 17:28:33  loetzsch
00378 * introduced Xabsl2Agent, continued Xabsl2Option
00379 *
00380 * Revision 1.6  2003/01/08 18:47:17  loetzsch
00381 * first version of state implementation
00382 *
00383 * Revision 1.5  2003/01/08 15:22:33  loetzsch
00384 * - started implementation of the option tree
00385 * - started the reading of the intermediate code
00386 *
00387 * Revision 1.4  2002/12/11 12:23:31  loetzsch
00388 * basic behaviors register their parameters in their constructor
00389 * the parameters array contains only references to doubles in the basic behavior
00390 *
00391 * Revision 1.3  2002/12/06 21:13:37  loetzsch
00392 * Decimal input symbols can now be registered at the engine
00393 *
00394 * Revision 1.2  2002/12/02 19:56:32  loetzsch
00395 * - Xabsl2Array now seems to work for more than 1 element
00396 * - Basic behaviors now can be registered at the engine
00397 * - Basic behaviors have access to their parameters
00398 *
00399 * Revision 1.1  2002/12/01 17:54:30  loetzsch
00400 * continued Xabsl2Engine: Xabsl2Array seems to work now
00401 *
00402 */
00403 

Generated on Thu Sep 23 19:57:42 2004 for GT2004 by doxygen 1.3.6