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