A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sqlite-data-output.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 Drexel University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Joe Kopena (tjkopena@cs.drexel.edu)
19  */
20 
21 #include <sstream>
22 
23 #include <sqlite3.h>
24 
25 #include "ns3/log.h"
26 #include "ns3/nstime.h"
27 
28 #include "data-collector.h"
29 #include "data-calculator.h"
30 #include "sqlite-data-output.h"
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("SqliteDataOutput");
35 
36 //--------------------------------------------------------------
37 //----------------------------------------------
39 {
40  NS_LOG_FUNCTION (this);
41 
42  m_filePrefix = "data";
43 }
45 {
46  NS_LOG_FUNCTION (this);
47 }
48 void
50 {
51  NS_LOG_FUNCTION (this);
52 
54  // end SqliteDataOutput::DoDispose
55 }
56 
57 int
58 SqliteDataOutput::Exec (std::string exe) {
59  NS_LOG_FUNCTION (this << exe);
60 
61  int res;
62  char **result;
63  int nrows, ncols;
64  char *errMsg = 0;
65 
66  NS_LOG_INFO ("executing '" << exe << "'");
67 
68  res = sqlite3_get_table (m_db,
69  exe.c_str (),
70  &result, &nrows, &ncols,
71  &errMsg);
72 
73  if (res != SQLITE_OK) {
74  NS_LOG_ERROR ("sqlite3 error: \"" << errMsg << "\"");
75  /*
76  } else {
77  // std::cout << "nrows " << nrows << " ncols " << ncols << std::endl;
78 
79  if (nrows > 0) {
80  for (int i = 0; i < ncols; i++) {
81  std::cout << " " << result[i];
82  }
83  std::cout << std::endl;
84 
85  for (int r = 1; r <= nrows; r++) {
86  for (int c = 0; c < ncols; c++) {
87  std::cout << " " << result[(r*ncols)+c];
88  }
89  std::cout << std::endl;
90  }
91  std::cout << std::endl;
92  }
93  */
94  }
95 
96  sqlite3_free_table (result);
97  return res;
98 
99  // end SqliteDataOutput::Exec
100 }
101 
102 //----------------------------------------------
103 void
105 {
106  NS_LOG_FUNCTION (this << &dc);
107 
108  std::string m_dbFile = m_filePrefix + ".db";
109 
110  if (sqlite3_open (m_dbFile.c_str (), &m_db)) {
111  NS_LOG_ERROR ("Could not open sqlite3 database \"" << m_dbFile << "\"");
112  NS_LOG_ERROR ("sqlite3 error \"" << sqlite3_errmsg (m_db) << "\"");
113  sqlite3_close (m_db);
115  return;
116  }
117 
118  std::string run = dc.GetRunLabel ();
119 
120  Exec ("create table if not exists Experiments (run, experiment, strategy, input, description text)");
121  Exec ("insert into Experiments (run,experiment,strategy,input,description) values ('" +
122  run + "', '" +
123  dc.GetExperimentLabel () + "', '" +
124  dc.GetStrategyLabel () + "', '" +
125  dc.GetInputLabel () + "', '" +
126  dc.GetDescription () + "')");
127 
128  Exec ("create table if not exists Metadata ( run text, key text, value)");
129 
130  for (MetadataList::iterator i = dc.MetadataBegin ();
131  i != dc.MetadataEnd (); i++) {
132  std::pair<std::string, std::string> blob = (*i);
133  Exec ("insert into Metadata (run,key,value) values ('" +
134  run + "', '" +
135  blob.first + "', '" +
136  blob.second + "')");
137  }
138 
139  Exec ("BEGIN");
140  SqliteOutputCallback callback (this, run);
141  for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
142  i != dc.DataCalculatorEnd (); i++) {
143  (*i)->Output (callback);
144  }
145  Exec ("COMMIT");
146 
147  sqlite3_close (m_db);
148 
149  // end SqliteDataOutput::Output
150 }
151 
153  (Ptr<SqliteDataOutput> owner, std::string run) :
154  m_owner (owner),
155  m_runLabel (run)
156 {
157  NS_LOG_FUNCTION (this << owner << run);
158 
159  m_owner->Exec ("create table if not exists Singletons ( run text, name text, variable text, value )");
160 
161  // end SqliteDataOutput::SqliteOutputCallback::SqliteOutputCallback
162 }
163 
164 void
166  std::string variable,
167  const StatisticalSummary *statSum)
168 {
169  NS_LOG_FUNCTION (this << key << variable << statSum);
170 
171  OutputSingleton (key,variable+"-count", (double)statSum->getCount ());
172  if (!isNaN (statSum->getSum ()))
173  OutputSingleton (key,variable+"-total", statSum->getSum ());
174  if (!isNaN (statSum->getMax ()))
175  OutputSingleton (key,variable+"-max", statSum->getMax ());
176  if (!isNaN (statSum->getMin ()))
177  OutputSingleton (key,variable+"-min", statSum->getMin ());
178  if (!isNaN (statSum->getSqrSum ()))
179  OutputSingleton (key,variable+"-sqrsum", statSum->getSqrSum ());
180  if (!isNaN (statSum->getStddev ()))
181  OutputSingleton (key,variable+"-stddev", statSum->getStddev ());
182 }
183 
184 
185 void
187  std::string variable,
188  int val)
189 {
190  NS_LOG_FUNCTION (this << key << variable << val);
191 
192  std::stringstream sstr;
193  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
194  m_runLabel << "', '" <<
195  key << "', '" <<
196  variable << "', " <<
197  val << ")";
198  m_owner->Exec (sstr.str ());
199 
200  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
201 }
202 void
204  std::string variable,
205  uint32_t val)
206 {
207  NS_LOG_FUNCTION (this << key << variable << val);
208 
209  std::stringstream sstr;
210  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
211  m_runLabel << "', '" <<
212  key << "', '" <<
213  variable << "', " <<
214  val << ")";
215  m_owner->Exec (sstr.str ());
216  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
217 }
218 void
220  std::string variable,
221  double val)
222 {
223  NS_LOG_FUNCTION (this << key << variable << val);
224 
225  std::stringstream sstr;
226  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
227  m_runLabel << "', '" <<
228  key << "', '" <<
229  variable << "', " <<
230  val << ")";
231  m_owner->Exec (sstr.str ());
232  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
233 }
234 void
236  std::string variable,
237  std::string val)
238 {
239  NS_LOG_FUNCTION (this << key << variable << val);
240 
241  std::stringstream sstr;
242  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
243  m_runLabel << "', '" <<
244  key << "', '" <<
245  variable << "', '" <<
246  val << "')";
247  m_owner->Exec (sstr.str ());
248  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
249 }
250 void
252  std::string variable,
253  Time val)
254 {
255  NS_LOG_FUNCTION (this << key << variable << val);
256 
257  std::stringstream sstr;
258  sstr << "insert into Singletons (run,name,variable,value) values ('" <<
259  m_runLabel << "', '" <<
260  key << "', '" <<
261  variable << "', " <<
262  val.GetTimeStep () << ")";
263  m_owner->Exec (sstr.str ());
264  // end SqliteDataOutput::SqliteOutputCallback::OutputSingleton
265 }
DataCalculatorList::iterator DataCalculatorEnd()
Returns an iterator to the past-the-end of the DataCalculator list.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual double getMin() const =0
Returns the minimum of the values.
Abstract class for calculating statistical data.
virtual double getSqrSum() const =0
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
std::string GetRunLabel() const
Return the runID label.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
Class to generate OMNeT output.
virtual double getStddev() const =0
Returns the standard deviation of the (weighted) observations.
MetadataList::iterator MetadataBegin()
Returns an iterator to the beginning of the metadata list.
virtual double getMax() const =0
Returns the maximum of the values.
virtual void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
std::string GetStrategyLabel() const
Return the strategy label.
std::string GetExperimentLabel() const
Return the experiment label.
SqliteOutputCallback(Ptr< SqliteDataOutput > owner, std::string run)
Constructor.
std::string m_filePrefix
File prefix for the DataOutputInterface.
int64_t GetTimeStep(void) const
Definition: nstime.h:354
virtual double getSum() const =0
void OutputSingleton(std::string key, std::string variable, int val)
Generates a single data output.
Collects data.
std::string GetDescription() const
Return the description label.
virtual void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
sqlite3 * m_db
pointer to the SQL database
bool isNaN(double x)
true if x is NaN
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:193
void OutputStatistic(std::string key, std::string variable, const StatisticalSummary *statSum)
Generates data statistics.
std::string GetInputLabel() const
Return the input label.
int Exec(std::string exe)
Execute a sqlite3 query.
virtual long getCount() const =0
Returns the number of observations.
virtual void Output(DataCollector &dc)
Outputs information from the provided DataCollector.
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.