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 "aggregatefunctsumexpression.h"
00036
00037 AggregateFunctSumExpression::AggregateFunctSumExpression(VarExpression * _var):
00038 AggregateFunctExpression(et_aggregatefunctsum, _var) {
00039 }
00040
00041 AggregateFunctSumExpression::AggregateFunctSumExpression(VarStepExpression * _varstep):
00042 AggregateFunctExpression(et_aggregatefunctsum, _varstep) {
00043 }
00044
00045 AggregateFunctSumExpression::~AggregateFunctSumExpression() {
00046 }
00047
00048 void AggregateFunctSumExpression::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 sum_val =
00057 calculateValue(eos, env, modus, empty_sequence);
00058 if (!empty_sequence) {
00059 const char *sum_str =
00060 MiscFunctions::getStringFromNumerical(sum_val);
00061 eos << sum_str;
00062 delete[]sum_str;
00063 } else {
00064 const char *sum_str =
00065 MiscFunctions::
00066 getStringFromNumerical(OUTPUT_SUM_ON_EMPTY_SEQUENCE);
00067 eos << sum_str;
00068 delete[]sum_str;
00069 }
00070 break;
00071 }
00072
00073 case EVAL_QUERY_SILENT:
00074 {
00075 bool empty_sequence = true;
00076 long double sum_val =
00077 calculateValue(eos, env, modus, empty_sequence);
00078 if (!empty_sequence) {
00079 cur_val.setNumVal(sum_val);
00080 } else {
00081 cur_val.setNumVal(OUTPUT_SUM_ON_EMPTY_SEQUENCE);
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 ("AggregateFunctSumExpression: Illegal Evaluation Mode",
00094 eid_runtime_illegalmode);
00095 break;
00096 }
00097 }
00098
00099 long double AggregateFunctSumExpression::calculateValue(OutputStream & eos,
00100 Environment * env,
00101 unsigned modus,
00102 bool & empty_sequence) {
00103
00104 var ? bit->init(env->getNodeBinding(var->getId())) :
00105 bit->init(env->getNodeBinding(varstep->getId()));
00106
00107 long double sum_ret = 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
00116 pcd = cur->getPCDataRepresentation();
00117 result =
00118 MiscFunctions::getSummationFrom(result,
00119 MiscFunctions::
00120 getNumericFromString(pcd));
00121
00122 free((char *) pcd);
00123
00124 forexp->evalSignOffForBinding(eos, env, cur);
00125 }
00126 bit->clear();
00127
00128 if (!empty_sequence) {
00129 sum_ret = MiscFunctions::getRoundFrom(result);
00130 }
00131
00132 return sum_ret;
00133 }