Bridge++  Ver. 1.1.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  vout.paranoiac(m_vl, "ParameterManager_YAML::read_params: filesize = %d\n", filesize);
47 
48  Communicator::broadcast(1, &filesize, io_node);
49 
50  buf = new char [filesize];
51 
52  fin.read(buf, filesize);
53 
54  Communicator::Base::broadcast(filesize, buf, io_node);
55  } else {
56  // receive from io_node
57 
58  Communicator::broadcast(1, &filesize, io_node);
59 
60  buf = new char [filesize];
61 
62  Communicator::Base::broadcast(filesize, buf, io_node);
63  }
64 
65  std::istringstream iss(buf);
66  read_params(iss, params);
67 
68  delete [] buf;
69 }
70 
71 
72 //====================================================================
73 void ParameterManager_YAML::read_params(std::istream& fin, Parameters *params_top)
74 {
75  typedef std::pair<int, Parameters *> Level;
76 
77  int linesize = 256;
78  char *ch = new char[linesize];
79  string line;
80  string keystr, valstr;
81 
82  std::stack<Level> level_reserve;
83 
84  Level top_level(-1, params_top);
85  level_reserve.push(top_level);
86 
87  Parameters *params = params_top;
88 
89  int indent = 0;
90  int indent_prev = indent;
91 
92  while (fin.getline(ch, linesize))
93  {
94  indent_prev = indent;
95 
96  line = ch;
97  indent = set_key_and_value(keystr, valstr, line);
98  // extract key and value from line.
99 
100  vout.paranoiac(m_vl, "\nline: \"%s\" (%u)\n", line.c_str(), line.length());
101  vout.paranoiac(m_vl, " key = \"%s\"\n", keystr.c_str());
102  vout.paranoiac(m_vl, " val = \"%s\"\n", valstr.c_str());
103  vout.paranoiac(m_vl, " indent = %d\n", indent);
104  vout.paranoiac(m_vl, " val length = %u\n", valstr.length());
105 
106  if (indent == -1) {
107  indent = indent_prev;
108  continue;
109  }
110 
111  if (indent < indent_prev) {
112  // go to upper levels until appropriate indent level found.
113  while (!level_reserve.empty())
114  {
115  Level level = level_reserve.top();
116 
117  if (level.first < indent) {
118  params = level.second;
119  break;
120  }
121 
122  level_reserve.pop();
123  }
124 
125  vout.paranoiac(m_vl, " go back to parent parameters.\n");
126  }
127 
128  if (valstr.length() == 0) { // map key found.
129  // parameter entry for next level. skip if unused.
130  params = params ? params->get_Parameters(keystr) : 0;
131 
132  // preserve parameter entry and associated indent level.
133  Level level(indent, params);
134  level_reserve.push(level);
135 
136  vout.paranoiac(m_vl, " go to subparameters.\n");
137 
138  continue;
139  }
140 
141  if (params && params->find_int(keystr)) {
142  int val = atoi(valstr.c_str());
143  params->set_int(keystr, val);
144 
145  vout.paranoiac(m_vl, " int entry found for key = \"%s\", value = %d\n", keystr.c_str(), val);
146 
147  continue;
148  }
149 
150  if (params && params->find_double(keystr)) {
151 // double val = (valstr.c_str());
152  double val = EvalExpr(valstr).parse();
153  params->set_double(keystr, val);
154 
155  vout.paranoiac(m_vl, " double entry found for key = \"%s\", value = %f\n", keystr.c_str(), val);
156 
157  continue;
158  }
159 
160  if (params && params->find_int_vector(keystr)) {
161  valarray<int> vec;
162  convert_int_vector(vec, valstr);
163  params->set_int_vector(keystr, vec);
164 
165  vout.paranoiac(m_vl, " int_vector entry found for key = \"%s\"\n", keystr.c_str());
166 
167  continue;
168  }
169 
170  if (params && params->find_double_vector(keystr)) {
171  valarray<double> vec;
172  convert_double_vector(vec, valstr);
173  params->set_double_vector(keystr, vec);
174 
175  vout.paranoiac(m_vl, " double_vector entry found for key = \"%s\"\n", keystr.c_str());
176 
177  continue;
178  }
179 
180  if (params && params->find_string(keystr)) {
181  params->set_string(keystr, valstr);
182 
183  vout.paranoiac(m_vl, " string entry found for key = \"%s\", value = \"%s\"\n", keystr.c_str(), valstr.c_str());
184 
185  continue;
186  }
187 
188  // verbosity entry
189  if (params && (keystr == "verbose_level")) {
191  params->set_VerboseLevel(vl);
192 
193  vout.paranoiac(m_vl, " verbosity entry found, value = %s(%d)\n", valstr.c_str(), vl);
194 
195  continue;
196  }
197 
198  // vout.crucial(m_vl, "ParameterManager_YAML: entry not found.\n");
199  // Communicator::abort();
200  }
201 
202  delete [] ch;
203 }
204 
205 
206 //====================================================================
207 int ParameterManager_YAML::set_key_and_value(string& keystr, string& valstr, string& line)
208 {
209  string::size_type i2 = line.find("#");
210 
211  if (i2 != string::npos) {
212  line.erase(i2, line.length() - i2);
213  }
214 
215  string::size_type i1 = line.find(":");
216  // cout << " position of : = " << i1 << endl;
217  if (i1 == string::npos) {
218  // cout << "npos\n";
219  keystr = "";
220  valstr = "";
221  return -1; // no ":" in the line: regarded as a blank line
222  }
223 
224  keystr = line.substr(0, i1);
225  valstr = line.substr(i1 + 1, line.length() - i1 - 1);
226 
227  int indent = remove_space(keystr);
228  remove_space(valstr);
229 
230  return indent;
231 }
232 
233 
234 //====================================================================
236 {
237  int indent = 0;
238 
239  while (str.length() > 0 && str[0] == ' ')
240  {
241  str.erase(0, 1);
242  ++indent;
243  }
244 
245  while (str.length() > 0 && str[str.length() - 1] == ' ')
246  {
247  str.erase(str.length() - 1, 1);
248  }
249 
250  return indent;
251 }
252 
253 
254 //====================================================================
256  string& valstr)
257 {
258  int size_max = 20;
259 
260  valarray<int> vec_tmp(size_max);
261  int size = 0;
262 
263  // cout << valstr << "--" << endl;
264 
265  if (valstr[0] != '[') {
266  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
268  }
269  valstr.erase(0, 1);
270 
271  if (valstr[valstr.length() - 1] != ']') {
272  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
274  }
275  valstr.erase(valstr.length() - 1, 1);
276 
277  // cout << valstr << "--" << endl;
278 
279 
280  while (valstr.length() > 0)
281  {
282  string::size_type i1 = valstr.find(",");
283  // cout << " position of : = " << i1 << endl;
284  if (i1 != string::npos) {
285  string elem = valstr.substr(0, i1);
286  valstr.erase(0, i1 + 1);
287  remove_space(elem);
288  // cout << elem << "--" << endl;
289  vec_tmp[size] = atoi(elem.c_str());
290  ++size;
291  // cout << valstr << "--" << endl;
292  } else {
293  string elem = valstr;
294  valstr.erase(0, elem.length());
295  remove_space(elem);
296  // cout << elem << "--" << endl;
297  vec_tmp[size] = atoi(elem.c_str());
298  ++size;
299  }
300 
301  if (size > size_max) {
302  vout.crucial(m_vl, "ParameterManager_YAML: too large vector size.\n");
304  }
305  }
306 
307  vec.resize(size);
308  for (int i = 0; i < size; ++i) {
309  vec[i] = vec_tmp[i];
310  // cout << "vec[" << i << "] = " << vec[i] << endl;
311  }
312 }
313 
314 
315 //====================================================================
317  string& valstr)
318 {
319  int size_max = 20;
320 
321  valarray<double> vec_tmp(size_max);
322  int size = 0;
323 
324  // cout << valstr << "--" << endl;
325 
326  if (valstr[0] != '[') {
327  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
329  }
330  valstr.erase(0, 1);
331 
332  if (valstr[valstr.length() - 1] != ']') {
333  vout.crucial(m_vl, "ParameterManager_YAML: wrong format of vector.\n");
335  }
336  valstr.erase(valstr.length() - 1, 1);
337 
338  // cout << valstr << "--" << endl;
339 
340 
341  while (valstr.length() > 0)
342  {
343  string::size_type i1 = valstr.find(",");
344  // cout << " position of : = " << i1 << endl;
345  if (i1 != string::npos) {
346  string elem = valstr.substr(0, i1);
347  valstr.erase(0, i1 + 1);
348  remove_space(elem);
349  // cout << elem << "--" << endl;
350  // vec_tmp[size] = atof(elem.c_str());
351  vec_tmp[size] = EvalExpr(elem).parse();
352  ++size;
353  // cout << valstr << "--" << endl;
354  } else {
355  string elem = valstr;
356  valstr.erase(0, elem.length());
357  remove_space(elem);
358  // cout << elem << "--" << endl;
359  // vec_tmp[size] = atof(elem.c_str());
360  vec_tmp[size] = EvalExpr(elem).parse();
361  ++size;
362  }
363 
364  if (size > size_max) {
365  vout.crucial(m_vl, "ParameterManager_YAML: too large vector size.\n");
367  }
368  }
369 
370  vec.resize(size);
371  for (int i = 0; i < size; ++i) {
372  vec[i] = vec_tmp[i];
373  // cout << "vec[" << i << "] = " << vec[i] << endl;
374  }
375 }
376 
377 
378 //====================================================================
379 //============================================================END=====