>xabsl   The Extensible Agent Behavior Specification Language

XabslEngine Class Library Reference

 

XabslDecimalExpression.cpp

Go to the documentation of this file.
00001 /** 
00002 * @file XabslDecimalExpression.cpp
00003 *
00004 * Implementation of DecimalExpression and derivates
00005 * 
00006 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00007 * @author <a href="http://www.sim.informatik.tu-darmstadt.de/pers/card/risler.html">Max Risler</a>
00008 */
00009 
00010 #include "XabslOption.h"
00011 #include "XabslDecimalExpression.h"
00012 #include "XabslBooleanExpression.h"
00013 #include "XabslEnumeratedExpression.h"
00014 
00015 namespace xabsl 
00016 {
00017   
00018 DecimalExpression::~DecimalExpression()
00019 {
00020 }
00021 
00022 DecimalExpression* DecimalExpression::create(InputSource& input, 
00023                                                          ErrorHandler& errorHandler,
00024                                                          Symbols& symbols,
00025                                                          Option& option,
00026                                                          State& state)
00027 {
00028   char c[100];
00029   input.readString(c,1);
00030   ArithmeticOperator* arithmeticOperator;
00031   DecimalExpression* operand1;
00032   DecimalExpression* operand2;
00033   
00034   switch (*c)
00035   {
00036   case 'i':
00037     return new DecimalInputSymbolRef(input,errorHandler,symbols,option,state);
00038   case 'o':
00039     return new DecimalOutputSymbolRef(input,errorHandler,symbols);
00040   case 'c':
00041     // constants are treates as decimal values (there is no difference from the engines point of view.)
00042   case 'v':
00043     return new DecimalValue(input,errorHandler);
00044   case 'p':
00045     return new DecimalOptionParameterRef(input,errorHandler,option);
00046   case '+':
00047     if (!DecimalExpression::createOperand(operand1,input,errorHandler,symbols,option,state))
00048       return 0;
00049     if (!DecimalExpression::createOperand(operand2,input,errorHandler,symbols,option,state))
00050       return 0;
00051     
00052     XABSL_DEBUG_INIT(errorHandler.message("creating + operator"));
00053     arithmeticOperator = new PlusOperator();
00054     arithmeticOperator->create(operand1,operand2);
00055     return arithmeticOperator;
00056   case '-':
00057     if (!DecimalExpression::createOperand(operand1,input,errorHandler,symbols,option,state))
00058       return 0;
00059     if (!DecimalExpression::createOperand(operand2,input,errorHandler,symbols,option,state))
00060       return 0;
00061     
00062     XABSL_DEBUG_INIT(errorHandler.message("creating - operator"));
00063     arithmeticOperator = new MinusOperator();
00064     arithmeticOperator->create(operand1,operand2);
00065     return arithmeticOperator;
00066   case '*':
00067     if (!DecimalExpression::createOperand(operand1,input,errorHandler,symbols,option,state))
00068       return 0;
00069     if (!DecimalExpression::createOperand(operand2,input,errorHandler,symbols,option,state))
00070       return 0;
00071     
00072     XABSL_DEBUG_INIT(errorHandler.message("creating * operator"));
00073     arithmeticOperator = new MultiplyOperator();
00074     arithmeticOperator->create(operand1,operand2);
00075     return arithmeticOperator;
00076   case 'd':
00077     if (!DecimalExpression::createOperand(operand1,input,errorHandler,symbols,option,state))
00078       return 0;
00079     if (!DecimalExpression::createOperand(operand2,input,errorHandler,symbols,option,state))
00080       return 0;
00081     
00082     XABSL_DEBUG_INIT(errorHandler.message("creating / operator"));
00083     arithmeticOperator = new DivideOperator();
00084     arithmeticOperator->create(operand1,operand2);
00085     return arithmeticOperator;
00086     
00087   case '%':
00088     if (!DecimalExpression::createOperand(operand1,input,errorHandler,symbols,option,state))
00089       return 0;
00090     if (!DecimalExpression::createOperand(operand2,input,errorHandler,symbols,option,state))
00091       return 0;
00092     
00093     XABSL_DEBUG_INIT(errorHandler.message("creating % operator"));
00094     arithmeticOperator = new ModOperator();
00095     arithmeticOperator->create(operand1,operand2);
00096     return arithmeticOperator;
00097   case 's':
00098     return new TimeRef(errorHandler,state.timeOfStateExecution);
00099   case 't':
00100     return new TimeRef(errorHandler,option.timeOfExecution);
00101   case 'q':
00102     return new ConditionalDecimalExpression(input,errorHandler,symbols,option,state);
00103   default:
00104     errorHandler.error("XabslDecimalExpression::create(): unknown expression type: \"%c\"",*c);
00105     return 0;
00106   }
00107 }
00108 
00109 bool DecimalExpression::createOperand(DecimalExpression*& operand,
00110                                             InputSource& input, 
00111                                             ErrorHandler& errorHandler,
00112                                             Symbols& symbols,
00113                                             Option& option,
00114                                             State& state)
00115 {
00116   operand = DecimalExpression::create(input,errorHandler,symbols,option,state);
00117   
00118   if (operand == 0) 
00119   {
00120     errorHandler.error("XabslDecimalExpression::createOperand(): created operand is 0");
00121     return false;
00122   }
00123   
00124   if (errorHandler.errorsOccurred)
00125   {
00126     errorHandler.error("XabslDecimalExpression::createOperand(): could not create operand");
00127     if (operand != 0) delete operand;
00128     return false;
00129   }
00130   
00131   return true;
00132 }
00133 
00134 DecimalValue::DecimalValue(InputSource& input, 
00135                                        ErrorHandler& errorHandler)
00136 {
00137   value = input.readValue();
00138   
00139   XABSL_DEBUG_INIT(errorHandler.message("created decimal value: \"%.2f\"",value));
00140 }
00141 
00142 double DecimalValue::getValue() const
00143 {
00144   return value;
00145 }
00146 
00147 DecimalOptionParameterRef::DecimalOptionParameterRef(InputSource& input, 
00148                                                    ErrorHandler& errorHandler,
00149                                                    Option& option)
00150 {
00151   char buf[100];
00152   input.readString(buf,99);
00153   
00154   XABSL_DEBUG_INIT(errorHandler.message("creating a reference to decimal option parameter \"%s\"",buf));
00155   
00156   if (!option.parameters->decimal.exists(buf))
00157   {
00158     errorHandler.error("XabslDecimalOptionParameterRef::DecimalOptionParameterRef(): decimal option parameter \"%s\" does not exist",buf);
00159     return;
00160   }
00161   
00162   parameter = option.parameters->decimal.getPElement(buf)->e;
00163 }
00164 
00165 double DecimalOptionParameterRef::getValue() const
00166 {
00167   return *parameter;
00168 }
00169 
00170 void ArithmeticOperator::create(DecimalExpression* operand1, DecimalExpression* operand2)
00171 {
00172   this->operand1 = operand1;
00173   this->operand2 = operand2;
00174 }
00175 
00176 ArithmeticOperator::~ArithmeticOperator()
00177 {
00178   if (operand1 != 0) delete operand1;
00179   if (operand2 != 0) delete operand2;
00180 }
00181 
00182 double PlusOperator::getValue() const
00183 {
00184   return operand1->getValue() + operand2->getValue();
00185 }
00186 
00187 double MinusOperator::getValue() const
00188 {
00189   return operand1->getValue() - operand2->getValue();
00190 }
00191 
00192 double MultiplyOperator::getValue() const
00193 {
00194   return operand1->getValue() * operand2->getValue();
00195 }
00196 
00197 double DivideOperator::getValue() const
00198 {
00199   double o2 = operand2->getValue();
00200   if (o2==0) 
00201     return operand1->getValue() / 0.0000001;
00202   else
00203     return operand1->getValue() / o2;
00204 }
00205 
00206 double ModOperator::getValue() const
00207 {
00208   return (int)operand1->getValue() % (int)operand2->getValue();
00209 }
00210 
00211 TimeRef::TimeRef(ErrorHandler& errorHandler,
00212                              unsigned& time) :
00213 time(time)
00214 {
00215   XABSL_DEBUG_INIT(errorHandler.message("creating a reference to state or option execution time"));
00216 }
00217 
00218 double TimeRef::getValue() const
00219 {
00220   return time;
00221 }
00222 
00223 DecimalInputSymbolRef::DecimalInputSymbolRef(InputSource& input, 
00224                                                                ErrorHandler& errorHandler,
00225                                                                Symbols& symbols,
00226                                                                Option& option,
00227                                                                State& state) :
00228   symbol(0), parameters(0)
00229 {
00230   char buf[100];
00231   input.readString(buf,99);
00232   
00233   XABSL_DEBUG_INIT(errorHandler.message("creating a reference to decimal input symbol \"%s\"",buf));
00234   
00235   if (!symbols.decimalInputSymbols.exists(buf))
00236   {
00237     errorHandler.error("XabslDecimalInputSymbolRef::DecimalInputSymbolRef(): decimal input symbol \"%s\" was not registered",buf);
00238     return;
00239   }
00240   
00241   symbol = symbols.decimalInputSymbols[buf];
00242 
00243   parameters = new ParameterAssignment(&symbol->parameters, errorHandler);
00244 
00245   parameters->create(input, symbols, option, state);
00246 }
00247 
00248 DecimalInputSymbolRef::~DecimalInputSymbolRef()
00249 {
00250   if (parameters != 0)
00251     delete parameters;
00252 }
00253 
00254 double DecimalInputSymbolRef::getValue() const
00255 {
00256   // set the symbol parameters
00257   if (parameters->set())
00258     symbol->parametersChanged();
00259 
00260   return symbol->getValue();
00261 }
00262 
00263 DecimalOutputSymbolRef::DecimalOutputSymbolRef(InputSource& input, 
00264                                                                ErrorHandler& errorHandler,
00265                                                                Symbols& symbols)
00266 {
00267   char buf[100];
00268   input.readString(buf,99);
00269   
00270   XABSL_DEBUG_INIT(errorHandler.message("creating a reference to a decimal output symbol \"%s\"",buf));
00271   
00272   if (!symbols.decimalOutputSymbols.exists(buf))
00273   {
00274     errorHandler.error("XabslDecimalOutputSymbolRef::DecimalOutputSymbolRef(): decimal output symbol \"%s\" was not registered",buf);
00275     return;
00276   }
00277   
00278   symbol = symbols.decimalOutputSymbols[buf];
00279 }
00280 
00281 double DecimalOutputSymbolRef::getValue() const
00282 {
00283   return symbol->getValue();
00284 }
00285 
00286 ConditionalDecimalExpression::ConditionalDecimalExpression(InputSource& input, 
00287     ErrorHandler& errorHandler,
00288     Symbols& symbols,
00289     Option& option,
00290     State& state)
00291 {
00292   XABSL_DEBUG_INIT(errorHandler.message("creating a question mark operator"));
00293 
00294   condition = BooleanExpression::create(input,errorHandler,symbols,option,state);
00295 
00296   if (condition == 0) 
00297   {
00298     errorHandler.error("XabslQuestionMarkOperator::QuestionMarkOperator(): created condition is 0");
00299     return;
00300   }
00301   else if (errorHandler.errorsOccurred)
00302   {
00303     errorHandler.error("XabslQuestionMarkOperator::QuestionMarkOperator(): could not create condition");
00304     delete condition;
00305     return;
00306   }
00307 
00308   if (!DecimalExpression::createOperand(expression1,input,errorHandler,symbols,option,state))
00309   {
00310     errorHandler.error("XabslQuestionMarkOperator::QuestionMarkOperator(): could not create decimal expression1");
00311     return;
00312   }
00313   
00314   if (!DecimalExpression::createOperand(expression2,input,errorHandler,symbols,option,state))
00315   {
00316     errorHandler.error("XabslQuestionMarkOperator::QuestionMarkOperator(): could not create decimal expression2");
00317     return;
00318   }
00319   
00320 }
00321 
00322 ConditionalDecimalExpression::~ConditionalDecimalExpression()
00323 {
00324   if (condition!=0) delete condition;
00325   if (expression1!=0) delete expression1;
00326   if (expression2!=0) delete expression2;
00327 }
00328 
00329 double ConditionalDecimalExpression::getValue() const
00330 {
00331   if (condition->getValue())
00332   {
00333     return expression1->getValue();
00334   }
00335   else
00336   {
00337     return expression2->getValue();
00338   }
00339 }
00340 
00341 } // namespace
00342 

Up | Main Page | Generated at Wed Aug 19 17:32:28 2009.