00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "aggregatefunctmaxexpression.h"
00036
00037 AggregateFunctMaxExpression::AggregateFunctMaxExpression(VarExpression * _var):
00038 AggregateFunctExpression(et_aggregatefunctmax, _var) {
00039 }
00040
00041 AggregateFunctMaxExpression::AggregateFunctMaxExpression(VarStepExpression * _varstep):
00042 AggregateFunctExpression(et_aggregatefunctmax, _varstep) {
00043 }
00044
00045 AggregateFunctMaxExpression::~AggregateFunctMaxExpression() {
00046 }
00047
00048 void AggregateFunctMaxExpression::eval(OutputStream & eos, Environment * env,
00049 unsigned modus) {
00050
00051 switch (modus) {
00052
00053 case EVAL_QUERY:
00054 {
00055 bool empty_sequence = true;
00056 long double max_val =
00057 calculateValue(eos, env, modus, empty_sequence);
00058 if (!empty_sequence) {
00059 const char *max_str =
00060 MiscFunctions::getStringFromNumerical(max_val);
00061 eos << max_str;
00062 delete[]max_str;
00063 } else {
00064 eos << OUTPUT_MAX_ON_EMPTY_SEQUENCE;
00065 }
00066 break;
00067 }
00068
00069 case EVAL_QUERY_SILENT:
00070 {
00071 bool empty_sequence = true;
00072 long double max_val =
00073 calculateValue(eos, env, modus, empty_sequence);
00074 if (!empty_sequence) {
00075 cur_val.setNumVal(max_val);
00076 } else {
00077 const char *max_empty_val =
00078 new char[strlen(OUTPUT_MAX_ON_EMPTY_SEQUENCE) + 1];
00079 strcpy((char *) max_empty_val, OUTPUT_MAX_ON_EMPTY_SEQUENCE);
00080 cur_val.setStrVal(max_empty_val);
00081 }
00082 break;
00083 }
00084
00085 case EVAL_SIGNOFF:
00086 forexp->eval(eos, env, EVAL_SIGNOFF);
00087 break;
00088
00089 default:
00090 throw
00091 RuntimeException
00092 ("AggregateFunctMaxExpression: Illegal Evaluation Mode",
00093 eid_runtime_illegalmode);
00094 break;
00095 }
00096 }
00097
00098 long double AggregateFunctMaxExpression::calculateValue(OutputStream & eos,
00099 Environment * env,
00100 unsigned modus,
00101 bool & empty_sequence) {
00102
00103 var ? bit->init(env->getNodeBinding(var->getId())) :
00104 bit->init(env->getNodeBinding(varstep->getId()));
00105
00106 long double max_ret = 0;
00107 long double count = 0;
00108 long double result = 0;
00109 const char *pcd = NULL;
00110
00111 while (BufferNode * cur =
00112 bit->getNext(READ_UP_TO_CLOSE_CONTEXT, LOCK_CONTEXT_ALWAYS_CLEAR)) {
00113
00114 empty_sequence = false;
00115 count++;
00116
00117 pcd = cur->getPCDataRepresentation();
00118
00119 if (count > 1) {
00120 result = max(result, MiscFunctions::getNumericFromString(pcd));
00121 } else {
00122 result = MiscFunctions::getNumericFromString(pcd);
00123 }
00124
00125 free((char *) pcd);
00126
00127 forexp->evalSignOffForBinding(eos, env, cur);
00128 }
00129 bit->clear();
00130
00131 if (!empty_sequence) {
00132 max_ret = MiscFunctions::getRoundFrom(result);
00133 }
00134
00135 return max_ret;
00136 }