A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
gnuplot-aggregator.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013 University of Washington
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: Mitch Watrous (watrous@u.washington.edu)
19
*/
20
21
#include <iostream>
22
#include <fstream>
23
#include <string>
24
25
#include "
gnuplot-aggregator.h
"
26
#include "ns3/abort.h"
27
#include "ns3/log.h"
28
29
namespace
ns3 {
30
31
NS_LOG_COMPONENT_DEFINE
(
"GnuplotAggregator"
);
32
NS_OBJECT_ENSURE_REGISTERED
(GnuplotAggregator);
33
34
TypeId
35
GnuplotAggregator::GetTypeId
()
36
{
37
static
TypeId
tid =
TypeId
(
"ns3::GnuplotAggregator"
)
38
.
SetParent
<
DataCollectionObject
> ()
39
;
40
41
return
tid;
42
}
43
44
GnuplotAggregator::GnuplotAggregator
(
const
std::string &outputFileNameWithoutExtension)
45
: m_outputFileNameWithoutExtension (outputFileNameWithoutExtension),
46
m_graphicsFileName (m_outputFileNameWithoutExtension +
".png"
),
47
m_title (
"Data Values"
),
48
m_xLegend (
"X Values"
),
49
m_yLegend (
"Y Values"
),
50
m_titleSet (false),
51
m_xAndYLegendsSet (false),
52
m_gnuplot (m_graphicsFileName)
53
{
54
NS_LOG_FUNCTION
(
this
);
55
}
56
57
GnuplotAggregator::~GnuplotAggregator
()
58
{
59
NS_LOG_FUNCTION
(
this
);
60
if
(!
m_titleSet
)
61
{
62
NS_LOG_WARN
(
"Warning: The plot title was not set for the gnuplot aggregator"
);
63
}
64
if
(!
m_xAndYLegendsSet
)
65
{
66
NS_LOG_WARN
(
"Warning: The axis legends were not set for the gnuplot aggregator"
);
67
}
68
69
std::string dataFileName =
m_outputFileNameWithoutExtension
+
".dat"
;
70
std::string plotFileName =
m_outputFileNameWithoutExtension
+
".plt"
;
71
std::string scriptFileName =
m_outputFileNameWithoutExtension
+
".sh"
;
72
73
// Open the gnuplot plot and data files.
74
std::ofstream plotFile;
75
plotFile.open (plotFileName.c_str ());
76
std::ofstream dataFile;
77
dataFile.open (dataFileName.c_str ());
78
79
// Skip any NaN's that appear in data.
80
m_gnuplot
.
AppendExtra
(
"set datafile missing \"-nan\""
);
81
82
// Write the gnuplot plot and data files.
83
m_gnuplot
.
GenerateOutput
(plotFile, dataFile, dataFileName);
84
85
// Close the gnuplot plot and data files.
86
plotFile.close ();
87
dataFile.close ();
88
89
// Open the shell script file.
90
std::ofstream scriptFile;
91
scriptFile.open (scriptFileName.c_str ());
92
93
// Write the shell script file.
94
scriptFile <<
"#!/bin/sh"
<< std::endl;
95
scriptFile << std::endl;
96
scriptFile <<
"gnuplot "
<< plotFileName << std::endl;
97
98
// Close the shell script file.
99
scriptFile.close ();
100
}
101
102
void
103
GnuplotAggregator::Write2d
(std::string context,
double
x
,
double
y)
104
{
105
NS_LOG_FUNCTION
(
this
<< context << x << y);
106
107
if
(
m_2dDatasetMap
.count (context) == 0)
108
{
109
NS_ABORT_MSG
(
"Dataset "
<< context <<
" has not been added"
);
110
}
111
112
if
(
m_enabled
)
113
{
114
// Add this 2D data point to its dataset.
115
m_2dDatasetMap
[context].Add (x, y);
116
}
117
}
118
119
void
120
GnuplotAggregator::Write2dWithXErrorDelta
(std::string context,
121
double
x
,
122
double
y,
123
double
errorDelta)
124
{
125
NS_LOG_FUNCTION
(
this
<< context << x << y << errorDelta);
126
127
if
(
m_2dDatasetMap
.count (context) == 0)
128
{
129
NS_ABORT_MSG
(
"Dataset "
<< context <<
" has not been added"
);
130
}
131
132
if
(
m_enabled
)
133
{
134
// Add this 2D data point with its error bar to its dataset.
135
m_2dDatasetMap
[context].Add (x, y, errorDelta);
136
}
137
}
138
139
void
140
GnuplotAggregator::Write2dWithYErrorDelta
(std::string context,
141
double
x
,
142
double
y,
143
double
errorDelta)
144
{
145
NS_LOG_FUNCTION
(
this
<< context << x << y << errorDelta);
146
147
if
(
m_2dDatasetMap
.count (context) == 0)
148
{
149
NS_ABORT_MSG
(
"Dataset "
<< context <<
" has not been added"
);
150
}
151
152
if
(
m_enabled
)
153
{
154
// Add this 2D data point with its error bar to its dataset.
155
m_2dDatasetMap
[context].Add (x, y, errorDelta);
156
}
157
}
158
159
void
160
GnuplotAggregator::Write2dWithXYErrorDelta
(std::string context,
161
double
x
,
162
double
y,
163
double
xErrorDelta,
164
double
yErrorDelta)
165
{
166
NS_LOG_FUNCTION
(
this
<< context << x << y << xErrorDelta << yErrorDelta);
167
168
if
(
m_2dDatasetMap
.count (context) == 0)
169
{
170
NS_ABORT_MSG
(
"Dataset "
<< context <<
" has not been added"
);
171
}
172
173
if
(
m_enabled
)
174
{
175
// Add this 2D data point with its error bar to its dataset.
176
m_2dDatasetMap
[context].Add (x, y, xErrorDelta, yErrorDelta);
177
}
178
}
179
180
void
181
GnuplotAggregator::SetTerminal
(
const
std::string &terminal)
182
{
183
// Change the extension for the graphics file.
184
m_graphicsFileName
=
m_outputFileNameWithoutExtension
+
"."
+ terminal;
185
186
// Update the gnuplot, too.
187
m_gnuplot
.
SetTerminal
(terminal);
188
m_gnuplot
.
SetOutputFilename
(
m_graphicsFileName
);
189
}
190
191
void
192
GnuplotAggregator::SetTitle
(
const
std::string &title)
193
{
194
NS_LOG_FUNCTION
(
this
<< title);
195
m_gnuplot
.
SetTitle
(title);
196
m_titleSet
=
true
;
197
}
198
199
void
200
GnuplotAggregator::SetLegend
(
const
std::string &xLegend,
const
std::string &yLegend)
201
{
202
NS_LOG_FUNCTION
(
this
<< xLegend << yLegend);
203
m_gnuplot
.
SetLegend
(xLegend, yLegend);
204
m_xAndYLegendsSet
=
true
;
205
}
206
207
void
208
GnuplotAggregator::SetExtra
(
const
std::string &extra)
209
{
210
NS_LOG_FUNCTION
(
this
<< extra);
211
m_gnuplot
.
SetExtra
(extra);
212
}
213
214
void
215
GnuplotAggregator::AppendExtra
(
const
std::string &extra)
216
{
217
NS_LOG_FUNCTION
(
this
<< extra);
218
m_gnuplot
.
AppendExtra
(extra);
219
}
220
221
void
222
GnuplotAggregator::Add2dDataset
(
const
std::string &dataset,
const
std::string &title)
223
{
224
NS_LOG_FUNCTION
(
this
<< dataset << title);
225
226
if
(
m_2dDatasetMap
.count (dataset) > 0)
227
{
228
NS_ABORT_MSG
(
"Dataset "
<< dataset <<
" has already been added"
);
229
}
230
231
// Add this dataset to the map so that its values can be saved.
232
Gnuplot2dDataset
gnuplot2dDataset (title);
233
m_2dDatasetMap
[dataset] = gnuplot2dDataset;
234
235
// Add this dataset to the plot so that its values can be plotted.
236
m_gnuplot
.
AddDataset
(
m_2dDatasetMap
[dataset]);
237
}
238
239
void
240
GnuplotAggregator::Set2dDatasetDefaultExtra
(
const
std::string &extra)
241
{
242
NS_LOG_FUNCTION
(extra);
243
Gnuplot2dDataset::SetDefaultExtra
(extra);
244
}
245
246
void
247
GnuplotAggregator::Set2dDatasetExtra
(
const
std::string &dataset,
const
std::string &extra)
248
{
249
NS_LOG_FUNCTION
(
this
<< dataset << extra);
250
if
(
m_2dDatasetMap
.count (dataset) == 0)
251
{
252
NS_ABORT_MSG
(
"Dataset "
<< dataset <<
" has not been added"
);
253
}
254
255
// Set the extra parameters for the dataset.
256
m_2dDatasetMap
[dataset].SetExtra (extra);
257
}
258
259
void
260
GnuplotAggregator::Write2dDatasetEmptyLine
(
const
std::string &dataset)
261
{
262
NS_LOG_FUNCTION
(
this
<< dataset);
263
if
(
m_2dDatasetMap
.count (dataset) == 0)
264
{
265
NS_ABORT_MSG
(
"Dataset "
<< dataset <<
" has not been added"
);
266
}
267
268
if
(
m_enabled
)
269
{
270
// Add an empty line to the dataset.
271
m_2dDatasetMap
[dataset].AddEmptyLine ();
272
}
273
}
274
275
void
276
GnuplotAggregator::Set2dDatasetDefaultStyle
(
enum
Gnuplot2dDataset::Style
style)
277
{
278
NS_LOG_FUNCTION
(style);
279
Gnuplot2dDataset::SetDefaultStyle
(style);
280
}
281
282
void
283
GnuplotAggregator::Set2dDatasetStyle
(
const
std::string &dataset,
enum
Gnuplot2dDataset::Style
style)
284
{
285
NS_LOG_FUNCTION
(
this
<< dataset << style);
286
if
(
m_2dDatasetMap
.count (dataset) == 0)
287
{
288
NS_ABORT_MSG
(
"Dataset "
<< dataset <<
" has not been added"
);
289
}
290
291
// Set the style for the dataset.
292
m_2dDatasetMap
[dataset].SetStyle (style);
293
}
294
295
void
296
GnuplotAggregator::Set2dDatasetDefaultErrorBars
(
enum
Gnuplot2dDataset::ErrorBars
errorBars)
297
{
298
NS_LOG_FUNCTION
(errorBars);
299
Gnuplot2dDataset::SetDefaultErrorBars
(errorBars);
300
}
301
302
void
303
GnuplotAggregator::Set2dDatasetErrorBars
(
const
std::string &dataset,
enum
Gnuplot2dDataset::ErrorBars
errorBars)
304
{
305
NS_LOG_FUNCTION
(
this
<< dataset << errorBars);
306
if
(
m_2dDatasetMap
.count (dataset) == 0)
307
{
308
NS_ABORT_MSG
(
"Dataset "
<< dataset <<
" has not been added"
);
309
}
310
311
// Set the error bars for the dataset.
312
m_2dDatasetMap
[dataset].SetErrorBars (errorBars);
313
}
314
315
void
316
GnuplotAggregator::SetKeyLocation
(
enum
GnuplotAggregator::KeyLocation
keyLocation)
317
{
318
NS_LOG_FUNCTION
(
this
<< keyLocation);
319
// Set the specifed key location.
320
switch
(keyLocation)
321
{
322
case
NO_KEY
:
323
m_gnuplot
.
AppendExtra
(
"set key off"
);
324
break
;
325
case
KEY_ABOVE
:
326
m_gnuplot
.
AppendExtra
(
"set key outside center above"
);
327
break
;
328
case
KEY_BELOW
:
329
m_gnuplot
.
AppendExtra
(
"set key outside center below"
);
330
break
;
331
default
:
332
m_gnuplot
.
AppendExtra
(
"set key inside"
);
333
break
;
334
}
335
}
336
337
}
// namespace ns3
338
src
stats
model
gnuplot-aggregator.cc
Generated on Fri Aug 30 2013 01:43:02 for ns-3 by
1.8.1.2