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 
56  OMPL_ERROR("Parameter '%s' was not found", key.c_str());
57  return false;
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 (const auto &it : kv)
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  auto 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 (const auto &param : params_)
90  params.push_back(param.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  auto it = params_.find(key);
111  if (it != params_.end())
112  return it->second;
113  return empty;
114 }
115 
116 void ompl::base::ParamSet::getParams(std::map<std::string, std::string> &params) const
117 {
118  for (const auto &param : params_)
119  params[param.first] = param.second->getValue();
120 }
121 
122 bool ompl::base::ParamSet::hasParam(const std::string &key) const
123 {
124  return params_.find(key) != params_.end();
125 }
126 
128 {
129  if (!hasParam(key))
130  throw Exception("Parameter '%s' is not defined", key);
131  return *getParam(key);
132 }
133 
134 void ompl::base::ParamSet::include(const ParamSet &other, const std::string &prefix)
135 {
136  const std::map<std::string, GenericParamPtr> &p = other.getParams();
137  if (prefix.empty())
138  for (const auto &it : p)
139  params_[it.first] = it.second;
140  else
141  for (const auto &it : p)
142  params_[prefix + "." + it.first] = it.second;
143 }
144 
145 void ompl::base::ParamSet::add(const GenericParamPtr &param)
146 {
147  params_[param->getName()] = param;
148 }
149 
150 void ompl::base::ParamSet::remove(const std::string &name)
151 {
152  params_.erase(name);
153 }
154 
156 {
157  params_.clear();
158 }
159 
160 void ompl::base::ParamSet::print(std::ostream &out) const
161 {
162  for (const auto &param : params_)
163  out << param.first << " = " << param.second->getValue() << std::endl;
164 }
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....
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()