A Discrete-Event Network Simulator
API
 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
36 {
37  static TypeId tid = TypeId ("ns3::GnuplotAggregator")
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 
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
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
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
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.
185 
186  // Update the gnuplot, too.
187  m_gnuplot.SetTerminal (terminal);
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.
237 }
238 
239 void
241 {
242  NS_LOG_FUNCTION (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
277 {
278  NS_LOG_FUNCTION (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
297 {
298  NS_LOG_FUNCTION (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
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 
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:749
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Base class for data collection framework objects.
void Write2dWithXErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x direction.
std::map< std::string, Gnuplot2dDataset > m_2dDatasetMap
Maps context strings to 2D datasets.
#define NS_ABORT_MSG(msg)
Abnormal program termination.
Definition: abort.h:44
GnuplotAggregator(const std::string &outputFileNameWithoutExtension)
Class to represent a 2D points plot.
Definition: gnuplot.h:113
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
std::string m_graphicsFileName
The graphics file name with its extension.
Gnuplot m_gnuplot
Used to create gnuplot files.
void Write2dWithXYErrorDelta(std::string context, double x, double y, double xErrorDelta, double yErrorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x and y directions.
static void SetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Definition: gnuplot.cc:147
void Add2dDataset(const std::string &dataset, const std::string &title)
Adds a 2D dataset to the plot.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
static void Set2dDatasetDefaultStyle(enum Gnuplot2dDataset::Style style)
Change default style for all newly created objects.
void Write2d(std::string context, double x, double y)
Writes a 2D value to a 2D gnuplot dataset.
void SetTitle(const std::string &title)
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
void Set2dDatasetStyle(const std::string &dataset, enum Gnuplot2dDataset::Style style)
Set the style of plotting to use for this dataset.
void Write2dDatasetEmptyLine(const std::string &dataset)
Add an empty line in the data output sequence.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
void AppendExtra(const std::string &extra)
void Set2dDatasetExtra(const std::string &dataset, const std::string &extra)
Add extra formatting parameters to this dataset.
void SetKeyLocation(enum KeyLocation keyLocation)
Set the location of the key in the plot.
void SetTerminal(const std::string &terminal)
static void Set2dDatasetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:703
bool m_xAndYLegendsSet
Set equal to true after setting the x and y legends.
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
void SetExtra(const std::string &extra)
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
bool m_enabled
Object's activation state.
static TypeId GetTypeId()
Get the type ID.
bool m_titleSet
Set equal to true after setting the title.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
std::string m_outputFileNameWithoutExtension
The output file name without any extension.
static void SetDefaultErrorBars(enum ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition: gnuplot.cc:348
static void SetDefaultStyle(enum Style style)
Change default style for all newly created objects.
Definition: gnuplot.cc:337
Style
The plotting style to use for this dataset.
Definition: gnuplot.h:119
ErrorBars
Whether errorbars should be used for this dataset.
Definition: gnuplot.h:133
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
static void Set2dDatasetDefaultErrorBars(enum Gnuplot2dDataset::ErrorBars errorBars)
Change default errorbars style for all newly created objects.
void Write2dWithYErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the y direction.
KeyLocation
The location of the key in the plot.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:203
void Set2dDatasetErrorBars(const std::string &dataset, enum Gnuplot2dDataset::ErrorBars errorBars)
Set the error bars to use for this dataset.
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610