bpp-core3  3.0.0
BppOParametrizableFormat.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 using namespace bpp;
8 
9 // From the STL:
10 #include <iomanip>
11 #include <algorithm>
12 
13 
14 using namespace std;
15 
17  OutputStream& out,
18  std::vector<std::string>& writtenNames,
19  bool printComma) const
20 {
21  ParameterList pl = parametrizable.getParameters();
22  int p = out.getPrecision();
23  out.setPrecision(12);
24  bool flag = printComma;
25  for (size_t i = 0; i < pl.size(); ++i)
26  {
27  if (find(writtenNames.begin(), writtenNames.end(), pl[i].getName()) == writtenNames.end())
28  {
29  if (flag)
30  out << ",";
31  else
32  flag = true;
33  string pname = parametrizable.getParameterNameWithoutNamespace(pl[i].getName());
34 
35  (out << pname << "=").enableScientificNotation(false) << pl[i].getValue();
36  }
37  }
38  out.setPrecision(p);
39 }
40 
42  OutputStream& out,
43  std::map<std::string, std::string>& globalAliases,
44  const std::vector<std::string>& names,
45  std::vector<std::string>& writtenNames,
46  bool printLocalAliases,
47  bool printComma) const
48 {
49  ParameterList pl = parametrizable.getIndependentParameters().createSubList(names);
50  int p = out.getPrecision();
51  out.setPrecision(12);
52  bool flag = printComma;
53  for (size_t i = 0; i < pl.size(); ++i)
54  {
55  if (find(writtenNames.begin(), writtenNames.end(), pl[i].getName()) == writtenNames.end())
56  {
57  if (flag)
58  out << ",";
59  else
60  flag = true;
61  string pname = parametrizable.getParameterNameWithoutNamespace(pl[i].getName());
62 
63  // Check for global aliases:
64  if (globalAliases.find(pl[i].getName()) == globalAliases.end())
65  {
66  (out << pname << "=").enableScientificNotation(false) << pl[i].getValue();
67  }
68  else
69  out << pname << "=" << globalAliases[pl[i].getName()];
70 
71  // Now check for local aliases:
72  if (printLocalAliases)
73  {
74  vector<string> aliases = parametrizable.getAlias(pname);
75  for (size_t j = 0; j < aliases.size(); ++j)
76  {
77  out << ", " << aliases[j] << "=" << pname;
78  }
79  }
80  writtenNames.push_back(pl[i].getName());
81  }
82  }
83  out.setPrecision(p);
84 }
virtual int getPrecision() const =0
Extend the Parametrizable interface with support for parameter aliases.
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
virtual std::vector< std::string > getAlias(const std::string &name) const =0
virtual const ParameterList & getParameters() const =0
Get all parameters available.
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This is the interface for all objects that imply parameters.
The parameter list object.
Definition: ParameterList.h:27
virtual OutputStream & setPrecision(int digit)=0
void write(const Parametrizable &parametrizable, OutputStream &out, std::vector< std::string > &writtenNames, bool printComma=false) const override
Write a Parametrizable to a stream.
virtual const ParameterList & getIndependentParameters() const =0
Get the minimal list of parameters to set the model.
OutputStream interface.
Definition: OutputStream.h:29
virtual std::string getParameterNameWithoutNamespace(const std::string &name) const =0
Resolves a parameter name according to the current namespace.