Direct Graphical Models  v.1.7.0
BaseRandomModel.cpp
1 #include "BaseRandomModel.h"
2 #include "macroses.h"
3 
4 namespace DirectGraphicalModels
5 {
6 
7 void CBaseRandomModel::save(const std::string &path, const std::string &name, short idx) const
8 {
9  std::string fileName = generateFileName(path, name, idx);
10  FILE *pFile = fopen(fileName.c_str(), "wb");
11  if (!pFile) {
12  DGM_WARNING("Can't create file %s. Data was NOT saved.", fileName.c_str());
13  return;
14  }
15  saveFile(pFile);
16  fclose(pFile);
17 }
18 
19 void CBaseRandomModel::load(const std::string &path, const std::string &name, short idx)
20 {
21  std::string fileName = generateFileName(path, name, idx);
22  FILE *pFile = fopen(fileName.c_str(), "rb");
23  DGM_ASSERT_MSG(pFile, "Can't load data from %s", fileName.c_str());
24  loadFile(pFile);
25  fclose(pFile);
26 }
27 
28 std::string CBaseRandomModel::generateFileName(const std::string &path, const std::string &_name, short idx) const
29 {
30  std::string name;
31  if (_name.empty()) {
32  std::string className = typeid(*this).name();
33  name = className.substr(className.find("::") + 3);
34  } else
35  name = _name;
36  char str[7] = {0};
37  if (idx >= 0) sprintf(str, "_%05d", idx);
38  return path + name + str + ".dat";
39 }
40 }
std::string generateFileName(const std::string &path, const std::string &name, short idx) const
Generates name of the data file for storing random model parameters.
virtual void load(const std::string &path, const std::string &name=std::string(), short idx=-1)
Loads the training data.
virtual void save(const std::string &path, const std::string &name=std::string(), short idx=-1) const
Saves the training data.
virtual void saveFile(FILE *pFile) const =0
Saves the random model into the file.
virtual void loadFile(FILE *pFile)=0
Loads the random model from the file.