bpp-core3  3.0.0
AbstractParameterAliasable.h
Go to the documentation of this file.
1 //
2 // File: AbstractParameterAliasable.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2009-05-14 17:08: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_ABSTRACTPARAMETERALIASABLE_H
42 #define BPP_NUMERIC_ABSTRACTPARAMETERALIASABLE_H
43 
44 
45 #include "AbstractParametrizable.h"
46 #include "ParameterAliasable.h"
47 
48 // From the STL:
49 #include <map>
50 
51 namespace bpp
52 {
57  public ParameterListener
58 {
59 private:
60  std::string id_;
61  size_t alias_;
63  std::string name_;
64  std::string from_;
65 
66 public:
67  AliasParameterListener(const std::string& id, size_t alias, ParameterList* pl, const std::string& from) :
68  id_(id),
69  alias_(alias),
70  pl_(pl),
71  name_(),
72  from_(from)
73  {
74  // This allow us to check if the parameter position have changed at some point...
75  name_ = (*pl_)[alias].getName();
76  }
77 
79  id_(apl.id_),
80  alias_(apl.alias_),
81  pl_(apl.pl_),
82  name_(apl.name_),
83  from_(apl.from_)
84  {}
85 
87  {
88  id_ = apl.id_;
89  alias_ = apl.alias_;
90  pl_ = apl.pl_;
91  name_ = apl.name_;
92  from_ = apl.from_;
93  return *this;
94  }
95 
96  AliasParameterListener* clone() const { return new AliasParameterListener(*this); }
97 
98 public:
99  const std::string& getId() const { return id_; }
100 
101  const std::string& getFrom() const { return from_; }
102 
103  void setParameterList(ParameterList* pl) { pl_ = pl; }
104 
106 
108  {
109  Parameter* p = &(*pl_)[alias_];
110  if (p->getName() != name_)
111  throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterValueChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
112  p->setValue(event.getParameter()->Parameter::getValue());
113  }
114 
116  {
117  Parameter* p = &(*pl_)[alias_];
118  if (p->getName() != name_)
119  throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterConstraintChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
121  }
122 
123  const std::string& getName() const { return name_; }
124 
125  void rename(const std::string& name) { name_ = name; }
126 
127  const std::string& getAlias() const { return (*pl_)[alias_].getName(); }
128 };
129 
141  public AbstractParametrizable,
142  public virtual ParameterAliasable
143 {
144 private:
146 
151  std::map<std::string, AliasParameterListener*> aliasListenersRegister_;
152 
153 public:
154  AbstractParameterAliasable(const std::string& prefix) :
155  AbstractParametrizable(prefix),
158  {}
159 
161 
163 
164  virtual ~AbstractParameterAliasable();
165 
166 public:
167  void setNamespace(const std::string& prefix);
168 
169  bool hasIndependentParameter(const std::string& name) const { return independentParameters_.hasParameter(getNamespace() + name); }
170 
172 
174 
183  void aliasParameters(const std::string& p1, const std::string& p2);
184 
185  void unaliasParameters(const std::string& p1, const std::string& p2);
186 
197  void aliasParameters(std::map<std::string, std::string>& unparsedParams, bool verbose);
198 
206 
214 
215 
222  virtual std::vector<std::string> getAlias(const std::string& name) const;
223 
229  virtual std::map<std::string, std::string> getAliases() const;
230 
236  std::string getFrom(const std::string& name) const;
237 
238  // void fireParameterChanged(const ParameterList& parameters)
239  // {
240  // independentParameters_.matchParametersValues(getParameters());
241  // }
242 
243 protected:
244  void addParameter_(Parameter* parameter)
245  {
248  }
249 
250  void addParameters_(const ParameterList& parameters)
251  {
253 
254  for (size_t i = 0; i < parameters.size(); i++)
255  {
257  }
258  }
259 
260  void shareParameter_(const std::shared_ptr<Parameter>& parameter)
261  {
264  }
265 
266  void shareParameters_(const ParameterList& parameters)
267  {
269  for (size_t i = 0; i < parameters.size(); i++)
270  {
272  }
273  }
274 
275  void includeParameters_(const ParameterList& parameters)
276  {
278  for (size_t i = 0; i < parameters.size(); i++)
279  {
281  }
282  }
283 
284 
285  void deleteParameter_(size_t index)
286  {
287  std::string name = getParameter_(index).getName();
291  }
292 
293  void deleteParameter_(std::string& name)
294  {
298  }
299 
300  void deleteParameters_(const std::vector<std::string>& names)
301  {
302  std::string x;
303  for (size_t i = 0; i < names.size(); i++)
304  {
305  x = names[i];
306  deleteParameter_(x);
307  }
308  }
309 
311  {
314  }
315 };
316 } // end of namespace bpp.
317 #endif // BPP_NUMERIC_ABSTRACTPARAMETERALIASABLE_H
A partial implementation of the Parametrizable interface.
void addParameters_(const ParameterList &parameters)
size_t getNumberOfIndependentParameters() const
Get the number of independent parameters.
virtual std::vector< std::string > getAlias(const std::string &name) const
const ParameterList & getIndependentParameters() const
Get the minimal list of parameters to set the model.
void unaliasParameters(const std::string &p1, const std::string &p2)
Detach two parameters previously set as 'aliased'.
void shareParameters_(const ParameterList &parameters)
void deleteParameters_(const std::vector< std::string > &names)
std::string getFrom(const std::string &name) const
void includeParameters_(const ParameterList &parameters)
void addParameter_(Parameter *parameter)
bool hasIndependentParameter(const std::string &name) const
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
virtual std::map< std::string, std::string > getAliases() const
AbstractParameterAliasable(const std::string &prefix)
void aliasParameters(const std::string &p1, const std::string &p2)
alias the parameters.
void shareParameter_(const std::shared_ptr< Parameter > &parameter)
ParameterList getAliasedParameters(const ParameterList &pl) const
Return the list of the names of the parameters that are aliased (directly or not) to one of the param...
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
ParameterList getFromParameters(const ParameterList &pl) const
Return the list of the names of the parameters from which the parameters of the list are aliased (dir...
std::map< std::string, AliasParameterListener * > aliasListenersRegister_
A partial implementation of the Parametrizable interface.
virtual void shareParameters_(const ParameterList &parameters)
virtual void addParameter_(Parameter *parameter)
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
virtual void addParameters_(const ParameterList &parameters)
const std::shared_ptr< Parameter > & getSharedParameter(const std::string &name) const
virtual void includeParameters_(const ParameterList &parameters)
virtual void deleteParameter_(size_t index)
std::string getParameterNameWithoutNamespace(const std::string &name) const
Resolves a parameter name according to the current namespace.
Parameter & getParameter_(const std::string &name)
Inner listener class used by AbstractParameterAliasable.
AliasParameterListener * clone() const
Create a copy of this object and send a pointer to it.
AliasParameterListener & operator=(const AliasParameterListener &apl)
const std::string & getName() const
void parameterValueChanged(ParameterEvent &event)
Notify a value change.
void parameterConstraintChanged(ParameterEvent &event)
Notify a constraint change.
const std::string & getFrom() const
void setParameterList(ParameterList *pl)
AliasParameterListener(const std::string &id, size_t alias, ParameterList *pl, const std::string &from)
void parameterNameChanged(ParameterEvent &event)
Notify a renaming action.
AliasParameterListener(const AliasParameterListener &apl)
const std::string & getId() const
void rename(const std::string &name)
const std::string & getAlias() const
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Extend the Parametrizable interface with support for parameter aliases.
const Parameter * getParameter() const
Definition: Parameter.h:78
The parameter list object.
Definition: ParameterList.h:65
virtual bool hasParameter(const std::string &name) const
size_t size() const
Definition: ParameterList.h:92
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Share a parameter at the end of the list.
virtual void deleteParameter(const std::string &name)
Delete a parameter from the list.
virtual void reset()
Reset the list: delete all parameters.
The parameter listener interface.
Definition: Parameter.h:91
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:110
virtual const std::shared_ptr< Constraint > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:233
virtual void setConstraint(std::shared_ptr< Constraint > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:131
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:211