A Discrete-Event Network Simulator
API
omnet-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 <fstream>
22 #include <cstdlib>
23 
24 #include "ns3/log.h"
25 #include "ns3/nstime.h"
26 
27 #include "data-collector.h"
28 #include "data-calculator.h"
29 #include "omnet-data-output.h"
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE ("OmnetDataOutput");
34 
35 //--------------------------------------------------------------
36 //----------------------------------------------
38 {
39  NS_LOG_FUNCTION (this);
40 
41  m_filePrefix = "data";
42 }
44 {
45  NS_LOG_FUNCTION (this);
46 }
47 void
49 {
50  NS_LOG_FUNCTION (this);
51 
53  // end OmnetDataOutput::DoDispose
54 }
55 
56 //----------------------------------------------
57 
58 inline bool isNumeric (const std::string& s) {
59  bool decimalPtSeen = false;
60  bool exponentSeen = false;
61  char last = '\0';
62 
63  for (std::string::const_iterator it = s.begin (); it != s.end (); it++)
64  {
65  if ((*it == '.') && (decimalPtSeen))
66  return false;
67  else if (*it == '.')
68  decimalPtSeen = true;
69  else if ((*it == 'e') && exponentSeen)
70  return false;
71  else if (*it == 'e')
72  {
73  exponentSeen = true;
74  decimalPtSeen = false;
75  }
76  else if (*it == '-' && it != s.begin () && last != 'e')
77  return false;
78 
79  last = *it;
80  }
81  return true;
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION (this << &dc);
88 
89  std::ofstream scalarFile;
90  std::string fn = m_filePrefix +"-"+dc.GetRunLabel ()+ ".sca";
91  scalarFile.open (fn.c_str (), std::ios_base::out);
92 
94  scalarFile << "run " << dc.GetRunLabel () << std::endl;
95  scalarFile << "attr experiment \"" << dc.GetExperimentLabel ()
96  << "\"" << std::endl;
97  scalarFile << "attr strategy \"" << dc.GetStrategyLabel ()
98  << "\"" << std::endl;
99  scalarFile << "attr measurement \"" << dc.GetInputLabel ()
100  << "\"" << std::endl;
101  scalarFile << "attr description \"" << dc.GetDescription ()
102  << "\"" << std::endl;
103 
104  for (MetadataList::iterator i = dc.MetadataBegin ();
105  i != dc.MetadataEnd (); i++) {
106  std::pair<std::string, std::string> blob = (*i);
107  scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\""
108  << std::endl;
109  }
110 
111  scalarFile << std::endl;
112  if (isNumeric (dc.GetInputLabel ())) {
113  scalarFile << "scalar . measurement \"" << dc.GetInputLabel ()
114  << "\"" << std::endl;
115  }
116  for (MetadataList::iterator i = dc.MetadataBegin ();
117  i != dc.MetadataEnd (); i++) {
118  std::pair<std::string, std::string> blob = (*i);
119  if (isNumeric (blob.second)) {
120  scalarFile << "scalar . \"" << blob.first << "\" \"" << blob.second << "\""
121  << std::endl;
122  }
123  }
124  OmnetOutputCallback callback (&scalarFile);
125 
126  for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
127  i != dc.DataCalculatorEnd (); i++) {
128  (*i)->Output (callback);
129  }
130 
131  scalarFile << std::endl << std::endl;
132  scalarFile.close ();
133 
134  // end OmnetDataOutput::Output
135 }
136 
137 
139  (std::ostream *scalar) :
140  m_scalar (scalar)
141 {
142  NS_LOG_FUNCTION (this << scalar);
143 }
144 
145 void
147  std::string name,
148  const StatisticalSummary *statSum)
149 {
150  NS_LOG_FUNCTION (this << context << name << statSum);
151 
152  if (context == "")
153  context = ".";
154  if (name == "")
155  name = "\"\"";
156  (*m_scalar) << "statistic " << context << " " << name << std::endl;
157  if (!isNaN (statSum->getCount ()))
158  (*m_scalar) << "field count " << statSum->getCount () << std::endl;
159  if (!isNaN (statSum->getSum ()))
160  (*m_scalar) << "field sum " << statSum->getSum () << std::endl;
161  if (!isNaN (statSum->getMean ()))
162  (*m_scalar) << "field mean " << statSum->getMean () << std::endl;
163  if (!isNaN (statSum->getMin ()))
164  (*m_scalar) << "field min " << statSum->getMin () << std::endl;
165  if (!isNaN (statSum->getMax ()))
166  (*m_scalar) << "field max " << statSum->getMax () << std::endl;
167  if (!isNaN (statSum->getSqrSum ()))
168  (*m_scalar) << "field sqrsum " << statSum->getSqrSum () << std::endl;
169  if (!isNaN (statSum->getStddev ()))
170  (*m_scalar) << "field stddev " << statSum->getStddev () << std::endl;
171 }
172 
173 void
175  std::string name,
176  int val)
177 {
178  NS_LOG_FUNCTION (this << context << name << val);
179 
180  if (context == "")
181  context = ".";
182  if (name == "")
183  name = "\"\"";
184  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
185  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
186 }
187 
188 void
190  std::string name,
191  uint32_t val)
192 {
193  NS_LOG_FUNCTION (this << context << name << val);
194 
195  if (context == "")
196  context = ".";
197  if (name == "")
198  name = "\"\"";
199  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
200  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
201 }
202 
203 void
205  std::string name,
206  double val)
207 {
208  NS_LOG_FUNCTION (this << context << name << val);
209 
210  if (context == "")
211  context = ".";
212  if (name == "")
213  name = "\"\"";
214  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
215  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
216 }
217 
218 void
220  std::string name,
221  std::string val)
222 {
223  NS_LOG_FUNCTION (this << context << name << val);
224 
225  if (context == "")
226  context = ".";
227  if (name == "")
228  name = "\"\"";
229  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
230  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
231 }
232 
233 void
235  std::string name,
236  Time val)
237 {
238  NS_LOG_FUNCTION (this << context << name << val);
239 
240  if (context == "")
241  context = ".";
242  if (name == "")
243  name = "\"\"";
244  (*m_scalar) << "scalar " << context << " " << name << " " << val.GetTimeStep () << std::endl;
245  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
246 }
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:95
#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:201
std::string GetRunLabel() const
Return the runID label.
virtual void Output(DataCollector &dc)
Outputs information from the provided DataCollector.
virtual double getStddev() const =0
Returns the standard deviation of the (weighted) observations.
virtual double getMean() const =0
Returns the mean 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.
OmnetOutputCallback(std::ostream *scalar)
Constructor.
virtual void DoDispose()
Destructor implementation.
Ptr< SampleEmitter > s
std::string GetStrategyLabel() const
Return the strategy label.
void OutputStatistic(std::string context, std::string name, const StatisticalSummary *statSum)
Generates data statistics.
Class to generate OMNeT output.
bool isNumeric(const std::string &s)
std::string GetExperimentLabel() const
Return the experiment label.
std::string m_filePrefix
File prefix for the DataOutputInterface.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetTimeStep(void) const
Definition: nstime.h:357
virtual double getSum() const =0
Collects data.
std::string GetDescription() const
Return the description label.
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
bool isNaN(double x)
true if x is NaN
std::string GetInputLabel() const
Return the input label.
void OutputSingleton(std::string context, std::string name, int val)
Generates a single data output.
virtual void DoDispose()
Destructor implementation.
virtual long getCount() const =0
Returns the number of observations.
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.