GenericParam.cpp
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 #include "ompl/base/GenericParam.h"
38 #include "ompl/util/Exception.h"
39 
40 const std::string& ompl::base::GenericParam::truthValueTo01Str(const std::string &value)
41 {
42  static const std::string falseValue = "0";
43  static const std::string trueValue = "1";
44  return (value.empty() || value == falseValue ||
45  value == "false" || value == "FALSE" || value == "False" || value == "f" || value == "F") ? falseValue : trueValue;
46 }
47 
48 bool ompl::base::ParamSet::setParam(const std::string &key, const std::string &value)
49 {
50  std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
51  if (it != params_.end())
52  return it->second->setValue(value);
53  else
54  {
55  OMPL_ERROR("Parameter '%s' was not found", key.c_str());
56  return false;
57  }
58 }
59 
60 bool ompl::base::ParamSet::setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown)
61 {
62  bool result = true;
63  for (std::map<std::string, std::string>::const_iterator it = kv.begin() ; it != kv.end() ; ++it)
64  {
65  if (ignoreUnknown)
66  if (!hasParam(it->first))
67  continue;
68  bool r = setParam(it->first, it->second);
69  result = result && r;
70  }
71  return result;
72 }
73 
74 bool ompl::base::ParamSet::getParam(const std::string &key, std::string &value) const
75 {
76  std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
77  if (it != params_.end())
78  {
79  value = it->second->getValue();
80  return true;
81  }
82  return false;
83 }
84 
85 void ompl::base::ParamSet::getParamNames(std::vector<std::string> &params) const
86 {
87  params.clear();
88  params.reserve(params_.size());
89  for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
90  params.push_back(it->first);
91 }
92 
93 void ompl::base::ParamSet::getParamValues(std::vector<std::string> &vals) const
94 {
95  std::vector<std::string> names;
96  getParamNames(names);
97  vals.resize(names.size());
98  for (std::size_t i = 0 ; i < names.size() ; ++i)
99  vals[i] = params_.find(names[i])->second->getValue();
100 }
101 
102 const std::map<std::string, ompl::base::GenericParamPtr>& ompl::base::ParamSet::getParams() const
103 {
104  return params_;
105 }
106 
107 const ompl::base::GenericParamPtr& ompl::base::ParamSet::getParam(const std::string &key) const
108 {
109  static GenericParamPtr empty;
110  std::map<std::string, GenericParamPtr>::const_iterator it = params_.find(key);
111  if (it != params_.end())
112  return it->second;
113  else
114  return empty;
115 }
116 
117 void ompl::base::ParamSet::getParams(std::map<std::string, std::string> &params) const
118 {
119  for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
120  params[it->first] = it->second->getValue();
121 }
122 
123 bool ompl::base::ParamSet::hasParam(const std::string &key) const
124 {
125  return params_.find(key) != params_.end();
126 }
127 
129 {
130  if (!hasParam(key))
131  throw Exception("Parameter '%s' is not defined", key.c_str());
132  return *getParam(key);
133 }
134 
135 void ompl::base::ParamSet::include(const ParamSet &other, const std::string &prefix)
136 {
137  const std::map<std::string, GenericParamPtr> &p = other.getParams();
138  if (prefix.empty())
139  for (std::map<std::string, GenericParamPtr>::const_iterator it = p.begin() ; it != p.end() ; ++it)
140  params_[it->first] = it->second;
141  else
142  for (std::map<std::string, GenericParamPtr>::const_iterator it = p.begin() ; it != p.end() ; ++it)
143  params_[prefix + "." + it->first] = it->second;
144 }
145 
146 void ompl::base::ParamSet::add(const GenericParamPtr &param)
147 {
148  params_[param->getName()] = param;
149 }
150 
151 void ompl::base::ParamSet::remove(const std::string &name)
152 {
153  params_.erase(name);
154 }
155 
157 {
158  params_.clear();
159 }
160 
161 void ompl::base::ParamSet::print(std::ostream &out) const
162 {
163  for (std::map<std::string, GenericParamPtr>::const_iterator it = params_.begin() ; it != params_.end() ; ++it)
164  out << it->first << " = " << it->second->getValue() << std::endl;
165 }
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.
const std::map< std::string, GenericParamPtr > & getParams() const
Get the map from parameter names to parameter descriptions.
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...
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:65
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...
Maintain a set of parameters.
Definition: GenericParam.h:234
void getParams(std::map< std::string, std::string > &params) const
Get the known parameter as a map from names to their values cast as string.
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...
void remove(const std::string &name)
Remove a parameter from the set.
#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()
GenericParam & operator[](const std::string &key)
Access operator for parameters, by name. If the parameter is not defined, an exception is thrown...
The exception type for ompl.
Definition: Exception.h:47
void add(const GenericParamPtr &param)
Add a parameter to the set.
void clear()
Clear all the set parameters.