Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / statistics / Accumulator.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_statistics_Accumulator_hpp
10 #define anna_statistics_Accumulator_hpp
11
12 #include <anna/core/RuntimeException.hpp>
13 #include <anna/core/functions.hpp>
14
15 // Standard
16 #include <string>
17
18 namespace anna {
19 namespace xml {
20 class Node;
21 }
22 }
23
24
25 namespace anna {
26
27 namespace statistics {
28
29
30 class Operation {
31 public:
32
33   enum Type {
34     //Sum = 0, // is not very useful
35     Average,
36     StandardDeviation,
37     BesselStandardDeviation, //This correction (the use of N - 1 instead of N) is known as Bessel's correction
38     Minimum,
39     Maximum
40   };
41 };
42
43 typedef struct {
44
45   unsigned long long Size;                           // Sample size
46   double Sum;                                        // Accumulated sum
47   double SquareSum;                                  // Accumulated sum of square values
48   double Average;                                    // Current average
49   //double SquareSumSampleDeviation;                   // Current total sum for square deviation items;
50   double MinimumProcessed;                           // Minimum value processed
51   double MaximumProcessed;                           // Maximum value processed
52   anna::Millisecond MinimumEventTimestampMs;       // Timestamp (ms) for minimum event
53   anna::Millisecond MaximumEventTimestampMs;       // Timestamp (ms) for maximum event
54   anna::Millisecond LastResetEventTimestampMs;     // Timestamp (ms) for last statistics reset
55
56
57   void reset() {
58     Size = (unsigned long long)0;
59     Sum = (double)0;
60     SquareSum = (double)0;
61     Average = (double)0;
62     //SquareSumSampleDeviation = (double)0;
63     MinimumProcessed = (double)0;
64     MaximumProcessed = (double)0;
65     MinimumEventTimestampMs = (anna::Millisecond)0;
66     MaximumEventTimestampMs = (anna::Millisecond)0;
67     LastResetEventTimestampMs = anna::Millisecond::getTime();
68   }
69
70 } _concept_data_t;
71
72 typedef std::map <int, _concept_data_t> _concept_data_map_t;
73 typedef std::map <int, _concept_data_t>::const_iterator _concept_data_map_iter;
74
75
76
77
78 //------------------------------------------------------------------------------
79 //------------------------------------------------------------ class Accumulator
80 //------------------------------------------------------------------------------
81 /**
82  * Accumulated data information for statistic sample.
83  * It can manage a set of concepts at the same time.
84  *
85  * @short Contains statistical sample information
86  */
87 class Accumulator {
88 public:
89
90   /**
91   * Constructor.
92   */
93   Accumulator();
94
95   /**
96   * Destructor.
97   */
98   ~Accumulator();
99
100
101   /**
102   * Process new value for the sample.
103   *
104   * @param conceptId statistical concept processed
105   * @param value Value for processed item
106   *
107   * @see reset()
108   */
109   void process(const int & conceptId, const double & value) throw(anna::RuntimeException);
110
111
112   /**
113   * Reset sample for all concepts
114   *
115   * @see process()
116   */
117   void reset(void) throw();
118
119
120   /**
121   * Reset sample for provided concept
122   *
123   * @param conceptId Concept identification to be reset
124   *
125   * @see process()
126   */
127   void reset(const int & conceptId) throw(anna::RuntimeException);
128
129
130   /**
131   * Copy operator
132   *
133   * @param accumulator Class instance source of data
134   *
135   * @return Returns copied reference
136   */
137   const Accumulator & operator = (const Accumulator & accumulator);
138
139
140
141   // Gets
142
143   /**
144   * Gets current sample size for any concept id
145   *
146   * @param conceptId Concept identification
147   *
148   * @return Sample size
149   *
150   * @see getValue()
151   * @see asXML()
152   */
153   unsigned long long int sampleSize(const int & conceptId) const throw(anna::RuntimeException);
154
155
156   /**
157   * Get statistical information
158   *
159   * @param conceptId Statistical concept
160   * @param operation Solicited operation
161   *
162   * @return Value for solicited conceptId/operation
163   *
164   * @see sampleSize()
165   * @see asXML()
166   */
167   double getValue(const int & conceptId, const Operation::Type & operation) const throw(anna::RuntimeException);
168
169
170   /**
171   * Class string representation
172   *
173   * @param numberOfDecimals Number of float decimals at representation string. Default is 2.
174   *
175   * @return String with class content
176   */
177   std::string asString(const int & numberOfDecimals = 2) const throw();
178
179
180   /**
181   * Class XML representation
182   *
183   * @param numberOfDecimals Number of float decimals at XML representation. Default is 2.
184   *
185   * @return XML with class content
186   */
187   anna::xml::Node* asXML(anna::xml::Node* parent, const int & numberOfDecimals = 2) const throw();
188
189
190 private:
191
192   void initialize(const int & conceptId) throw();
193   _concept_data_t * getConcept(const int & conceptId) const throw(anna::RuntimeException);
194   // Return NULL if no data is found for the concept Id
195   // Launch exception if concept id is not a valid concept registered at Engine
196
197   /*mutable */_concept_data_map_t a_concept_data_map;
198
199   std::string floatFormat(const int & numberOfDecimals) const throw();
200
201   double getStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);
202   double getBesselStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);
203 };
204
205 }
206 }
207
208 #endif