bpp-core  2.1.0
ParameterList.cpp
Go to the documentation of this file.
00001 //
00002 // File: ParameterList.cpp
00003 // Created by: Julien Dutheil
00004 // Created on: Wed Oct 15 18:17:29 2003
00005 //
00006 
00007 /*
00008    Copyright or © or Copr. Julien Dutheil, (November 19, 2004)
00009 
00010    This software is a computer program whose purpose is to provide classes
00011    for numerical calculus.
00012 
00013    This software is governed by the CeCILL  license under French law and
00014    abiding by the rules of distribution of free software.  You can  use,
00015    modify and/ or redistribute the software under the terms of the CeCILL
00016    license as circulated by CEA, CNRS and INRIA at the following URL
00017    "http://www.cecill.info".
00018 
00019    As a counterpart to the access to the source code and  rights to copy,
00020    modify and redistribute granted by the license, users are provided only
00021    with a limited warranty  and the software's author,  the holder of the
00022    economic rights,  and the successive licensors  have only  limited
00023    liability.
00024 
00025    In this respect, the user's attention is drawn to the risks associated
00026    with loading,  using,  modifying and/or developing or reproducing the
00027    software by the user in light of its specific status of free software,
00028    that may mean  that it is complicated to manipulate,  and  that  also
00029    therefore means  that it is reserved for developers  and  experienced
00030    professionals having in-depth computer knowledge. Users are therefore
00031    encouraged to load and test the software's suitability as regards their
00032    requirements in conditions enabling the security of their systems and/or
00033    data to be ensured and,  more generally, to use and operate it in the
00034    same conditions as regards security.
00035 
00036    The fact that you are presently reading this means that you have had
00037    knowledge of the CeCILL license and that you accept its terms.
00038  */
00039 
00040 #include "ParameterList.h"
00041 #include "../Text/StringTokenizer.h"
00042 
00043 using namespace bpp;
00044 
00045 #include <iostream>
00046 #include <algorithm>
00047 using namespace std;
00048 
00051 ParameterList::ParameterList(const ParameterList& pl) :
00052   parameters_(pl.size())
00053 {
00054   // Now copy all parameters:
00055   for (unsigned int i = 0; i < size(); i++)
00056   {
00057     parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
00058   }
00059 }
00060 
00063 ParameterList& ParameterList::operator=(const ParameterList& pl)
00064 {
00065   // First delete all parameters:
00066   reset();
00067 
00068   // Then resize the vector:
00069   parameters_.resize(pl.size());
00070 
00071   // Now copy all parameters:
00072   for (unsigned int i = 0; i < pl.size(); i++)
00073   {
00074     parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
00075   }
00076 
00077   return *this;
00078 }
00079 
00082 ParameterList::~ParameterList()
00083 {
00084   // Delete all parameter objects.
00085   reset();
00086 }
00087 
00088 /******************************************************************************/
00089 const Parameter& ParameterList::getParameter(const std::string& name) const throw (ParameterNotFoundException)
00090 {
00091   for (unsigned int i = 0; i < size(); i++)
00092   {
00093     const Parameter* p = parameters_[i];
00094     if (p->getName() == name) return *p;
00095   }
00096   throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
00097 }
00098 
00099 /******************************************************************************/
00100 double ParameterList::getParameterValue(const std::string& name) const throw (ParameterNotFoundException)
00101 {
00102   for (unsigned int i = 0; i < size(); i++)
00103   {
00104     const Parameter* p = parameters_[i];
00105     if (p->getName() == name) return p->getValue();
00106   }
00107   throw ParameterNotFoundException("ParameterList::getParameterValue('name').", name);
00108 }
00109 
00110 /******************************************************************************/
00111 Parameter& ParameterList::getParameter(const std::string& name) throw (ParameterNotFoundException)
00112 {
00113   for (unsigned int i = 0; i < size(); i++)
00114   {
00115     Parameter* p = parameters_[i];
00116     if (p->getName() == name) return *p;
00117   }
00118   throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
00119 }
00120 
00121 /******************************************************************************/
00122 ParameterList ParameterList::subList(const std::vector<std::string>& names) const throw (ParameterNotFoundException)
00123 {
00124   ParameterList pl;
00125   for (unsigned int i = 0; i < names.size(); i++)
00126   {
00127     Parameter param = getParameter(names[i]);
00128     pl.addParameter(param);
00129   }
00130   return pl;
00131 }
00132 
00133 /******************************************************************************/
00134 ParameterList ParameterList::subList(const std::string& name) const throw (ParameterNotFoundException)
00135 {
00136   ParameterList pl;
00137   Parameter param = getParameter(name);
00138   pl.addParameter(param);
00139   return pl;
00140 }
00141 
00142 /******************************************************************************/
00143 ParameterList ParameterList::subList(const std::vector<size_t>& parameters) const
00144 {
00145   ParameterList pl;
00146   for (unsigned int i = 0; i < parameters.size(); i++)
00147   {
00148     if (parameters[i] < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameters[i]]->clone()));
00149   }
00150   return pl;
00151 }
00152 
00153 /******************************************************************************/
00154 ParameterList ParameterList::subList(size_t parameter) const
00155 {
00156   ParameterList pl;
00157   if (parameter < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameter]->clone()));
00158   return pl;
00159 }
00160 
00161 /******************************************************************************/
00162 ParameterList ParameterList::getCommonParametersWith(const ParameterList& params) const
00163 {
00164   ParameterList pl;
00165   for (unsigned int i = 0; i < params.size(); i++)
00166   {
00167     const Parameter& p = params[i];
00168     if (hasParameter(p.getName()))
00169       pl.parameters_.push_back(dynamic_cast<Parameter*>(p.clone()));                                                                        
00170     // We use push_back instead of addParameter because we are sure the name is not duplicated.
00171   }
00172 
00173   return pl;
00174 }
00175 
00176 /******************************************************************************/
00177 
00178 std::vector<std::string> ParameterList::getParameterNames() const
00179 {
00180   vector<string> pNames(size());
00181   for (unsigned int i = 0; i < size(); i++)
00182   {
00183     pNames[i] = parameters_[i]->getName();
00184   }
00185   return pNames;
00186 }
00187 
00188 /****************************************************************************/
00189 
00190 vector<string> ParameterList::getMatchingParameterNames(const string& pattern) const
00191 {
00192   vector<string> pNames;
00193   for (unsigned int i = 0; i < size(); i++)
00194     {
00195       string name = parameters_[i]->getName();
00196 
00197       StringTokenizer stj(pattern, "*", true, false);
00198       size_t pos1, pos2;
00199       bool flag(true);
00200       string g=stj.nextToken();
00201       pos1=name.find(g);
00202       if (pos1!=0)
00203         flag=false;
00204       pos1+=g.length();
00205       while (flag && stj.hasMoreToken()){
00206         g=stj.nextToken();
00207         pos2=name.find(g,pos1);
00208         if (pos2 == string::npos){
00209           flag=false;
00210           break;
00211         }
00212         pos1=pos2+g.length();
00213       }
00214       if (flag &&
00215           ((g.length()==0) || (pos1==name.length()) || (name.rfind(g)==name.length()-g.length())))
00216         pNames.push_back(name);
00217     }
00218 
00219   return pNames;
00220 }
00221 
00222 /******************************************************************************/
00223 
00224 void ParameterList::addParameter(const Parameter& param) throw (ParameterException)
00225 {
00226   if (hasParameter(param.getName()))
00227     throw ParameterException("ParameterList::addParameter. Parameter with name '" + param.getName() + "' already exists.", &param);
00228   parameters_.push_back(dynamic_cast<Parameter*>(param.clone()));
00229 }
00230 
00231 /******************************************************************************/
00232 
00233 void ParameterList::addParameter(Parameter* param) throw (ParameterException)
00234 {
00235   if (hasParameter(param->getName()))
00236     throw ParameterException("ParameterList::addParameter. Parameter with name '" + param->getName() + "' already exists.", param);
00237   parameters_.push_back(param);
00238 }
00239 
00240 /******************************************************************************/
00241 
00242 void ParameterList::setParameter(size_t index, const Parameter& param) throw (IndexOutOfBoundsException)
00243 {
00244   if (index >= size()) throw IndexOutOfBoundsException("ParameterList::setParameter.", index, 0, size());
00245   delete parameters_[index];
00246   parameters_[index] = dynamic_cast<Parameter*>(param.clone());
00247 }
00248 
00249 /******************************************************************************/
00250 
00251 void ParameterList::includeParameters(const ParameterList& params)
00252 {
00253   for (unsigned int i = 0; i < params.size(); i++)
00254   {
00255     if (hasParameter(params[i].getName()))
00256       setParameterValue(params[i].getName(), params[i].getValue());
00257     else
00258       parameters_.push_back(dynamic_cast<Parameter*>(params[i].clone()));
00259   }
00260 }
00261 
00262 /******************************************************************************/
00263 
00264 void ParameterList::addParameters(const ParameterList& params)
00265 throw (ParameterException)
00266 {
00267   for (unsigned int i = 0; i < params.size(); i++)
00268   {
00269     addParameter(params[i]);
00270   }
00271 }
00272 
00273 /******************************************************************************/
00274 
00275 void ParameterList::setParameterValue(const string& name, double value)
00276 throw (ParameterNotFoundException, ConstraintException)
00277 {
00278   Parameter* p = &getParameter(name);
00279   p->setValue(value);
00280 }
00281 
00282 /******************************************************************************/
00283 
00284 void ParameterList::setAllParametersValues(const ParameterList& params)
00285 throw (ParameterNotFoundException, ConstraintException)
00286 {
00287   // First we check if all values are correct:
00288   for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
00289   {
00290     const Parameter* p = &params.getParameter((*it)->getName());
00291     if ((*it)->hasConstraint() && !(*it)->getConstraint()->isCorrect(p->getValue()))
00292       throw ConstraintException("ParameterList::setParametersValues()", *it, p->getValue());
00293   }
00294 
00295   // If all values are ok, we set them:
00296   for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
00297   {
00298     const Parameter* p = &params.getParameter((*it)->getName());
00299     (*it)->setValue(p->getValue());
00300   }
00301 }
00302 
00303 /******************************************************************************/
00304 
00305 void ParameterList::setParametersValues(const ParameterList& params)
00306 {
00307   // First we check if all values are correct:
00308   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00309   {
00310     if (hasParameter((*it)->getName()))
00311     {
00312       Parameter* p = &getParameter((*it)->getName());
00313       if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
00314         throw ConstraintException("ParameterList::setParametersValues()", p, (*it)->getValue());
00315     }
00316   }
00317 
00318   // If all values are ok, we set them:
00319   {
00320     for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00321     {
00322       if (hasParameter((*it)->getName()))
00323       {
00324         Parameter* p = &getParameter((*it)->getName());
00325         p->setValue((*it)->getValue());
00326       }
00327     }
00328   }
00329 }
00330 
00331 /******************************************************************************/
00332 
00333 bool ParameterList::testParametersValues(const ParameterList& params) const
00334 {
00335   // First we check if all values are correct:
00336   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00337   {
00338     if (hasParameter((*it)->getName()))
00339     {
00340       const Parameter* p = &getParameter((*it)->getName());
00341       if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
00342         throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
00343     }
00344   }
00345 
00346   // If all values are ok, we test them:
00347   bool ch = 0;
00348 
00349   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00350   {
00351     if (hasParameter((*it)->getName()))
00352     {
00353       const Parameter* p = &getParameter((*it)->getName());
00354       if (p->getValue() != (*it)->getValue())
00355         ch |= 1;
00356     }
00357   }
00358   return ch;
00359 }
00360 
00361 /******************************************************************************/
00362 
00363 bool ParameterList::matchParametersValues(const ParameterList& params)
00364 throw (ConstraintException)
00365 {
00366   // First we check if all values are correct:
00367   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00368   {
00369     if (hasParameter((*it)->getName()))
00370     {
00371       Parameter* p = &getParameter((*it)->getName());
00372       if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
00373         throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
00374     }
00375   }
00376 
00377   // If all values are ok, we set them:
00378   bool ch = 0;
00379 
00380   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00381   {
00382     if (hasParameter((*it)->getName()))
00383     {
00384       Parameter* p = &getParameter((*it)->getName());
00385       if (p->getValue() != (*it)->getValue())
00386         ch |= 1;
00387       p->setValue((*it)->getValue());
00388     }
00389   }
00390   return ch;
00391 }
00392 
00393 /******************************************************************************/
00394 void ParameterList::setAllParameters(const ParameterList& params)
00395 throw (ParameterNotFoundException)
00396 {
00397   for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
00398   {
00399     const Parameter* p = &params.getParameter((*it)->getName());
00400     **it = *p;
00401   }
00402 }
00403 
00404 /******************************************************************************/
00405 void ParameterList::setParameters(const ParameterList& params)
00406 throw (ParameterNotFoundException)
00407 {
00408   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00409   {
00410     Parameter* p = &getParameter((*it)->getName());
00411     *p = **it;
00412   }
00413 }
00414 
00415 /******************************************************************************/
00416 bool ParameterList::hasParameter(const std::string& name) const
00417 {
00418   for (unsigned int i = 0; i < size(); i++)
00419   {
00420     const Parameter* p = parameters_[i];
00421     if (p->getName() == name)
00422       return true;
00423   }
00424   return false;
00425 }
00426 
00427 /******************************************************************************/
00428 void ParameterList::matchParameters(const ParameterList& params)
00429 {
00430   for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
00431   {
00432     if (hasParameter((*it)->getName()))
00433     {
00434       Parameter* p = &getParameter((*it)->getName());
00435       *p = **it;
00436     }
00437   }
00438 }
00439 
00440 /******************************************************************************/
00441 void ParameterList::deleteParameter(const std::string& name) throw (ParameterNotFoundException)
00442 {
00443   for (unsigned int i = 0; i < size(); i++)
00444   {
00445     Parameter* p = parameters_[i];
00446     if (p->getName() == name)
00447     {
00448       delete p;
00449       parameters_.erase(parameters_.begin() + i);
00450       return;
00451     }
00452   }
00453   throw ParameterNotFoundException("ParameterList::deleteParameter", name);
00454 }
00455 
00456 /******************************************************************************/
00457 void ParameterList::deleteParameters(const std::vector<std::string>& names) throw (ParameterNotFoundException)
00458 {
00459   for (unsigned int i = 0; i < names.size(); i++)
00460   {
00461     deleteParameter(names[i]);
00462   }
00463 }
00464 
00465 /******************************************************************************/
00466 void ParameterList::deleteParameter(size_t index) throw (IndexOutOfBoundsException)
00467 {
00468   if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
00469   Parameter* p = parameters_[index];
00470   delete p;
00471   parameters_.erase(parameters_.begin() + index);
00472 }
00473 
00474 /******************************************************************************/
00475 void ParameterList::deleteParameters(const std::vector<size_t>& indices) throw (IndexOutOfBoundsException)
00476 {
00477   vector<size_t> tmp(indices);
00478   sort(tmp.begin(), tmp.end());
00479   for (vector<size_t>::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); i++)
00480   {
00481     size_t index = *i;
00482     if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
00483     Parameter* p = parameters_[index];
00484     delete p;
00485     parameters_.erase(parameters_.begin() + index);
00486   }
00487 }
00488 
00489 /******************************************************************************/
00490 size_t ParameterList::whichParameterHasName(const std::string& name) const throw (ParameterNotFoundException)
00491 {
00492   for (size_t i = 0; i < size(); i++)
00493   {
00494     if (parameters_[i]->getName() == name) return i;
00495   }
00496   throw ParameterNotFoundException("ParameterList::whichParameterHasName.", name);
00497 }
00498 
00499 /******************************************************************************/
00500 void ParameterList::printParameters(OutputStream& out) const
00501 {
00502   (out << "Name:\tValue:\tConstraint:").endLine();
00503   (out << "_________________________________________________").endLine();
00504   for (unsigned int i = 0; i < size(); i++)
00505   {
00506     out << parameters_[i]->getName();
00507     out << "\t" << parameters_[i]->getValue();
00508     out << (parameters_[i]->hasConstraint() ? "\t" + parameters_[i]->getConstraint()->getDescription() : string(""));
00509     out.endLine();
00510   }
00511 }
00512 
00513 /******************************************************************************/
00514 void ParameterList::reset()
00515 {
00516   for (unsigned int i = 0; i < size(); i++)
00517   {
00518     delete parameters_[i];
00519   }
00520   parameters_.resize(0);
00521 }
00522 
00523 /******************************************************************************/
00524 
 All Classes Namespaces Files Functions Variables Typedefs Friends