aGrUM  0.20.3
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 61 of file O3prmBNReader_tpl.h.

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

64  :
65  BNReader< GUM_SCALAR >(bn, filename) {
66  GUM_CONSTRUCTOR(O3prmBNReader);
67  _bn_ = bn;
68  _filename_ = filename;
69  _entityName_ = entityName == "" ? _getEntityName_(filename) : entityName;
70  _classpath_ = classpath;
71  }
std::string _filename_
Definition: O3prmBNReader.h:99
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
static std::string _getEntityName_(const std::string &filename)
std::string _entityName_
BayesNet< GUM_SCALAR > * _bn_
std::string _classpath_
+ Here is the call graph for this function:

◆ ~O3prmBNReader()

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

Definition at line 74 of file O3prmBNReader_tpl.h.

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

74  {
75  GUM_DESTRUCTOR(O3prmBNReader);
76  }
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

◆ _generateBN_()

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

Definition at line 173 of file O3prmBNReader_tpl.h.

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

173  {
174  system.instantiate();
175  BayesNetFactory< GUM_SCALAR > factory(_bn_);
176  system.groundedBN(factory);
177  _bn_->setProperty("name", _entityName_);
178  }
std::string _entityName_
BayesNet< GUM_SCALAR > * _bn_
+ 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 53 of file O3prmBNReader_tpl.h.

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

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

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

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

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

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

◆ 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 82 of file O3prmBNReader_tpl.h.

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

82  {
83  prm::o3prm::O3prmReader< GUM_SCALAR > reader;
84  if (_classpath_ != "") { reader.addClassPath(_classpath_); }
85  reader.readFile(_filename_);
86  gum::prm::PRM< GUM_SCALAR >* prm = reader.prm();
87  _errors_ = reader.errorsContainer();
88 
89 
90  if (errors() == 0) {
91  std::string instanceName = "";
92  if (prm->isSystem(_entityName_)) {
94  } else if (prm->isClass(_entityName_)) {
95  ParseError warn(false,
96  "No system '" + _entityName_
97  + "' found but class found. Generating unnamed instance.",
98  _filename_,
99  0);
100  _errors_.add(warn);
102  instanceName = _getInstanceName_(_entityName_);
103  auto i = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName, prm->getClass(_entityName_));
104  s.add(i);
105  _generateBN_(s);
106  instanceName += "."; // to be removed in _getVariableName_
107  } else if (prm->classes().size() == 1) {
108  const std::string& entityName = (*prm->classes().begin())->name();
109  ParseError warn(false,
110  "Unique class '" + entityName + "' found. Generating unnamed instance.",
111  _filename_,
112  0);
113  _errors_.add(warn);
114 
115  gum::prm::PRMSystem< GUM_SCALAR > s("S_" + entityName);
116  instanceName = _getInstanceName_(entityName);
117  auto i = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName, prm->getClass(entityName));
118  s.add(i);
119  _generateBN_(s);
120 
121  // force the name of the BN to be the name of the class instead of the name
122  // of the file
123  _bn_->setProperty("name", entityName);
124  instanceName += "."; // to be removed in _getVariableName_
125  } else {
126  ParseError err(true,
127  "Neither system nor class '" + _entityName_ + "' and more than one class.",
128  _filename_,
129  0);
130  _errors_.add(err);
131  }
132 
133  // renaming variables in th BN
135  for (auto node: _bn_->nodes()) {
136  // keeping the complete name in description
137  const std::string& nn = _bn_->variable(node).name();
138  _bn_->variable(node).setDescription(nn);
139 
140  // trying to simplify the
141  auto start = nn.find_first_of('(');
142  auto end = nn.find_first_of(')');
143  if (0 < start && start < end && end < nn.size()) {
144  auto path = nn.substr(0, start);
145  auto type = nn.substr(start + 1, end - start - 1);
146  auto name = nn.substr(end + 1, std::string::npos);
147 
148  std::string newNameRadical = _getVariableName_(path, type, name, instanceName);
149 
150  std::string newName = newNameRadical;
151  // forcing newName to be unique
152  int num = 0;
153  while (names.contains(newName)) {
154  newName = newNameRadical + std::to_string(++num);
155  }
156 
157  names.insert(newName);
158  _bn_->changeVariableName(node, newName);
159  } else {
160  ParseError warn(false, "Name " + nn + " cannot be simplified.", _filename_, 0);
161  _errors_.add(warn);
162  }
163  }
164  }
165 
166  delete prm;
167 
168  return errors();
169  }
void _generateBN_(prm::PRMSystem< GUM_SCALAR > &system)
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
std::string _filename_
Definition: O3prmBNReader.h:99
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...
static std::string _getVariableName_(const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")
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:119
void add(ParseError error)
Add an error object to the container.
static std::string _getInstanceName_(const std::string &classname)
ErrorsContainer _errors_
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
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:140
std::string _entityName_
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:234
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:65
BayesNet< GUM_SCALAR > * _bn_
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:108
Size errors()
publishing Errors API
Definition: O3prmBNReader.h:74
std::string _classpath_
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ 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 { _errors_.elegantErrors(o); }
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 90 of file O3prmBNReader.h.

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

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

◆ _classpath_

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

Definition at line 101 of file O3prmBNReader.h.

◆ _entityName_

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

Definition at line 102 of file O3prmBNReader.h.

◆ _errors_

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

Definition at line 105 of file O3prmBNReader.h.

◆ _filename_

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

Definition at line 99 of file O3prmBNReader.h.


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