bpp-core3  3.0.0
AbstractParameterAliasable.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_ABSTRACTPARAMETERALIASABLE_H
6 #define BPP_NUMERIC_ABSTRACTPARAMETERALIASABLE_H
7 
8 
10 #include "ParameterAliasable.h"
11 
12 // From the STL:
13 #include <map>
14 
15 namespace bpp
16 {
21  public ParameterListener
22 {
23 private:
24  std::string id_;
25  size_t alias_;
27  std::string name_;
28  std::string from_;
29 
30 public:
31  AliasParameterListener(const std::string& id, size_t alias, ParameterList* pl, const std::string& from) :
32  id_(id),
33  alias_(alias),
34  pl_(pl),
35  name_(),
36  from_(from)
37  {
38  // This allow us to check if the parameter position have changed at some point...
39  name_ = (*pl_)[alias].getName();
40  }
41 
43  id_(apl.id_),
44  alias_(apl.alias_),
45  pl_(apl.pl_),
46  name_(apl.name_),
47  from_(apl.from_)
48  {}
49 
51  {
52  id_ = apl.id_;
53  alias_ = apl.alias_;
54  pl_ = apl.pl_;
55  name_ = apl.name_;
56  from_ = apl.from_;
57  return *this;
58  }
59 
60  AliasParameterListener* clone() const { return new AliasParameterListener(*this); }
61 
62 public:
63  const std::string& getId() const { return id_; }
64 
65  const std::string& getFrom() const { return from_; }
66 
67  void setParameterList(ParameterList* pl) { pl_ = pl; }
68 
70 
72  {
73  Parameter* p = &(*pl_)[alias_];
74  if (p->getName() != name_)
75  throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterValueChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
76  p->setValue(event.parameter()->Parameter::getValue());
77  }
78 
80  {
81  Parameter* p = &(*pl_)[alias_];
82  if (p->getName() != name_)
83  throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterConstraintChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
84  p->setConstraint(event.parameter()->getConstraint());
85  }
86 
87  const std::string& getName() const { return name_; }
88 
89  void rename(const std::string& name) { name_ = name; }
90 
91  const std::string& getAlias() const { return (*pl_)[alias_].getName(); }
92 };
93 
105  public AbstractParametrizable,
106  public virtual ParameterAliasable
107 {
108 private:
110 
115  std::map<std::string, std::shared_ptr<AliasParameterListener>> aliasListenersRegister_;
116 
117 public:
118  AbstractParameterAliasable(const std::string& prefix) :
119  AbstractParametrizable(prefix),
120  independentParameters_(),
121  aliasListenersRegister_()
122  {}
123 
125 
127 
129 
130 public:
131  void setNamespace(const std::string& prefix);
132 
133  bool hasIndependentParameter(const std::string& name) const { return independentParameters_.hasParameter(getNamespace() + name); }
134 
135  const ParameterList& getIndependentParameters() const { return independentParameters_; }
136 
137  size_t getNumberOfIndependentParameters() const { return independentParameters_.size(); }
138 
145  void aliasParameters(const std::string& p1, const std::string& p2);
146 
147  void unaliasParameters(const std::string& p1, const std::string& p2);
148 
157  void aliasParameters(std::map<std::string, std::string>& unparsedParams, bool verbose);
158 
163  ParameterList getAliasedParameters(const ParameterList& pl) const;
164 
169  ParameterList getFromParameters(const ParameterList& pl) const;
170 
171 
177  virtual std::vector<std::string> getAlias(const std::string& name) const;
178 
182  virtual std::map<std::string, std::string> getAliases() const;
183 
188  std::string getFrom(const std::string& name) const;
189 
190  // void fireParameterChanged(const ParameterList& parameters)
191  // {
192  // independentParameters_.matchParametersValues(getParameters());
193  // }
194 
195 protected:
196  void addParameter_(Parameter* parameter)
197  {
199  independentParameters_.shareParameter(getParameter(getParameterNameWithoutNamespace(parameter->getName())));
200  }
201 
202  void addParameters_(const ParameterList& parameters)
203  {
205 
206  for (size_t i = 0; i < parameters.size(); ++i)
207  {
208  independentParameters_.shareParameter(getParameter(getParameterNameWithoutNamespace(parameters[i].getName())));
209  }
210  }
211 
212  void shareParameter_(const std::shared_ptr<Parameter>& parameter)
213  {
215  independentParameters_.shareParameter(parameter);
216  }
217 
218  void shareParameters_(const ParameterList& parameters)
219  {
221  for (size_t i = 0; i < parameters.size(); ++i)
222  {
223  independentParameters_.shareParameter(getParameter(getParameterNameWithoutNamespace(parameters[i].getName())));
224  }
225  }
226 
227  void includeParameters_(const ParameterList& parameters)
228  {
230  for (size_t i = 0; i < parameters.size(); ++i)
231  {
232  independentParameters_.shareParameter(getParameter(getParameterNameWithoutNamespace(parameters[i].getName())));
233  }
234  }
235 
236 
237  void deleteParameter_(size_t index)
238  {
239  std::string name = getParameter_(index).getName();
241  if (independentParameters_.hasParameter(name))
242  independentParameters_.deleteParameter(name);
243  }
244 
245  void deleteParameter_(std::string& name)
246  {
248  if (independentParameters_.hasParameter(name))
249  independentParameters_.deleteParameter(name);
250  }
251 
252  void deleteParameters_(const std::vector<std::string>& names)
253  {
254  std::string x;
255  for (size_t i = 0; i < names.size(); ++i)
256  {
257  x = names[i];
258  deleteParameter_(x);
259  }
260  }
261 
263  {
265  independentParameters_.reset();
266  }
267 };
268 } // end of namespace bpp.
269 #endif // BPP_NUMERIC_ABSTRACTPARAMETERALIASABLE_H
The parameter listener interface.
Definition: Parameter.h:53
Extend the Parametrizable interface with support for parameter aliases.
const ParameterList & getIndependentParameters() const
Get the minimal list of parameters to set the model.
virtual void reset()
Reset the list: delete all parameters.
A partial implementation of the Parametrizable interface.
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Share a parameter at the end of the list.
size_t size() const
Definition: ParameterList.h:56
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
void addParameter_(Parameter *parameter)
void shareParameter_(const std::shared_ptr< Parameter > &parameter)
size_t getNumberOfIndependentParameters() const
Get the number of independent parameters.
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.
const std::string & getName() const
const Parameter * parameter() const
Definition: Parameter.h:42
The parameter list object.
Definition: ParameterList.h:27
bool hasIndependentParameter(const std::string &name) const
AliasParameterListener & operator=(const AliasParameterListener &apl)
void shareParameters_(const ParameterList &parameters)
void deleteParameters_(const std::vector< std::string > &names)
virtual std::shared_ptr< const ConstraintInterface > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:196
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:55
A partial implementation of the Parametrizable interface.
void addParameters_(const ParameterList &parameters)
const std::string & getFrom() const
AliasParameterListener * clone() const
Create a copy of this object and send a pointer to it.
virtual void includeParameters_(const ParameterList &parameters)
std::map< std::string, std::shared_ptr< AliasParameterListener > > aliasListenersRegister_
virtual bool hasParameter(const std::string &name) const
const std::string & getId() const
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
void includeParameters_(const ParameterList &parameters)
void parameterConstraintChanged(ParameterEvent &event)
Notify a constraint change.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
const std::string & getAlias() const
virtual void addParameter_(Parameter *parameter)
void parameterNameChanged(ParameterEvent &event)
Notify a renaming action.
AbstractParameterAliasable(const std::string &prefix)
void parameterValueChanged(ParameterEvent &event)
Notify a value change.
AliasParameterListener(const std::string &id, size_t alias, ParameterList *pl, const std::string &from)
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
void rename(const std::string &name)
virtual void shareParameters_(const ParameterList &parameters)
virtual void deleteParameter_(size_t index)
void setParameterList(ParameterList *pl)
Inner listener class used by AbstractParameterAliasable.
virtual void addParameters_(const ParameterList &parameters)
AliasParameterListener(const AliasParameterListener &apl)