CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Private Member Functions | Private Attributes
mkfit::ConfigJsonPatcher Class Reference

#include <IterationConfig.h>

Classes

struct  PatchReport
 

Public Member Functions

void cd (const std::string &path)
 
void cd_top (const std::string &path="")
 
void cd_up (const std::string &path="")
 
 ConfigJsonPatcher (bool verbose=false)
 
std::string dump (int indent=2)
 
nlohmann::jsonget (const std::string &path)
 
template<class T >
void load (const T &o)
 
template<typename T >
void replace (const std::string &path, T val)
 
template<typename T >
void replace (int first, int last, const std::string &path, T val)
 
int replace (const nlohmann::json &j)
 
template<>
void save (IterationsInfo &o)
 
template<class T >
void save (T &o)
 
 ~ConfigJsonPatcher ()
 

Private Member Functions

std::string exc_hdr (const char *func=nullptr) const
 
std::string get_abs_path () const
 

Private Attributes

nlohmann::jsonm_current = nullptr
 
std::unique_ptr< nlohmann::jsonm_json
 
std::vector< nlohmann::json * > m_json_stack
 
std::vector< std::string > m_path_stack
 
bool m_verbose = false
 

Detailed Description

Definition at line 305 of file IterationConfig.h.

Constructor & Destructor Documentation

◆ ConfigJsonPatcher()

mkfit::ConfigJsonPatcher::ConfigJsonPatcher ( bool  verbose = false)

Definition at line 226 of file IterationConfig.cc.

◆ ~ConfigJsonPatcher()

mkfit::ConfigJsonPatcher::~ConfigJsonPatcher ( )
default

Member Function Documentation

◆ cd()

void mkfit::ConfigJsonPatcher::cd ( const std::string &  path)

◆ cd_top()

void mkfit::ConfigJsonPatcher::cd_top ( const std::string &  path = "")

Definition at line 295 of file IterationConfig.cc.

References cd(), m_current, m_json, m_json_stack, m_path_stack, and castor_dqm_sourceclient_file_cfg::path.

Referenced by load(), mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

295  {
296  m_current = m_json.get();
297  m_json_stack.clear();
298  m_path_stack.clear();
299  if (!path.empty())
300  cd(path);
301  }
nlohmann::json * m_current
std::vector< nlohmann::json * > m_json_stack
void cd(const std::string &path)
std::vector< std::string > m_path_stack
std::unique_ptr< nlohmann::json > m_json

◆ cd_up()

void mkfit::ConfigJsonPatcher::cd_up ( const std::string &  path = "")

Definition at line 284 of file IterationConfig.cc.

References cd(), m_current, m_json_stack, m_path_stack, and castor_dqm_sourceclient_file_cfg::path.

Referenced by replace().

284  {
285  if (m_json_stack.empty())
286  throw std::runtime_error("JSON stack empty on cd_up");
287 
288  m_current = m_json_stack.back();
289  m_json_stack.pop_back();
290  m_path_stack.pop_back();
291  if (!path.empty())
292  cd(path);
293  }
nlohmann::json * m_current
std::vector< nlohmann::json * > m_json_stack
void cd(const std::string &path)
std::vector< std::string > m_path_stack

◆ dump()

std::string mkfit::ConfigJsonPatcher::dump ( int  indent = 2)

Definition at line 444 of file IterationConfig.cc.

References validateAlignments::indent, and m_json.

Referenced by mkfit::ConfigJson::test_Patcher().

444 { return m_json->dump(indent); }
std::unique_ptr< nlohmann::json > m_json

◆ exc_hdr()

std::string mkfit::ConfigJsonPatcher::exc_hdr ( const char *  func = nullptr) const
private

Definition at line 238 of file IterationConfig.cc.

References EcalMonitorTask_cff::func, get_abs_path(), alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by replace().

238  {
239  std::string s;
240  s.reserve(128);
241  s = "ConfigJsonPatcher";
242  if (func) {
243  s += "::";
244  s += func;
245  }
246  s += " '";
247  s += get_abs_path();
248  s += "' ";
249  return s;
250  }
std::string get_abs_path() const

◆ get()

nlohmann::json & mkfit::ConfigJsonPatcher::get ( const std::string &  path)

◆ get_abs_path()

std::string mkfit::ConfigJsonPatcher::get_abs_path ( ) const
private

Definition at line 230 of file IterationConfig.cc.

