aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
gum::O3prmBNReader< GUM_SCALAR > Class Template Reference

Read an O3PRM and transform the gum::prm::PRMSystem into gum::BayesNet. More...

#include <agrum/PRM/o3prm/O3prmBNReader.h>

+ Inheritance diagram for gum::O3prmBNReader< GUM_SCALAR >:
+ Collaboration diagram for gum::O3prmBNReader< GUM_SCALAR >:

Public Member Functions

 O3prmBNReader (BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
 
 ~O3prmBNReader ()
 
Size proceed ()
 parse the file More...
 
Size errors ()
 publishing Errors API More...
 
Size warnings ()
 

of errors

More...
 
Idx errLine (Idx i)
 publishing Errors API More...
 
Idx errCol (Idx i)
 col of ith error or warning More...
 
bool errIsError (Idx i)
 type of ith error or warning More...
 
std::string errMsg (Idx i)
 message of ith error or warning More...
 
void showElegantErrors (std::ostream &o=std::cerr)
 send on std::cerr the list of errors More...
 
void showElegantErrorsAndWarnings (std::ostream &o=std::cerr)
 send on std::cerr the list of errors or warnings More...
 
void showErrorCounts (std::ostream &o=std::cerr)
 send on std::cerr the number of errors and the number of warnings More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::O3prmBNReader< GUM_SCALAR >

Read an O3PRM and transform the gum::prm::PRMSystem into gum::BayesNet.

Template Parameters
GUM_SCALARThe scalar type used both for the gum::prm::PRM and the gum::BayesNet.

Definition at line 57 of file O3prmBNReader.h.

Constructor & Destructor Documentation

◆ O3prmBNReader()

template<typename GUM_SCALAR >
gum::O3prmBNReader< GUM_SCALAR >::O3prmBNReader ( BayesNet< GUM_SCALAR > *  bn,
const std::string &  filename,
const std::string &  entityName = "",
const std::string &  classPath = "" 
)

Definition at line 67 of file O3prmBNReader_tpl.h.

References gum::O3prmBNReader< GUM_SCALAR >::bn__, gum::O3prmBNReader< GUM_SCALAR >::classpath__, gum::O3prmBNReader< GUM_SCALAR >::entityName__, gum::O3prmBNReader< GUM_SCALAR >::filename__, and gum::O3prmBNReader< GUM_SCALAR >::getEntityName__().

70  :
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  }
std::string classpath__
std::string filename__
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
BayesNet< GUM_SCALAR > * bn__
static std::string getEntityName__(const std::string &filename)
std::string entityName__
+ Here is the call graph for this function:

◆ ~O3prmBNReader()

template<typename GUM_SCALAR >
gum::O3prmBNReader< GUM_SCALAR >::~O3prmBNReader ( )

Definition at line 80 of file O3prmBNReader_tpl.h.

80  {
81  GUM_DESTRUCTOR(O3prmBNReader);
82  }
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")

Member Function Documentation

◆ errCol()

template<typename GUM_SCALAR >
Idx gum::O3prmBNReader< GUM_SCALAR >::errCol ( Idx  i)
inline

col of ith error or warning

Definition at line 81 of file O3prmBNReader.h.

References gum::ParseError::column, gum::ErrorsContainer::error(), and gum::O3prmBNReader< GUM_SCALAR >::errors__.

81 { return errors__.error(i).column; }
ErrorsContainer errors__
ParseError error(Idx i) const
Returns the i-th error.
Idx column
The column of this gum::ParseError, default is 0.
+ Here is the call graph for this function:

◆ errIsError()

template<typename GUM_SCALAR >
bool gum::O3prmBNReader< GUM_SCALAR >::errIsError ( Idx  i)
inline

type of ith error or warning

Definition at line 83 of file O3prmBNReader.h.

References gum::ErrorsContainer::error(), gum::O3prmBNReader< GUM_SCALAR >::errors__, and gum::ParseError::is_error.

83 { return errors__.error(i).is_error; }
ErrorsContainer errors__
bool is_error
If false, this gum::ParseError is a warning.
ParseError error(Idx i) const
Returns the i-th error.
+ Here is the call graph for this function:

◆ errLine()

template<typename GUM_SCALAR >
Idx gum::O3prmBNReader< GUM_SCALAR >::errLine ( Idx  i)
inline

publishing Errors API

of errors

Definition at line 79 of file O3prmBNReader.h.

