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

# Tools/Xabsl2/Xabsl2Engine/Xabsl2BooleanExpression.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Xabsl2BooleanExpression.cpp
    00003 *
    00004 * Implementation of Xabsl2BooleanExpression and derivates
    00005 *
    00006 * @author Martin Lötzsch
    00007 */
    00008
    00009 #include "Xabsl2BooleanExpression.h"
    00010 #include "Xabsl2Option.h"
    00011
    00012 Xabsl2BooleanExpression::~Xabsl2BooleanExpression()
    00013 {
    00014 }
    00015
    00016 Xabsl2BooleanExpression* Xabsl2BooleanExpression::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   Xabsl2BooleanExpression* booleanOperand = 0;
    00026   Xabsl2DecimalExpression* decimalOperand1 = 0;
    00027   Xabsl2DecimalExpression* decimalOperand2 = 0;
    00028   Xabsl2RelationalAndEqualityOperator* relationalAndEqualityOperator = 0;
    00029   input.readString(c,1);
    00030
    00031   switch (*c)
    00032   {
    00033   case 'r':
    00034     return new Xabsl2BooleanInputSymbolRef(input, errorHandler, symbols);
    00035   case 't':
    00036     return new Xabsl2subsequentOptionReachedTargetStateCondition(subsequentOption, errorHandler);
    00037   case 'c':
    00038     return new Xabsl2EnumeratedInputSymbolComparison(input, errorHandler,symbols);
    00039   case '&':
    00040     {
    00041       XABSL2_DEBUG_INIT(errorHandler.message("creating and operator"));
    00042
    00043       int numberOfOperands = (int)input.readValue();
    00044
    00045       Xabsl2AndOperator* andOperator = new Xabsl2AndOperator();
    00046
    00047       for (int i=0; i<numberOfOperands; i++)
    00048       {
    00049         if (!Xabsl2BooleanExpression::createOperand(booleanOperand, input, subsequentOption, errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00050         {
    00051           delete andOperator;
    00052           return 0;
    00053         }
    00054
    00055         andOperator->addOperand(booleanOperand);
    00056       }
    00057
    00058       return andOperator;
    00059     }
    00060   case '|':
    00061     {
    00062       XABSL2_DEBUG_INIT(errorHandler.message("creating or operator"));
    00063
    00064       int numberOfOperands = (int)input.readValue();
    00065
    00066       Xabsl2OrOperator* orOperator = new Xabsl2OrOperator();
    00067
    00068       for (int i=0; i<numberOfOperands; i++)
    00069       {
    00070         if (!Xabsl2BooleanExpression::createOperand(booleanOperand, input, subsequentOption, errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00071         {
    00072           delete orOperator;
    00073           return 0;
    00074         }
    00075         orOperator->addOperand(booleanOperand);
    00076       }
    00077
    00078       return orOperator;
    00079     }
    00080   case '!':
    00081     XABSL2_DEBUG_INIT(errorHandler.message("creating not operator"));
    00082
    00083     if (!Xabsl2BooleanExpression::createOperand(booleanOperand, input, subsequentOption, errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00084       return 0;
    00085
    00086     return new Xabsl2NotOperator(booleanOperand);
    00087   case '=':
    00088     XABSL2_DEBUG_INIT(errorHandler.message("creating == operator"));
    00089
    00090     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input, subsequentOption, errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00091       return 0;
    00092
    00093     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input, subsequentOption, errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00094       return 0;
    00095
    00096     relationalAndEqualityOperator = new Xabsl2EqualToOperator();
    00097     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00098     return relationalAndEqualityOperator;
    00099   case 'n':
    00100     XABSL2_DEBUG_INIT(errorHandler.message("creating != operator"));
    00101
    00102     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00103       return 0;
    00104
    00105     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00106       return 0;
    00107
    00108     relationalAndEqualityOperator = new Xabsl2NotEqualToOperator();
    00109     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00110     return relationalAndEqualityOperator;
    00111   case '<':
    00112     XABSL2_DEBUG_INIT(errorHandler.message("creating < operator"));
    00113
    00114     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00115       return 0;
    00116
    00117     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00118       return 0;
    00119
    00120     relationalAndEqualityOperator = new Xabsl2LessThanOperator();
    00121     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00122     return relationalAndEqualityOperator;
    00123   case 'l':
    00124     XABSL2_DEBUG_INIT(errorHandler.message("creating <= operator"));
    00125
    00126     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00127       return 0;
    00128
    00129     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00130       return 0;
    00131
    00132     relationalAndEqualityOperator = new Xabsl2LessThanOrEqualToOperator();
    00133     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00134     return relationalAndEqualityOperator;
    00135   case '>':
    00136     XABSL2_DEBUG_INIT(errorHandler.message("creating > operator"));
    00137
    00138     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00139       return 0;
    00140
    00141     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00142       return 0;
    00143
    00144     relationalAndEqualityOperator = new Xabsl2GreaterThanOperator();
    00145     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00146     return relationalAndEqualityOperator;
    00147   case 'g':
    00148     XABSL2_DEBUG_INIT(errorHandler.message("creating >= operator"));
    00149
    00150     if (!Xabsl2DecimalExpression::createOperand(decimalOperand1,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00151       return 0;
    00152
    00153     if (!Xabsl2DecimalExpression::createOperand(decimalOperand2,input,subsequentOption,errorHandler, parameters, symbols, timeOfOptionExecution, timeOfStateExecution))
    00154       return 0;
    00155
    00156     relationalAndEqualityOperator = new Xabsl2GreaterThanOrEqualToOperator();
    00157     relationalAndEqualityOperator->create(decimalOperand1,decimalOperand2);
    00158     return relationalAndEqualityOperator;
    00159   default:
    00160     errorHandler.error("Xabsl2BooleanExpression::create(): unknown expression type \"%c\"",*c);
    00161   }
    00162   return 0;
    00163 }
    00164
    00165 bool Xabsl2BooleanExpression::createOperand(Xabsl2BooleanExpression*& operand,
    00166                                             Xabsl2InputSource& input,
    00167                                             Xabsl2Option* subsequentOption,
    00168                                             Xabsl2ErrorHandler& errorHandler,
    00169                                             Xabsl2Array<double>& parameters,
    00170                                             Xabsl2Symbols& symbols,
    00171                                             unsigned long& timeOfOptionExecution,
    00172                                             unsigned long& timeOfStateExecution)
    00173 {
    00174   operand = Xabsl2BooleanExpression::create(input,subsequentOption,errorHandler,
    00175     parameters, symbols, timeOfOptionExecution,timeOfStateExecution);
    00176
    00177   if (operand == 0)
    00178   {
    00179     errorHandler.error("Xabsl2BooleanExpression::createOperand(): created operand is 0");
    00180     return false;
    00181   }
    00182
    00183   if (errorHandler.errorsOccurred)
    00184   {
    00185     errorHandler.error("Xabsl2BooleanExpression::createOperand(): could not create operand");
    00186     if (operand != 0) delete operand;
    00187     return false;
    00188   }
    00189
    00190   return true;
    00191 }
    00192
    00193 Xabsl2AndOperator::Xabsl2AndOperator()
    00194 {
    00195   operands.clear();
    00196 }
    00197
    00198 Xabsl2AndOperator::~Xabsl2AndOperator()
    00199 {
    00200   for (int i=0; i< operands.getSize(); i++)
    00201   {
    00202     if (operands[i]!=0) delete operands[i];
    00203   }
    00204 }
    00205
    00206 bool Xabsl2AndOperator::getValue()
    00207 {
    00208   for (int i=0; i< operands.getSize(); i++)
    00209   {
    00210     if (operands[i]->getValue() == false) return false;
    00211   }
    00212   return true;
    00213 }
    00214
    00215 void Xabsl2AndOperator::addOperand(Xabsl2BooleanExpression* operand)
    00216 {
    00217   operands.append("",operand);
    00218 }
    00219
    00220 Xabsl2OrOperator::Xabsl2OrOperator()
    00221 {
    00222   operands.clear();
    00223 }
    00224
    00225 Xabsl2OrOperator::~Xabsl2OrOperator()
    00226 {
    00227   for (int i=0; i< operands.getSize(); i++)
    00228   {
    00229     if (operands[i]!=0) delete operands[i];
    00230   }
    00231 }
    00232
    00233 bool Xabsl2OrOperator::getValue()
    00234 {
    00235   for (int i=0; i< operands.getSize(); i++)
    00236   {
    00237     if (operands[i]->getValue() == true) return true;
    00238   }
    00239   return false;
    00240 }
    00241
    00242 void Xabsl2OrOperator::addOperand(Xabsl2BooleanExpression* operand)
    00243 {
    00244   operands.append("",operand);
    00245 }
    00246
    00247 Xabsl2NotOperator::Xabsl2NotOperator(Xabsl2BooleanExpression* operand1) :
    00248 operand1(operand1)
    00249 {
    00250 }
    00251
    00252 Xabsl2NotOperator::~Xabsl2NotOperator()
    00253 {
    00254   if (operand1!=0) delete operand1;
    00255 }
    00256
    00257 bool Xabsl2NotOperator::getValue()
    00258 {
    00259   return !(operand1->getValue());
    00260 }
    00261
    00262 Xabsl2BooleanInputSymbolRef::Xabsl2BooleanInputSymbolRef(Xabsl2InputSource& input,
    00263                                                          Xabsl2ErrorHandler& errorHandler,
    00264                                                          Xabsl2Symbols& symbols)
    00265 {
    00266   char buf[100];
    00267   input.readString(buf,99);
    00268
    00269   XABSL2_DEBUG_INIT(errorHandler.message("creating reference to boolean input symbol \"%s\"",buf));
    00270
    00271   if (!symbols.existsBooleanInputSymbol(buf))
    00272   {
    00273     errorHandler.error("Xabsl2BooleanInputSymbolRef::Xabls2BooleanInputSymbolRef(): boolean input symbol \"%s\" was not registered at the engine",buf);
    00274     return;
    00275   }
    00276
    00277   symbol = symbols.getBooleanInputSymbol(buf);
    00278 }
    00279
    00280 bool Xabsl2BooleanInputSymbolRef::getValue()
    00281 {
    00282   return symbol->getValue();
    00283 }
    00284
    00285 Xabsl2subsequentOptionReachedTargetStateCondition
    00286 ::Xabsl2subsequentOptionReachedTargetStateCondition(Xabsl2Option* subsequentOption,
    00287                                                    Xabsl2ErrorHandler& errorHandler)
    00288                                                    : subsequentOption(subsequentOption)
    00289 {
    00290   XABSL2_DEBUG_INIT(errorHandler.message("creating a \"subsequent-option-reached-target-state\" element"));
    00291 }
    00292
    00293 bool Xabsl2subsequentOptionReachedTargetStateCondition::getValue()
    00294 {
    00295   if (subsequentOption==0)
    00296   {
    00297     return false;
    00298   }
    00299   else
    00300   {
    00301     return subsequentOption->getOptionReachedATargetState();
    00302   }
    00303 }
    00304
    00305 Xabsl2EnumeratedInputSymbolComparison::Xabsl2EnumeratedInputSymbolComparison(Xabsl2InputSource& input,
    00306                                                                              Xabsl2ErrorHandler& errorHandler, Xabsl2Symbols& symbols)
    00307 {
    00308   char buf[100];
    00309   input.readString(buf,99);
    00310
    00311   XABSL2_DEBUG_INIT(errorHandler.message("creating comparison to enumerated input symbol \"%s\"",buf));
    00312
    00313   if (!symbols.existsEnumeratedInputSymbol(buf))
    00314   {
    00315     errorHandler.error("Xabsl2EnumeratedInputSymbolComparison::Xabsl2EnumeratedInputSymbolComparison(): enumerated input symbol \"%s\" was not registered", buf);
    00316     return;
    00317   }
    00318
    00319   symbol = symbols.getEnumeratedInputSymbol(buf);
    00320
    00321   input.readString(buf,99);
    00322   if (!symbol->enumElements.exists(buf))
    00323   {
    00324     errorHandler.error("Xabsl2EnumeratedInputSymbolComparison::Xabsl2EnumeratedInputSymbolComparison(): enum element \"%s\" enumerated input symbol \"%s\" was not registered", buf, symbol->n);
    00325     return;
    00326   }
    00327
    00328   value = symbol->enumElements.getElement(buf)->v;
    00329 }
    00330
    00331 bool Xabsl2EnumeratedInputSymbolComparison::getValue()
    00332 {
    00333   return (symbol->getValue() == value);
    00334 }
    00335
    00336 void Xabsl2RelationalAndEqualityOperator::create(Xabsl2DecimalExpression* operand1,
    00337                                                  Xabsl2DecimalExpression* operand2)
    00338 {
    00339   this->operand1 = operand1;
    00340   this->operand2 = operand2;
    00341 }
    00342
    00343 Xabsl2RelationalAndEqualityOperator ::~Xabsl2RelationalAndEqualityOperator ()
    00344 {
    00345   if (operand1!=0) delete operand1;
    00346   if (operand2!=0) delete operand2;
    00347 }
    00348
    00349 bool Xabsl2EqualToOperator::getValue()
    00350 {
    00351   return (operand1->getValue() == operand2->getValue());
    00352 }
    00353
    00354 bool Xabsl2NotEqualToOperator::getValue()
    00355 {
    00356   return (operand1->getValue() != operand2->getValue());
    00357 }
    00358
    00359 bool Xabsl2LessThanOperator::getValue()
    00360 {
    00361   return (operand1->getValue() < operand2->getValue());
    00362 }
    00363
    00364 bool Xabsl2LessThanOrEqualToOperator::getValue()
    00365 {
    00366   return (operand1->getValue() <= operand2->getValue());
    00367 }
    00368
    00369 bool Xabsl2GreaterThanOperator::getValue()
    00370 {
    00371   return (operand1->getValue() > operand2->getValue());
    00372 }
    00373
    00374 bool Xabsl2GreaterThanOrEqualToOperator::getValue()
    00375 {
    00376   return (operand1->getValue() >= operand2->getValue());
    00377 }