References m_path_stack, AlCaHLTBitMon_ParallelJobs::p, alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by exc_hdr(), and replace().

230  {
231  std::string s;
232  s.reserve(64);
233  for (auto &p : m_path_stack)
234  s += p;
235  return s;
236  }
std::vector< std::string > m_path_stack

◆ load()

template<class T >
template void mkfit::ConfigJsonPatcher::load< IterationConfig > ( const T o)

Definition at line 253 of file IterationConfig.cc.

References cd_top(), m_json, and EcalTangentSkim_cfg::o.

Referenced by mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

253  {
254  m_json = std::make_unique<nlohmann::json>();
255  *m_json = o;
256  cd_top();
257  }
std::unique_ptr< nlohmann::json > m_json
void cd_top(const std::string &path="")

◆ replace() [1/3]

template<typename T >
void mkfit::ConfigJsonPatcher::replace ( const std::string &  path,
T  val 
)

◆ replace() [2/3]

template<typename T >
void mkfit::ConfigJsonPatcher::replace ( int  first,
int  last,
const std::string &  path,
T  val 
)

Definition at line 313 of file IterationConfig.cc.

References dqmdumpme::first, mps_fire::i, dqmdumpme::last, m_current, castor_dqm_sourceclient_file_cfg::path, and heppy_batch::val.

313  {
314  nlohmann::json::json_pointer jp(path);
315  for (int i = first; i <= last; ++i) {
316  m_current->at(i).at(jp) = val;
317  }
318  }
nlohmann::json * m_current

◆ replace() [3/3]

int mkfit::ConfigJsonPatcher::replace ( const nlohmann::json j)

Definition at line 328 of file IterationConfig.cc.

References cd(), cd_up(), gather_cfg::cout, exc_hdr(), dqmdumpme::first, get_abs_path(), mps_fire::i, createfilelist::int, dqmiolumiharvest::j, submitPVResolutionJobs::key, dqmdumpme::last, visualization-live-secondInstance_cfg::m, m_current, m_verbose, replace(), alignCSCRings::s, AlCaHLTBitMon_QueryRunRegistry::string, to_string(), and relativeConstraints::value.

