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 /* static */
48 TypeId
50 {
51  static TypeId tid = TypeId ("ns3::OmnetDataOutput")
53  .SetGroupName ("Stats")
54  .AddConstructor<OmnetDataOutput> ()
55  ;
56  return tid;
57 }
58 
59 void
61 {
62  NS_LOG_FUNCTION (this);
63 
65  // end OmnetDataOutput::DoDispose
66 }
67 
68 //----------------------------------------------
69 
70 inline bool isNumeric (const std::string& s) {
71  bool decimalPtSeen = false;
72  bool exponentSeen = false;
73  char last = '\0';
74 
75  for (std::string::const_iterator it = s.begin (); it != s.end (); it++)
76  {
77  if ((*it == '.') && (decimalPtSeen))
78  return false;
79  else if (*it == '.')
80  decimalPtSeen = true;
81  else if ((*it == 'e') && exponentSeen)
82  return false;
83  else if (*it == 'e')
84  {
85  exponentSeen = true;
86  decimalPtSeen = false;
87  }
88  else if (*it == '-' && it != s.begin () && last != 'e')
89  return false;
90 
91  last = *it;
92  }
93  return true;
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION (this << &dc);
100 
101  std::ofstream scalarFile;
102  std::string fn = m_filePrefix +"-"+dc.GetRunLabel ()+ ".sca";
103  scalarFile.open (fn.c_str (), std::ios_base::out);
104 
106  scalarFile << "run " << dc.GetRunLabel () << std::endl;
107  scalarFile << "attr experiment \"" << dc.GetExperimentLabel ()
108  << "\"" << std::endl;
109  scalarFile << "attr strategy \"" << dc.GetStrategyLabel ()
110  << "\"" << std::endl;
111  scalarFile << "attr measurement \"" << dc.GetInputLabel ()
112  << "\"" << std::endl;
113  scalarFile << "attr description \"" << dc.GetDescription ()
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  scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\""
120  << std::endl;
121  }
122 
123  scalarFile << std::endl;
124  if (isNumeric (dc.GetInputLabel ())) {
125  scalarFile << "scalar . measurement \"" << dc.GetInputLabel ()
126  << "\"" << std::endl;
127  }
128  for (MetadataList::iterator i = dc.MetadataBegin ();
129  i != dc.MetadataEnd (); i++) {
130  std::pair<std::string, std::string> blob = (*i);
131  if (isNumeric (blob.second)) {
132  scalarFile << "scalar . \"" << blob.first << "\" \"" << blob.second << "\""
133  << std::endl;
134  }
135  }
136  OmnetOutputCallback callback (&scalarFile);
137 
138  for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
139  i != dc.DataCalculatorEnd (); i++) {
140  (*i)->Output (callback);
141  }
142 
143  scalarFile << std::endl << std::endl;
144  scalarFile.close ();
145 
146  // end OmnetDataOutput::Output
147 }
148 
149 
151  (std::ostream *scalar) :
152  m_scalar (scalar)
153 {
154  NS_LOG_FUNCTION (this << scalar);
155 }
156 
157 void
159  std::string name,
160  const StatisticalSummary *statSum)
161 {
162  NS_LOG_FUNCTION (this << context << name << statSum);
163 
164  if (context == "")
165  context = ".";
166  if (name == "")
167  name = "\"\"";
168  (*m_scalar) << "statistic " << context << " " << name << std::endl;
169  if (!isNaN (statSum->getCount ()))
170  (*m_scalar) << "field count " << statSum->getCount () << std::endl;
171  if (!isNaN (statSum->getSum ()))
172  (*m_scalar) << "field sum " << statSum->getSum () << std::endl;
173  if (!isNaN (statSum->getMean ()))
174  (*m_scalar) << "field mean " << statSum->getMean () << std::endl;
175  if (!isNaN (statSum->getMin ()))
176  (*m_scalar) << "field min " << statSum->getMin () << std::endl;
177  if (!isNaN (statSum->getMax ()))
178  (*m_scalar) << "field max " << statSum->getMax () << std::endl;
179  if (!isNaN (statSum->getSqrSum ()))
180  (*m_scalar) << "field sqrsum " << statSum->getSqrSum () << std::endl;
181  if (!isNaN (statSum->getStddev ()))
182  (*m_scalar) << "field stddev " << statSum->getStddev () << std::endl;
183 }
184 
185 void
187  std::string name,
188  int val)
189 {
190  NS_LOG_FUNCTION (this << context << name << val);
191 
192  if (context == "")
193  context = ".";
194  if (name == "")
195  name = "\"\"";
196  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
197  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
198 }
199 
200 void
202  std::string name,
203  uint32_t val)
204 {
205  NS_LOG_FUNCTION (this << context << name << val);
206 
207  if (context == "")
208  context = ".";
209  if (name == "")
210  name = "\"\"";
211  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
212  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
213 }
214 
215 void
217  std::string name,
218  double val)
219 {
220  NS_LOG_FUNCTION (this << context << name << val);
221 
222  if (context == "")
223  context = ".";
224  if (name == "")
225  name = "\"\"";
226  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
227  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
228 }
229 
230 void
232  std::string name,
233  std::string val)
234 {
235  NS_LOG_FUNCTION (this << context << name << val);
236 
237  if (context == "")
238  context = ".";
239  if (name == "")
240  name = "\"\"";
241  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
242  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
243 }
244 
245 void
247  std::string name,
248  Time val)
249 {
250  NS_LOG_FUNCTION (this << context << name << val);
251 
252  if (context == "")
253  context = ".";
254  if (name == "")
255  name = "\"\"";
256  (*m_scalar) << "scalar " << context << " " << name << " " << val.GetTimeStep () << std::endl;
257  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
258 }
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:103
Abstract Data Output Interface class s
#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.
Outputs data in a format compatible with OMNeT library and framework.
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:205
virtual void Output(DataCollector &dc)
Outputs information from the provided DataCollector.
std::string GetInputLabel() const
Return the input label.
std::string GetStrategyLabel() const
Return the strategy label.
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.
std::string GetExperimentLabel() const
Return the experiment label.
std::string GetDescription() const
Return the description 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 GetRunLabel() const
Return the runID label.
std::string m_filePrefix
File prefix for the DataOutputInterface.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Register this type.
virtual double getSum() const =0
Collects data.
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
bool isNaN(double x)
true if x is NaN
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.
a unique identifier for an interface.
Definition: type-id.h:58
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:415