bpp-core3  3.0.0
AbstractParametrizable.h
Go to the documentation of this file.
1 //
2 // File: AbstractParametrizable.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2009-03-29 09:10:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 19, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
42 #define BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
43 
44 
45 #include "Parametrizable.h"
46 
47 // From the STL:
48 #include <map>
49 
50 namespace bpp
51 {
63  public virtual Parametrizable
64 {
65 private:
67  std::string prefix_;
68 
69 public:
70  AbstractParametrizable(const std::string& prefix) : parameters_(), prefix_(prefix) {}
71 
73 
74 public:
75  bool hasParameter(const std::string& name) const { return parameters_.hasParameter(prefix_ + name); }
76 
77  const ParameterList& getParameters() const { return parameters_; }
78 
79  const Parameter& getParameter(const std::string& name) const
80  {
81  return parameters_.getParameter(prefix_ + name);
82  }
83 
84  const std::shared_ptr<Parameter>& getSharedParameter(const std::string& name) const
85  {
86  return parameters_.getSharedParameter(prefix_ + name);
87  }
88 
89  double getParameterValue(const std::string& name) const
90  {
91  return getParameter(name).getValue();
92  }
93 
94  void setAllParametersValues(const ParameterList& parameters)
95  {
97  fireParameterChanged(parameters);
98  }
99 
100  void setParameterValue(const std::string& name, double value)
101  {
102  parameters_.setParameterValue(prefix_ + name, value);
104  }
105 
106  void setParametersValues(const ParameterList& parameters)
107  {
108  parameters_.setParametersValues(parameters);
109  fireParameterChanged(parameters);
110  }
111 
112  bool matchParametersValues(const ParameterList& parameters)
113  {
114  std::unique_ptr< std::vector<size_t> > updatedParameters(new std::vector<size_t>());
115  bool test = parameters_.matchParametersValues(parameters, updatedParameters.get());
116  if (test)
117  fireParameterChanged(parameters.shareSubList(*updatedParameters));
118  return test;
119  }
120 
121  size_t getNumberOfParameters() const { return parameters_.size(); }
122 
123  void setNamespace(const std::string& prefix);
124 
125  std::string getNamespace() const { return prefix_; }
126 
127  std::string getParameterNameWithoutNamespace(const std::string& name) const;
128 
134  virtual void fireParameterChanged(const ParameterList& parameters) {}
135 
136 protected:
137  virtual void addParameter_(Parameter* parameter)
138  {
139  if (parameter)
140  parameters_.addParameter(parameter);
141  }
142 
143  virtual void addParameters_(const ParameterList& parameters)
144  {
145  parameters_.addParameters(parameters);
146  }
147 
148  virtual void shareParameter_(const std::shared_ptr<Parameter>& parameter)
149  {
150  parameters_.shareParameter(parameter);
151  }
152 
153  virtual void shareParameters_(const ParameterList& parameters)
154  {
155  parameters_.shareParameters(parameters);
156  }
157 
158  virtual void includeParameters_(const ParameterList& parameters)
159  {
160  parameters_.includeParameters(parameters);
161  }
162 
163  virtual void deleteParameter_(size_t index)
164  {
165  if (index >= parameters_.size())
166  throw IndexOutOfBoundsException("AbstractParametrizable::deleteParameter_.", index, 0, parameters_.size() - 1);
168  }
169 
170  virtual void deleteParameter_(std::string& name)
171  {
173  }
174 
175  virtual void deleteParameters_(const std::vector<std::string>& names)
176  {
178  }
179 
181  {
182  parameters_.reset();
183  }
184 
190  Parameter& getParameter_(const std::string& name)
191  {
192  if (!hasParameter(name))
193  throw ParameterNotFoundException("AbstractParametrizable::getParameter_().", prefix_ + name);
194  return parameters_.getParameter(prefix_ + name);
195  }
196 
202  Parameter& getParameterWithNamespace_(const std::string& name)
203  {
204  return getParameter_(name);
205  }
211  const Parameter& getParameterWithNamespace_(const std::string& name) const
212  {
213  return getParameter(name);
214  }
215 
216  Parameter& getParameter_(size_t index)
217  {
218  if (index >= parameters_.size())
219  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
220  return parameters_[index];
221  }
222 
223  const Parameter& getParameter_(size_t index) const
224  {
225  if (index >= parameters_.size())
226  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
227  return parameters_[index];
228  }
229 
230 
232 
237  const std::shared_ptr<Parameter>& getSharedParameter(size_t i) const
238  {
240  }
241 
242  std::shared_ptr<Parameter>& getSharedParameter(size_t i)
243  {
245  }
246 };
247 } // end of namespace bpp.
248 #endif // BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
A partial implementation of the Parametrizable interface.
ParameterList & getParameters_()
Get all parameters available.
virtual void shareParameters_(const ParameterList &parameters)
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
virtual void addParameter_(Parameter *parameter)
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
virtual void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
const ParameterList & getParameters() const
Get all parameters available.
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
virtual void deleteParameter_(std::string &name)
const Parameter & getParameter_(size_t index) const
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
Parameter & getParameterWithNamespace_(const std::string &name)
virtual void addParameters_(const ParameterList &parameters)
AbstractParametrizable(const std::string &prefix)
const std::shared_ptr< Parameter > & getSharedParameter(size_t i) const
const std::shared_ptr< Parameter > & getSharedParameter(const std::string &name) const
virtual void includeParameters_(const ParameterList &parameters)
virtual void deleteParameters_(const std::vector< std::string > &names)
bool hasParameter(const std::string &name) const
Tell if there is a parameter with specified name.
size_t getNumberOfParameters() const
Get the number of parameters.
std::shared_ptr< Parameter > & getSharedParameter(size_t i)
virtual void deleteParameter_(size_t index)
Parameter & getParameter_(size_t index)
const Parameter & getParameterWithNamespace_(const std::string &name) const
double getParameterValue(const std::string &name) const
Get the value for parameter of name 'name'.
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
std::string getParameterNameWithoutNamespace(const std::string &name) const
Resolves a parameter name according to the current namespace.
Parameter & getParameter_(const std::string &name)
Index out of bounds exception class.
Definition: Exceptions.h:170
The parameter list object.
Definition: ParameterList.h:65
virtual bool hasParameter(const std::string &name) const
virtual const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
virtual void addParameters(const ParameterList &params)
Add new parameters at the end of the list.
size_t size() const
Definition: ParameterList.h:92
virtual ParameterList shareSubList(const std::vector< std::string > &names) const
Share given parameters as a sublist.
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Share a parameter at the end of the list.
virtual void includeParameters(const ParameterList &params)
Add parameters to the list. If the parameter already exists, only the value is updated,...
virtual void deleteParameter(const std::string &name)
Delete a parameter from the list.
virtual void setParametersValues(const ParameterList &params)
Update the parameters from the ones in params that have matching names.
virtual void reset()
Reset the list: delete all parameters.
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
virtual void shareParameters(const ParameterList &params)
Share parameters with a given list. They are added the end of this list.
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
Delete several parameters from the list.
virtual const std::shared_ptr< Parameter > & getSharedParameter(size_t i) const
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
virtual void setAllParametersValues(const ParameterList &params)
Set the parameters to be equals to params.
Exception thrown when a parameter is not found, for instance in a ParameterList.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
This is the interface for all objects that imply parameters.