All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GenericParam.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_BASE_GENERIC_PARAM_
38 #define OMPL_BASE_GENERIC_PARAM_
39 
40 #include "ompl/util/Console.h"
41 #include "ompl/util/ClassForward.h"
42 #include <boost/function.hpp>
43 #include <boost/lexical_cast.hpp>
44 #include <iostream>
45 #include <string>
46 #include <vector>
47 #include <map>
48 
49 namespace ompl
50 {
51  namespace base
52  {
53 
55 
56  OMPL_CLASS_FORWARD(GenericParam);
58 
65  {
66  public:
67 
69  GenericParam(const std::string &name) : name_(name)
70  {
71  }
72 
73  virtual ~GenericParam(void)
74  {
75  }
76 
78  const std::string& getName(void) const
79  {
80  return name_;
81  }
82 
84  void setName(const std::string &name)
85  {
86  name_ = name;
87  }
88 
90  virtual bool setValue(const std::string &value) = 0;
91 
93  virtual std::string getValue(void) const = 0;
94 
96  template<typename T>
97  GenericParam& operator=(const T &value)
98  {
99  try
100  {
101  setValue(boost::lexical_cast<std::string>(value));
102  }
103  catch (boost::bad_lexical_cast &e)
104  {
105  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
106  }
107  return *this;
108  }
109 
111  void setRangeSuggestion(const std::string &rangeSuggestion)
112  {
113  rangeSuggestion_ = rangeSuggestion;
114  }
115 
117  const std::string& getRangeSuggestion(void) const
118  {
119  return rangeSuggestion_;
120  }
121 
122  protected:
123 
125  std::string name_;
126 
140  std::string rangeSuggestion_;
141  };
142 
143 
145  template<typename T>
147  {
148  public:
149 
151  typedef boost::function<void(T)> SetterFn;
152 
154  typedef boost::function<T()> GetterFn;
155 
158  SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
159  GenericParam(name), setter_(setter), getter_(getter)
160  {
161  if (!setter_)
162  OMPL_ERROR("Setter function must be specified for parameter");
163  }
164 
165  virtual ~SpecificParam(void)
166  {
167  }
168 
169  virtual bool setValue(const std::string &value)
170  {
171  bool result = true;
172  try
173  {
174  if (setter_)
175  setter_(boost::lexical_cast<T>(value));
176  }
177  catch (boost::bad_lexical_cast &e)
178  {
179  result = false;
180  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
181  }
182 
183  if (getter_)
184  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
185  else
186  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
187  return result;
188  }
189 
190  virtual std::string getValue(void) const
191  {
192  if (getter_)
193  try
194  {
195  return boost::lexical_cast<std::string>(getter_());
196  }
197  catch (boost::bad_lexical_cast &e)
198  {
199  OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
200  return "";
201  }
202  else
203  return "";
204  }
205 
206  protected:
207 
210 
213  };
214 
216  class ParamSet
217  {
218  public:
219 
221  template<typename T>
222  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
223  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
224  {
225  params_[name].reset(new SpecificParam<T>(name, setter, getter));
226  }
227 
229  void add(const GenericParamPtr &param);
230 
232  void remove(const std::string &name);
233 
235  void include(const ParamSet &other, const std::string &prefix = "");
236 
250  bool setParam(const std::string &key, const std::string &value);
251 
253  bool getParam(const std::string &key, std::string &value) const;
254 
260  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
261 
263  void getParams(std::map<std::string, std::string> &params) const;
264 
266  void getParamNames(std::vector<std::string> &params) const;
267 
269  void getParamValues(std::vector<std::string> &vals) const;
270 
272  const std::map<std::string, GenericParamPtr>& getParams(void) const;
273 
275  const GenericParamPtr& getParam(const std::string &key) const;
276 
278  bool hasParam(const std::string &key) const;
279 
281  GenericParam& operator[](const std::string &key);
282 
284  std::size_t size(void) const
285  {
286  return params_.size();
287  }
288 
290  void clear(void);
291 
293  void print(std::ostream &out) const;
294 
295  private:
296 
297  std::map<std::string, GenericParamPtr> params_;
298  };
299  }
300 }
301 
302 #endif
void print(std::ostream &out) const
Print the parameters to a stream.
bool hasParam(const std::string &key) const
Check whether this set of parameters includes the parameter named key.
boost::function< void(T)> SetterFn
The type for the 'setter' function for this parameter.
Definition: GenericParam.h:151
std::string name_
The name of the parameter.
Definition: GenericParam.h:125
void include(const ParamSet &other, const std::string &prefix="")
Include the params of a different ParamSet into this one. Optionally include a prefix for each of the...
const std::string & getName(void) const
Get the name of the parameter.
Definition: GenericParam.h:78
GenericParam(const std::string &name)
The constructor of a parameter takes the name of the parameter (name)
Definition: GenericParam.h:69
virtual std::string getValue(void) const =0
Retrieve the value of the parameter, as a string.
Motion planning algorithms often employ parameters to guide their exploration process. (e.g., goal biasing). Motion planners (and some of their components) use this class to declare what the parameters are, in a generic way, so that they can be set externally.
Definition: GenericParam.h:64
virtual bool setValue(const std::string &value)
Set the value of the parameter. The value is taken in as a string, but converted to the type of that ...
Definition: GenericParam.h:169
void getParamNames(std::vector< std::string > &params) const
List the names of the known parameters.
bool setParams(const std::map< std::string, std::string > &kv, bool ignoreUnknown=false)
Set the values for a set of parameters. The parameter names are the keys in the map kv...
bool getParam(const std::string &key, std::string &value) const
Get the value of the parameter named key. Store the value as string in value and return true if the p...
This is a helper class that instantiates parameters with different data types.
Definition: GenericParam.h:146
Maintain a set of parameters.
Definition: GenericParam.h:216
bool setParam(const std::string &key, const std::string &value)
Algorithms in OMPL often have parameters that can be set externally. While each algorithm will have t...
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
void getParamValues(std::vector< std::string > &vals) const
List the values of the known parameters, in the same order as getParamNames()
SetterFn setter_
The setter function for this parameter.
Definition: GenericParam.h:209
std::size_t size(void) const
Get the number of parameters maintained by this instance.
Definition: GenericParam.h:284
GenericParam & operator[](const std::string &key)
Access operator for parameters, by name. If the parameter is not defined, an exception is thrown...
void setName(const std::string &name)
Set the name of the parameter.
Definition: GenericParam.h:84
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
GenericParam & operator=(const T &value)
Assignment operator by type. This is just for convenience, as it just calls setValue() ...
Definition: GenericParam.h:97
const std::string & getRangeSuggestion(void) const
Get the suggested range of values.
Definition: GenericParam.h:117
#define OMPL_DEBUG(fmt,...)
Log a formatted debugging string.
Definition: Console.h:70
const std::map< std::string, GenericParamPtr > & getParams(void) const
Get the map from parameter names to parameter descriptions.
void declareParam(const std::string &name, const typename SpecificParam< T >::SetterFn &setter, const typename SpecificParam< T >::GetterFn &getter=typename SpecificParam< T >::GetterFn())
This function declares a parameter name, and specifies the setter and getter functions.
Definition: GenericParam.h:222
boost::function< T()> GetterFn
The type for the 'getter' function for this parameter.
Definition: GenericParam.h:154
void add(const GenericParamPtr &param)
Add a parameter to the set.
void setRangeSuggestion(const std::string &rangeSuggestion)
Set a suggested range.
Definition: GenericParam.h:111
void clear(void)
Clear all the set parameters.
std::string rangeSuggestion_
Suggested range for the parameter.
Definition: GenericParam.h:140
SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter=GetterFn())
An explicit instantiation of a parameter name requires the setter function and optionally the getter ...
Definition: GenericParam.h:158
virtual bool setValue(const std::string &value)=0
Set the value of the parameter. The value is taken in as a string, but converted to the type of that ...
GetterFn getter_
The getter function for this parameter.
Definition: GenericParam.h:212
virtual std::string getValue(void) const
Retrieve the value of the parameter, as a string.
Definition: GenericParam.h:190