Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parameterManager_YAML.cpp
Go to the documentation of this file.
1 
14 #include "parameterManager_YAML.h"
15 #include "communicator.h"
16 #include <iostream>
17 #include <sstream>
18 #include <stack>
19 #include "evalexpr.h"
20 
21 using std::string;
22 using std::valarray;
23 using Bridge::vout;
24 
25 //====================================================================
26 void ParameterManager_YAML::read_params(const std::string& params_file, Parameters *params)
27 {
28  const int io_node = 0; // node id for file i/o.
29 
30  int filesize = 0;
31  char *buf = 0;
32 
33  if (Communicator::nodeid() == io_node) {
34  // load and distribute
35  std::ifstream fin(params_file.c_str());
36  if (!fin) {
37  vout.crucial(m_vl, "ParameterManager_YAML: unable to read parameter file: %s.\n", params_file.c_str());
38 
40  }
41 
42  fin.seekg(0, std::ios::end);
43  filesize = fin.tellg();
44  fin.seekg(0, std::ios::beg);
45 
46  int padding = 8 - (filesize % 8);
47 
48  vout.paranoiac(m_vl, "ParameterManager_YAML::read_params: filesize = %d, padding = %d\n", filesize, padding);
49 
50  filesize += padding;
51 
52  Communicator::broadcast(1, &filesize, io_node);
53 
54  buf = new char [filesize];
55  memset(buf, 0, filesize);
56 
57  fin.read(buf, filesize - padding);
58 
59  Communicator::Base::broadcast(filesize, buf, io_node);
60  } else {
61  // receive from io_node
62 
63  Communicator::broadcast(1, &filesize, io_node);
64 
65  buf = new char [filesize];
66  memset(buf, 0, filesize);
67 
68  Communicator::Base::broadcast(filesize, buf, io_node);
69  }
70 
71  std::istringstream iss(buf);
72  read_params(iss, params);
73 
74  delete [] buf;
75 }
76 
77 
78 //====================================================================
79 void ParameterManager_YAML::read_params(std::istream& fin, Parameters *params_top)
80 {
81  typedef std::pair<int, Parameters *> Level;
82 
83  int linesize = 256;
84  char *ch = new char[linesize];
85 
86  std::stack<Level> level_reserve;
87 
88  Level top_level(-1, params_top);
89  level_reserve.push(top_level);
90 
91  Parameters *params = params_top;
92 
93  int indent = 0;
94  int indent_prev = indent;
95 
96  while (fin.getline(ch, linesize))
97  {
98  indent_prev = indent;
99 
100  string line(ch);
101  string keystr, valstr;
102 
103  indent = set_key_and_value(keystr, valstr, line);
104  // extract key and value from line.
105 
106  vout.paranoiac(m_vl, "\nline: \"%s\" (%u)\n", line.c_str(), line.length());
107  vout.paranoiac(m_vl, " key = \"%s\"\n", keystr.c_str());
108  vout.paranoiac(m_vl, " val = \"%s\"\n", valstr.c_str());
109  vout.paranoiac(m_vl, " indent = %d\n", indent);
110  vout.paranoiac(m_vl, " val length = %u\n", valstr.length());
111 
112  if (indent == -1) {
113  indent = indent_prev;
114  continue;
115  }
116 
117  if (indent < indent_prev) {
118  // go to upper levels until appropriate indent level found.
119  while (!level_reserve.empty())
120  {
121  Level level = level_reserve.top();
122 
123  if (level.first < indent) {
124  params = level.second;
125  break;
126  }
127 
128  level_reserve.pop();
129  }
130 
131  vout.paranoiac(m_vl, " go back to parent parameters.\n");
132  }
133 
134  if (valstr.length() == 0) { // map key found.
135  // parameter entry for next level. skip if unused.
136  params = params ? params->get_Parameters(keystr) : 0;
137 
138  // preserve parameter entry and associated indent level.
139  Level level(indent, params);
140  level_reserve.push(level);
141 
142  vout.paranoiac(m_vl, " go to subparameters.\n");
143 
144  continue;
145  }
146 
147  if (params && params->find_int(keystr)) {
148  int val = atoi(valstr.c_str());
149  params->set_int(keystr, val);
150 
151  vout.paranoiac(m_vl, " int entry found for key = \"%s\", value = %d\n", keystr.c_str(), val);
152 
153  continue;
154  }
155 
156  if (params && params->find_double(keystr)) {
157 // double val = (valstr.c_str());
158  double val = EvalExpr(valstr).parse();
159  params->set_double(keystr, val);
160 
161  vout.paranoiac(m_vl, " double entry found for key = \"%s\", value = %f\n", keystr.c_str(), val);
162 
163  continue;
164  }
165 
166  if (params && params->find_int_vector(keystr)) {
167  valarray<int> vec;
168  convert_int_vector(vec, valstr);
169  params->set_int_vector(keystr, vec);
170 
171  vout.paranoiac(m_vl, " int_vector entry found for key = \"%s\"\n", keystr.c_str());
172 
173  continue;
174  }
175 
176  if (params && params->find_double_vector(keystr)) {
177  valarray<double> vec;
178  convert_double_vector(vec, valstr);
179  params->set_double_vector(keystr, vec);
180 
181  vout.paranoiac(m_vl, " double_vector entry found for key = \"%s\"\n", keystr.c_str());
182 
183  continue;
184  }
185 
186  if (params && params->find_string(keystr)) {
187  params->set_string(keystr, valstr);
188 
189  vout.paranoiac(m_vl, " string entry found for key = \"%s\", value = \"%s\"\n", keystr.c_str(), valstr.c_str());
190 
191  continue;
192  }
193 
194  // verbosity entry
195  if (params && (keystr == "verbose_level")) {
197  params->set_VerboseLevel(vl);
198 
199  vout.paranoiac(m_vl, " verbosity entry found, value = %s(%d)\n", valstr.c_str(), vl);
200 
201  continue;
202  }
203 
204  // vout.crucial(m_vl, "ParameterManager_YAML: entry not found.\n");
205  // Communicator::abort();
206  }
207 
208  delete [] ch;
209 }
210 
211 
212 //====================================================================
213 int ParameterManager_YAML::set_key_and_value(string& keystr, string& valstr, string& line)
214 {
215  string::size_type i2 = line.find("#");
216 
217  if (i2 != string::npos) {
218  line.erase(i2, line.length() - i2);
219  }
220 
221  string::size_type i1 = line.find(":");
222  // cout << " position of : = " << i1 << endl;
223  if (i1 == string::npos) {
224  // cout << "npos\n";
225  keystr = "";
226  valstr = "";
227  return -1; // no ":" in the line: regarded as a blank line
228  }
229 
230  keystr = line.substr(0, i1);
231  valstr = line.substr(i1 + 1, line.length() - i1 - 1);
232 
233  int indent = remove_space(keystr);
234  remove_space(valstr);
235 
236  return indent;
237 }
238 
239 
240 //====================================================================
242 {
243  int indent = 0;
244 
245  while (str.length() > 0 && str[0] == ' ')
246  {
247  str.erase(0, 1);
248  ++indent;
249  }
250 
251  while (str.length() > 0 && str[str.length() - 1] == ' ')
252  {
253  str.erase(str.length() - 1, 1);
254  }
255 
256  return indent;
257 }
258 
259 
260 //====================================================================
262  string& valstr)
263 {
264  int size_max = 20;
265 
266  valarray<int> vec_tmp(size_max);
267  int size = 0;
268 
269  // cout << valstr << "--" << endl;
270 
271  if (valstr[0] != '[') {
272  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
274  }
275  valstr.erase(0, 1);
276 
277  if (valstr[valstr.length() - 1] != ']') {
278  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
280  }
281  valstr.erase(valstr.length() - 1, 1);
282 
283  // cout << valstr << "--" << endl;
284 
285 
286  while (valstr.length() > 0)
287  {
288  string::size_type i1 = valstr.find(",");
289  // cout << " position of : = " << i1 << endl;
290  if (i1 != string::npos) {
291  string elem = valstr.substr(0, i1);
292  valstr.erase(0, i1 + 1);
293  remove_space(elem);
294  // cout << elem << "--" << endl;
295  vec_tmp[size] = atoi(elem.c_str());
296  ++size;
297  // cout << valstr << "--" << endl;
298  } else {
299  string elem = valstr;
300  valstr.erase(0, elem.length());
301  remove_space(elem);
302  // cout << elem << "--" << endl;
303  vec_tmp[size] = atoi(elem.c_str());
304  ++size;
305  }
306 
307  if (size > size_max) {
308  vout.crucial(m_vl, "ParameterManager_YAML: too large vector size.\n");
310  }
311  }
312 
313  vec.resize(size);
314  for (int i = 0; i < size; ++i) {
315  vec[i] = vec_tmp[i];
316  // cout << "vec[" << i << "] = " << vec[i] << endl;
317  }
318 }
319 
320 
321 //====================================================================
323  string& valstr)
324 {
325  int size_max = 20;
326 
327  valarray<double> vec_tmp(size_max);
328  int size = 0;
329 
330  // cout << valstr << "--" << endl;
331 
332  if (valstr[0] != '[') {
333  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
335  }
336  valstr.erase(0, 1);
337 
338  if (valstr[valstr.length() - 1] != ']') {
339  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
341  }
342  valstr.erase(valstr.length() - 1, 1);
343 
344  // cout << valstr << "--" << endl;
345 
346 
347  while (valstr.length() > 0)
348  {
349  string::size_type i1 = valstr.find(",");
350  // cout << " position of : = " << i1 << endl;
351  if (i1 != string::npos) {
352  string elem = valstr.substr(0, i1);
353  valstr.erase(0, i1 + 1);
354  remove_space(elem);
355  // cout << elem << "--" << endl;
356  // vec_tmp[size] = atof(elem.c_str());
357  vec_tmp[size] = EvalExpr(elem).parse();
358  ++size;
359  // cout << valstr << "--" << endl;
360  } else {
361  string elem = valstr;
362  valstr.erase(0, elem.length());
363  remove_space(elem);
364  // cout << elem << "--" << endl;
365  // vec_tmp[size] = atof(elem.c_str());
366  vec_tmp[size] = EvalExpr(elem).parse();
367  ++size;
368  }
369 
370  if (size > size_max) {
371  vout.crucial(m_vl, "ParameterManager_YAML: too large vector size.\n");
373  }
374  }
375 
376  vec.resize(size);
377  for (int i = 0; i < size; ++i) {
378  vec[i] = vec_tmp[i];
379  // cout << "vec[" << i << "] = " << vec[i] << endl;
380  }
381 }
382 
383 
384 //====================================================================
385 //============================================================END=====
bool find_int(const string &) const
Definition: parameters.cpp:220
BridgeIO vout
Definition: bridgeIO.cpp:207
void read_params(const std::string &params_file, Parameters *params)
read parameters from file.
static void abort()
terminate communicator
void set_int(const string &key, const int value)
Definition: parameters.cpp:262
bool find_double(const string &) const
Definition: parameters.cpp:227
double parse()
Definition: evalexpr.cpp:188
void convert_double_vector(std::valarray< double > &vec, std::string &valstr)
convert from string to double vector.
int remove_space(std::string &)
remove spaces from both side of a string.
static int broadcast(size_t size, void *data, int sender)
Class for parameters.
Definition: parameters.h:40
void set_VerboseLevel(Bridge::VerboseLevel value)
Definition: parameters.cpp:317
Parameters * get_Parameters(const string &key) const
Definition: parameters.cpp:100
void set_string(const string &key, const string &value)
Definition: parameters.cpp:298
bool find_string(const string &) const
Definition: parameters.cpp:248
int set_key_and_value(std::string &keystr, std::string &valstr, std::string &line)
extract key and value from a given line.
EvalExpr class for algebraic expression in parameter strings.
Definition: evalexpr.h:38
bool find_double_vector(const string &) const
Definition: parameters.cpp:241
void set_double_vector(const string &key, const std::valarray< double > &value)
Definition: parameters.cpp:289
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:62
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
void convert_int_vector(std::valarray< int > &vec, std::string &valstr)
convert from string to int vector.
void set_double(const string &key, const double value)
Definition: parameters.cpp:271
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:25
Bridge::VerboseLevel m_vl
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
static int nodeid()
alternative name for self().
Definition: communicator.h:88
void set_int_vector(const string &key, const std::valarray< int > &value)
Definition: parameters.cpp:280
bool find_int_vector(const string &) const
Definition: parameters.cpp:234
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:191