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

# Tools/Xabsl2/Xabsl2Engine/Xabsl2DecimalExpression.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Xabsl2DecimalExpression.cpp
    00003 *
    00004 * Implementation of Xabsl2DecimalExpression and derivates
    00005 *
    00006 * @author Martin Lötzsch
    00007 */
    00008
    00009 #include "Xabsl2DecimalExpression.h"
    00010 #include "Xabsl2BooleanExpression.h"
    00011
    00012 Xabsl2DecimalExpression::~Xabsl2DecimalExpression()
    00013 {
    00014 }
    00015
    00016 Xabsl2DecimalExpression* Xabsl2DecimalExpression::create(Xabsl2InputSource& input,
    00017                                                          Xabsl2Option* subsequentOption,
    00018                                                          Xabsl2ErrorHandler& errorHandler,
    00019                                                          Xabsl2Array<double>& parameters,
    00020                                                          Xabsl2Symbols& symbols,
    00021                                                          unsigned long& timeOfOptionExecution,
    00022                                                          unsigned long& timeOfStateExecution)
    00023 {
    00024   char c[100];
    00025   input.readString(c,1);
    00026   Xabsl2ArithmeticOperator* arithmeticOperator;
    00027   Xabsl2DecimalExpression* operand1;
    00028   Xabsl2DecimalExpression* operand2;
    00029
    00030   switch (*c)
    00031   {
    00032   case 'r':
    00033     return new Xabsl2DecimalInputSymbolRef(input,errorHandler,symbols);
    00034   case 'f':
    00035     return new Xabsl2DecimalInputFunctionCall(input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution);
    00036   case 'c':
    00037     // constants are treates as decimal values (there is no difference from the engines point of view.)
    00038   case 'v':
    00039     return new Xabsl2DecimalValue(input,errorHandler, symbols);
    00040   case 'p':
    00041     return new Xabsl2OptionParameterRef(input,errorHandler,parameters);
    00042   case '+':
    00043     if (!Xabsl2DecimalExpression::createOperand(operand1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00044       return 0;
    00045     if (!Xabsl2DecimalExpression::createOperand(operand2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00046       return 0;
    00047
    00048     XABSL2_DEBUG_INIT(errorHandler.message("creating + operator"));
    00049     arithmeticOperator = new Xabsl2PlusOperator();
    00050     arithmeticOperator->create(operand1,operand2);
    00051     return arithmeticOperator;
    00052   case '-':
    00053     if (!Xabsl2DecimalExpression::createOperand(operand1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00054       return 0;
    00055     if (!Xabsl2DecimalExpression::createOperand(operand2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00056       return 0;
    00057
    00058     XABSL2_DEBUG_INIT(errorHandler.message("creating - operator"));
    00059     arithmeticOperator = new Xabsl2MinusOperator();
    00060     arithmeticOperator->create(operand1,operand2);
    00061     return arithmeticOperator;
    00062   case '*':
    00063     if (!Xabsl2DecimalExpression::createOperand(operand1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00064       return 0;
    00065     if (!Xabsl2DecimalExpression::createOperand(operand2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00066       return 0;
    00067
    00068     XABSL2_DEBUG_INIT(errorHandler.message("creating * operator"));
    00069     arithmeticOperator = new Xabsl2MultiplyOperator();
    00070     arithmeticOperator->create(operand1,operand2);
    00071     return arithmeticOperator;
    00072   case 'd':
    00073     if (!Xabsl2DecimalExpression::createOperand(operand1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00074       return 0;
    00075     if (!Xabsl2DecimalExpression::createOperand(operand2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00076       return 0;
    00077
    00078     XABSL2_DEBUG_INIT(errorHandler.message("creating / operator"));
    00079     arithmeticOperator = new Xabsl2DivideOperator();
    00080     arithmeticOperator->create(operand1,operand2);
    00081     return arithmeticOperator;
    00082
    00083   case '%':
    00084     if (!Xabsl2DecimalExpression::createOperand(operand1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00085       return 0;
    00086     if (!Xabsl2DecimalExpression::createOperand(operand2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00087       return 0;
    00088
    00089     XABSL2_DEBUG_INIT(errorHandler.message("creating % operator"));
    00090     arithmeticOperator = new Xabsl2ModOperator();
    00091     arithmeticOperator->create(operand1,operand2);
    00092     return arithmeticOperator;
    00093   case 's':
    00094     return new Xabsl2TimeRef(errorHandler,timeOfStateExecution);
    00095   case 'o':
    00096     return new Xabsl2TimeRef(errorHandler,timeOfOptionExecution);
    00097   case 'q':
    00098     return new Xabsl2ConditionalExpression(input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution);
    00099   default:
    00100     errorHandler.error("Xabsl2DecimalExpression::create(): unknown expression type: \"%c\"",*c);
    00101     return 0;
    00102   }
    00103 }
    00104
    00105 bool Xabsl2DecimalExpression::createOperand(Xabsl2DecimalExpression*& operand,
    00106                                             Xabsl2InputSource& input,
    00107                                             Xabsl2Option* subsequentOption,
    00108                                             Xabsl2ErrorHandler& errorHandler,
    00109                                             Xabsl2Array<double>& parameters,
    00110                                             Xabsl2Symbols& symbols,
    00111                                             unsigned long& timeOfOptionExecution,
    00112                                             unsigned long& timeOfStateExecution)
    00113 {
    00114   operand = Xabsl2DecimalExpression::create(input,subsequentOption,errorHandler,parameters, symbols,
    00115     timeOfOptionExecution,timeOfStateExecution);
    00116
    00117   if (operand == 0)
    00118   {
    00119     errorHandler.error("Xabsl2DecimalExpression::createOperand(): created operand is 0");
    00120     return false;
    00121   }
    00122
    00123   if (errorHandler.errorsOccurred)
    00124   {
    00125     errorHandler.error("Xabsl2DecimalExpression::createOperand(): could not create operand");
    00126     if (operand != 0) delete operand;
    00127     return false;
    00128   }
    00129
    00130   return true;
    00131 }
    00132
    00133 Xabsl2DecimalInputSymbolRef::Xabsl2DecimalInputSymbolRef(Xabsl2InputSource& input,
    00134                                                          Xabsl2ErrorHandler& errorHandler,
    00135                                                          Xabsl2Symbols& symbols)
    00136 {
    00137   char buf[100];
    00138   input.readString(buf,99);
    00139   XABSL2_DEBUG_INIT(errorHandler.message("creating a reference to decimal input symbol \"%s\"",buf));
    00140
    00141   if (!symbols.existsDecimalInputSymbol(buf))
    00142   {
    00143     errorHandler.error("Xabsl2DecimalInputSymbolRef::Xabsl2DecimalInputSymbolRef(): decimal input symbol \"%s\" was not registered",buf);
    00144     return;
    00145   }
    00146
    00147   symbol = symbols.getDecimalInputSymbol(buf);
    00148 }
    00149
    00150 double Xabsl2DecimalInputSymbolRef::getValue()
    00151 {
    00152   return symbol->getValue();
    00153 }
    00154
    00155 Xabsl2DecimalValue::Xabsl2DecimalValue(Xabsl2InputSource& input,
    00156                                        Xabsl2ErrorHandler& errorHandler,
    00157                                        Xabsl2Symbols& symbols)
    00158 {
    00159   value = input.readValue();
    00160
    00161   XABSL2_DEBUG_INIT(errorHandler.message("created decimal value: \"%.2f\"",value));
    00162 }
    00163
    00164 double Xabsl2DecimalValue::getValue()
    00165 {
    00166   return value;
    00167 }
    00168
    00169 Xabsl2OptionParameterRef::Xabsl2OptionParameterRef(Xabsl2InputSource& input,
    00170                                                    Xabsl2ErrorHandler& errorHandler,
    00171                                                    Xabsl2Array<double>& parameters)
    00172 {
    00173   char buf[100];
    00174   input.readString(buf,99);
    00175
    00176   XABSL2_DEBUG_INIT(errorHandler.message("creating a reference to option parameter \"%s\"",buf));
    00177
    00178   if (!parameters.exists(buf))
    00179   {
    00180     errorHandler.error("Xabsl2OptionParameterRef::Xabsl2OptionParameterRef(): option parameter \"%s\" does not exist",buf);
    00181     return;
    00182   }
    00183
    00184   parameter = &parameters.getPElement(buf)->e;
    00185 }
    00186
    00187 double Xabsl2OptionParameterRef::getValue()
    00188 {
    00189   return *parameter;
    00190 }
    00191
    00192 void Xabsl2ArithmeticOperator::create(Xabsl2DecimalExpression* operand1, Xabsl2DecimalExpression* operand2)
    00193 {
    00194   this->operand1 = operand1;
    00195   this->operand2 = operand2;
    00196 }
    00197
    00198 Xabsl2ArithmeticOperator::~Xabsl2ArithmeticOperator()
    00199 {
    00200   if (operand1 != 0) delete operand1;
    00201   if (operand2 != 0) delete operand2;
    00202 }
    00203
    00204 double Xabsl2PlusOperator::getValue()
    00205 {
    00206   return operand1->getValue() + operand2->getValue();
    00207 }
    00208
    00209 double Xabsl2MinusOperator::getValue()
    00210 {
    00211   return operand1->getValue() - operand2->getValue();
    00212 }
    00213
    00214 double Xabsl2MultiplyOperator::getValue()
    00215 {
    00216   return operand1->getValue() * operand2->getValue();
    00217 }
    00218
    00219 double Xabsl2DivideOperator::getValue()
    00220 {
    00221   double o2 = operand2->getValue();
    00222   if (o2==0)
    00223     return operand1->getValue() / 0.0000001;
    00224   else
    00225     return operand1->getValue() / o2;
    00226 }
    00227
    00228 double Xabsl2ModOperator::getValue()
    00229 {
    00230   return (int)operand1->getValue() % (int)operand2->getValue();
    00231 }
    00232
    00233 Xabsl2TimeRef::Xabsl2TimeRef(Xabsl2ErrorHandler& errorHandler,
    00234                              unsigned long& time) :
    00235 time(time)
    00236 {
    00237   XABSL2_DEBUG_INIT(errorHandler.message("creating a reference to state or option execution time"));
    00238 }
    00239
    00240 double Xabsl2TimeRef::getValue()
    00241 {
    00242   return time;
    00243 }
    00244
    00245 Xabsl2DecimalInputFunctionCall::Xabsl2DecimalInputFunctionCall(Xabsl2InputSource& input,
    00246                                                                Xabsl2Option* subsequentOption,
    00247                                                                Xabsl2ErrorHandler& errorHandler,
    00248                                                                Xabsl2Array<double>& parameters,
    00249                                                                Xabsl2Symbols& symbols,
    00250                                                                unsigned long& timeOfOptionExecution,
    00251                                                                unsigned long& timeOfStateExecution)
    00252 {
    00253   char buf[100];
    00254   input.readString(buf,99);
    00255
    00256   XABSL2_DEBUG_INIT(errorHandler.message("creating a call to decimal input function \"%s\"",buf));
    00257
    00258   if (!symbols.existsDecimalInputFunction(buf))
    00259   {
    00260     errorHandler.error("Xabsl2DecimalInputFunctionCall::Xabsl2DecimalInputFunctionCall(): decimal input function \"%s\" was not registered",buf);
    00261     return;
    00262   }
    00263
    00264   function = symbols.getDecimalInputFunction(buf);
    00265
    00266   int numberOfParameterValues = (int)input.readValue();
    00267
    00268   for (int i=0; i< numberOfParameterValues; i++)
    00269   {
    00270     input.readString(buf,99);
    00271     if (!function->parameters.exists(buf))
    00272     {
    00273       errorHandler.error("Xabsl2DecimalInputFunctionCall::Xabsl2DecimalInputFunctionCall(): parameter \"%s\" of function \"%s\" does not exist.", buf,function->n);
    00274       return;
    00275     }
    00276     functionParameters.append(buf,&function->parameters.getPElement(buf)->e);
    00277
    00278     XABSL2_DEBUG_INIT(errorHandler.message("creating expression for parameter \"%s\"",buf));
    00279
    00280     Xabsl2DecimalExpression* parameterValue;
    00281     if (!Xabsl2DecimalExpression::createOperand(parameterValue,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00282     {
    00283       errorHandler.error("Xabsl2DecimalInputFunctionCall::Xabsl2DecimalInputFunctionCall(): could not create decimal expression",buf);
    00284       return;
    00285     }
    00286     parameterValues.append(buf,parameterValue);
    00287   }
    00288 }
    00289
    00290 Xabsl2DecimalInputFunctionCall::~Xabsl2DecimalInputFunctionCall()
    00291 {
    00292   for (int i=0; i<parameterValues.getSize(); i++)
    00293     if (parameterValues[i] != 0)
    00294       delete parameterValues[i];
    00295 }
    00296
    00297 double Xabsl2DecimalInputFunctionCall::getValue()
    00298 {
    00299   // set the function parameters
    00300   for (int i=0; i<functionParameters.getSize(); i++)
    00301     *functionParameters[i] = parameterValues[i]->getValue();
    00302
    00303   // call the function
    00304   return function->getValue();
    00305 }
    00306
    00307 Xabsl2ConditionalExpression::Xabsl2ConditionalExpression(Xabsl2InputSource& input,
    00308     Xabsl2Option* subsequentOption,
    00309     Xabsl2ErrorHandler& errorHandler,
    00310     Xabsl2Array<double>& parameters,
    00311     Xabsl2Symbols& symbols,
    00312     unsigned long& timeOfOptionExecution,
    00313     unsigned long& timeOfStateExecution)
    00314 {
    00315   XABSL2_DEBUG_INIT(errorHandler.message("creating a question mark operator",buf));
    00316
    00317   condition = Xabsl2BooleanExpression::create(input,subsequentOption,errorHandler,parameters, symbols,
    00318     timeOfOptionExecution,timeOfStateExecution);
    00319
    00320   if (condition == 0)
    00321   {
    00322     errorHandler.error("Xabsl2QuestionMarkOperator::Xabsl2QuestionMarkOperator(): created condition is 0");
    00323     return;
    00324   }
    00325   else if (errorHandler.errorsOccurred)
    00326   {
    00327     errorHandler.error("Xabsl2QuestionMarkOperator::Xabsl2QuestionMarkOperator(): could not create condition");
    00328     delete condition;
    00329     return;
    00330   }
    00331
    00332   if (!Xabsl2DecimalExpression::createOperand(expression1,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00333   {
    00334     errorHandler.error("Xabsl2QuestionMarkOperator::Xabsl2QuestionMarkOperator(): could not create decimal expression1");
    00335     return;
    00336   }
    00337
    00338   if (!Xabsl2DecimalExpression::createOperand(expression2,input,subsequentOption,errorHandler,parameters,symbols,timeOfOptionExecution,timeOfStateExecution))
    00339   {
    00340     errorHandler.error("Xabsl2QuestionMarkOperator::Xabsl2QuestionMarkOperator(): could not create decimal expression2");
    00341     return;
    00342   }
    00343
    00344 }
    00345
    00346 Xabsl2ConditionalExpression::~Xabsl2ConditionalExpression()
    00347 {
    00348   if (condition!=0) delete condition;
    00349   if (expression1!=0) delete expression1;
    00350   if (expression2!=0) delete expression2;
    00351 }
    00352
    00353 double Xabsl2ConditionalExpression::getValue()
    00354 {
    00355   if (condition->getValue())
    00356   {
    00357     return expression1->getValue();
    00358   }
    00359   else
    00360   {
    00361     return expression2->getValue();
    00362   }
    00363 }