bpp-core3  3.0.0
Parameter.cpp
Go to the documentation of this file.
1 //
2 // File: Parameter.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-10-15 15:40:47
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 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 #include <cmath>
42 
43 #include "Parameter.h"
44 
45 // From Utils:
46 #include "../Text/TextTools.h"
47 
48 using namespace bpp;
49 
50 #include <iostream>
51 using namespace std;
52 
53 /******************************************************************************/
54 
55 ParameterEvent::ParameterEvent(Parameter* parameter) : parameter_(parameter) {}
56 
59 Parameter::Parameter(const std::string& name, double value, std::shared_ptr<Constraint> constraint, double precision) :
60  name_(name), value_(0), precision_(0), constraint_(constraint), listeners_(), listenerAttach_()
61 {
62  setValue(value);
63  setPrecision(precision);
64 }
65 
67  name_(p.name_),
68  value_(p.value_),
69  precision_(p.precision_),
70  constraint_(p.constraint_ ? std::shared_ptr<Constraint>(p.constraint_->clone()) : 0),
71  listeners_(p.listeners_),
72  listenerAttach_(p.listenerAttach_)
73 {
74  for (size_t i = 0; i < listeners_.size(); i++)
75  {
76  if (listenerAttach_[i])
77  listeners_[i] = dynamic_cast<ParameterListener*>(p.listeners_[i]->clone());
78  }
79 }
80 
82 {
83  name_ = p.name_;
84  value_ = p.value_;
86  constraint_ = p.constraint_ ? std::shared_ptr<Constraint>(p.constraint_->clone()) : 0;
89  for (size_t i = 0; i < listeners_.size(); i++)
90  {
91  if (listenerAttach_[i])
92  listeners_[i] = dynamic_cast<ParameterListener*>(p.listeners_[i]->clone());
93  }
94  return *this;
95 }
96 
100 {
101  for (size_t i = 0; i < listeners_.size(); i++)
102  {
103  if (listenerAttach_[i])
104  delete listeners_[i];
105  }
106 }
107 
110 void Parameter::setValue(double value)
111 {
112  if (std::abs(value - value_) > precision_ / 2)
113  {
114  if (constraint_ && !constraint_->isCorrect(value))
115  throw ConstraintException("Parameter::setValue", this, value);
116  value_ = value;
117  ParameterEvent event(this);
119  }
120 }
121 
124 void Parameter::setPrecision(double precision)
125 {
126  precision_ = (precision < 0) ? 0 : precision;
127 }
128 
131 void Parameter::setConstraint(std::shared_ptr<Constraint> constraint)
132 {
133  if (constraint != 0 && !constraint->isCorrect(value_))
134  throw ConstraintException("Parameter::setConstraint", this, value_);
135 
136  constraint_ = constraint;
137 }
138 
139 
140 std::shared_ptr<Constraint> Parameter::removeConstraint()
141 {
142  auto c = constraint_;
143  constraint_ = 0;
144  return c;
145 }
146 
147 /******************************************************************************/
148 
149 void Parameter::removeParameterListener(const std::string& listenerId)
150 {
151  for (unsigned int i = 0; i < listeners_.size(); i++)
152  {
153  if (listeners_[i]->getId() == listenerId)
154  {
155  if (listenerAttach_[i])
156  delete listeners_[i];
157  listeners_.erase(listeners_.begin() + i);
158  listenerAttach_.erase(listenerAttach_.begin() + i);
159  }
160  }
161 }
162 
163 /******************************************************************************/
164 
165 bool Parameter::hasParameterListener(const std::string& listenerId)
166 {
167  for (unsigned int i = 0; i < listeners_.size(); i++)
168  {
169  if (listeners_[i]->getId() == listenerId)
170  return true;
171  }
172  return false;
173 }
174 
175 /******************************************************************************/
176 
177 const std::shared_ptr<IntervalConstraint> Parameter::R_PLUS(new IntervalConstraint(true, 0, true));
178 const std::shared_ptr<IntervalConstraint> Parameter::R_PLUS_STAR(new IntervalConstraint(true, 0, false));
179 const std::shared_ptr<IntervalConstraint> Parameter::R_MINUS(new IntervalConstraint(false, 0, true));
180 const std::shared_ptr<IntervalConstraint> Parameter::R_MINUS_STAR(new IntervalConstraint(false, 0, false));
181 const std::shared_ptr<IntervalConstraint> Parameter::PROP_CONSTRAINT_IN(new IntervalConstraint(0, 1, true, true));
182 const std::shared_ptr<IntervalConstraint> Parameter::PROP_CONSTRAINT_EX(new IntervalConstraint(0, 1, false, false));
183 
184 /******************************************************************************/
Exception thrown when a value do not match a given constraint.
The constraint interface.
Definition: Constraints.h:66
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:139
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:55
The parameter listener interface.
Definition: Parameter.h:91
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 void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:110
virtual std::shared_ptr< Constraint > removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:140
std::vector< ParameterListener * > listeners_
Definition: Parameter.h:141
static const std::shared_ptr< IntervalConstraint > R_PLUS
Definition: Parameter.h:325
static const std::shared_ptr< IntervalConstraint > R_MINUS
Definition: Parameter.h:327
std::vector< bool > listenerAttach_
Definition: Parameter.h:142
std::shared_ptr< Constraint > constraint_
Definition: Parameter.h:140
virtual bool hasParameterListener(const std::string &listenerId)
Tell is there is a listener with a given id from this parameter.
Definition: Parameter.cpp:165
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:329
virtual ~Parameter()
Definition: Parameter.cpp:99
virtual void setConstraint(std::shared_ptr< Constraint > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:131
void setPrecision(double precision)
Set the precision of this parameter.
Definition: Parameter.cpp:124
static const std::shared_ptr< IntervalConstraint > R_MINUS_STAR
Definition: Parameter.h:328
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
Definition: Parameter.h:330
double precision_
Definition: Parameter.h:139
double value_
Definition: Parameter.h:138
std::string name_
Definition: Parameter.h:137
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
Definition: Parameter.h:326
Parameter & operator=(const Parameter &param)
Assignment operator.
Definition: Parameter.cpp:81
void fireParameterValueChanged(ParameterEvent &event)
Definition: Parameter.h:308
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0.
Definition: Parameter.h:150