bpp-core  2.1.0
AbstractParameterAliasable.h
Go to the documentation of this file.
00001 //
00002 // File: AbstractParameterAliasable.h
00003 // Created by: Julien Dutheil
00004 // Created on: Thu May 14 17:08 2009
00005 //
00006 
00007 /*
00008 Copyright or © or Copr. Bio++ Development Team, (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 #ifndef _ABSTRACTPARAMETERALIASABLE_H_
00041 #define _ABSTRACTPARAMETERALIASABLE_H_
00042 
00043 #include "AbstractParametrizable.h"
00044 #include "ParameterAliasable.h"
00045 
00046 //From the STL:
00047 #include <map>
00048 
00049 namespace bpp
00050 {
00051 
00055   class AliasParameterListener:
00056     public ParameterListener
00057   {
00058     private:
00059       std::string id_;
00060       size_t alias_;
00061       ParameterList *pl_;
00062       std::string name_;
00063       std::string from_;
00064 
00065     public:
00066     AliasParameterListener(const std::string& id, size_t alias, ParameterList* pl, const std::string& from):
00067         id_(id),
00068         alias_(alias),
00069         pl_(pl),
00070         name_(),
00071         from_(from)
00072       {
00073         //This allow us to check if the parameter position have changed at some point...
00074         name_ = (*pl_)[alias].getName();
00075       }
00076 
00077       AliasParameterListener(const AliasParameterListener& apl):
00078         id_(apl.id_),
00079         alias_(apl.alias_),
00080         pl_(apl.pl_),
00081         name_(apl.name_),
00082         from_(apl.from_)
00083       {}
00084 
00085       AliasParameterListener& operator=(const AliasParameterListener& apl)
00086       {
00087         id_    = apl.id_;
00088         alias_ = apl.alias_;
00089         pl_    = apl.pl_;
00090         name_  = apl.name_;
00091         from_  = apl.from_;
00092         return *this;
00093       }
00094 
00095       AliasParameterListener* clone() const { return new AliasParameterListener(*this); }
00096 
00097     public:
00098       const std::string& getId() const { return id_; }
00099 
00100       const std::string& getFrom() const { return from_; }
00101     
00102       void setParameterList(ParameterList* pl) { pl_ = pl; }
00103 
00104       void parameterNameChanged(ParameterEvent& event) throw (Exception) {}
00105     
00106       void parameterValueChanged(ParameterEvent& event) throw (Exception)
00107       {
00108         Parameter* p = &(*pl_)[alias_];
00109         if (p->getName() != name_)
00110           throw Exception("AbstractParameterAliasable::AliasParameterListener::parameterValueChanged. Error, aliased parameter have change, maybe because it was renamed, or a parameter was removed?");
00111         p->setValue(event.getParameter()->getValue());
00112       }
00113 
00114       const std::string& getName() const { return name_; }
00115 
00116       void rename(const std::string& name) { name_ = name; }
00117 
00118       const std::string& getAlias() const { return (*pl_)[alias_].getName(); }
00119       
00120   };
00121 
00132   class AbstractParameterAliasable:
00133     public AbstractParametrizable,
00134     public virtual ParameterAliasable
00135   {
00136     private:
00137 
00138       mutable ParameterList independentParameters_;
00139 
00144       std::map<std::string, AliasParameterListener *> aliasListenersRegister_;
00145   
00146     public:
00147       AbstractParameterAliasable(const std::string& prefix) :
00148         AbstractParametrizable(prefix),
00149         independentParameters_(),
00150         aliasListenersRegister_()
00151       {}
00152 
00153       AbstractParameterAliasable(const AbstractParameterAliasable& ap);
00154     
00155       AbstractParameterAliasable& operator=(const AbstractParameterAliasable& ap);
00156 
00157       virtual ~AbstractParameterAliasable();
00158 
00159     public:
00160       void setNamespace(const std::string& prefix);
00161  
00162       const ParameterList& getIndependentParameters() const { return independentParameters_; }
00163     
00164       size_t getNumberOfIndependentParameters() const { return independentParameters_.size(); }
00165 
00166       void aliasParameters(const std::string& p1, const std::string& p2) throw (ParameterNotFoundException, Exception);
00167 
00168       void unaliasParameters(const std::string& p1, const std::string& p2) throw (ParameterNotFoundException, Exception);
00169 
00175       std::vector<std::string> getAlias(const std::string& name) const;
00176 
00181       std::string getFrom(const std::string& name) const;
00182       
00183       void fireParameterChanged(const ParameterList& parameters)
00184       {
00185         independentParameters_.matchParametersValues(getParameters());
00186       }
00187 
00188     protected:
00189       void addParameter_(Parameter* parameter)
00190       {
00191         AbstractParametrizable::addParameter_(parameter);
00192         independentParameters_.addParameter(parameter->clone());
00193       }
00194 
00195       void addParameters_(const ParameterList& parameters)
00196       {
00197         AbstractParametrizable::addParameters_(parameters);
00198         independentParameters_.addParameters(parameters);
00199       }
00200 
00201       void deleteParameter_(size_t index) throw (IndexOutOfBoundsException)
00202       {
00203         std::string name = getParameter_(index).getName();
00204         AbstractParametrizable::deleteParameter_(index);
00205         if (independentParameters_.hasParameter(name))
00206         independentParameters_.deleteParameter(name);
00207       }
00208 
00209       void resetParameters_()
00210       {
00211         AbstractParametrizable::resetParameters_();
00212         independentParameters_.reset();
00213       }
00214 
00215   };
00216 
00217 } //end of namespace bpp.
00218 
00219 #endif //_ABSTRACTPARAMETERALIASABLE_H_
00220 
 All Classes Namespaces Files Functions Variables Typedefs Friends