A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
omnet-data-output.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Drexel University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Joe Kopena (tjkopena@cs.drexel.edu)
7 */
8
9#include "omnet-data-output.h"
10
11#include "data-calculator.h"
12#include "data-collector.h"
13
14#include "ns3/log.h"
15#include "ns3/nstime.h"
16
17#include <cstdlib>
18#include <fstream>
19
20using namespace ns3;
21
22NS_LOG_COMPONENT_DEFINE("OmnetDataOutput");
23
24//--------------------------------------------------------------
25//----------------------------------------------
32
37
38/* static */
41{
42 static TypeId tid = TypeId("ns3::OmnetDataOutput")
44 .SetGroupName("Stats")
45 .AddConstructor<OmnetDataOutput>();
46 return tid;
47}
48
49void
51{
52 NS_LOG_FUNCTION(this);
53
55 // end OmnetDataOutput::DoDispose
56}
57
58//----------------------------------------------
59
60inline bool
61isNumeric(const std::string& s)
62{
63 bool decimalPtSeen = false;
64 bool exponentSeen = false;
65 char last = '\0';
66
67 for (auto it = s.begin(); it != s.end(); it++)
68 {
69 if ((*it == '.' && decimalPtSeen) || (*it == 'e' && exponentSeen) ||
70 (*it == '-' && it != s.begin() && last != 'e'))
71 {
72 return false;
73 }
74 else if (*it == '.')
75 {
76 decimalPtSeen = true;
77 }
78 else if (*it == 'e')
79 {
80 exponentSeen = true;
81 decimalPtSeen = false;
82 }
83
84 last = *it;
85 }
86 return true;
87}
88
89void
91{
92 NS_LOG_FUNCTION(this << &dc);
93
94 std::ofstream scalarFile;
95 std::string fn = m_filePrefix + "-" + dc.GetRunLabel() + ".sca";
96 scalarFile.open(fn, std::ios_base::out);
97
98 /// @todo add timestamp to the runlevel
99 scalarFile << "run " << dc.GetRunLabel() << std::endl;
100 scalarFile << "attr experiment \"" << dc.GetExperimentLabel() << "\"" << std::endl;
101 scalarFile << "attr strategy \"" << dc.GetStrategyLabel() << "\"" << std::endl;
102 scalarFile << "attr measurement \"" << dc.GetInputLabel() << "\"" << std::endl;
103 scalarFile << "attr description \"" << dc.GetDescription() << "\"" << std::endl;
104
105 for (auto i = dc.MetadataBegin(); i != dc.MetadataEnd(); i++)
106 {
107 std::pair<std::string, std::string> blob = (*i);
108 scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\"" << std::endl;
109 }
110
111 scalarFile << std::endl;
112 if (isNumeric(dc.GetInputLabel()))
113 {
114 scalarFile << "scalar . measurement \"" << dc.GetInputLabel() << "\"" << std::endl;
115 }
116 for (auto i = dc.MetadataBegin(); i != dc.MetadataEnd(); i++)
117 {
118 std::pair<std::string, std::string> blob = (*i);
119 if (isNumeric(blob.second))
120 {
121 scalarFile << "scalar . \"" << blob.first << "\" \"" << blob.second << "\""
122 << std::endl;
123 }
124 }
125 OmnetOutputCallback callback(&scalarFile);
126
127 for (auto i = dc.DataCalculatorBegin(); i != dc.DataCalculatorEnd(); i++)
128 {
129 (*i)->Output(callback);
130 }
131
132 scalarFile << std::endl << std::endl;
133 scalarFile.close();
134
135 // end OmnetDataOutput::Output
136}
137
139 : m_scalar(scalar)
140{
141 NS_LOG_FUNCTION(this << scalar);
142}
143
144void
146 std::string name,
147 const StatisticalSummary* statSum)
148{
149 NS_LOG_FUNCTION(this << context << name << statSum);
150
151 if (context.empty())
152 {
153 context = ".";
154 }
155 if (name.empty())
156 {
157 name = "\"\"";
158 }
159 (*m_scalar) << "statistic " << context << " " << name << std::endl;
160 if (!std::isnan(statSum->getCount()))
161 {
162 (*m_scalar) << "field count " << statSum->getCount() << std::endl;
163 }
164 if (!std::isnan(statSum->getSum()))
165 {
166 (*m_scalar) << "field sum " << statSum->getSum() << std::endl;
167 }
168 if (!std::isnan(statSum->getMean()))
169 {
170 (*m_scalar) << "field mean " << statSum->getMean() << std::endl;
171 }
172 if (!std::isnan(statSum->getMin()))
173 {
174 (*m_scalar) << "field min " << statSum->getMin() << std::endl;
175 }
176 if (!std::isnan(statSum->getMax()))
177 {
178 (*m_scalar) << "field max " << statSum->getMax() << std::endl;
179 }
180 if (!std::isnan(statSum->getSqrSum()))
181 {
182 (*m_scalar) << "field sqrsum " << statSum->getSqrSum() << std::endl;
183 }
184 if (!std::isnan(statSum->getStddev()))
185 {
186 (*m_scalar) << "field stddev " << statSum->getStddev() << std::endl;
187 }
188}
189
190void
192 std::string name,
193 int val)
194{
195 NS_LOG_FUNCTION(this << context << name << val);
196
197 if (context.empty())
198 {
199 context = ".";
200 }
201 if (name.empty())
202 {
203 name = "\"\"";
204 }
205 (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
206 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
207}
208
209void
211 std::string name,
212 uint32_t val)
213{
214 NS_LOG_FUNCTION(this << context << name << val);
215
216 if (context.empty())
217 {
218 context = ".";
219 }
220 if (name.empty())
221 {
222 name = "\"\"";
223 }
224 (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
225 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
226}
227
228void
230 std::string name,
231 double val)
232{
233 NS_LOG_FUNCTION(this << context << name << val);
234
235 if (context.empty())
236 {
237 context = ".";
238 }
239 if (name.empty())
240 {
241 name = "\"\"";
242 }
243 (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
244 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
245}
246
247void
249 std::string name,
250 std::string val)
251{
252 NS_LOG_FUNCTION(this << context << name << val);
253
254 if (context.empty())
255 {
256 context = ".";
257 }
258 if (name.empty())
259 {
260 name = "\"\"";
261 }
262 (*m_scalar) << "scalar " << context << " " << name << " " << val << std::endl;
263 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
264}
265
266void
268 std::string name,
269 Time val)
270{
271 NS_LOG_FUNCTION(this << context << name << val);
272
273 if (context.empty())
274 {
275 context = ".";
276 }
277 if (name.empty())
278 {
279 name = "\"\"";
280 }
281 (*m_scalar) << "scalar " << context << " " << name << " " << val.GetTimeStep() << std::endl;
282 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton
283}
Collects data.
std::string GetExperimentLabel() const
Return the experiment label.
DataCalculatorList::iterator DataCalculatorBegin()
Returns an iterator to the beginning of the DataCalculator list.
DataCalculatorList::iterator DataCalculatorEnd()
Returns an iterator to the past-the-end of the DataCalculator list.
std::string GetDescription() const
Return the description label.
MetadataList::iterator MetadataBegin()
Returns an iterator to the beginning of the metadata list.
MetadataList::iterator MetadataEnd()
Returns an iterator to the past-the-end of the metadata list.
std::string GetStrategyLabel() const
Return the strategy label.
std::string GetRunLabel() const
Return the runID label.
std::string GetInputLabel() const
Return the input label.
Abstract Data Output Interface class s.
void DoDispose() override
Destructor implementation.
std::string m_filePrefix
File prefix for the DataOutputInterface.
Class to generate OMNeT output.
OmnetOutputCallback(std::ostream *scalar)
Constructor.
void OutputSingleton(std::string context, std::string name, int val) override
Generates a single data output.
void OutputStatistic(std::string context, std::string name, const StatisticalSummary *statSum) override
Generates data statistics.
Outputs data in a format compatible with OMNeT library and framework.
void DoDispose() override
Destructor implementation.
void Output(DataCollector &dc) override
Outputs information from the provided DataCollector.
static TypeId GetTypeId()
Register this type.
Abstract class for calculating statistical data.
virtual double getMax() const =0
Returns the maximum of the values.
virtual double getMean() const =0
Returns the mean of the (weighted) observations.
virtual double getStddev() const =0
Returns the standard deviation of the (weighted) observations.
virtual long getCount() const =0
Returns the number of observations.
virtual double getMin() const =0
Returns the minimum of the values.
virtual double getSum() const =0
virtual double getSqrSum() const =0
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:434
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool isNumeric(const std::string &s)