bpp-core3  3.0.0
Parameter.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <cmath>
6 #include <algorithm>
7 
8 #include "Parameter.h"
9 
10 // From Utils:
11 #include "../Text/TextTools.h"
12 
13 using namespace bpp;
14 
15 #include <iostream>
16 using namespace std;
17 
18 /******************************************************************************/
19 
20 ParameterEvent::ParameterEvent(Parameter* parameter) : parameter_(parameter) {}
21 
24 Parameter::Parameter(const std::string& name, double value, std::shared_ptr<ConstraintInterface> constraint, double precision) :
25  name_(name), value_(0), precision_(0), constraint_(constraint), listeners_()
26 {
27  setValue(value);
28  setPrecision(precision);
29 }
30 
32  name_(p.name_),
33  value_(p.value_),
37 {}
38 
40 {
41  name_ = p.name_;
42  value_ = p.value_;
46  return *this;
47 }
48 
52 
55 void Parameter::setValue(double value)
56 {
57  if (std::abs(value - value_) > precision_ / 2)
58  {
59  if (constraint_ && !constraint_->isCorrect(value))
60  throw ConstraintException("Parameter::setValue", this, value);
61  value_ = value;
62  ParameterEvent event(this);
64  }
65 }
66 
69 void Parameter::setPrecision(double precision)
70 {
71  precision_ = (precision < 0) ? 0 : precision;
72 }
73 
76 void Parameter::setConstraint(std::shared_ptr<ConstraintInterface> constraint)
77 {
78  if (constraint != nullptr && !constraint->isCorrect(value_))
79  throw ConstraintException("Parameter::setConstraint", this, value_);
80 
82 }
83 
84 
85 std::shared_ptr<ConstraintInterface> Parameter::removeConstraint()
86 {
87  auto c = constraint_;
88  constraint_ = nullptr;
89  return c;
90 }
91 
92 /******************************************************************************/
93 
94 void Parameter::removeParameterListener(const std::string& listenerId)
95 {
96  listeners_.erase(std::remove_if(listeners_.begin(), listeners_.end(),
97  [&listenerId](std::shared_ptr<ParameterListener>& pl) {
98  return pl->getId() == listenerId; // put your condition here
99  }), listeners_.end());
100 }
101 
102 /******************************************************************************/
103 
104 bool Parameter::hasParameterListener(const std::string& listenerId)
105 {
106  for (auto& listener : listeners_)
107  {
108  if (listener->getId() == listenerId)
109  return true;
110  }
111  return false;
112 }
113 
114 /******************************************************************************/
115 
116 const std::shared_ptr<IntervalConstraint> Parameter::R_PLUS(new IntervalConstraint(true, 0, true));
117 const std::shared_ptr<IntervalConstraint> Parameter::R_PLUS_STAR(new IntervalConstraint(true, 0, false));
118 const std::shared_ptr<IntervalConstraint> Parameter::R_MINUS(new IntervalConstraint(false, 0, true));
119 const std::shared_ptr<IntervalConstraint> Parameter::R_MINUS_STAR(new IntervalConstraint(false, 0, false));
120 const std::shared_ptr<IntervalConstraint> Parameter::PROP_CONSTRAINT_IN(new IntervalConstraint(0, 1, true, true));
121 const std::shared_ptr<IntervalConstraint> Parameter::PROP_CONSTRAINT_EX(new IntervalConstraint(0, 1, false, false));
122 
123 /******************************************************************************/
double precision_
Definition: Parameter.h:103
static const std::shared_ptr< IntervalConstraint > R_PLUS
Definition: Parameter.h:307
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0...
Definition: Parameter.h:113
std::vector< std::shared_ptr< ParameterListener > > listeners_
Definition: Parameter.h:105
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:20
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:101
void fireParameterValueChanged(ParameterEvent &event)
Definition: Parameter.h:290
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
virtual void setConstraint(std::shared_ptr< ConstraintInterface > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:76
void setPrecision(double precision)
Set the precision of this parameter.
Definition: Parameter.cpp:69
std::string name_
Definition: Parameter.h:101
virtual void removeParameterListener(const std::string &listenerId)
Remove all listeners with a given id from this parameter.
Definition: Parameter.cpp:94
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:55
static const std::shared_ptr< IntervalConstraint > R_MINUS
Definition: Parameter.h:309
virtual ~Parameter()
Definition: Parameter.cpp:51
static const std::shared_ptr< IntervalConstraint > R_MINUS_STAR
Definition: Parameter.h:310
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
Definition: Parameter.h:312
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:311
double value_
Definition: Parameter.h:102
virtual bool hasParameterListener(const std::string &listenerId)
Tell is there is a listener with a given id from this parameter.
Definition: Parameter.cpp:104
std::shared_ptr< ConstraintInterface > constraint_
Definition: Parameter.h:104
Exception thrown when a value do not match a given constraint.
virtual const ConstraintInterface & constraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:203
Parameter & operator=(const Parameter &param)
Assignment operator.
Definition: Parameter.cpp:39
virtual std::shared_ptr< ConstraintInterface > removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:85
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
Definition: Parameter.h:308