aGrUM  0.20.2
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 56 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 66 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

69  :
70  BNReader< GUM_SCALAR >(bn, filename) {
71  GUM_CONSTRUCTOR(O3prmBNReader);
72  bn__ = bn;
73  filename__ = filename;
74  entityName__ = entityName == "" ? getEntityName__(filename) : entityName;
75  classpath__ = classpath;
76  }
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 79 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

79  {
80  GUM_DESTRUCTOR(O3prmBNReader);
81  }
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
+ Here is the call graph for this function:

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 80 of file O3prmBNReader.h.

80 { 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.

◆ errIsError()

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

type of ith error or warning

Definition at line 82 of file O3prmBNReader.h.

82 { 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.

◆ errLine()

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

publishing Errors API

of errors

Definition at line 78 of file O3prmBNReader.h.

78 { 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.

◆ errMsg()

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

message of ith error or warning

Definition at line 84 of file O3prmBNReader.h.

84 { return errors__.error(i).msg; }
std::string msg
The gum::ParseError message.
ErrorsContainer errors__
ParseError error(Idx i) const
Returns the i-th error.

◆ errors()

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

publishing Errors API

of errors

Definition at line 74 of file O3prmBNReader.h.

74 { return errors__.error_count; }
Size error_count
Number of errors detected.
ErrorsContainer errors__

◆ generateBN__()

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

Definition at line 189 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

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

◆ getEntityName__()

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

Definition at line 58 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

58  {
59  auto b = filename.find_last_of("/\\");
60  auto e = filename.find_last_of(".") - 1;
61  GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
62  return filename.substr(b + 1, e - b);
63  }
+ Here is the call graph for this function:

◆ getInstanceName__()

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

Definition at line 50 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

50  {
51  auto res = classname.substr(0, 4);
52  std::transform(res.begin(), res.end(), res.begin(), ::tolower);
53  return res;
54  }
+ Here is the call 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 35 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

38  {
39  auto res = path + name; // path ends up with a "."
40  if (toRemove != "") {
41  if (res.substr(0, toRemove.size()) == toRemove) {
42  res = res.substr(toRemove.size());
43  }
44  }
45  return res;
46  }
+ Here is the call 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 87 of file O3prmBNReader_tpl.h.

References gum::Set< Key, Alloc >::emplace().

87  {
88  prm::o3prm::O3prmReader< GUM_SCALAR > reader;
89  if (classpath__ != "") { reader.addClassPath(classpath__); }
90  reader.readFile(filename__);
91  gum::prm::PRM< GUM_SCALAR >* prm = reader.prm();
92  errors__ = reader.errorsContainer();
93 
94 
95  if (errors() == 0) {
96  std::string instanceName = "";
97  if (prm->isSystem(entityName__)) {
99  } else if (prm->isClass(entityName__)) {
100  ParseError warn(
101  false,
102  "No system '" + entityName__
103  + "' found but class found. Generating unnamed instance.",
104  filename__,
105  0);
106  errors__.add(warn);
108  instanceName = getInstanceName__(entityName__);
109  auto i
110  = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName,
111  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);
126  auto i
127  = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName,
128  prm->getClass(entityName));
129  s.add(i);
130  generateBN__(s);
131 
132  // force the name of the BN to be the name of the class instead of the name
133  // of the file
134  bn__->setProperty("name", entityName);
135  instanceName += "."; // to be removed in getVariableName__
136  } else {
137  ParseError err(true,
138  "Neither system nor class '" + entityName__
139  + "' and more than one class.",
140  filename__,
141  0);
142  errors__.add(err);
143  }
144 
145  // renaming variables in th BN
147  for (auto node: bn__->nodes()) {
148  // keeping the complete name in description
149  const std::string& nn = bn__->variable(node).name();
150  bn__->variable(node).setDescription(nn);
151 
152  // trying to simplify the
153  auto start = nn.find_first_of('(');
154  auto end = nn.find_first_of(')');
155  if (0 < start && start < end && end < nn.size()) {
156  auto path = nn.substr(0, start);
157  auto type = nn.substr(start + 1, end - start - 1);
158  auto name = nn.substr(end + 1, std::string::npos);
159 
160  std::string newNameRadical
161  = getVariableName__(path, type, name, instanceName);
162 
163  std::string newName = newNameRadical;
164  // forcing newName to be unique
165  int num = 0;
166  while (names.contains(newName)) {
167  newName = newNameRadical + std::to_string(++num);
168  }
169 
170  names.insert(newName);
171  bn__->changeVariableName(node, newName);
172  } else {
173  ParseError warn(false,
174  "Name " + nn + " cannot be simplified.",
175  filename__,
176  0);
177  errors__.add(warn);
178  }
179  }
180  }
181 
182  delete prm;
183 
184  return errors();
185  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
std::string classpath__
std::string filename__
bool isClass(const std::string &name) const
Definition: PRM_tpl.h:78
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:62
const Set< PRMClass< GUM_SCALAR > *> & classes() const
Returns the Set of all Class<GUM_SCALAR> in this PRM.
Definition: PRM_tpl.h:121
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:498
void generateBN__(prm::PRMSystem< GUM_SCALAR > &system)
bool isSystem(const std::string &name) const
Definition: PRM_tpl.h:88
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:145
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:236
BayesNet< GUM_SCALAR > * bn__
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:65
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:109
Size errors()
publishing Errors API
Definition: O3prmBNReader.h:74
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
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 87 of file O3prmBNReader.h.

87  {
89  }
ErrorsContainer errors__
void elegantErrors(std::ostream &o) const
Print errors on output stream.

◆ 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 92 of file O3prmBNReader.h.

92  {
94  }
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
ErrorsContainer errors__

◆ 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 97 of file O3prmBNReader.h.

97  {
99  }
void syntheticResults(std::ostream &o) const
Print errors on output stream.
ErrorsContainer errors__

◆ warnings()

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

of errors

Definition at line 76 of file O3prmBNReader.h.

76 { return errors__.warning_count; }
ErrorsContainer errors__

Member Data Documentation

◆ bn__

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

Definition at line 108 of file O3prmBNReader.h.

◆ classpath__

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

Definition at line 105 of file O3prmBNReader.h.

◆ entityName__

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

Definition at line 106 of file O3prmBNReader.h.

◆ errors__

template<typename GUM_SCALAR >
ErrorsContainer gum::O3prmBNReader< GUM_SCALAR >::errors__
private

Definition at line 109 of file O3prmBNReader.h.

◆ filename__

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

Definition at line 103 of file O3prmBNReader.h.


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