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

# Tools/Xabsl2/Xabsl2Engine/Xabsl2Option.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Xabsl2Option.cpp
    00003 *
    00004 * Implementation of class Xabsl2Option and helper classes
    00005 *
    00006 * @author Martin Lötzsch
    00007 */
    00008
    00009 #include "Xabsl2Option.h"
    00010
    00011 Xabsl2Statement* Xabsl2Statement::createStatement(Xabsl2InputSource& input,
    00012                                                   Xabsl2Option* subsequentOption,
    00013                                                   Xabsl2ErrorHandler& errorHandler,
    00014                                                   Xabsl2Array<Xabsl2State*>& states,
    00015                                                   Xabsl2Array<double>& parameters,
    00016                                                   Xabsl2Symbols& symbols,
    00017                                                   unsigned long& timeOfOptionExecution,
    00018                                                   unsigned long& timeOfStateExecution)
    00019 {
    00020   char c[100];
    00021
    00022   input.readString(c,1);
    00023
    00024   switch (*c)
    00025   {
    00026   case 't':
    00027     return new Xabsl2TransitionToState(input, errorHandler, states);
    00028   case 'i':
    00029     return new Xabsl2IfElseBlock(input, subsequentOption,errorHandler, states,
    00030       parameters, symbols, timeOfOptionExecution, timeOfStateExecution);
    00031   default:
    00032     errorHandler.error("Xabsl2Statement::create(): unknown type \"%c\"",*c);
    00033     return 0;
    00034   }
    00035 }
    00036
    00037 Xabsl2Statement::~Xabsl2Statement()
    00038 {
    00039 }
    00040
    00041 Xabsl2IfElseBlock::Xabsl2IfElseBlock(Xabsl2InputSource& input,
    00042                                                          Xabsl2Option* subsequentOption,
    00043                                      Xabsl2ErrorHandler& errorHandler,
    00044                                      Xabsl2Array<Xabsl2State*>& states,
    00045                                      Xabsl2Array<double>& parameters,
    00046                                      Xabsl2Symbols& symbols,
    00047                                      unsigned long& timeOfOptionExecution,
    00048                                      unsigned long& timeOfStateExecution) :
    00049 ifCondition(0), ifStatement(0), elseStatement(0)
    00050 {
    00051
    00052   // if case
    00053   XABSL2_DEBUG_INIT(errorHandler.message("creating if statement"));
    00054
    00055   ifCondition = Xabsl2BooleanExpression::create(input, subsequentOption,errorHandler,
    00056     parameters, symbols, timeOfOptionExecution, timeOfStateExecution);
    00057   if (errorHandler.errorsOccurred)
    00058   {
    00059     errorHandler.error("Xabsl2IfElseBlock::Xabsl2IfElseBlock(): could not create if condition");
    00060     return;
    00061   }
    00062
    00063   ifStatement = Xabsl2Statement::createStatement(input,subsequentOption,errorHandler,
    00064     states,parameters, symbols, timeOfOptionExecution, timeOfStateExecution);
    00065   if (errorHandler.errorsOccurred)
    00066   {
    00067     errorHandler.error("Xabsl2IfElseBlock::Xabsl2IfElseBlock(): could not create if statement");
    00068     return;
    00069   }
    00070
    00071   // else if cases
    00072   int numberOfElseIfStatements = (int)input.readValue();
    00073
    00074   for (int i=0;i<numberOfElseIfStatements; i++)
    00075   {
    00076     XABSL2_DEBUG_INIT(errorHandler.message("creating else-if statement"));
    00077
    00078     elseIfConditions.append("else-if",Xabsl2BooleanExpression::create(input, subsequentOption,
    00079       errorHandler, parameters, symbols,timeOfOptionExecution, timeOfStateExecution));
    00080     if (errorHandler.errorsOccurred)
    00081     {
    00082       errorHandler.error("Xabsl2IfElseBlock::Xabsl2IfElseBlock(): could not create else-if condition");
    00083       return;
    00084     }
    00085
    00086     elseIfStatements.append("else-if",Xabsl2Statement::createStatement(input,subsequentOption,
    00087       errorHandler,states, parameters, symbols, timeOfOptionExecution, timeOfStateExecution));
    00088     if (errorHandler.errorsOccurred)
    00089     {
    00090       errorHandler.error("Xabsl2IfElseBlock::Xabsl2IfElseBlock(): could not create else-if statement");
    00091       return;
    00092     }
    00093   }
    00094
    00095   // else case
    00096   XABSL2_DEBUG_INIT(errorHandler.message("creating else statement"));
    00097
    00098   elseStatement = Xabsl2Statement::createStatement(input, subsequentOption,
    00099     errorHandler, states, parameters, symbols, timeOfOptionExecution, timeOfStateExecution);
    00100   if (errorHandler.errorsOccurred)
    00101     errorHandler.error("Xabsl2IfElseBlock::Xabsl2IfElseBlock(): could not create else statement");
    00102
    00103 }
    00104
    00105 Xabsl2IfElseBlock::~Xabsl2IfElseBlock()
    00106 {
    00107   if (ifCondition != 0) delete ifCondition;
    00108   if (ifStatement != 0) delete ifStatement;
    00109
    00110   int i;
    00111   for (i=0; i<elseIfStatements.getSize(); i++)
    00112   {
    00113     if (elseIfConditions[i] != 0)
    00114       delete elseIfConditions[i];
    00115     if (elseIfStatements[i] != 0)
    00116       delete elseIfStatements[i];
    00117   }
    00118
    00119   if (elseStatement != 0) delete elseStatement;
    00120 }
    00121
    00122 Xabsl2State* Xabsl2IfElseBlock::getNextState()
    00123 {
    00124   if (ifCondition->getValue()) return ifStatement->getNextState();
    00125
    00126   for (int i=0; i<elseIfConditions.getSize(); i++)
    00127   {
    00128     if (elseIfConditions[i]->getValue()) return elseIfStatements[i]->getNextState();
    00129   }
    00130
    00131   return elseStatement->getNextState();
    00132 }
    00133
    00134 Xabsl2TransitionToState::Xabsl2TransitionToState(Xabsl2InputSource& input,
    00135                                                  Xabsl2ErrorHandler& errorHandler,
    00136                                                  Xabsl2Array<Xabsl2State*>& states)
    00137 {
    00138   char buf[100];
    00139   input.readString(buf,99);
    00140
    00141   nextState = states.getElement(buf);
    00142
    00143   XABSL2_DEBUG_INIT(errorHandler.message("creating a transition to state \"%s\"",nextState->n));
    00144 }
    00145
    00146 Xabsl2State* Xabsl2TransitionToState::getNextState()
    00147 {
    00148   return nextState;
    00149 }
    00150
    00151 Xabsl2State::Xabsl2State(const char* name, Xabsl2ErrorHandler& errorHandler,
    00152                          unsigned long (*pTimeFunction)())
    00153 : Xabsl2NamedItem(name), errorHandler(errorHandler), decisionTree(0),
    00154 targetState(false), pTimeFunction(pTimeFunction)
    00155 {
    00156 }
    00157
    00158 Xabsl2State::~Xabsl2State()
    00159 {
    00160   if (decisionTree != 0) delete decisionTree;
    00161   for (int i=0; i<parameterValues.getSize(); i++)
    00162     if (parameterValues[i]!=0)
    00163       delete parameterValues[i];
    00164 }
    00165
    00166 void Xabsl2State::create(Xabsl2InputSource& input,
    00167                          Xabsl2Array<Xabsl2Option*>& options,
    00168                          Xabsl2Array<Xabsl2BasicBehavior&>& basicBehaviors,
    00169                          Xabsl2Array<Xabsl2State*>& states,
    00170                          Xabsl2Array<double>& parameters,
    00171                          Xabsl2Symbols& symbols,
    00172                          unsigned long& timeOfOptionExecution)
    00173 {
    00174   XABSL2_DEBUG_INIT(errorHandler.message("creating state \"%s\"",n));
    00175
    00176   char c[100]; char buf[100];
    00177
    00178   // target state or not
    00179   input.readString(c,1);
    00180   if (*c == '1')
    00181   {
    00182     targetState = true;
    00183   }
    00184
    00185   // subsequent option or basic behavior
    00186   input.readString(c,1);
    00187   input.readString(buf,99);
    00188   switch (*c)
    00189   {
    00190   case 'o':
    00191     subsequentOption = options.getElement(buf);
    00192     subsequentBasicBehavior = 0;
    00193     break;
    00194   case 'b':
    00195     if (!basicBehaviors.exists(buf))
    00196     {
    00197       errorHandler.error("Xabsl2State::create(): the subsequent basic behavior \"%s\" was not registered",buf);
    00198       return;
    00199     }
    00200     subsequentBasicBehavior = &basicBehaviors.getElement(buf);
    00201     subsequentOption = 0;
    00202     break;
    00203   }
    00204
    00205   // read the parameters
    00206   int numberOfParameters = (int)input.readValue();
    00207
    00208   int i;
    00209   for (i = 0; i<numberOfParameters; i++)
    00210   {
    00211     input.readString(buf,99);
    00212     XABSL2_DEBUG_INIT(errorHandler.message("creating expession for set parameter \"%s\"",buf));
    00213
    00214     if (subsequentOption != 0)
    00215     {
    00216       if (!subsequentOption->parameters.exists(buf))
    00217       {
    00218         errorHandler.error("Xabsl2State::create(): the subsequent option does not have a parameter \"%s\"",buf);
    00219         return;
    00220       }
    00221       parametersOfSubsequentBehavior.append(buf,&subsequentOption->parameters.getPElement(buf)->e);
    00222     }
    00223     else
    00224     {
    00225       if (!subsequentBasicBehavior->parameters.exists(buf))
    00226       {
    00227         errorHandler.error("Xabsl2State::create(): the subsequent basic behavior does not have a parameter \"%s\"",buf);
    00228         return;
    00229       }
    00230       parametersOfSubsequentBehavior.append(buf,&subsequentBasicBehavior->parameters.getPElement(buf)->e);
    00231     }
    00232
    00233     parameterValues.append(buf,Xabsl2DecimalExpression::create(input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution));
    00234     if (errorHandler.errorsOccurred)
    00235     {
    00236       errorHandler.error("Xabsl2State::create(): could not create decimal expression for parameter \"%s\"",buf);
    00237       return;
    00238     }
    00239   }
    00240
    00241   // set output symbols
    00242   int numberOfSetOutputSymbol = (int)input.readValue();
    00243
    00244   for(i = 0; i< numberOfSetOutputSymbol; i++)
    00245   {
    00246     input.readString(buf,99);
    00247     XABSL2_DEBUG_INIT(errorHandler.message("creating reference to enumerated output symbol \"%s\"",buf));
    00248
    00249     if (!symbols.existsEnumeratedOutputSymbol(buf))
    00250     {
    00251       errorHandler.error("Xabsl2State::create(): enumerated output symbol \"%s\" does not exist",buf);
    00252       return;
    00253     }
    00254     outputSymbols.append(buf,symbols.getEnumeratedOutputSymbol(buf));
    00255     input.readString(buf,99);
    00256
    00257     if (!outputSymbols[i]->enumElements.exists(buf))
    00258     {
    00259       errorHandler.error("Xabsl2State::create(): enum element \"%s\" of enumerated output symbol \"%s\" does not exist",buf, outputSymbols[i]->n);
    00260       return;
    00261     }
    00262     outputSymbolValues.append(buf,outputSymbols[i]->enumElements.getElement(buf)->v);
    00263   }
    00264
    00265   // transition to state or if / else block
    00266   decisionTree = Xabsl2Statement::createStatement(input,subsequentOption,errorHandler,
    00267     states,parameters, symbols, timeOfOptionExecution,timeOfStateExecution);
    00268   if (errorHandler.errorsOccurred)
    00269     errorHandler.error("Xabsl2State::create(): could not create decision tree for state \"%s\"",n);
    00270 }
    00271
    00272 Xabsl2State* Xabsl2State::getNextState()
    00273 {
    00274   timeOfStateExecution = pTimeFunction() - timeWhenStateWasActivated;
    00275
    00276   Xabsl2State* nextState = decisionTree->getNextState();
    00277
    00278   return nextState;
    00279 }
    00280
    00281 void Xabsl2State::setOutputSymbols()
    00282 {
    00283   int i;
    00284
    00285   // set all parameters of the next subsequent behavior to 0
    00286   if (subsequentBasicBehavior!=0)
    00287   {
    00288     for (i=0;i<subsequentBasicBehavior->parameters.getSize();i++)
    00289     {
    00290       subsequentBasicBehavior->parameters[i] = 0;
    00291     }
    00292   }
    00293   else
    00294   {
    00295     for (i=0;i<subsequentOption->parameters.getSize();i++)
    00296     {
    00297       subsequentOption->parameters.setElement(i,0);
    00298     }
    00299   }
    00300
    00301   // Sets the subsequent behavior parameters.
    00302   for (i=0; i<parametersOfSubsequentBehavior.getSize(); i++)
    00303   {
    00304     *parametersOfSubsequentBehavior[i] = parameterValues[i]->getValue();
    00305   }
    00306
    00307   for (i = 0; i < outputSymbols.getSize(); i++)
    00308   {
    00309     outputSymbols[i]->activeValue = outputSymbolValues[i];
    00310     outputSymbols[i]->activeValueWasSet = true;
    00311   }
    00312 }
    00313
    00314 void Xabsl2State::reset()
    00315 {
    00316   timeWhenStateWasActivated = pTimeFunction();
    00317   timeOfStateExecution = 0;
    00318 }
    00319
    00320 bool Xabsl2State::isTargetState() const
    00321 {
    00322   return targetState;
    00323 }
    00324
    00325 Xabsl2Option::Xabsl2Option(const char* name,
    00326                            Xabsl2InputSource& input,
    00327                            Xabsl2ErrorHandler& errorHandler,
    00328                            unsigned long (*pTimeFunction)())
    00329                            : Xabsl2NamedItem(name), activeState(0), optionIsActive(false), optionWasActive(false),
    00330                            initialState(0), errorHandler(errorHandler),
    00331                            pTimeFunction(pTimeFunction)
    00332 {
    00333   int numberOfOptionParameters = (int)input.readValue();
    00334   char buf[100];
    00335   for (int i=0; i< numberOfOptionParameters; i++)
    00336   {
    00337     input.readString(buf,99);
    00338     XABSL2_DEBUG_INIT(errorHandler.message("registering option parameter \"%s\" for option \"%s\"",buf,n));
    00339
    00340     parameters.append(buf,0);
    00341   }
    00342 }
    00343
    00344 Xabsl2Option::~Xabsl2Option()
    00345 {
    00346   for (int i=0;i<states.getSize();i++)
    00347     if (states[i]!=0)
    00348       delete states[i];
    00349 }
    00350
    00351 void Xabsl2Option::create(Xabsl2InputSource& input,
    00352                           Xabsl2Array<Xabsl2Option*>& options,
    00353                           Xabsl2Array<Xabsl2BasicBehavior&>& basicBehaviors,
    00354                           Xabsl2Symbols& symbols
    00355                           )
    00356 {
    00357   int i;
    00358
    00359   // the number of states of the option
    00360   int numberOfStates = (int)input.readValue();
    00361
    00362   char stateName[100];
    00363
    00364   // register all states to the array
    00365   for (i=0; i< numberOfStates; i++)
    00366   {
    00367     input.readString(stateName, 99);
    00368     states.append(stateName, new Xabsl2State(stateName,errorHandler,pTimeFunction));
    00369   }
    00370   XABSL2_DEBUG_INIT(errorHandler.message("Xabsl2Option::create(): registered %i states",numberOfStates));
    00371
    00372   // set the initial state
    00373   input.readString(stateName,99);
    00374   initialState = states.getElement(stateName);
    00375   activeState = initialState;
    00376
    00377   XABSL2_DEBUG_INIT(errorHandler.message("Xabsl2Option::create(): initial state: \"%s\"",initialState->n));
    00378
    00379   // create the states and their subelements
    00380   for (i=0; i< numberOfStates; i++)
    00381   {
    00382     states[i]->create(input, options, basicBehaviors, states, parameters, symbols, timeOfOptionExecution);
    00383     if (errorHandler.errorsOccurred)
    00384     {
    00385       errorHandler.error("Xabsl2Option::create(): could not create state \"%s\"",states[i]->n);
    00386       return;
    00387     }
    00388   }
    00389
    00390 }
    00391
    00392 void Xabsl2Option::execute()
    00393 {
    00394   if (!optionWasActive)
    00395   {
    00396     timeWhenOptionWasActivated = pTimeFunction();
    00397     activeState = initialState;
    00398     activeState->reset();
    00399   }
    00400   optionIsActive = true;
    00401   timeOfOptionExecution = pTimeFunction() - timeWhenOptionWasActivated;
    00402
    00403   Xabsl2State* newState = activeState->getNextState();
    00404
    00405   if (newState != activeState)
    00406   {
    00407     newState->reset();
    00408   }
    00409   activeState = newState;
    00410   activeState->setOutputSymbols();
    00411 }
    00412
    00413 bool Xabsl2Option::getOptionReachedATargetState() const
    00414 {
    00415   if (optionWasActive && activeState->isTargetState())
    00416   {
    00417     return true;
    00418   }
    00419   else
    00420   {
    00421     return false;
    00422   }
    00423 }