From 5e975db36e1cb9aef0155876e780233004b0c710 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 12:34:26 +0200 Subject: [PATCH] a bunch of new DefaultValue subclasses --- src/core/random-variable-default-value.cc | 119 ++++++++++++++++++++++ src/core/random-variable-default-value.h | 50 +++++++++ src/core/rectangle-default-value.cc | 106 +++++++++++++++++++ src/core/rectangle-default-value.h | 58 +++++++++++ 4 files changed, 333 insertions(+) create mode 100644 src/core/random-variable-default-value.cc create mode 100644 src/core/random-variable-default-value.h create mode 100644 src/core/rectangle-default-value.cc create mode 100644 src/core/rectangle-default-value.h diff --git a/src/core/random-variable-default-value.cc b/src/core/random-variable-default-value.cc new file mode 100644 index 000000000..df36d02ba --- /dev/null +++ b/src/core/random-variable-default-value.cc @@ -0,0 +1,119 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#include "random-variable-default-value.h" + +namespace ns3 { + +RandomVariableDefaultValue::RandomVariableDefaultValue (std::string name, + std::string help, + std::string defaultValue) + : DefaultValueBase (name, help), + m_defaultValue (defaultValue), + m_value (defaultValue) +{ + if (!Parse (defaultValue, false, 0)) + { + NS_FATAL_ERROR ("Invalid Random Variable specification: " << defaultValue); + } + DefaultValueList::Add (this); +} + +RandomVariable * +RandomVariableDefaultValue::GetCopy (void) +{ + RandomVariable *variable; + bool ok = Parse (m_value, true, &variable); + NS_ASSERT (ok); + return variable; +} +double +RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) +{ + double v; + std::istringstream iss; + iss.str (value); + iss >> v; + ok = !iss.bad () && !iss.fail (); + return v; +} +bool +RandomVariableDefaultValue::Parse (const std::string &value, + bool mustCreate, RandomVariable **pVariable) +{ + std::string::size_type pos = value.find_first_of(":"); + if (pos == std::string::npos) + { + return false; + } + bool ok; + std::string type = value.substr (0, pos); + std::string v = value.substr (pos+1, std::string::npos); + if (type == "Constant") + { + double constant = ReadAsDouble (v, ok); + if (mustCreate) + { + *pVariable = new ConstantVariable (constant); + } + return ok; + } + else if (type == "Uniform") + { + std::string::size_type maxPos = v.find_first_of(":"); + if (maxPos == std::string::npos) + { + return false; + } + std::string min = v.substr (0, maxPos); + std::string max = v.substr (maxPos + 1, std::string::npos); + double minVal; + double maxVal; + minVal = ReadAsDouble (min, ok); + maxVal = ReadAsDouble (max, ok); + if (mustCreate) + { + *pVariable = new UniformVariable (minVal, maxVal); + } + return ok; + } + else + { + // XXX parse other types of distributions. + return false; + } +} +bool +RandomVariableDefaultValue::DoParseValue (const std::string &value) +{ + return Parse (value, false, 0); +} +std::string +RandomVariableDefaultValue::DoGetType (void) const +{ + return "(Uniform:min:max|Constant:cst)"; +} +std::string +RandomVariableDefaultValue::DoGetDefaultValue (void) const +{ + return m_defaultValue; +} + +} // namespace ns3 diff --git a/src/core/random-variable-default-value.h b/src/core/random-variable-default-value.h new file mode 100644 index 000000000..782c62331 --- /dev/null +++ b/src/core/random-variable-default-value.h @@ -0,0 +1,50 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef RANDOM_VARIABLE_DEFAULT_VALUE_H +#define RANDOM_VARIABLE_DEFAULT_VALUE_H + +#include "random-variable.h" +#include "default-value.h" + +namespace ns3 { + +class RandomVariableDefaultValue : public DefaultValueBase +{ + public: + RandomVariableDefaultValue (std::string name, + std::string help, + std::string defaultValue); + + RandomVariable *GetCopy (void); +private: + bool Parse (const std::string &value, bool mustCreate, RandomVariable **pVariable); + double ReadAsDouble (const std::string value, bool &ok); + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + std::string m_defaultValue; + std::string m_value; +}; + +} // namespace ns3 + +#endif /* RANDOM_VARIABLE_DEFAULT_VALUE_H */ diff --git a/src/core/rectangle-default-value.cc b/src/core/rectangle-default-value.cc new file mode 100644 index 000000000..96fa63be7 --- /dev/null +++ b/src/core/rectangle-default-value.cc @@ -0,0 +1,106 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ + +#include "rectangle-default-value.h" + +namespace ns3 { + +RectangleDefaultValue::RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax) + : DefaultValueBase (name, help), + m_xMinDefault (xMin), + m_xMaxDefault (xMax), + m_yMinDefault (yMin), + m_yMaxDefault (yMax), + m_xMin (xMin), + m_xMax (xMax), + m_yMin (yMin), + m_yMax (yMax) +{ + DefaultValueList::Add (this); +} + +double +RectangleDefaultValue::GetMinX (void) const +{ + return m_xMin; +} +double +RectangleDefaultValue::GetMinY (void) const +{ + return m_yMin; +} +double +RectangleDefaultValue::GetMaxX (void) const +{ + return m_xMax; +} +double +RectangleDefaultValue::GetMaxY (void) const +{ + return m_yMax; +} +bool +RectangleDefaultValue::DoParseValue (const std::string &value) +{ + std::string::size_type xMinStart = 0; + std::string::size_type xMinEnd = value.find_first_of(":", xMinStart); + std::string::size_type xMaxStart = xMinEnd + 1; + std::string::size_type xMaxEnd = value.find_first_of(":", xMaxStart); + std::string::size_type yMinStart = xMaxEnd + 1; + std::string::size_type yMinEnd = value.find_first_of(":", yMinStart); + std::string::size_type yMaxStart = yMinEnd + 1; + std::string::size_type yMaxEnd = std::string::npos; + + std::string xMinString = value.substr (xMinStart, xMinEnd); + std::string xMaxString = value.substr (xMaxStart, xMaxEnd); + std::string yMinString = value.substr (yMinStart, yMinEnd); + std::string yMaxString = value.substr (yMaxStart, yMaxEnd); + + std::istringstream iss; + iss.str (xMinString); + iss >> m_xMin; + iss.str (xMaxString); + iss >> m_xMax; + iss.str (yMinString); + iss >> m_yMin; + iss.str (yMaxString); + iss >> m_yMax; + + return !iss.bad () && !iss.fail (); +} +std::string +RectangleDefaultValue::DoGetType (void) const +{ + return "(xMin:xMax:yMin:yMax)"; +} +std::string +RectangleDefaultValue::DoGetDefaultValue (void) const +{ + std::ostringstream oss; + oss << m_xMinDefault << ":" << m_xMaxDefault << ":" << m_yMinDefault << ":" << m_yMaxDefault; + return oss.str (); +} + + +} // namespace ns3 diff --git a/src/core/rectangle-default-value.h b/src/core/rectangle-default-value.h new file mode 100644 index 000000000..f81388780 --- /dev/null +++ b/src/core/rectangle-default-value.h @@ -0,0 +1,58 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef RECTANGLE_DEFAULT_VALUE_H +#define RECTANGLE_DEFAULT_VALUE_H + +#include +#include "default-value.h" + +namespace ns3 { + +class RectangleDefaultValue : public DefaultValueBase +{ + public: + RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax); + + double GetMinX (void) const; + double GetMinY (void) const; + double GetMaxX (void) const; + double GetMaxY (void) const; + private: + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + double m_xMinDefault; + double m_xMaxDefault; + double m_yMinDefault; + double m_yMaxDefault; + double m_xMin; + double m_xMax; + double m_yMin; + double m_yMax; +}; + +} // namespace ns3 + +#endif /* RECTANGLE_DEFAULT_VALUE_H */