bpp-core3  3.0.0
AbstractParameterAliasable.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractParameterAliasable.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2009-03-29 09:10: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 
43 #include "VectorTools.h"
44 
45 using namespace bpp;
46 using namespace std;
47 
50  independentParameters_(),
51  aliasListenersRegister_()
52 {
53  for (size_t i = 0; i < ap.independentParameters_.size(); i++)
54  {
56  }
57 
58  // Actualize the register with adequate pointers:
59  for (map<string, AliasParameterListener*>::const_iterator it = ap.aliasListenersRegister_.begin();
60  it != ap.aliasListenersRegister_.end();
61  it++)
62  {
63  AliasParameterListener* listener = it->second->clone();
64  listener->setParameterList(&getParameters_());
65  aliasListenersRegister_[it->first] = listener;
66  // Now correct parameters with appropriate pointers:
67  for (unsigned int i = 0; i < getNumberOfParameters(); ++i)
68  {
69  if (getParameters_()[i].hasParameterListener(it->first))
70  {
71  getParameters_()[i].removeParameterListener(it->first);
72  getParameters_()[i].addParameterListener(listener, false);
73  }
74  }
75  }
76 }
77 
79 {
80  AbstractParametrizable::operator=(ap);
81 
82  for (size_t i = 0; i < ap.independentParameters_.size(); i++)
83  {
85  }
86 
87  // Actualize the register with adequate pointers:
88  for (map<string, AliasParameterListener*>::const_iterator it = ap.aliasListenersRegister_.begin();
89  it != ap.aliasListenersRegister_.end();
90  it++)
91  {
92  AliasParameterListener* listener = it->second->clone();
93  listener->setParameterList(&getParameters_());
94  aliasListenersRegister_[it->first] = listener;
95  // Now correct parameters with appropriate pointers:
96  for (unsigned int i = 0; i < getNumberOfParameters(); ++i)
97  {
98  if (getParameters_()[i].hasParameterListener(it->first))
99  {
100  getParameters_()[i].removeParameterListener(it->first);
101  getParameters_()[i].addParameterListener(listener, false);
102  }
103  }
104  }
105  return *this;
106 }
107 
109 {
110  // Delete the registry content:
111  for (map<string, AliasParameterListener*>::iterator it = aliasListenersRegister_.begin();
112  it != aliasListenersRegister_.end();
113  it++)
114  {
115  delete it->second;
116  }
117 }
118 
119 void AbstractParameterAliasable::aliasParameters(const std::string& p1, const std::string& p2)
120 {
121  // In case this is the first time we call this method:
124 
125  if (!hasParameter(p1))
126  throw ParameterNotFoundException("AbstractParameterAliasable::aliasParameters", p1);
127  if (!hasParameter(p2))
128  throw ParameterNotFoundException("AbstractParameterAliasable::aliasParameters", p2);
130  throw Exception("AbstractParameterAliasable::aliasParameters. Parameter " + p2 + " does not exist in independent parameters. Perhaps it is already aliased to a parameter and can't be aliased twice.");
131 
132  string id = "__alias_" + p2 + "_to_" + p1;
133  string idCheck = "__alias_" + p1 + "_to_" + p2;
134 
135  if (aliasListenersRegister_.find(idCheck) != aliasListenersRegister_.end())
136  throw Exception("AbstractParameterAliasable::aliasParameters. Trying to alias parameter " + p2 + " to " + p1 + ", but parameter " + p1 + " is already aliased to parameter " + p2 + ".");
137  Parameter* param1 = &getParameter_(p1);
138  Parameter* param2 = &getParameter_(p2);
139 
140  if (!param1->hasConstraint())
141  {
142  if (param2->hasConstraint())
143  {
144  ApplicationTools::displayWarning("Aliasing parameter " + p2 + " to " + p1 + ". " + p1 + " gets the constraints of " + p2 + ": " + param2->getConstraint()->getDescription());
145  param1->setConstraint(param2->getConstraint());
146  }
147  }
148  else
149  // We use a small trick here, we test the constraints on the basis of their string description (C++ does not provide a default operator==() :( ).
150  if (param2->hasConstraint() && (param1->getConstraint()->getDescription() != param2->getConstraint()->getDescription()))
151  {
152  std::shared_ptr<Constraint> nc(*param2->getConstraint() & *param1->getConstraint());
153  ApplicationTools::displayWarning("Aliasing parameter " + p2 + " to " + p1 + " with different constraints. They get the intersection of both constraints : " + nc->getDescription());
154 
155  param2->setConstraint(nc);
156  param1->setConstraint(nc);
157  }
158 
159  // Every thing seems ok, let's create the listener and register it:
160 
161  AliasParameterListener* aliasListener = new AliasParameterListener(id, getParameters().whichParameterHasName(getNamespace() + p2), &getParameters_(), p1);
162 
163  aliasListenersRegister_[id] = aliasListener;
164 
165  // Now we add it to the appropriate parameter, that is p1.
166  // The parameter will not own the listener, the bookkeeping being achieved by the register:
167  param1->addParameterListener(aliasListener, false);
168 
169  // Finally we remove p2 from the list of independent parameters:
171 }
172 
173 void AbstractParameterAliasable::aliasParameters(map<string, string>& unparsedParams, bool verbose)
174 {
175  const ParameterList& pl = getParameters();
176  ParameterList plpars;
177 
178  for (size_t i = 0; i < pl.size(); i++)
179  {
180  if (unparsedParams.find(pl[i].getName()) == unparsedParams.end())
181  plpars.addParameter(pl[i].clone());
182  }
183 
184  size_t unp_s = unparsedParams.size();
185  while (unp_s != 0)
186  {
187  auto it = unparsedParams.begin();
188  while (it != unparsedParams.end())
189  {
190  Parameter* pp = 0;
191  try
192  {
193  pp = &plpars.getParameter(it->second);
194  }
195  catch (ParameterNotFoundException& e)
196  {
197  if (!pl.hasParameter(it->second))
198  throw ParameterNotFoundException("Unknown aliasing parameter", it->first + "->" + it->second);
199  continue;
200  }
201  unique_ptr<Parameter> p2(pp->clone());
202  p2->setName(it->first);
203  plpars.addParameter(p2.release());
204  plpars.getParameter(it->first);
205  aliasParameters(it->second, it->first);
206  if (verbose)
207  ApplicationTools::displayResult("Parameter alias found", it->first + " -> " + it->second + " = " + TextTools::toString(pp->getValue()));
208  it = unparsedParams.erase(it);
209  }
210 
211  if (unparsedParams.size() == unp_s)
212  throw Exception("Error, there is a cycle in aliasing starting with " + unparsedParams.begin()->first);
213  else
214  unp_s = unparsedParams.size();
215  }
216 
217  matchParametersValues(plpars);
218 }
219 
220 
221 void AbstractParameterAliasable::unaliasParameters(const std::string& p1, const std::string& p2)
222 {
223  if (!hasParameter(p1))
224  throw ParameterNotFoundException("AbstractParameterAliasable::unaliasParameters", p1);
225  if (!hasParameter(p2))
226  throw ParameterNotFoundException("AbstractParameterAliasable::unaliasParameters", p2);
227 
228  string id = "__alias_" + p2 + "_to_" + p1;
229  map<string, AliasParameterListener*>::iterator it = aliasListenersRegister_.find(id);
230  if (it == aliasListenersRegister_.end())
231  throw Exception("AbstractParameterAliasable::unaliasParameters. Parameter " + p2 + " is not aliased to parameter " + p1 + ".");
232  // Remove the listener:
234  delete it->second;
235  aliasListenersRegister_.erase(it);
236  // Finally we re-add p2 to the list of independent parameters:
238 }
239 
240 void AbstractParameterAliasable::setNamespace(const std::string& prefix)
241 {
242  string currentName;
243 
244  // We modify all the listeners
245  for (map<string, AliasParameterListener*>::iterator it = aliasListenersRegister_.begin();
246  it != aliasListenersRegister_.end();
247  it++)
248  {
249  currentName = it->second->getName();
250  if (TextTools::startsWith(currentName, getNamespace()))
251  it->second->rename(prefix + currentName.substr(getNamespace().size()));
252  else
253  it->second->rename(prefix + currentName);
254  }
255 
256 // Finally we notify the mother class:
258 }
259 
260 vector<string> AbstractParameterAliasable::getAlias(const string& name) const
261 {
262  vector<string> aliases;
263 
264  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
265  it != aliasListenersRegister_.end();
266  it++)
267  {
268  if (it->second->getFrom() == name)
269  {
270  string alias = it->second->getAlias();
271  aliases.push_back(alias);
272  if (alias != name)
273  {
274  vector<string> chainAliases = getAlias(alias);
275  VectorTools::append(aliases, chainAliases);
276  }
277  }
278  }
279  return aliases;
280 }
281 
282 
283 std::map<std::string, std::string> AbstractParameterAliasable::getAliases() const
284 {
285  map<string, string> aliases;
286 
287  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
288  it != aliasListenersRegister_.end();
289  it++)
290  {
291  string name = it->second->getFrom();
292 
293  vector<string> alias = getAlias(name);
294 
295  for (size_t i = 0; i < alias.size(); i++)
296  {
297  aliases[alias[i]] = name;
298  }
299  }
300 
301  return aliases;
302 }
303 
305 {
306  ParameterList aliases;
307  bool b = false;
308 
309  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
310  it != aliasListenersRegister_.end();
311  it++)
312  {
313  if ((pl.hasParameter(it->second->getFrom()) ||
314  aliases.hasParameter(it->second->getFrom()))
315  && !(aliases.hasParameter(it->second->getAlias()) ||
316  pl.hasParameter(it->second->getAlias())))
317  {
318  b = true;
319  aliases.addParameter(getParameter(it->second->getAlias()));
320  }
321  }
322 
323  while (b)
324  {
325  b = false;
326 
327  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
328  it != aliasListenersRegister_.end();
329  it++)
330  {
331  if (aliases.hasParameter(it->second->getFrom())
332  && !(aliases.hasParameter(it->second->getAlias()) ||
333  pl.hasParameter(it->second->getAlias())))
334  {
335  b = true;
336  aliases.addParameter(getParameter(it->second->getAlias()));
337  }
338  }
339  }
340 
341  return aliases;
342 }
343 
344 string AbstractParameterAliasable::getFrom(const string& name) const
345 {
346  string from = "";
347 
348  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
349  it != aliasListenersRegister_.end();
350  it++)
351  {
352  if (it->second->getName() == name)
353  {
354  from = it->second->getFrom();
355  break;
356  }
357  }
358 
359  return from;
360 }
361 
362 
364 {
365  ParameterList from;
366  bool b = false;
367 
368  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
369  it != aliasListenersRegister_.end();
370  it++)
371  {
372  if ((pl.hasParameter(it->second->getAlias()) ||
373  from.hasParameter(it->second->getAlias()))
374  && !(from.hasParameter(it->second->getFrom()) ||
375  pl.hasParameter(it->second->getFrom())))
376  {
377  b = true;
378  from.addParameter(getParameter(it->second->getFrom()));
379  }
380  }
381 
382  while (b)
383  {
384  b = false;
385 
386  for (map<string, AliasParameterListener*>::const_iterator it = aliasListenersRegister_.begin();
387  it != aliasListenersRegister_.end();
388  it++)
389  {
390  if (from.hasParameter(it->second->getAlias())
391  && !(from.hasParameter(it->second->getFrom()) ||
392  pl.hasParameter(it->second->getFrom())))
393  {
394  b = true;
395  from.addParameter(getParameter(it->second->getFrom()));
396  }
397  }
398  }
399 
400  return from;
401 }
A partial implementation of the Parametrizable interface.
virtual std::vector< std::string > getAlias(const std::string &name) const
void unaliasParameters(const std::string &p1, const std::string &p2)
Detach two parameters previously set as 'aliased'.
std::string getFrom(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.
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.
ParameterList & getParameters_()
Get all parameters available.
const ParameterList & getParameters() const
Get all parameters available.
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
const std::shared_ptr< Parameter > & getSharedParameter(const std::string &name) const
bool hasParameter(const std::string &name) const
Tell if there is a parameter with specified name.
size_t getNumberOfParameters() const
Get the number of parameters.
const Parameter & getParameter(const std::string &name) const
Get the parameter with specified name.
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
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.
void setParameterList(ParameterList *pl)
static void displayWarning(const std::string &text)
Print a warning message.
static void displayResult(const std::string &text, const T &result)
Print a result message.
virtual Clonable * clone() const =0
Create a copy of this object and send a pointer to it.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
The parameter list object.
Definition: ParameterList.h:65
virtual bool hasParameter(const std::string &name) const
virtual const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
size_t size() const
Definition: ParameterList.h:92
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
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 shareParameters(const ParameterList &params)
Share parameters with a given list. They are added the end of this list.
Exception thrown when a parameter is not found, for instance in a ParameterList.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void removeParameterListener(const std::string &listenerId)
Remove all listeners with a given id from this parameter.
Definition: Parameter.cpp:149
virtual const std::shared_ptr< Constraint > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:233
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:177
virtual void setConstraint(std::shared_ptr< Constraint > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:131
virtual void addParameterListener(ParameterListener *listener, bool attachListener=true)
Add a new listener to this parameter.
Definition: Parameter.h:278
virtual bool hasConstraint() const
Tells if this parameter has a constraint.
Definition: Parameter.h:249
static void append(std::vector< T > &vec1, const std::vector< T > &vec2)
Append the content of a std::vector to another one.
Definition: VectorTools.h:1938
bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
Definition: TextTools.cpp:440
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153