ModelConfig.cxx
Go to the documentation of this file.
1 #include "ModelConfig.h"
2 
3 #include <wordexp.h>
4 #include <boost/property_tree/json_parser.hpp>
5 
7 {
8  wordexp_t p;
9 
10  int s = wordexp(path.c_str(), &p, WRDE_NOCMD | WRDE_SHOWERR);
11 
12  if ((s != 0) || (p.we_wordc != 1)) {
13  wordfree(&p);
14  return path;
15  }
16 
17  std::string result(p.we_wordv[0]);
18  wordfree(&p);
19 
20  return result;
21 }
22 
24  const std::string &savedir,
25  const std::vector<InputConfigKeys> &scalarInputKeys,
26  const std::vector<InputConfigKeys> &vectorInputKeys,
27  const std::vector<std::string> &outputKeys
28 ) : savedir(ModelConfig::expandPath(savedir)),
29  loaded(false),
30  scalarInputKeys(scalarInputKeys),
31  vectorInputKeys(vectorInputKeys),
32  outputKeys(outputKeys)
33 {
34  scalarInputs.reserve(scalarInputKeys.size());
35  vectorInputs.reserve(vectorInputKeys.size());
36  outputNodes.reserve(outputKeys.size());
37 }
38 
40 {
41  return loaded;
42 }
43 
45 {
46  if (loaded) {
47  return;
48  }
49 
50  const std::string configPath = getConfigPath();
51  try {
52  parseConfig(configPath);
53  }
54  catch (const std::exception& e) {
55  throw std::runtime_error(
56  "Failed to parse config file: " + configPath + ". : " + e.what()
57  );
58  }
59 
60  loaded = true;
61 }
62 
64  pt::ptree &tree, const InputConfigKeys &keys
65 )
66 {
67  std::string nodeName = tree.get<std::string>(keys.nodeName);
68  std::vector<std::string> varNames;
69 
70  for (auto &stree : tree.get_child(keys.varNames)) {
71  varNames.push_back(stree.second.get_value<std::string>());
72  }
73 
74  return InputConfig{ std::move(nodeName), std::move(varNames) };
75 }
76 
78 {
79  pt::ptree tree;
80  pt::read_json(fname, tree);
81 
82  for (const auto &inputKeys : scalarInputKeys) {
83  scalarInputs.emplace_back(parseInput(tree, inputKeys));
84  }
85 
86  for (const auto &inputKeys : vectorInputKeys) {
87  vectorInputs.emplace_back(parseInput(tree, inputKeys));
88  }
89 
90  for (const auto &outputKey : outputKeys) {
91  outputNodes.emplace_back(tree.get<std::string>(outputKey));
92  }
93 }
94 
96 {
97  return savedir;
98 }
99 
101 {
102  return savedir + "/" + CONFIG_NAME;
103 }
104 
106 {
107  return savedir + "/" + MODEL_NAME;
108 }
109 
110 const std::vector<InputConfig>& ModelConfig::getScalarInputs() const
111 {
112  return scalarInputs;
113 }
114 
115 const std::vector<InputConfig>& ModelConfig::getVectorInputs() const
116 {
117  return vectorInputs;
118 }
119 
120 const std::vector<std::string>& ModelConfig::getOutputNodes() const
121 {
122  return outputNodes;
123 }
124 
std::string getConfigPath() const
std::vector< std::string > outputKeys
Definition: ModelConfig.h:129
std::vector< InputConfigKeys > scalarInputKeys
Definition: ModelConfig.h:127
std::string nodeName
Definition: ModelConfig.h:84
std::vector< std::string > outputNodes
Definition: ModelConfig.h:133
static QCString result
std::vector< InputConfigKeys > vectorInputKeys
Definition: ModelConfig.h:128
std::string string
Definition: nybbler.cc:12
void parseConfig(const std::string &fname)
Definition: ModelConfig.cxx:77
std::string getModelPath() const
std::string varNames
Definition: ModelConfig.h:85
std::string savedir
Definition: ModelConfig.h:118
std::string getSavedir() const
Definition: ModelConfig.cxx:95
static std::string expandPath(const std::string &path)
Definition: ModelConfig.cxx:6
const double e
std::vector< InputConfig > vectorInputs
Definition: ModelConfig.h:132
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
const std::vector< InputConfig > & getVectorInputs() const
const std::vector< std::string > & getOutputNodes() const
std::vector< InputConfig > scalarInputs
Definition: ModelConfig.h:131
static InputConfig parseInput(pt::ptree &tree, const InputConfigKeys &keys)
Definition: ModelConfig.cxx:63
const std::string CONFIG_NAME
Definition: ModelConfig.h:55
bool isLoaded() const
Definition: ModelConfig.cxx:39
const std::vector< InputConfig > & getScalarInputs() const
ModelConfig(const std::string &savedir, const std::vector< InputConfigKeys > &scalarInputKeys, const std::vector< InputConfigKeys > &vectorInputKeys, const std::vector< std::string > &outputKeys)
Definition: ModelConfig.cxx:23
static QCString * s
Definition: config.cpp:1042
const std::string MODEL_NAME
Definition: ModelConfig.h:56
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33