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 <functional>
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()
74  {
75  }
76 
78  const std::string& getName() 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() 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() const
118  {
119  return rangeSuggestion_;
120  }
121 
122  protected:
123 
126  template<typename T>
127  const std::string& maybeWrapBool(const std::string &value) const
128  {
129  return boost::is_same<T, bool>::value ? truthValueTo01Str(value) : value;
130  }
131 
133  std::string name_;
134 
148  std::string rangeSuggestion_;
149 
150  private:
152  static const std::string& truthValueTo01Str(const std::string &value);
153  };
154 
155 
157  template<typename T>
159  {
160  public:
161 
163  typedef std::function<void(T)> SetterFn;
164 
166  typedef std::function<T()> GetterFn;
167 
170  SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
171  GenericParam(name), setter_(setter), getter_(getter)
172  {
173  if (!setter_ && !getter_)
174  OMPL_ERROR("At least one setter or getter function must be specified for parameter");
175  }
176 
177  virtual ~SpecificParam()
178  {
179  }
180 
181  virtual bool setValue(const std::string &value)
182  {
183  bool result = true;
184  try
185  {
186  if (setter_)
187  setter_(boost::lexical_cast<T>(GenericParam::maybeWrapBool<T>(value)));
188  }
189  catch (boost::bad_lexical_cast &e)
190  {
191  result = false;
192  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
193  }
194 
195  if (getter_)
196  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
197  else
198  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
199  return result;
200  }
201 
202  virtual std::string getValue() const
203  {
204  if (getter_)
205  try
206  {
207  return boost::lexical_cast<std::string>(getter_());
208  }
209  catch (boost::bad_lexical_cast &e)
210  {
211  OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
212  return "";
213  }
214  else
215  return "";
216  }
217 
218  protected:
219 
221  SetterFn setter_;
222 
224  GetterFn getter_;
225  };
226 
228 
229  OMPL_CLASS_FORWARD(ParamSet);
231 
233  class ParamSet
234  {
235  public:
236 
238  template<typename T>
239  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
240  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
241  {
242  params_[name].reset(new SpecificParam<T>(name, setter, getter));
243  }
244 
246  void add(const GenericParamPtr &param);
247 
249  void remove(const std::string &name);
250 
252  void include(const ParamSet &other, const std::string &prefix = "");
253 
267  bool setParam(const std::string &key, const std::string &value);
268 
270  bool getParam(const std::string &key, std::string &value) const;
271 
277  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
278 
280  void getParams(std::map<std::string, std::string> &params) const;
281 
283  void getParamNames(std::vector<std::string> &params) const;
284 
286  void getParamValues(std::vector<std::string> &vals) const;
287 
289  const std::map<std::string, GenericParamPtr>& getParams() const;
290 
292  const GenericParamPtr& getParam(const std::string &key) const;
293 
295  bool hasParam(const std::string &key) const;
296 
298  GenericParam& operator[](const std::string &key);
299 
301  std::size_t size() const
302  {
303  return params_.size();
304  }
305 
307  void clear();
308 
310  void print(std::ostream &out) const;
311 
312  private:
313 
314  std::map<std::string, GenericParamPtr> params_;
315  };
316  }
317 }
318 
319 #endif
std::string name_
The name of the parameter.
Definition: GenericParam.h:133
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() 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
std::function< T()> GetterFn
The type for the &#39;getter&#39; function for this parameter.
Definition: GenericParam.h:166
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:181
std::size_t size() const
Get the number of parameters maintained by this instance.
Definition: GenericParam.h:301
virtual std::string getValue() const
Retrieve the value of the parameter, as a string.
Definition: GenericParam.h:202
This is a helper class that instantiates parameters with different data types.
Definition: GenericParam.h:158
Maintain a set of parameters.
Definition: GenericParam.h:233
std::function< void(T)> SetterFn
The type for the &#39;setter&#39; function for this parameter.
Definition: GenericParam.h:163
Main namespace. Contains everything in this library.
Definition: Cost.h:42
const std::string & maybeWrapBool(const std::string &value) const
Bool values such as "false" cannot be converted to bool using lexical_cast. We need to map those to "...
Definition: GenericParam.h:127
const std::string & getRangeSuggestion() const
Get the suggested range of values.
Definition: GenericParam.h:117
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
SetterFn setter_
The setter function for this parameter.
Definition: GenericParam.h:221
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
#define OMPL_DEBUG(fmt,...)
Log a formatted debugging string.
Definition: Console.h:70
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:239
const std::string & getName() const
Get the name of the parameter.
Definition: GenericParam.h:78
void setRangeSuggestion(const std::string &rangeSuggestion)
Set a suggested range.
Definition: GenericParam.h:111
std::string rangeSuggestion_
Suggested range for the parameter.
Definition: GenericParam.h:148
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:170
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:224