bpp-core3  3.0.0
BppOParametrizableFormat.cpp
Go to the documentation of this file.
1 //
2 // File: BppOParametrizableFormat.cpp
3 // Authors:
4 // Laurent Guéguen
5 // Created: lundi 3 septembre 2012, à 15h 37
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
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 
43 
44 using namespace bpp;
45 
46 // From the STL:
47 #include <iomanip>
48 #include <algorithm>
49 
50 
51 using namespace std;
52 
54  OutputStream& out,
55  std::vector<std::string>& writtenNames,
56  bool printComma) const
57 {
58  ParameterList pl = parametrizable->getParameters();
59  int p = out.getPrecision();
60  out.setPrecision(12);
61  bool flag = printComma;
62  for (size_t i = 0; i < pl.size(); ++i)
63  {
64  if (find(writtenNames.begin(), writtenNames.end(), pl[i].getName()) == writtenNames.end())
65  {
66  if (flag)
67  out << ",";
68  else
69  flag = true;
70  string pname = parametrizable->getParameterNameWithoutNamespace(pl[i].getName());
71 
72  (out << pname << "=").enableScientificNotation(false) << pl[i].getValue();
73  }
74  }
75  out.setPrecision(p);
76 }
77 
79  OutputStream& out,
80  std::map<std::string, std::string>& globalAliases,
81  const std::vector<std::string>& names,
82  std::vector<std::string>& writtenNames,
83  bool printLocalAliases,
84  bool printComma) const
85 {
86  ParameterList pl = parametrizable->getIndependentParameters().createSubList(names);
87  int p = out.getPrecision();
88  out.setPrecision(12);
89  bool flag = printComma;
90  for (size_t i = 0; i < pl.size(); ++i)
91  {
92  if (find(writtenNames.begin(), writtenNames.end(), pl[i].getName()) == writtenNames.end())
93  {
94  if (flag)
95  out << ",";
96  else
97  flag = true;
98  string pname = parametrizable->getParameterNameWithoutNamespace(pl[i].getName());
99 
100  // Check for global aliases:
101  if (globalAliases.find(pl[i].getName()) == globalAliases.end())
102  {
103  (out << pname << "=").enableScientificNotation(false) << pl[i].getValue();
104  }
105  else
106  out << pname << "=" << globalAliases[pl[i].getName()];
107 
108  // Now check for local aliases:
109  if (printLocalAliases)
110  {
111  vector<string> aliases = parametrizable->getAlias(pname);
112  for (size_t j = 0; j < aliases.size(); ++j)
113  {
114  out << ", " << aliases[j] << "=" << pname;
115  }
116  }
117  writtenNames.push_back(pl[i].getName());
118  }
119  }
120  out.setPrecision(p);
121 }
void write(const Parametrizable *parametrizable, OutputStream &out, std::vector< std::string > &writtenNames, bool printComma=false) const
Write a Parametrizable to a stream.
OutputStream interface.
Definition: OutputStream.h:67
virtual int getPrecision() const =0
virtual OutputStream & setPrecision(int digit)=0
Extend the Parametrizable interface with support for parameter aliases.
virtual const ParameterList & getIndependentParameters() const =0
Get the minimal list of parameters to set the model.
virtual std::vector< std::string > getAlias(const std::string &name) const =0
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
This is the interface for all objects that imply parameters.
virtual std::string getParameterNameWithoutNamespace(const std::string &name) const =0
Resolves a parameter name according to the current namespace.
virtual const ParameterList & getParameters() const =0
Get all parameters available.