bpp-core3  3.0.0
AbstractParametrizable.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
6 #define BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
7 
8 
9 #include "Parametrizable.h"
10 
11 // From the STL:
12 #include <map>
13 
14 namespace bpp
15 {
27  public virtual Parametrizable
28 {
29 private:
31  std::string prefix_;
32 
33 public:
34  AbstractParametrizable(const std::string& prefix) : parameters_(), prefix_(prefix) {}
35 
37 
38 public:
39  bool hasParameter(const std::string& name) const override { return parameters_.hasParameter(prefix_ + name); }
40 
41  const ParameterList& getParameters() const override { return parameters_; }
42 
43  const Parameter& parameter(const std::string& name) const override
44  {
45  return parameters_.parameter(prefix_ + name);
46  }
47 
48  const std::shared_ptr<Parameter>& getParameter(const std::string& name) const
49  {
50  return parameters_.getParameter(prefix_ + name);
51  }
52 
53  double getParameterValue(const std::string& name) const override
54  {
55  return parameter(name).getValue();
56  }
57 
58  void setAllParametersValues(const ParameterList& parameters) override
59  {
60  parameters_.setAllParametersValues(parameters);
61  fireParameterChanged(parameters);
62  }
63 
64  void setParameterValue(const std::string& name, double value) override
65  {
66  parameters_.setParameterValue(prefix_ + name, value);
67  fireParameterChanged(parameters_.createSubList(prefix_ + name));
68  }
69 
70  void setParametersValues(const ParameterList& parameters) override
71  {
72  parameters_.setParametersValues(parameters);
73  fireParameterChanged(parameters);
74  }
75 
76  bool matchParametersValues(const ParameterList& parameters) override
77  {
78  std::unique_ptr< std::vector<size_t>> updatedParameters(new std::vector<size_t>());
79  bool test = parameters_.matchParametersValues(parameters, updatedParameters.get());
80  if (test)
81  fireParameterChanged(parameters.shareSubList(*updatedParameters));
82  return test;
83  }
84 
85  void removeConstraint(const std::string& name) override
86  {
88  }
89 
90  void setConstraint(const std::string& name, std::shared_ptr<ConstraintInterface> constraint) override
91  {
92  getParameter_(name).setConstraint(constraint);
93  }
94 
95  size_t getNumberOfParameters() const override { return parameters_.size(); }
96 
97  void setNamespace(const std::string& prefix) override;
98 
99  std::string getNamespace() const override { return prefix_; }
100 
101  std::string getParameterNameWithoutNamespace(const std::string& name) const override;
102 
108  virtual void fireParameterChanged(const ParameterList& parameters) {}
109 
110 protected:
112  {
113  if (parameter)
114  parameters_.addParameter(parameter);
115  }
116 
117  virtual void addParameters_(const ParameterList& parameters)
118  {
119  parameters_.addParameters(parameters);
120  }
121 
122  virtual void shareParameter_(const std::shared_ptr<Parameter>& parameter)
123  {
124  parameters_.shareParameter(parameter);
125  }
126 
127  virtual void shareParameters_(const ParameterList& parameters)
128  {
129  parameters_.shareParameters(parameters);
130  }
131 
132  virtual void includeParameters_(const ParameterList& parameters)
133  {
134  parameters_.includeParameters(parameters);
135  }
136 
137  virtual void deleteParameter_(size_t index)
138  {
139  if (index >= parameters_.size())
140  throw IndexOutOfBoundsException("AbstractParametrizable::deleteParameter_.", index, 0, parameters_.size() - 1);
141  parameters_.deleteParameter(index);
142  }
143 
144  virtual void deleteParameter_(std::string& name)
145  {
146  parameters_.deleteParameter(name);
147  }
148 
149  virtual void deleteParameters_(const std::vector<std::string>& names)
150  {
151  parameters_.deleteParameters(names);
152  }
153 
155  {
156  parameters_.reset();
157  }
158 
164  Parameter& getParameter_(const std::string& name)
165  {
166  if (!hasParameter(name))
167  throw ParameterNotFoundException("AbstractParametrizable::getParameter_().", prefix_ + name);
168  return parameters_.parameter(prefix_ + name);
169  }
170 
176  Parameter& getParameterWithNamespace_(const std::string& name)
177  {
178  return getParameter_(name);
179  }
185  const Parameter& getParameterWithNamespace_(const std::string& name) const
186  {
187  return parameter(name);
188  }
189 
190  Parameter& getParameter_(size_t index)
191  {
192  if (index >= parameters_.size())
193  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
194  return parameters_[index];
195  }
196 
197  const Parameter& getParameter_(size_t index) const
198  {
199  if (index >= parameters_.size())
200  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
201  return parameters_[index];
202  }
203 
204 
205  ParameterList& getParameters_() override { return parameters_; }
206 
211  const std::shared_ptr<Parameter>& getParameter(size_t i) const
212  {
213  return parameters_.getParameter(i);
214  }
215 
216  std::shared_ptr<Parameter>& getParameter(size_t i)
217  {
218  return parameters_.getParameter(i);
219  }
220 };
221 } // end of namespace bpp.
222 #endif // BPP_NUMERIC_ABSTRACTPARAMETRIZABLE_H
Exception thrown when a parameter is not found, for instance in a ParameterList.
const std::shared_ptr< Parameter > & getParameter(size_t i) const
std::string getNamespace() const override
void setAllParametersValues(const ParameterList &parameters) override
Set the parameters values to be equals to those of parameters.
virtual const Parameter & parameter(const std::string &name) const
Get the parameter with name name.
virtual void includeParameters(const ParameterList &params)
Add parameters to the list. If the parameter already exists, only the value is updated, otherwise the new parameter is added at the end of the list.
virtual void reset()
Reset the list: delete all parameters.
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
Parameter & getParameter_(size_t index)
void setConstraint(const std::string &name, std::shared_ptr< ConstraintInterface > constraint) override
Set/Change the constraint associated with one parameter.
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 deleteParameter_(std::string &name)
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
Delete several parameters from the list.
void setNamespace(const std::string &prefix) override
Set the namespace for the parameter names.
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Share a parameter at the end of the list.
const std::shared_ptr< Parameter > & getParameter(const std::string &name) const
size_t size() const
Definition: ParameterList.h:56
std::shared_ptr< Parameter > & getParameter(size_t i)
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
virtual void setAllParametersValues(const ParameterList &params)
Set the parameters to be equals to params.
virtual void setConstraint(std::shared_ptr< ConstraintInterface > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:76
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.
double getParameterValue(const std::string &name) const override
Get the value for parameter of name &#39;name&#39;.
This is the interface for all objects that imply parameters.
Parameter & getParameterWithNamespace_(const std::string &name)
The parameter list object.
Definition: ParameterList.h:27
virtual void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
virtual void addParameters(const ParameterList &params)
Add new parameters at the end of the list.
AbstractParametrizable(const std::string &prefix)
void setParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
const Parameter & getParameterWithNamespace_(const std::string &name) const
bool matchParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
virtual void includeParameters_(const ParameterList &parameters)
virtual bool hasParameter(const std::string &name) const
bool hasParameter(const std::string &name) const override
Tell if there is a parameter with specified name.
virtual void shareParameters(const ParameterList &params)
Share parameters with a given list. They are added the end of this list.
ParameterList & getParameters_() override
Get all parameters available.
void removeConstraint(const std::string &name) override
Remove the constraint associated with one parameter, if any.
virtual void deleteParameters_(const std::vector< std::string > &names)
virtual ParameterList shareSubList(const std::vector< std::string > &names) const
Share given parameters as a sublist.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
std::string getParameterNameWithoutNamespace(const std::string &name) const override
Resolves a parameter name according to the current namespace.
void setParameterValue(const std::string &name, double value) override
Set the value of parameter with name name to be equal to value.
virtual void addParameter_(Parameter *parameter)
const Parameter & getParameter_(size_t index) const
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
virtual void shareParameters_(const ParameterList &parameters)
virtual void deleteParameter_(size_t index)
Parameter & getParameter_(const std::string &name)
Index out of bounds exception class.
Definition: Exceptions.h:131
size_t getNumberOfParameters() const override
Get the number of parameters.
virtual const std::shared_ptr< Parameter > & getParameter(size_t i) const
Definition: ParameterList.h:69
const Parameter & parameter(const std::string &name) const override
Get the parameter with specified name.
virtual void setParametersValues(const ParameterList &params)
Update the parameters from the ones in params that have matching names.
const ParameterList & getParameters() const override
Get all parameters available.
virtual std::shared_ptr< ConstraintInterface > removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:85
virtual void addParameters_(const ParameterList &parameters)