aGrUM  0.14.2
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 54 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 64 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().

67  :
68  BNReader< GUM_SCALAR >(bn, filename) {
69  GUM_CONSTRUCTOR(O3prmBNReader);
70  __bn = bn;
71  __filename = filename;
72  __entityName = entityName == "" ? __getEntityName(filename) : entityName;
73  __classpath = classpath;
74  }
std::string __entityName
static std::string __getEntityName(const std::string &filename)
std::string __filename
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
std::string __classpath
BayesNet< GUM_SCALAR > * __bn
+ Here is the call graph for this function:

◆ ~O3prmBNReader()

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

Definition at line 77 of file O3prmBNReader_tpl.h.

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

Member Function Documentation

◆ __generateBN()

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

Definition at line 165 of file O3prmBNReader_tpl.h.

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

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

166  {
167  system.instantiate();
168  BayesNetFactory< GUM_SCALAR > factory(__bn);
169  system.groundedBN(factory);
170  }
BayesNet< GUM_SCALAR > * __bn
+ 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 56 of file O3prmBNReader_tpl.h.

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

56  {
57  auto b = filename.find_last_of("/\\");
58  auto e = filename.find_last_of(".") - 1;
59  GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
60  return filename.substr(b + 1, e - b);
61  }
+ 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 48 of file O3prmBNReader_tpl.h.

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

48  {
49  auto res = classname.substr(0, 4);
50  std::transform(res.begin(), res.end(), res.begin(), ::tolower);
51  return res;
52  }
+ 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 33 of file O3prmBNReader_tpl.h.

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

36  {
37  auto res = path + name; // path ends up with a "."
38  if (toRemove != "") {
39  if (res.substr(0, toRemove.size()) == toRemove) {
40  res = res.substr(toRemove.size());
41  }
42  }
43  return res;
44  }
+ Here is the caller graph for this function:

◆ errCol()

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

col of ith error or warning

Definition at line 78 of file O3prmBNReader.h.

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

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

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

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

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

76 { 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 82 of file O3prmBNReader.h.

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

82 { 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 72 of file O3prmBNReader.h.

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

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

72 { return __errors.error_count; }
Size error_count
Number of errors detected.
ErrorsContainer __errors
+ 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 85 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 >::__errors, gum::O3prmBNReader< GUM_SCALAR >::__filename, gum::O3prmBNReader< GUM_SCALAR >::__generateBN(), gum::O3prmBNReader< GUM_SCALAR >::__getInstanceName(), gum::O3prmBNReader< GUM_SCALAR >::__getVariableName(), gum::prm::PRMSystem< GUM_SCALAR >::add(), gum::ErrorsContainer::add(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::addClassPath(), gum::Set< Key, Alloc >::contains(), gum::O3prmBNReader< GUM_SCALAR >::errors(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::errorsContainer(), gum::prm::PRM< GUM_SCALAR >::getClass(), gum::prm::PRM< GUM_SCALAR >::getSystem(), 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().

85  {
86  prm::o3prm::O3prmReader< GUM_SCALAR > reader;
87  if (__classpath != "") { reader.addClassPath(__classpath); }
88  reader.readFile(__filename);
89  gum::prm::PRM< GUM_SCALAR >* prm = reader.prm();
90  __errors = reader.errorsContainer();
91 
92 
93  if (errors() == 0) {
94  std::string instanceName = "";
95  if (prm->isSystem(__entityName)) {
97  } else {
98  if (prm->isClass(__entityName)) {
99  ParseError warn(
100  false,
101  "No system '" + __entityName
102  + "' found but class found. Generating unnamed instance.",
103  __filename,
104  0);
105  __errors.add(warn);
107  instanceName = __getInstanceName(__entityName);
109  instanceName, prm->getClass(__entityName));
110  s.add(i);
111  __generateBN(s);
112  instanceName += "."; // to be removed in __getVariableName
113  } else {
114  ParseError err(true,
115  "Neither system nor class '" + __entityName + "'.",
116  __filename,
117  0);
118  __errors.add(err);
119  }
120  }
121 
122  // renaming variables in th BN
123 
125  for (auto node : __bn->nodes()) {
126  // keeping the complete name in description
127  const std::string& nn = __bn->variable(node).name();
128  __bn->variable(node).setDescription(nn);
129 
130  // trying to simplify the
131  auto start = nn.find_first_of('(');
132  auto end = nn.find_first_of(')');
133  if (0 < start && start < end && end < nn.size()) {
134  auto path = nn.substr(0, start);
135  auto type = nn.substr(start + 1, end - start - 1);
136  auto name = nn.substr(end + 1, std::string::npos);
137 
138  std::string newNameRadical =
139  __getVariableName(path, type, name, instanceName);
140 
141  std::string newName = newNameRadical;
142  // forcing newName to be unique
143  int num = 0;
144  while (names.contains(newName)) {
145  newName = newNameRadical + std::to_string(++num);
146  }
147 
148  names.insert(newName);
149  __bn->changeVariableName(node, newName);
150  } else {
151  ParseError warn(
152  false, "Name " + nn + " cannot be simplified.", __filename, 0);
153  __errors.add(warn);
154  }
155  }
156  }
157 
158  delete prm;
159 
160  return errors();
161  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
bool isClass(const std::string &name) const
Definition: PRM_tpl.h:76
std::string __entityName
void add(NodeId id, PRMInstance< GUM_SCALAR > &instance)
Add an PRMInstance<GUM_SCALAR> to a given PRMReferenceSlot, PRMSlotChain<GUM_SCALAR> or output node...
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:60
ErrorsContainer __errors
void add(ParseError error)
Add an error object to the container.
void __generateBN(prm::PRMSystem< GUM_SCALAR > &system)
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
std::string __classpath
bool isSystem(const std::string &name) const
Definition: PRM_tpl.h:86
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:143
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:226
static std::string __getInstanceName(const std::string &classname)
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:63
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:107
Size errors()
publishing Errors API
Definition: O3prmBNReader.h:72
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
BayesNet< GUM_SCALAR > * __bn
+ 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 85 of file O3prmBNReader.h.

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

85  {
87  }
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 90 of file O3prmBNReader.h.

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

90  {
92  }
ErrorsContainer __errors
void elegantErrorsAndWarnings(std::ostream &o) const
Print errors on output stream.
+ 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 95 of file O3prmBNReader.h.

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

95  {
97  }
ErrorsContainer __errors
void syntheticResults(std::ostream &o) const
Print errors on output stream.
+ 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: