1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_statistics_Accumulator_hpp
38 #define anna_statistics_Accumulator_hpp
40 #include <anna/core/RuntimeException.hpp>
41 #include <anna/core/functions.hpp>
55 namespace statistics {
62 //Sum = 0, // is not very useful
65 BesselStandardDeviation, //This correction (the use of N - 1 instead of N) is known as Bessel's correction
73 unsigned long long Size; // Sample size
74 double Sum; // Accumulated sum
75 double SquareSum; // Accumulated sum of square values
76 double Average; // Current average
77 //double SquareSumSampleDeviation; // Current total sum for square deviation items;
78 double MinimumProcessed; // Minimum value processed
79 double MaximumProcessed; // Maximum value processed
80 anna::Millisecond MinimumEventTimestampMs; // Timestamp (ms) for minimum event
81 anna::Millisecond MaximumEventTimestampMs; // Timestamp (ms) for maximum event
82 anna::Millisecond LastResetEventTimestampMs; // Timestamp (ms) for last statistics reset
86 Size = (unsigned long long)0;
88 SquareSum = (double)0;
90 //SquareSumSampleDeviation = (double)0;
91 MinimumProcessed = (double)0;
92 MaximumProcessed = (double)0;
93 MinimumEventTimestampMs = (anna::Millisecond)0;
94 MaximumEventTimestampMs = (anna::Millisecond)0;
95 LastResetEventTimestampMs = anna::Millisecond::getTime();
100 typedef std::map <int, _concept_data_t> _concept_data_map_t;
101 typedef std::map <int, _concept_data_t>::const_iterator _concept_data_map_iter;
106 //------------------------------------------------------------------------------
107 //------------------------------------------------------------ class Accumulator
108 //------------------------------------------------------------------------------
110 * Accumulated data information for statistic sample.
111 * It can manage a set of concepts at the same time.
113 * @short Contains statistical sample information
130 * Process new value for the sample.
132 * @param conceptId statistical concept processed
133 * @param value Value for processed item
137 void process(const int & conceptId, const double & value) throw(anna::RuntimeException);
141 * Reset sample for all concepts
145 void reset(void) throw();
149 * Reset sample for provided concept
151 * @param conceptId Concept identification to be reset
155 void reset(const int & conceptId) throw(anna::RuntimeException);
161 * @param accumulator Class instance source of data
163 * @return Returns copied reference
165 const Accumulator & operator = (const Accumulator & accumulator);
172 * Gets current sample size for any concept id
174 * @param conceptId Concept identification
176 * @return Sample size
181 unsigned long long int sampleSize(const int & conceptId) const throw(anna::RuntimeException);
185 * Get statistical information
187 * @param conceptId Statistical concept
188 * @param operation Solicited operation
190 * @return Value for solicited conceptId/operation
195 double getValue(const int & conceptId, const Operation::Type & operation) const throw(anna::RuntimeException);
199 * Class string representation
201 * @param numberOfDecimals Number of float decimals at representation string. Default is 2.
203 * @return String with class content
205 std::string asString(const int & numberOfDecimals = 2) const throw();
209 * Class XML representation
211 * @param numberOfDecimals Number of float decimals at XML representation. Default is 2.
213 * @return XML with class content
215 anna::xml::Node* asXML(anna::xml::Node* parent, const int & numberOfDecimals = 2) const throw();
220 void initialize(const int & conceptId) throw();
221 _concept_data_t * getConcept(const int & conceptId) const throw(anna::RuntimeException);
222 // Return NULL if no data is found for the concept Id
223 // Launch exception if concept id is not a valid concept registered at Engine
225 /*mutable */_concept_data_map_t a_concept_data_map;
227 std::string floatFormat(const int & numberOfDecimals) const throw();
229 double getStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);
230 double getBesselStandardDeviation(const _concept_data_t * conceptData) const throw(anna::RuntimeException);