aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
O3prmBNReader_tpl.h
Go to the documentation of this file.
1 
30 // to ease Parser
32 
33 namespace gum {
34  template < typename GUM_SCALAR >
35  INLINE std::string
37  const std::string& type,
38  const std::string& name,
39  const std::string& toRemove) {
40  auto res = path + name; // path ends up with a "."
41  if (toRemove != "") {
42  if (res.substr(0, toRemove.size()) == toRemove) {
43  res = res.substr(toRemove.size());
44  }
45  }
46  return res;
47  }
48 
49  template < typename GUM_SCALAR >
50  std::string
51  O3prmBNReader< GUM_SCALAR >::__getInstanceName(const std::string& classname) {
52  auto res = classname.substr(0, 4);
53  std::transform(res.begin(), res.end(), res.begin(), ::tolower);
54  return res;
55  }
56 
57  template < typename GUM_SCALAR >
58  std::string
59  O3prmBNReader< GUM_SCALAR >::__getEntityName(const std::string& filename) {
60  auto b = filename.find_last_of("/\\");
61  auto e = filename.find_last_of(".") - 1;
62  GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
63  return filename.substr(b + 1, e - b);
64  }
65 
66  template < typename GUM_SCALAR >
68  const std::string& filename,
69  const std::string& entityName,
70  const std::string& classpath) :
71  BNReader< GUM_SCALAR >(bn, filename) {
72  GUM_CONSTRUCTOR(O3prmBNReader);
73  __bn = bn;
74  __filename = filename;
75  __entityName = entityName == "" ? __getEntityName(filename) : entityName;
76  __classpath = classpath;
77  }
78 
79  template < typename GUM_SCALAR >
81  GUM_DESTRUCTOR(O3prmBNReader);
82  }
83 
87  template < typename GUM_SCALAR >
90  if (__classpath != "") { reader.addClassPath(__classpath); }
91  reader.readFile(__filename);
92  gum::prm::PRM< GUM_SCALAR >* prm = reader.prm();
93  __errors = reader.errorsContainer();
94 
95 
96  if (errors() == 0) {
97  std::string instanceName = "";
98  if (prm->isSystem(__entityName)) {
100  } else if (prm->isClass(__entityName)) {
101  ParseError warn(
102  false,
103  "No system '" + __entityName
104  + "' found but class found. Generating unnamed instance.",
105  __filename,
106  0);
107  __errors.add(warn);
109  instanceName = __getInstanceName(__entityName);
111  instanceName, prm->getClass(__entityName));
112  s.add(i);
113  __generateBN(s);
114  instanceName += "."; // to be removed in __getVariableName
115  } else if (prm->classes().size() == 1) {
116  const std::string& entityName = (*prm->classes().begin())->name();
117  ParseError warn(false,
118  "Unique class '" + entityName
119  + "' found. Generating unnamed instance.",
120  __filename,
121  0);
122  __errors.add(warn);
123 
124  gum::prm::PRMSystem< GUM_SCALAR > s("S_" + entityName);
125  instanceName = __getInstanceName(entityName);
127  instanceName, prm->getClass(entityName));
128  s.add(i);
129  __generateBN(s);
130 
131  // force the name of the BN to be the name of the class instead of the name
132  // of the file
133  __bn->setProperty("name", entityName);
134  instanceName += "."; // to be removed in __getVariableName
135  } else {
136  ParseError err(true,
137  "Neither system nor class '" + __entityName
138  + "' and more than one class.",
139  __filename,
140  0);
141  __errors.add(err);
142  }
143 
144  // renaming variables in th BN
146  for (auto node: __bn->nodes()) {
147  // keeping the complete name in description
148  const std::string& nn = __bn->variable(node).name();
149  __bn->variable(node).setDescription(nn);
150 
151  // trying to simplify the
152  auto start = nn.find_first_of('(');
153  auto end = nn.find_first_of(')');
154  if (0 < start && start < end && end < nn.size()) {
155  auto path = nn.substr(0, start);
156  auto type = nn.substr(start + 1, end - start - 1);
157  auto name = nn.substr(end + 1, std::string::npos);
158 
159  std::string newNameRadical =
160  __getVariableName(path, type, name, instanceName);
161 
162  std::string newName = newNameRadical;
163  // forcing newName to be unique
164  int num = 0;
165  while (names.contains(newName)) {
166  newName = newNameRadical + std::to_string(++num);
167  }
168 
169  names.insert(newName);
170  __bn->changeVariableName(node, newName);
171  } else {
172  ParseError warn(
173  false, "Name " + nn + " cannot be simplified.", __filename, 0);
174  __errors.add(warn);
175  }
176  }
177  }
178 
179  delete prm;
180 
181  return errors();
182  }
183 
184 
185  template < typename GUM_SCALAR >
188  system.instantiate();
190  system.groundedBN(factory);
191  __bn->setProperty("name", __entityName);
192  }
193 } // namespace gum
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:583
Class representing a Bayesian Network.
Definition: BayesNet.h:78
bool isClass(const std::string &name) const
Definition: PRM_tpl.h:79
std::string __entityName
static std::string __getEntityName(const std::string &filename)
Size readFile(const std::string &file, const std::string &module="")
Read file and load its content using a PRMFactory. The package parameter set the file&#39;s content packa...
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
static std::string __getVariableName(const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")
std::string __filename
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
gum::prm::PRM< GUM_SCALAR > * prm()
Definition: O3prmReader.h:110
const Set< PRMClass< GUM_SCALAR > *> & classes() const
Returns the Set of all Class<GUM_SCALAR> in this PRM.
Definition: PRM_tpl.h:122
This class is used to represent parsing errors for the different parser implemented in aGrUM...
ErrorsContainer __errors
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
const ErrorsContainer & errorsContainer() const
publishing Errors API
void add(ParseError error)
Add an error object to the container.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Pure virtual class for reading a BN from a file.
Definition: BNReader.h:55
This class read O3PRM files and creates the corresponding gum::prm::PRM.
Definition: O3prmReader.h:69
void __generateBN(prm::PRMSystem< GUM_SCALAR > &system)
NodeId add(PRMInstance< GUM_SCALAR > *i)
Add an PRMInstance to this system.
Definition: PRMSystem_tpl.h:59
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
std::string __classpath
void groundedBN(BayesNetFactory< GUM_SCALAR > &factory) const
Returns the grounded Bayesian Network of this system.
Definition: PRMSystem_tpl.h:82
bool isSystem(const std::string &name) const
Definition: PRM_tpl.h:89
PRMSystem< GUM_SCALAR > & getSystem(const std::string &name)
Returns a constant reference on a PRMSystem<GUM_SCALAR> given it&#39;s name.
Definition: PRM_tpl.h:146
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:229
static std::string __getInstanceName(const std::string &classname)
void instantiate()
Instantiate all the PRMInstance in this PRMSystem.
void addClassPath(const std::string &class_path)
Add a list of paths to look for o3prm files.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
PRMClass< GUM_SCALAR > & getClass(const std::string &name)
Returns a constant reference on a Class<GUM_SCALAR> given it&#39;s name.
Definition: PRM_tpl.h:110
Size errors()
publishing Errors API
Definition: O3prmBNReader.h:75
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:615
Read an O3PRM and transform the gum::prm::PRMSystem into gum::BayesNet.
Definition: O3prmBNReader.h:57
Size proceed()
parse the file
A factory class to ease BayesNet construction.
Definition: BayesNet.h:45
BayesNet< GUM_SCALAR > * __bn