bpp-phyl3  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 <Bpp/Exceptions.h>
9 
10 
11 using namespace std;
12 
13 namespace bpp
14 {
15 // Parameter node
16 
17 ConfiguredParameter::ConfiguredParameter (const Context& context, NodeRefVec&& deps, const Parameter& parameter)
18  : Parameter(parameter), Value<Parameter*>(deps, this), context_(context)
19 {}
20 
22  : Parameter(std::move(parameter)), Value<Parameter*>(deps, this), context_(context)
23 {}
24 
26 
28 {
29  return "Parameter(" + getName () + ")\nvalue=" + std::to_string(getValue());
30 }
31 
32 std::string ConfiguredParameter::debugInfo () const
33 {
34  return "";
35 }
36 
37 std::string ConfiguredParameter::color () const
38 {
39  auto& name = getName();
40  if (name.substr(0, 5) == "BrLen")
41  return "#00ff00";
42  else
43  return "#ff8800";
44 }
45 
46 // Parameter node additional arguments = (type of bpp::Parameter).
47 // Everything else is determined by the node dependencies.
48 
50 {
51  const auto* derived = dynamic_cast<const Self*>(&other);
52  if (derived == nullptr)
53  {
54  return false;
55  }
56  else
57  {
58  return this->getName() == derived->getName();
59  }
60 }
61 
63 {
64  const auto& bppFS = *this;
65  return typeid (bppFS).hash_code ();
66 }
67 
69 {
70  if (&node == this)
71  {
73  }
74  else
75  return this->dependency(0)->derive(c, node);
76 }
77 
79 {
80  return ConfiguredParameter::create (c, NodeRefVec{NumericMutable<double>::create(c, accessValueConstCast<double>(*dependency(0)))}, *this);
81 }
82 
84 {
85  return ConfiguredParameter::create (c, std::move (deps), *this);
86 }
87 
89 {
90  // Update with the numerical dependency
91  auto& v = accessValueConstCast<double>(*this->dependency (0));
92  if (Parameter::getValue () != v)
93  {
95  }
96 }
97 
99 // ValueFromConfiguredParameter
100 
102  NodeRefVec&& deps)
103  : Value<double>(std::move (deps)) {}
104 
106 {
107  return dependency(0)->debugInfo();
108 }
109 
110 // ValueFromConfiguredParameter additional arguments = ().
112 {
113  const auto* derived = dynamic_cast<const Self*>(&other);
114  return derived != nullptr;
115 }
116 
117 std::shared_ptr<ValueFromConfiguredParameter> ValueFromConfiguredParameter::create (Context& c, NodeRefVec&& deps)
118 {
119  checkDependenciesNotNull (typeid (Self), deps);
120  checkDependencyVectorSize (typeid (Self), deps, 1);
121  checkNthDependencyIs<ConfiguredParameter>(typeid (Self), deps, 0);
122  return cachedAs<ValueFromConfiguredParameter>(c, std::make_shared<ValueFromConfiguredParameter>(std::move (deps)));
123 }
124 
126 {
127  if (&node == this)
128  {
130  }
131  else
132  return this->dependency(0)->derive(c, node);
133 }
134 
136 {
137  auto& name = accessValueConstCast<const ConfiguredParameter*>(*this->dependency (0))->getName();
138  if (name.substr(0, 5) == "BrLen")
139  return "#66ff66";
140  else
141  return "#ffcc00";
142 }
143 
145 {
146  return ValueFromConfiguredParameter::create (c, {std::move (deps)});
147 }
148 
150 {
151  const auto* param = accessValueConstCast<const ConfiguredParameter*>(*this->dependency (0));
152  this->accessValueMutable () = param->getValue();
153 }
154 } // namespace bpp
Data flow node representing a parameter.
Definition: Parameter.h:27
void compute() override
Computation implementation.
Definition: Parameter.cpp:88
NodeRef derive(Context &c, const Node_DF &node) override
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Parameter.cpp:68
std::size_t hashAdditionalArguments() const override
Return the hash of node-specific configuration.
Definition: Parameter.cpp:62
std::string description() const override
Node pretty name (default = type name).
Definition: Parameter.cpp:27
std::string color() const override
Definition: Parameter.cpp:37
double getValue() const override
Definition: Parameter.h:98
std::string debugInfo() const override
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Parameter.cpp:32
NodeRef recreate(Context &c, NodeRefVec &&deps) override
Recreate the node with different dependencies.
Definition: Parameter.cpp:83
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Parameter &param)
Build a new ConfiguredParameter node.
Definition: Parameter.h:36
bool compareAdditionalArguments(const Node_DF &other) const override
Compare node-specific configuration to another.
Definition: Parameter.cpp:49
ConfiguredParameter(const ConfiguredParameter &param)
Definition: Parameter.h:63
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
Context for dataflow node construction.
Definition: DataFlow.h:527
Base dataflow Node class.
Definition: DataFlow.h:152
const NodeRef & dependency(std::size_t i) const noexcept
Definition: DataFlow.h:185
static std::shared_ptr< Self > create(Context &, Args &&... args)
Build a new NumericMutable node with T(args...) value.
virtual void setValue(double value)
virtual double getValue() const
virtual const std::string & getName() const
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps)
Definition: Parameter.cpp:117
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Parameter.cpp:144
ValueFromConfiguredParameter(NodeRefVec &&deps)
Definition: Parameter.cpp:101
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Parameter.cpp:105
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Parameter.cpp:125
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
Definition: Parameter.cpp:111
void compute() final
Computation implementation.
Definition: Parameter.cpp:149
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
double & accessValueMutable() noexcept
Definition: DataFlow.h:416
Defines the basic types of data flow nodes.
void checkDependenciesNotNull(const std::type_info &contextNodeType, const NodeRefVec &deps)
Checks that all dependencies are not null, throws if not.
Definition: DataFlow.cpp:103
std::string to_string(const NoDimension &)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
void checkDependencyVectorSize(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t expectedSize)
Definition: DataFlow.cpp:83
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78
Specialisation of Dimension<T> for floating point types.