bpp-core  2.2.0
Parameter.cpp
Go to the documentation of this file.
1 //
2 // File: Parameter.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Oct 15 15:40:47 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 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 #include "Parameter.h"
41 #include <cmath>
42 
43 //From Utils:
44 #include "../Text/TextTools.h"
45 
46 using namespace bpp;
47 
48 #include <iostream>
49 using namespace std;
50 
51 /******************************************************************************/
52 
53 ParameterEvent::ParameterEvent(Parameter* parameter): parameter_(parameter) {}
54 
57 Parameter::Parameter(const std::string& name, double value, Constraint* constraint, bool attachConstraint, double precision)
58  throw (ConstraintException) :
59  name_(name), value_(0), precision_(0), constraint_(constraint), attach_(attachConstraint), listeners_(), listenerAttach_()
60 {
61  // This may throw a ConstraintException:
62  setValue(value);
63  setPrecision(precision);
64 }
65 
66 Parameter::Parameter(const std::string& name, double value, const Constraint* constraint, double precision)
67  throw (ConstraintException) :
68  name_(name), value_(0), precision_(0), constraint_(constraint ? constraint->clone() : 0), attach_(true), listeners_(), listenerAttach_()
69 {
70  // This may throw a ConstraintException:
71  setValue(value);
72  setPrecision(precision);
73 }
74 
76  name_(p.name_),
77  value_(p.value_),
78  precision_(p.precision_),
79  constraint_(0),
80  attach_(p.attach_),
81  listeners_(p.listeners_),
82  listenerAttach_(p.listenerAttach_)
83 {
84  if (p.attach_ && p.constraint_)
86  else
88  for (size_t i = 0; i < listeners_.size(); i++)
89  if (listenerAttach_[i])
90  listeners_[i] = dynamic_cast<ParameterListener*>(p.listeners_[i]->clone());
91 }
92 
94 {
95  name_ = p.name_;
96  value_ = p.value_;
98  attach_ = p.attach_;
99  if (p.attach_ && p.constraint_)
101  else
105  for (size_t i = 0; i < listeners_.size(); i++)
106  if (listenerAttach_[i])
107  listeners_[i] = dynamic_cast<ParameterListener*>(p.listeners_[i]->clone());
108  return *this;
109 }
110 
114 {
115  if (attach_ && constraint_) delete constraint_;
116  for (size_t i = 0; i < listeners_.size(); i++)
117  if (listenerAttach_[i])
118  delete listeners_[i];
119 }
120 
123 void Parameter::setValue(double value) throw (ConstraintException)
124 {
125  if (std::abs(value-value_)>precision_/2){
126  if (constraint_ && !constraint_->isCorrect(value))
127  throw ConstraintException("Parameter::setValue", this, value);
128  value_ = value;
129  ParameterEvent event(this);
130  fireParameterValueChanged(event);
131  }
132 }
133 
136 void Parameter::setPrecision(double precision)
137 {
138  precision_=(precision<0)?0:precision;
139 }
140 
143 void Parameter::setConstraint(Constraint* constraint, bool attach)
144 {
145  if (!constraint){
146  if (constraint_ && attach_)
147  delete constraint_;
148  constraint_=0;
149  attach_=false;
150  }
151  else {
152  if (constraint->isCorrect(value_)) {
153  if (constraint_ && attach_)
154  delete constraint_;
155  constraint_ = constraint;
156  attach_= attach;
157  }
158  else throw ConstraintException("Parameter::setConstraint", this, value_);
159  }
160 }
161 
162 
164 {
165  const Constraint * c = constraint_;
166  constraint_ = 0;
167  return c;
168 }
169 
170 /******************************************************************************/
171 
172 void Parameter::removeParameterListener(const std::string& listenerId)
173 {
174  for (unsigned int i = 0; i < listeners_.size(); i++)
175  {
176  if (listeners_[i]->getId() == listenerId)
177  {
178  if (listenerAttach_[i]) delete listeners_[i];
179  listeners_.erase(listeners_.begin() + i);
180  listenerAttach_.erase(listenerAttach_.begin() + i);
181  }
182  }
183 }
184 
185 /******************************************************************************/
186 
187 bool Parameter::hasParameterListener(const std::string& listenerId)
188 {
189  for (unsigned int i = 0; i < listeners_.size(); i++)
190  if (listeners_[i]->getId() == listenerId)
191  return true;
192  return false;
193 }
194 
195 /******************************************************************************/
196 
197 const IntervalConstraint Parameter::R_PLUS(true, 0, true);
198 
199 const IntervalConstraint Parameter::R_PLUS_STAR(true, 0, false);
200 
201 const IntervalConstraint Parameter::R_MINUS(false, 0, true);
202 
203 const IntervalConstraint Parameter::R_MINUS_STAR(false, 0, false);
204 
205 const IntervalConstraint Parameter::PROP_CONSTRAINT_IN(0, 1, true, true);
206 
207 const IntervalConstraint Parameter::PROP_CONSTRAINT_EX(0, 1, false, false);
208 
209 /******************************************************************************/
210 
double precision_
Definition: Parameter.h:141
The parameter listener interface.
Definition: Parameter.h:92
static const IntervalConstraint R_MINUS_STAR
Definition: Parameter.h:335
virtual const Constraint * removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:163
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0...
Definition: Parameter.h:152
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:53
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:135
This class allows to perform a correspondence analysis.
std::vector< ParameterListener * > listeners_
Definition: Parameter.h:144
static const IntervalConstraint PROP_CONSTRAINT_EX
Definition: Parameter.h:337
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
static const IntervalConstraint R_PLUS_STAR
Definition: Parameter.h:333
void setPrecision(double precision)
Set the precision of this parameter.
Definition: Parameter.cpp:136
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
std::string name_
Definition: Parameter.h:139
virtual void removeParameterListener(const std::string &listenerId)
Remove all listeners with a given id from this parameter.
Definition: Parameter.cpp:172
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
virtual ~Parameter()
Definition: Parameter.cpp:113
static const IntervalConstraint R_MINUS
Definition: Parameter.h:334
virtual void setConstraint(Constraint *constraint, bool attach=false)
Set a constraint to this parameter.
Definition: Parameter.cpp:143
Constraint * clone() const =0
Create a copy of this object and send a pointer to it.
The constraint interface.
Definition: Constraints.h:62
double value_
Definition: Parameter.h:140
Constraint * constraint_
Definition: Parameter.h:142
virtual bool hasParameterListener(const std::string &listenerId)
Tell is there is a listener with a given id from this parameter.
Definition: Parameter.cpp:187
static const IntervalConstraint PROP_CONSTRAINT_IN
Definition: Parameter.h:336
Exception thrown when a value do not match a given constraint.
std::vector< bool > listenerAttach_
Definition: Parameter.h:145
static const IntervalConstraint R_PLUS
Definition: Parameter.h:332
Parameter & operator=(const Parameter &param)
Assignment operator.
Definition: Parameter.cpp:93