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 "aggregatefunctavgexpression.h"
00036
00037 AggregateFunctAvgExpression::AggregateFunctAvgExpression(VarExpression * _var):
00038 AggregateFunctExpression(et_aggregatefunctavg, _var) {
00039 }
00040
00041 AggregateFunctAvgExpression::AggregateFunctAvgExpression(VarStepExpression * _varstep):
00042 AggregateFunctExpression(et_aggregatefunctavg, _varstep) {
00043 }
00044
00045 AggregateFunctAvgExpression::~AggregateFunctAvgExpression() {
00046 }
00047
00048 void AggregateFunctAvgExpression::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 avg_val =
00057 calculateValue(eos, env, modus, empty_sequence);
00058 if (!empty_sequence) {
00059 const char *avg_str =
00060 MiscFunctions::getStringFromNumerical(avg_val);
00061 eos << avg_str;
00062 delete[]avg_str;
00063 } else {
00064 eos << OUTPUT_AVG_ON_EMPTY_SEQUENCE;
00065 }
00066 break;
00067 }
00068
00069 case EVAL_QUERY_SILENT:
00070 {
00071 bool empty_sequence = true;
00072 long double avg_val =
00073 calculateValue(eos, env, modus, empty_sequence);
00074 if (!empty_sequence) {
00075 cur_val.setNumVal(avg_val);
00076 } else {
00077 const char *avg_empty_val =
00078 new char[strlen(OUTPUT_AVG_ON_EMPTY_SEQUENCE) + 1];
00079 strcpy((char *) avg_empty_val, OUTPUT_AVG_ON_EMPTY_SEQUENCE);
00080 cur_val.setStrVal(avg_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 ("AggregateFunctAvgExpression: Illegal Evaluation Mode",
00093 eid_runtime_illegalmode);
00094 break;
00095 }
00096 }
00097
00098 long double AggregateFunctAvgExpression::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 avg_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 result =
00119 MiscFunctions::getSummationFrom(result,
00120 MiscFunctions::
00121 getNumericFromString(pcd));
00122
00123 free((char *) pcd);
00124
00125 forexp->evalSignOffForBinding(eos, env, cur);
00126 }
00127 bit->clear();
00128
00129 if (!empty_sequence) {
00130 avg_ret =
00131 MiscFunctions::getRoundFrom(MiscFunctions::
00132 getDivisionFrom(result, count));
00133 }
00134
00135 return avg_ret;
00136 }