References gum::ErrorsContainer::error(), gum::O3prmBNReader< GUM_SCALAR >::errors__, and gum::ParseError::line.

79 { return errors__.error(i).line; }
ErrorsContainer errors__
Idx line
The line of this gum::ParseError.
ParseError error(Idx i) const
Returns the i-th error.
+ Here is the call graph for this function:

◆ errMsg()

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::errMsg ( Idx  i)
inline

message of ith error or warning

Definition at line 85 of file O3prmBNReader.h.

References gum::ErrorsContainer::error(), gum::O3prmBNReader< GUM_SCALAR >::errors__, and gum::ParseError::msg.

85 { return errors__.error(i).msg; }
std::string msg
The gum::ParseError message.
ErrorsContainer errors__
ParseError error(Idx i) const
Returns the i-th error.
+ Here is the call graph for this function:

◆ errors()

template<typename GUM_SCALAR >
Size gum::O3prmBNReader< GUM_SCALAR >::errors ( )
inline

publishing Errors API

of errors

Definition at line 75 of file O3prmBNReader.h.

References gum::ErrorsContainer::error_count, and gum::O3prmBNReader< GUM_SCALAR >::errors__.

Referenced by gum::O3prmBNReader< GUM_SCALAR >::proceed().

75 { return errors__.error_count; }
Size error_count
Number of errors detected.
ErrorsContainer errors__
+ Here is the caller graph for this function:

◆ generateBN__()

template<typename GUM_SCALAR >
void gum::O3prmBNReader< GUM_SCALAR >::generateBN__ ( prm::PRMSystem< GUM_SCALAR > &  system)
private

Definition at line 186 of file O3prmBNReader_tpl.h.

References gum::O3prmBNReader< GUM_SCALAR >::bn__, gum::O3prmBNReader< GUM_SCALAR >::entityName__, gum::prm::PRMSystem< GUM_SCALAR >::groundedBN(), and gum::prm::PRMSystem< GUM_SCALAR >::instantiate().

Referenced by gum::O3prmBNReader< GUM_SCALAR >::proceed().

187  {
188  system.instantiate();
189  BayesNetFactory< GUM_SCALAR > factory(bn__);
190  system.groundedBN(factory);
191  bn__->setProperty("name", entityName__);
192  }
BayesNet< GUM_SCALAR > * bn__
std::string entityName__
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getEntityName__()

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::getEntityName__ ( const std::string &  filename)
staticprivate

Definition at line 59 of file O3prmBNReader_tpl.h.

Referenced by gum::O3prmBNReader< GUM_SCALAR >::O3prmBNReader().

59  {
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  }
+ Here is the caller graph for this function:

◆ getInstanceName__()

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::getInstanceName__ ( const std::string &  classname)
staticprivate

Definition at line 51 of file O3prmBNReader_tpl.h.

Referenced by gum::O3prmBNReader< GUM_SCALAR >::proceed().

51  {
52  auto res = classname.substr(0, 4);
53  std::transform(res.begin(), res.end(), res.begin(), ::tolower);
54  return res;
55  }
+ Here is the caller graph for this function:

◆ getVariableName__()

template<typename GUM_SCALAR >
INLINE std::string gum::O3prmBNReader< GUM_SCALAR >::getVariableName__ ( const std::string &  path,
const std::string &  type,
const std::string &  name,
const std::string &  toRemove = "" 
)
staticprivate

Definition at line 36 of file O3prmBNReader_tpl.h.

Referenced by gum::O3prmBNReader< GUM_SCALAR >::proceed().

39  {
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  }
+ Here is the caller graph for this function:

◆ proceed()

template<typename GUM_SCALAR >
Size gum::O3prmBNReader< GUM_SCALAR >::proceed ( )
virtual

parse the file

parse.

Returns
the number of detected errors and warnings
Exceptions
IOErrorif file not exists
Returns
the number of detected errors
Exceptions
IOErrorif file not exists

Implements gum::BNReader< GUM_SCALAR >.

Definition at line 88 of file O3prmBNReader_tpl.h.