328  {
329  if (j.is_null())
330  throw std::runtime_error(exc_hdr(__func__) + "null not expected");
331 
332  if (j.is_boolean() || j.is_number() || j.is_string()) {
333  throw std::runtime_error(exc_hdr(__func__) + "value not expected on this parsing level");
334  }
335 
336  int n_replaced = 0;
337 
338  if (j.is_object()) {
339  static const std::regex index_range_re("^\\[(\\d+)..(\\d+)\\]$", std::regex::optimize);
340 
341  for (auto &[key, value] : j.items()) {
342  std::smatch m;
343  std::regex_search(key, m, index_range_re);
344 
345  if (m.size() == 3) {
346  if (!m_current->is_array())
347  throw std::runtime_error(exc_hdr(__func__) + "array range encountered when current json is not an array");
348  int first = std::stoi(m.str(1));
349  int last = std::stoi(m.str(2));
350  for (int i = first; i <= last; ++i) {
351  std::string s("/");
352  s += std::to_string(i);
353  cd(s);
354  if (value.is_array()) {
355  for (auto &el : value)
356  n_replaced += replace(el);
357  } else {
358  n_replaced += replace(value);
359  }
360  cd_up();
361  }
362  } else if (value.is_array() || value.is_object()) {
363  std::string s("/");
364  s += key;
365  cd(s);
366  n_replaced += replace(value);
367  cd_up();
368  } else if (value.is_number() || value.is_boolean() || value.is_string()) {
369  std::string s("/");
370  s += key;
371  nlohmann::json::json_pointer jp(s);
372  if (m_current->at(jp) != value) {
373  if (m_verbose)
374  std::cout << " " << get_abs_path() << s << ": " << m_current->at(jp) << " -> " << value << "\n";
375 
376  m_current->at(jp) = value;
377  ++n_replaced;
378  }
379  } else {
380  throw std::runtime_error(exc_hdr(__func__) + "unexpected value type");
381  }
382  }
383  } else if (j.is_array() && j.empty()) {
384  } else if (j.is_array()) {
385  // Arrays are somewhat tricky.
386  // At the moment all elements are expected to be objects.
387  // This means arrays of basic types are not supported (like layer index arrays).
388  // Should not be too hard to add support for this.
389  // Now, the objects in the array can be of two kinds:
390  // a) Their keys can be json_pointer strings starting with numbers or ranges [i_low..i_high].
391  // b) They can be actual elements of the array. In this case we require the length of
392  // the array to be equal to existing length in the configuration.
393  // It is not allowed for these two kinds to mix.
394 
395  // Determine the kind of array: json_ptr or object
396 
397  static const std::regex index_re("^(?:\\[\\d+..\\d+\\]|\\d+(?:/.*)?)$", std::regex::optimize);
398 
399  bool has_index = false, has_plain = false;
400  for (int i = 0; i < (int)j.size(); ++i) {
401  const nlohmann::json &el = j[i];
402 
403  if (!el.is_object())
404  throw std::runtime_error(exc_hdr(__func__) + "array elements expected to be objects");
405 
406  for (nlohmann::json::const_iterator it = el.begin(); it != el.end(); ++it) {
407  if (std::regex_search(it.key(), index_re)) {
408  has_index = true;
409  if (has_plain)
410  throw std::runtime_error(exc_hdr(__func__) + "indexed array entry following plain one");
411  } else {
412  has_plain = true;
413  if (has_index)
414  throw std::runtime_error(exc_hdr(__func__) + "plain array entry following indexed one");
415  }
416  }
417  }
418  if (has_index) {
419  for (auto &element : j) {
420  n_replaced += replace(element);
421  }
422  } else {
423  if (m_current && !m_current->is_array())
424  throw std::runtime_error(exc_hdr(__func__) + "plain array detected when current is not an array");
425  if (m_current->size() != j.size())
426  throw std::runtime_error(exc_hdr(__func__) + "plain array of different size than at current pos");
427 
428  std::string s;
429  for (int i = 0; i < (int)j.size(); ++i) {
430  s = "/";
431  s += std::to_string(i);
432  cd(s);
433  n_replaced += replace(j[i]);
434  cd_up();
435  }
436  }
437  } else {
438  throw std::runtime_error(exc_hdr(__func__) + "unexpected json type");
439  }
440 
441  return n_replaced;
442  }
nlohmann::json * m_current
nlohmann::json json
static std::string to_string(const XMLCh *ch)
void replace(const std::string &path, T val)
std::string exc_hdr(const char *func=nullptr) const
key
prepare the HTCondor submission files and eventually submit them
void cd(const std::string &path)
Definition: value.py:1
void cd_up(const std::string &path="")
std::string get_abs_path() const

◆ save() [1/2]

template<>
void mkfit::ConfigJsonPatcher::save ( IterationsInfo o)

Definition at line 270 of file IterationConfig.cc.

References mkfit::from_json(), mps_fire::i, and EcalTangentSkim_cfg::o.

270  {
271  auto &itc_arr = m_json->at("m_iterations");
272  for (int i = 0; i < o.size(); ++i) {
273  from_json(itc_arr[i], o[i]);
274  }
275  }
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
std::unique_ptr< nlohmann::json > m_json

◆ save() [2/2]

template<class T >
template void mkfit::ConfigJsonPatcher::save< IterationConfig > ( T o)

Definition at line 262 of file IterationConfig.cc.

References mkfit::from_json(), m_json, and EcalTangentSkim_cfg::o.

Referenced by mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

262  {
263  from_json(*m_json, o);
264  }
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
std::unique_ptr< nlohmann::json > m_json

Member Data Documentation

◆ m_current

nlohmann::json* mkfit::ConfigJsonPatcher::m_current = nullptr
private

Definition at line 327 of file IterationConfig.h.

Referenced by cd(), cd_top(), cd_up(), get(), and replace().

◆ m_json

std::unique_ptr<nlohmann::json> mkfit::ConfigJsonPatcher::m_json
private

Definition at line 326 of file IterationConfig.h.

Referenced by cd_top(), dump(), load(), and save().

◆ m_json_stack

std::vector<nlohmann::json *> mkfit::ConfigJsonPatcher::m_json_stack
private

Definition at line 330 of file IterationConfig.h.

Referenced by cd(), cd_top(), and cd_up().

◆ m_path_stack

std::vector<std::string> mkfit::ConfigJsonPatcher::m_path_stack
private

Definition at line 331 of file IterationConfig.h.

Referenced by cd(), cd_top(), cd_up(), and get_abs_path().

◆ m_verbose

bool mkfit::ConfigJsonPatcher::m_verbose = false
private

Definition at line 333 of file IterationConfig.h.

Referenced by replace().