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  m_filePrefix = "data";
41 
43 }
45 {
47 }
48 void
50 {
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 
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 }
143 
144 void
146  std::string name,
147  const StatisticalSummary *statSum)
148 {
149  if (context == "")
150  context = ".";
151  if (name == "")
152  name = "\"\"";
153  (*m_scalar) << "statistic " << context << " " << name << std::endl;
154  if (!isNaN (statSum->getCount ()))
155  (*m_scalar) << "field count " << statSum->getCount () << std::endl;
156  if (!isNaN (statSum->getSum ()))
157  (*m_scalar) << "field sum " << statSum->getSum () << std::endl;
158  if (!isNaN (statSum->getMean ()))
159  (*m_scalar) << "field mean " << statSum->getMean () << std::endl;
160  if (!isNaN (statSum->getMin ()))
161  (*m_scalar) << "field min " << statSum->getMin () << std::endl;
162  if (!isNaN (statSum->getMax ()))
163  (*m_scalar) << "field max " << statSum->getMax () << std::endl;
164  if (!isNaN (statSum->getSqrSum ()))
165  (*m_scalar) << "field sqrsum " << statSum->getSqrSum () << std::endl;
166  if (!isNaN (statSum->getStddev ()))
167  (*m_scalar) << "field stddev " << statSum->getStddev () << std::endl;
168 }
169 
170 void
172  std::string name,
173  int val)
174 {
175  if (context == "")
176  context = ".";
177  if (name == "")
178  name = "\"\"";
179  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
180  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
181 }
182 
183 void
185  std::string name,
186  uint32_t val)
187 {
188  if (context == "")
189  context = ".";
190  if (name == "")
191  name = "\"\"";
192  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
193  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
194 }
195 
196 void
198  std::string name,
199  double val)
200 {
201  if (context == "")
202  context = ".";
203  if (name == "")
204  name = "\"\"";
205  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
206  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
207 }
208 
209 void
211  std::string name,
212  std::string val)
213 {
214  if (context == "")
215  context = ".";
216  if (name == "")
217  name = "\"\"";
218  (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
219  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
220 }
221 
222 void
224  std::string name,
225  Time val)
226 {
227  if (context == "")
228  context = ".";
229  if (name == "")
230  name = "\"\"";
231  (*m_scalar) << "scalar " << context << " " << name << " " << val.GetTimeStep () << std::endl;
232  // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
233 }
DataCalculatorList::iterator DataCalculatorEnd()
Returns an iterator to the past-the-end of the DataCalculator list.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
virtual double getMin() const =0
Returns the minimum of the values.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
Abstract class for calculating statistical data.
virtual double getSqrSum() const =0
std::string GetRunLabel() const
Return the runID label.
virtual void Output(DataCollector &dc)
Outputs information from the provided DataCollector.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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:356
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.