References gum::prm::PRMSystem< GUM_SCALAR >::add(), gum::ErrorsContainer::add(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::addClassPath(), gum::O3prmBNReader< GUM_SCALAR >::bn__, gum::prm::PRM< GUM_SCALAR >::classes(), gum::O3prmBNReader< GUM_SCALAR >::classpath__, gum::Set< Key, Alloc >::contains(), gum::O3prmBNReader< GUM_SCALAR >::entityName__, gum::O3prmBNReader< GUM_SCALAR >::errors(), gum::O3prmBNReader< GUM_SCALAR >::errors__, gum::prm::o3prm::O3prmReader< GUM_SCALAR >::errorsContainer(), gum::O3prmBNReader< GUM_SCALAR >::filename__, gum::O3prmBNReader< GUM_SCALAR >::generateBN__(), gum::prm::PRM< GUM_SCALAR >::getClass(), gum::O3prmBNReader< GUM_SCALAR >::getInstanceName__(), gum::prm::PRM< GUM_SCALAR >::getSystem(), gum::O3prmBNReader< GUM_SCALAR >::getVariableName__(), gum::Set< Key, Alloc >::insert(), gum::prm::PRM< GUM_SCALAR >::isClass(), gum::prm::PRM< GUM_SCALAR >::isSystem(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::prm(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::readFile(), and gum::to_string().

88  {
89  prm::o3prm::O3prmReader< GUM_SCALAR > reader;
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  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
std::string classpath__
std::string filename__
bool isClass(const std::string &name) const
Definition: PRM_tpl.h:79
void add(NodeId id, PRMInstance< GUM_SCALAR > &instance)
Add an PRMInstance<GUM_SCALAR> to a given PRMReferenceSlot, PRMSlotChain<GUM_SCALAR> or output node...
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
const Set< PRMClass< GUM_SCALAR > *> & classes() const
Returns the Set of all Class<GUM_SCALAR> in this PRM.
Definition: PRM_tpl.h:122
static std::string getInstanceName__(const std::string &classname)
void add(ParseError error)
Add an error object to the container.
ErrorsContainer errors__
static std::string getVariableName__(const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")
std::string to_string(const Formula &f)
Definition: formula_inl.h:499
void generateBN__(prm::PRMSystem< GUM_SCALAR > &system)
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
BayesNet< GUM_SCALAR > * bn__
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
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
std::string entityName__
+ Here is the call graph for this function:

◆ showElegantErrors()

template<typename GUM_SCALAR >
void gum::O3prmBNReader< GUM_SCALAR >::showElegantErrors ( std::ostream &  o = std::cerr)
inline

send on std::cerr the list of errors

Definition at line 88 of file O3prmBNReader.h.

References gum::ErrorsContainer::elegantErrors(), and gum::O3prmBNReader< GUM_SCALAR >::errors__.

88  {
90  }
ErrorsContainer errors__
void elegantErrors(std::ostream &o) const
Print errors on output stream.
+ Here is the call graph for this function:

◆ showElegantErrorsAndWarnings()

template<typename GUM_SCALAR >
void gum::O3prmBNReader< GUM_SCALAR >::showElegantErrorsAndWarnings ( std::ostream &  o = std::cerr)
inline

send on std::cerr the list of errors or warnings

Definition at line 93 of file O3prmBNReader.h.

References gum::ErrorsContainer::elegantErrorsAndWarnings(), and gum::O3prmBNReader< GUM_SCALAR >::errors__.

93  {
95  }
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
ErrorsContainer errors__
+ Here is the call graph for this function:

◆ showErrorCounts()

template<typename GUM_SCALAR >
void gum::O3prmBNReader< GUM_SCALAR >::showErrorCounts ( std::ostream &  o = std::cerr)
inline

send on std::cerr the number of errors and the number of warnings

Definition at line 98 of file O3prmBNReader.h.

References gum::O3prmBNReader< GUM_SCALAR >::errors__, and gum::ErrorsContainer::syntheticResults().

98  {
100  }
void syntheticResults(std::ostream &o) const
Print errors on output stream.
ErrorsContainer errors__
+ Here is the call graph for this function:

◆ warnings()

template<typename GUM_SCALAR >
Size gum::O3prmBNReader< GUM_SCALAR >::warnings ( )
inline

Member Data Documentation

◆ bn__

template<typename GUM_SCALAR >
BayesNet< GUM_SCALAR >* gum::O3prmBNReader< GUM_SCALAR >::bn__
private

◆ classpath__

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::classpath__
private

◆ entityName__

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::entityName__
private

◆ errors__

◆ filename__

template<typename GUM_SCALAR >
std::string gum::O3prmBNReader< GUM_SCALAR >::filename__
private

The documentation for this class was generated from the following files: