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 "aggregatefunctvarpopexpression.h"
00036
00037 AggregateFunctVarPopExpression::AggregateFunctVarPopExpression(VarExpression * _var):
00038 AggregateFunctExpression(et_aggregatefunctvarpop, _var) {
00039 }
00040
00041 AggregateFunctVarPopExpression::AggregateFunctVarPopExpression(VarStepExpression * _varstep):
00042 AggregateFunctExpression(et_aggregatefunctvarpop, _varstep) {
00043 }
00044
00045 AggregateFunctVarPopExpression::~AggregateFunctVarPopExpression() {
00046 }
00047
00048 void AggregateFunctVarPopExpression::eval(OutputStream & eos,
00049 Environment * env, unsigned modus) {
00050
00051 switch (modus) {
00052
00053 case EVAL_QUERY:
00054 {
00055 bool empty_sequence = true;
00056 long double varpop_val =
00057 calculateValue(eos, env, modus, empty_sequence);
00058 if (!empty_sequence) {
00059 const char *varpop_str =
00060 MiscFunctions::getStringFromNumerical(varpop_val);
00061 eos << varpop_str;
00062 delete[]varpop_str;
00063 } else {
00064 eos << OUTPUT_VARPOP_ON_EMPTY_SEQUENCE;
00065 }
00066 break;
00067 }
00068
00069 case EVAL_QUERY_SILENT:
00070 {
00071 bool empty_sequence = true;
00072 long double varpop_val =
00073 calculateValue(eos, env, modus, empty_sequence);
00074 if (!empty_sequence) {
00075 cur_val.setNumVal(varpop_val);
00076 } else {
00077 const char *varpop_empty_val =
00078 new char[strlen(OUTPUT_VARPOP_ON_EMPTY_SEQUENCE) + 1];
00079 strcpy((char *) varpop_empty_val,
00080 OUTPUT_VARPOP_ON_EMPTY_SEQUENCE);
00081 cur_val.setStrVal(varpop_empty_val);
00082 }
00083 break;
00084 }
00085
00086 case EVAL_SIGNOFF:
00087 forexp->eval(eos, env, EVAL_SIGNOFF);
00088 break;
00089
00090 default:
00091 throw
00092 RuntimeException
00093 ("AggregateFunctVarPopExpression: Illegal Evaluation Mode",
00094 eid_runtime_illegalmode);
00095 break;
00096 }
00097 }
00098
00099 long double AggregateFunctVarPopExpression::calculateValue(OutputStream & eos,
00100 Environment * env,
00101 unsigned modus,
00102 bool &
00103 empty_sequence) {
00104
00105 var ? bit->init(env->getNodeBinding(var->getId())) :
00106 bit->init(env->getNodeBinding(varstep->getId()));
00107
00108 long double varpop_ret = 0;
00109 long double result = 0;
00110 vector < long double >*pcds = new vector < long double >;
00111 const char *pcd = NULL;
00112
00113 while (BufferNode * cur =
00114 bit->getNext(READ_UP_TO_CLOSE_CONTEXT, LOCK_CONTEXT_ALWAYS_CLEAR)) {
00115
00116 empty_sequence = false;
00117
00118 pcd = cur->getPCDataRepresentation();
00119 long double tmp = MiscFunctions::getNumericFromString(pcd);
00120
00121 result = MiscFunctions::getSummationFrom(result, tmp);
00122 pcds->push_back(tmp);
00123
00124 free((char *) pcd);
00125
00126 forexp->evalSignOffForBinding(eos, env, cur);
00127 }
00128 bit->clear();
00129
00130 if (!empty_sequence) {
00131 long double avg_val =
00132 MiscFunctions::getDivisionFrom(result, pcds->size());
00133 result = 0;
00134 for (unsigned i = 0; i < pcds->size(); i++) {
00135 result +=
00136 MiscFunctions::getPowerFrom(MiscFunctions::
00137 getSubtractionFrom((*pcds)[i],
00138 avg_val), 2);
00139 }
00140 varpop_ret =
00141 MiscFunctions::getRoundFrom(MiscFunctions::
00142 getDivisionFrom(result, pcds->size()));
00143 }
00144 delete pcds;
00145
00146 return varpop_ret;
00147 }