bpp-core  2.2.0
AbstractParameterAliasable.h
Go to the documentation of this file.
1 //
2 // File: AbstractParameterAliasable.h
3 // Created by: Julien Dutheil
4 // Created on: Thu May 14 17:08 2009
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 19, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _ABSTRACTPARAMETERALIASABLE_H_
41 #define _ABSTRACTPARAMETERALIASABLE_H_
42 
43 #include "AbstractParametrizable.h"
44 #include "ParameterAliasable.h"
45 
46 //From the STL:
47 #include <map>
48 
49 namespace bpp
50 {
51 
56  public ParameterListener
57  {
58  private:
59  std::string id_;
60  size_t alias_;
62  std::string name_;
63  std::string from_;
64 
65  public:
66  AliasParameterListener(const std::string& id, size_t alias, ParameterList* pl, const std::string& from):
67  id_(id),
68  alias_(alias),
69  pl_(pl),
70  name_(),
71  from_(from)
72  {
73  //This allow us to check if the parameter position have changed at some point...
74  name_ = (*pl_)[alias].getName();
75  }
76 
78  id_(apl.id_),
79  alias_(apl.alias_),
80  pl_(apl.pl_),
81  name_(apl.name_),
82  from_(apl.from_)
83  {}
84 
86  {
87  id_ = apl.id_;
88  alias_ = apl.alias_;
89  pl_ = apl.pl_;
90  name_ = apl.name_;
91  from_ = apl.from_;
92  return *this;
93  }
94 
95  AliasParameterListener* clone() const { return new AliasParameterListener(*this); }
96 
97  public:
98  const std::string& getId() const { return id_; }
99 
100  const std::string& getFrom() const { return from_; }
101 
102  void setParameterList(ParameterList* pl) { pl_ = pl; }
103 
105 
107  {
108  Parameter* p = &(*pl_)[alias_];
109  if (p->getName() != name_)
110  throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterValueChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
111  p->setValue(event.getParameter()->getValue());
112  }
113 
114  const std::string& getName() const { return name_; }
115 
116  void rename(const std::string& name) { name_ = name; }
117 
118  const std::string& getAlias() const { return (*pl_)[alias_].getName(); }
119 
120  };
121 
133  public AbstractParametrizable,
134  public virtual ParameterAliasable
135  {
136  private:
137 
139 
144  std::map<std::string, AliasParameterListener *> aliasListenersRegister_;
145 
146  public:
147  AbstractParameterAliasable(const std::string& prefix) :
148  AbstractParametrizable(prefix),
151  {}
152 
154 
156 
157  virtual ~AbstractParameterAliasable();
158 
159  public:
160  void setNamespace(const std::string& prefix);
161 
163 
165 
166  void aliasParameters(const std::string& p1, const std::string& p2) throw (ParameterNotFoundException, Exception);
167 
168  void unaliasParameters(const std::string& p1, const std::string& p2) throw (ParameterNotFoundException, Exception);
169 
180  void aliasParameters(std::map<std::string, std::string>& unparsedParams, bool verbose);
181 
189 
190 
197  std::vector<std::string> getAlias(const std::string& name) const;
198 
204  std::string getFrom(const std::string& name) const;
205 
206  void fireParameterChanged(const ParameterList& parameters)
207  {
209  }
210 
211  protected:
212  void addParameter_(Parameter* parameter)
213  {
216  }
217 
218  void addParameters_(const ParameterList& parameters)
219  {
222  }
223 
224  void deleteParameter_(size_t index) throw (IndexOutOfBoundsException)
225  {
226  std::string name = getParameter_(index).getName();
230  }
231 
232  void deleteParameter_(std::string& name) throw (IndexOutOfBoundsException)
233  {
237  }
238 
239  void deleteParameters_(const std::vector<std::string>& names)
240  {
241  std::string x;
242  for (size_t i=0; i<names.size(); i++)
243  {
244  x=names[i];
245  deleteParameter_(x);
246  }
247  }
248 
250  {
253  }
254 
255  };
256 
257 } //end of namespace bpp.
258 
259 #endif //_ABSTRACTPARAMETERALIASABLE_H_
260 
Exception thrown when a parameter is not found, for instance in a ParameterList.
The parameter listener interface.
Definition: Parameter.h:92
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
std::vector< std::string > getAlias(const std::string &name) const
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:199
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.
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.
void unaliasParameters(const std::string &p1, const std::string &p2)
Detach two parameters previously set as &#39;aliased&#39;.
A partial implementation of the Parametrizable interface.
size_t size() const
Definition: ParameterList.h:90
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
void addParameter_(Parameter *parameter)
size_t getNumberOfIndependentParameters() const
Get the number of independent parameters.
virtual void deleteParameter(const std::string &name)
Delete a parameter from the list.
const std::string & getName() const
The parameter list object.
Definition: ParameterList.h:61
virtual void addParameters(const ParameterList &params)
Add new parameters at the end of the list.
AliasParameterListener & operator=(const AliasParameterListener &apl)
void deleteParameters_(const std::vector< std::string > &names)
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
A partial implementation of the Parametrizable interface.
std::map< std::string, AliasParameterListener * > aliasListenersRegister_
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 bool hasParameter(const std::string &name) const
const std::string & getId() const
std::string getFrom(const std::string &name) const
void addParameters_(const ParameterList &parameters)
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:234
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
void addParameter_(Parameter *parameter)
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
Exception base class.
Definition: Exceptions.h:57
const std::string & getAlias() const
const ParameterList & getParameters() const
Get all parameters available.
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)
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
void rename(const std::string &name)
void aliasParameters(const std::string &p1, const std::string &p2)
Set two parameters as &#39;aliased&#39;.
Parameter & getParameter_(const std::string &name)
Index out of bounds exception class.
Definition: Exceptions.h:298
void setParameterList(ParameterList *pl)
Inner listener class used by AbstractParameterAliasable.
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...
AliasParameterListener(const AliasParameterListener &apl)