A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //--------------------------------------------------------------
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 OmnetDataOutput::DoDispose
55 }
56 
57 //----------------------------------------------
58 
59 inline bool isNumeric (const std::string& s) {
60  bool decimalPtSeen = false;
61  bool exponentSeen = false;
62  char last = '\0';
63 
64  for (std::string::const_iterator it = s.begin (); it != s.end (); it++)
65  {
66  if ((*it == '.') && (decimalPtSeen))
67  return false;
68  else if (*it == '.')
69  decimalPtSeen = true;
70  else if ((*it == 'e') && exponentSeen)
71  return false;
72  else if (*it == 'e')
73  {
74  exponentSeen = true;
75  decimalPtSeen = false;
76  }
77  else if (*it == '-' && it != s.begin () && last != 'e')
78  return false;
79 
80  last = *it;
81  }
82  return true;
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << &dc);
89 
90  std::ofstream scalarFile;
91  std::string fn = m_filePrefix +"-"+dc.GetRunLabel ()+ ".sca";
92  scalarFile.open (fn.c_str (), std::ios_base::out);
93 
95  scalarFile << "run " << dc.GetRunLabel () << std::endl;
96  scalarFile << "attr experiment \"" << dc.GetExperimentLabel ()
97  << "\"" << std::endl;
98  scalarFile << "attr strategy \"" << dc.GetStrategyLabel ()
99  << "\"" << std::endl;
100  scalarFile << "attr measurement \"" << dc.GetInputLabel ()
101  << "\"" << std::endl;
102  scalarFile << "attr description \"" << dc.GetDescription ()
103  << "\"" << std::endl;
104 
105  for (MetadataList::iterator i = dc.MetadataBegin ();
106  i != dc.MetadataEnd (); i++) {
107  std::pair<std::string, std::string> blob = (*i);
108  scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\""
109  << std::endl;
110  }
111 
112  scalarFile << std::endl;
113  if (isNumeric (dc.GetInputLabel ())) {
114  scalarFile << "scalar . measurement \"" << dc.GetInputLabel ()
115  << "\"" << std::endl;
116  }
117  for (MetadataList::iterator i = dc.MetadataBegin ();
118  i != dc.MetadataEnd (); i++) {
119  std::pair<std::string, std::string> blob = (*i);
120  if (isNumeric (blob.second)) {
121  scalarFile << "scalar . \"" << blob.first << "\" \"" << blob.second << "\""
122  << std::endl;
123  }
124  }
125  OmnetOutputCallback callback (&scalarFile);
126 
127  for (DataCalculatorList::iterator i = dc.DataCalculatorBegin ();
128  i != dc.DataCalculatorEnd (); i++) {
129  (*i)->Output (callback);
130  }
131 
132  scalarFile << std::endl << std::endl;
133  scalarFile.close ();
134 
135  // end OmnetDataOutput::Output
136 }
137 
138 
140  (std::ostream *scalar) :
141  m_scalar (scalar)
142 {
143  NS_LOG_FUNCTION (this << scalar);
144 }
145 
146 void
148  std::string name,
149  const StatisticalSummary *statSum)
150 {
151  NS_LOG_FUNCTION (this << context << name << statSum);
152 
153  if (context == "")
154  context = ".";
155  if (name == "")
156  name = "\"\"";
157  (*m_scalar) << "statistic " << context << " " << name << std::endl;
158  if (!isNaN (statSum->getCount ()))
159  (*m_scalar) << "field count " << statSum->getCount () << std::endl;
160  if (!isNaN (statSum->getSum ()))
161  (*m_scalar) << "field sum " << statSum->getSum () << std::endl;
162  if (!isNaN (statSum->getMean ()))
163  (*m_scalar) << "field mean " << statSum->getMean () << std::endl;
164  if (!isNaN (statSum->getMin ()))
165  (*m_scalar) << "field min " << statSum->getMin () << std::endl;
166  if (!isNaN (statSum->getMax ()))
167  (*m_scalar) << "field max " << statSum->getMax () << std::endl;
168  if (!isNaN (statSum->getSqrSum ()))
169  (*m_scalar) << "field sqrsum " << statSum->getSqrSum () << std::endl;
170  if (!isNaN (statSum->getStddev ()))
171  (*m_scalar) << "field stddev " << statSum->getStddev () << std::endl;
172 }
173 
174 void
176  std::string name,
177  int val)
178 {
179  NS_LOG_FUNCTION (this << context << name << val);
180 
181  if (context == "")
182  context = ".";
183  if (name == "")
184  name = "\"\"";
185  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
186  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
187 }
188 
189 void
191  std::string name,
192  uint32_t val)
193 {
194  NS_LOG_FUNCTION (this << context << name << val);
195 
196  if (context == "")
197  context = ".";
198  if (name == "")
199  name = "\"\"";
200  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
201  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
202 }
203 
204 void
206  std::string name,
207  double val)
208 {
209  NS_LOG_FUNCTION (this << context << name << val);
210 
211  if (context == "")
212  context = ".";
213  if (name == "")
214  name = "\"\"";
215  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
216  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
217 }
218 
219 void
221  std::string name,
222  std::string val)
223 {
224  NS_LOG_FUNCTION (this << context << name << val);
225 
226  if (context == "")
227  context = ".";
228  if (name == "")
229  name = "\"\"";
230  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
231  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
232 }
233 
234 void
236  std::string name,
237  Time val)
238 {
239  NS_LOG_FUNCTION (this << context << name << val);
240 
241  if (context == "")
242  context = ".";
243  if (name == "")
244  name = "\"\"";
245  (*m_scalar) << "scalar " << context << " " << name << " " << val.GetTimeStep () << std::endl;
246  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
247 }
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
#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.
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()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.
int64_t GetTimeStep(void) const
Definition: nstime.h:354
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()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.