1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 #include <anna/statistics/Engine.hpp>
11 #include <anna/statistics/internal/sccs.hpp>
13 #include <anna/core/functions.hpp>
14 #include <anna/xml/xml.hpp>
21 using namespace anna::statistics;
24 //******************************************************************************
25 //----------------------------------------------------------------------- Engine
26 //******************************************************************************
28 statistics::sccs::activate();
30 a_sequence_concept_id = 0;
34 //------------------------------------------------------------------------------
35 //--------------------------------------------------------- Engine::addConcept()
36 //------------------------------------------------------------------------------
37 int Engine::addConcept(const std::string & description, const std::string & unit, const bool & integerNatureSample) throw() {
38 a_sequence_concept_id++;
39 _concept_identification_t aux;
40 aux.SampleFile = ""; // sample dump disabled by default for new concepts
41 aux.Description = description;
43 aux.IntegerNatureSample = integerNatureSample;
44 a_concept_identification_map[a_sequence_concept_id] = aux;
45 return (a_sequence_concept_id);
49 //------------------------------------------------------------------------------
50 //--------------------------------------------------------- Engine::getConcept()
51 //------------------------------------------------------------------------------
52 bool Engine::getConcept(const int & id, std::string & description, std::string & unit, bool & integerNatureSample) const throw() {
53 _concept_identification_map_iter it = a_concept_identification_map.find(id);
55 if(it == a_concept_identification_map.end()) return false;
57 description = (*it).second.Description;
58 unit = (*it).second.Unit;
59 integerNatureSample = (*it).second.IntegerNatureSample;
64 //------------------------------------------------------------------------------
65 //---------------------------------------------------- Engine::enableSampleLog()
66 //------------------------------------------------------------------------------
67 bool Engine::enableSampleLog(const int & id, const char *sampleFileName) throw() {
68 _concept_identification_map_nc_iter it;
69 std::string providedName = sampleFileName ? sampleFileName : "sample";
70 std::string *SampleFile_ptr;
71 std::string realName = "";
74 it = a_concept_identification_map.find(id);
76 if(it == a_concept_identification_map.end()) return false;
78 if(providedName != "") realName = anna::functions::asString("%s.%d.csv", providedName.c_str(), id);
81 SampleFile_ptr = &((*it).second.SampleFile);
82 *SampleFile_ptr = realName;
87 _concept_identification_map_nc_iter it_min(a_concept_identification_map.begin());
88 _concept_identification_map_nc_iter it_max(a_concept_identification_map.end());
90 for(it = it_min; it != it_max; it++) {
93 if(providedName != "") realName = anna::functions::asString("%s.%d.csv", providedName.c_str(), (*it).first);
95 SampleFile_ptr = &((*it).second.SampleFile);
96 *SampleFile_ptr = realName;
103 //------------------------------------------------------------------------------
104 //--------------------------------------------------- Engine::disableSampleLog()
105 //------------------------------------------------------------------------------
106 bool Engine::disableSampleLog(const int & id) throw() {
107 _concept_identification_map_nc_iter it = a_concept_identification_map.find(id);
109 if(it == a_concept_identification_map.end()) return false;
112 std::string *SampleFile_ptr = &((*it).second.SampleFile);
113 *SampleFile_ptr = "";
118 //------------------------------------------------------------------------------
119 //--------------------------------------------------- Engine::disableSampleLog()
120 //------------------------------------------------------------------------------
121 bool Engine::logSample(const int & conceptId, const anna::Millisecond & unixTimestamp, const double & value) const throw() {
122 _concept_identification_map_iter it = a_concept_identification_map.find(conceptId);
124 if(it == a_concept_identification_map.end()) return false;
126 std::string target = (*it).second.SampleFile;
128 if(target != "") { // enabled
129 std::ofstream out(target.c_str(), std::ifstream::out | std::ifstream::app);
130 // Write and close (quiza no deberia cerrar cada vez...)
131 std::string log = anna::functions::asString(unixTimestamp);
133 log += anna::functions::asString(value, (*it).second.IntegerNatureSample ? "%.0f" : "%e");
135 out.write(log.c_str(), log.size());
142 //------------------------------------------------------------------------------
143 //----------------------------------------------------------- Engine::asString()
144 //------------------------------------------------------------------------------
145 std::string Engine::asString(void) const throw() {
147 _concept_identification_map_iter iter;
148 _concept_identification_map_iter iter_min(a_concept_identification_map.begin());
149 _concept_identification_map_iter iter_max(a_concept_identification_map.end());
150 trace = "\n=============================";
151 trace += "\nStatistic Engine Information";
152 trace += "\n============================";
155 trace += "\nWARNING: engine is disabled !";
157 if(a_concept_identification_map.size() == 0) {
158 trace += "\nNo concepts found (empty map)";
162 trace += "\nNumber of elements (registered concepts) = "; trace += anna::functions::asString(a_concept_identification_map.size());
164 for(iter = iter_min; iter != iter_max; iter++) {
166 trace += "\n Concept id: "; trace += anna::functions::asString((*iter).first);
168 if((*iter).second.SampleFile != "") trace += "\n Sample file: "; trace += (*iter).second.SampleFile;
170 trace += "\n Description: "; trace += (*iter).second.Description;
171 trace += "\n Unit: "; trace += (*iter).second.Unit;
172 trace += "\n Integer Nature Sample: "; trace += (*iter).second.IntegerNatureSample ? "yes" : "no";
180 //------------------------------------------------------------------------------
181 //-------------------------------------------------------------- Engine::asXML()
182 //------------------------------------------------------------------------------
183 anna::xml::Node* Engine::asXML(anna::xml::Node* parent, const int & numberOfDecimals) const throw() {
184 anna::xml::Node* result = parent->createChild("anna.statistics.Engine");
185 _concept_identification_map_iter iter;
186 _concept_identification_map_iter iter_min(a_concept_identification_map.begin());
187 _concept_identification_map_iter iter_max(a_concept_identification_map.end());
188 int size = a_concept_identification_map.size();
189 result->createAttribute("State", enabled() ? "Enabled" : "Disabled");
190 anna::xml::Node* registeredConcepts = result->createChild("ConceptsList");
191 registeredConcepts->createAttribute("Size", (size > 0) ? anna::functions::asString(size) : "No concepts found (empty map)");
193 for(iter = iter_min; iter != iter_max; iter++) {
194 anna::xml::Node* concept = registeredConcepts->createChild("Concept");
195 concept->createAttribute("Id", anna::functions::asString((*iter).first));
197 if((*iter).second.SampleFile != "") concept->createAttribute("SampleFile", (*iter).second.SampleFile);
199 concept->createAttribute("Description", (*iter).second.Description);
200 concept->createAttribute("Unit", (*iter).second.Unit);
201 concept->createAttribute("IntegerNatureSample", (*iter).second.IntegerNatureSample ? "yes" : "no");