bpp-core  2.2.0
AbstractParametrizable.h
Go to the documentation of this file.
1 //
2 // File: AbstractParametrizable.h
3 // Created by: Julien Dutheil
4 // Created on: Sun Mar 29 09:10 2009
5 // Created from file Parametrizable.h
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 _ABSTRACTPARAMETRIZABLE_H_
42 #define _ABSTRACTPARAMETRIZABLE_H_
43 
44 #include "Parametrizable.h"
45 
46 //From the STL:
47 #include <map>
48 
49 namespace bpp
50 {
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 throw (ParameterNotFoundException)
80  {
81  return parameters_.getParameter(prefix_ + name);
82  }
83 
84  double getParameterValue(const std::string& name) const
86  {
87  return getParameter(name).getValue();
88  }
89 
90  void setAllParametersValues(const ParameterList & parameters)
92  {
94  fireParameterChanged(parameters);
95  }
96 
97  void setParameterValue(const std::string& name, double value)
99  {
100  parameters_.setParameterValue(prefix_ + name, value);
102  }
103 
104  void setParametersValues(const ParameterList& parameters)
106  {
107  parameters_.setParametersValues(parameters);
108  fireParameterChanged(parameters);
109  }
110 
111  bool matchParametersValues(const ParameterList& parameters)
112  throw (ConstraintException)
113  {
114  std::auto_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.subList(*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) = 0;
135 
136  protected:
137  void addParameter_(Parameter* parameter)
138  {
139  if (parameter)
140  parameters_.addParameter(parameter);
141  }
142 
143  void addParameters_(const ParameterList& parameters)
144  {
145  parameters_.addParameters(parameters);
146  }
147 
148  void deleteParameter_(size_t index) throw (IndexOutOfBoundsException)
149  {
150  if (index >= parameters_.size())
151  throw IndexOutOfBoundsException("AbstractParametrizable::deleteParameter_.", index, 0, parameters_.size() - 1);
153  }
154 
155  void deleteParameter_(std::string& name)
156  {
158  }
159 
160  void deleteParameters_(const std::vector<std::string>& names)
161  {
163  }
164 
166  {
167  parameters_.reset();
168  }
169 
175  Parameter& getParameter_(const std::string& name) throw (ParameterNotFoundException)
176  {
177  return parameters_.getParameter(prefix_ + name);
178  }
179 
186  {
187  return getParameter_(name);
188  }
194  const Parameter& getParameterWithNamespace_(const std::string& name) const throw (ParameterNotFoundException)
195  {
196  return getParameter(name);
197  }
198 
200  {
201  if (index >= parameters_.size())
202  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
203  return parameters_[index];
204  }
205  const Parameter& getParameter_(size_t index) const throw (IndexOutOfBoundsException)
206  {
207  if(index >= parameters_.size())
208  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
209  return parameters_[index];
210  }
211 
213 };
214 
215 } //end of namespace bpp.
216 
217 #endif //_ABSTRACTPARAMETRIZABLE_H_
218 
bool hasParameter(const std::string &name) const
Tell if there is a parameter with specified name.
Exception thrown when a parameter is not found, for instance in a ParameterList.
std::string getParameterNameWithoutNamespace(const std::string &name) const
Resolves a parameter name according to the current namespace.
virtual void fireParameterChanged(const ParameterList &parameters)=0
Notify the class when one or several parameters have changed.
virtual ParameterList subList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
virtual void reset()
Reset the list: delete all parameters.
void deleteParameters_(const std::vector< std::string > &names)
Parameter & getParameter_(size_t index)
This class allows to perform a correspondence analysis.
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
A partial implementation of the Parametrizable interface.
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
Delete several parameters from the list.
size_t size() const
Definition: ParameterList.h:90
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setAllParametersValues(const ParameterList &params)
Set the parameters to be equals to params.
void deleteParameter_(std::string &name)
virtual void deleteParameter(const std::string &name)
Delete a parameter from the list.
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
size_t getNumberOfParameters() const
Get the number of parameters.
This is the interface for all objects that imply parameters.
Parameter & getParameterWithNamespace_(const std::string &name)
The parameter list object.
Definition: ParameterList.h:61
virtual void addParameters(const ParameterList &params)
Add new parameters at the end of the list.
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
AbstractParametrizable(const std::string &prefix)
const Parameter & getParameterWithNamespace_(const std::string &name) const
virtual bool hasParameter(const std::string &name) const
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
void addParameters_(const ParameterList &parameters)
void addParameter_(Parameter *parameter)
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
const ParameterList & getParameters() const
Get all parameters available.
const Parameter & getParameter_(size_t index) const
virtual const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:241
Exception thrown when a value do not match a given constraint.
Parameter & getParameter_(const std::string &name)
Index out of bounds exception class.
Definition: Exceptions.h:298
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
virtual void setParametersValues(const ParameterList &params)
Update the parameters from the ones in params that have matching names.
double getParameterValue(const std::string &name) const
Get the value for parameter of name &#39;name&#39;.