bpp-core3  3.0.0
AutoParameter.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 <iostream>
6 
7 #include "AutoParameter.h"
8 #include "NumConstants.h"
9 
10 using namespace std;
11 
12 // Utils:
13 #include "../Text/TextTools.h"
14 #include "../App/ApplicationTools.h"
15 
16 using namespace bpp;
17 
18 /******************************************************************************/
19 
20 string AutoParameter::CONSTRAINTS_AUTO = "auto";
21 string AutoParameter::CONSTRAINTS_IGNORE = "ignore";
22 string AutoParameter::CONSTRAINTS_KEEP = "keep";
23 
26 AutoParameter::AutoParameter(const std::string& name, double value, std::shared_ptr<ConstraintInterface> constraint) :
27  Parameter(name, value, constraint), messageHandler_(ApplicationTools::message) {}
28 
30 
32 
34 {
37  return *this;
38 }
39 
40 /******************************************************************************/
41 
42 void AutoParameter::setValue(double value)
43 {
44  try
45  {
46  // First we try to assign this value:
47  Parameter::setValue(value);
48  }
49  catch (ConstraintException& ce)
50  {
51  // Aie, there's a pb here...
52  if (messageHandler_)
53  {
54  (*messageHandler_) << "Constraint match at parameter ";
55  (*messageHandler_) << name_;
56  (*messageHandler_) << ", badValue = ";
57  (*messageHandler_) << ce.getBadValue();
58  (*messageHandler_) << " ";
59  (*messageHandler_ << constraint_->getDescription()).endLine();
60  }
61  double limit = constraint_->getAcceptedLimit(value);
62  try
63  { // We try to assign the limit then.
64  Parameter::setValue(limit);
65  }
66  catch (ConstraintException& ce2)
67  {
68  // Aie, the limit is not reachable, so we perform a smaller step...
69  // Parameter::setValue((getValue() + limit) / 2);
70  try
71  {
72  // Try on the right:
74  }
75  catch (ConstraintException& ce3)
76  {
77  // Try on the left:
79  }
80  }
81  }
82 }
83 
84 /******************************************************************************/
AutoParameter & operator=(const AutoParameter &param)
Assignment operator.
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
virtual double getBadValue() const
Get the value that doesn&#39;t match the constraint.
This class provides some common tools for developping applications.
std::string name_
Definition: Parameter.h:101
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:55
std::shared_ptr< OutputStream > messageHandler_
Definition: AutoParameter.h:27
static double TINY()
Definition: NumConstants.h:46
The AutoParameter class.
Definition: AutoParameter.h:23
AutoParameter(const std::string &name="", double value=0, std::shared_ptr< ConstraintInterface > constraint=nullptr)
Build a new AutoParameter.
std::shared_ptr< ConstraintInterface > constraint_
Definition: Parameter.h:104
Exception thrown when a value do not match a given constraint.
Parameter & operator=(const Parameter &param)
Assignment operator.
Definition: Parameter.cpp:39
virtual void setValue(double value)
Set the value of this parameter.