aGrUM 2.3.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
Size errors ()
 publishing Errors API
Size warnings ()
Idx errLine (Idx i)
 publishing Errors API
Idx errCol (Idx i)
 col of ith error or warning
bool errIsError (Idx i)
 type of ith error or warning
std::string errMsg (Idx i)
 message of ith error or warning
void showElegantErrors (std::ostream &o=std::cerr)
 send on std::cerr the list of errors
void showElegantErrorsAndWarnings (std::ostream &o=std::cerr)
 send on std::cerr the list of errors or warnings
void showErrorCounts (std::ostream &o=std::cerr)
 send on std::cerr the number of errors and the number of warnings

Private Member Functions

void _generateBN_ (prm::PRMSystem< GUM_SCALAR > &system)

Static Private Member Functions

static std::string _getVariableName_ (const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")
static std::string _getEntityName_ (const std::string &filename)
static std::string _getInstanceName_ (const std::string &classname)

Private Attributes

std::string _filename_
std::string _classpath_
std::string _entityName_
BayesNet< GUM_SCALAR > * _bn_
ErrorsContainer _errors_

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

85 :
88 _bn_ = bn;
92 }
BNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
Constructor A reader is defined for reading a defined file.
Read an O3PRM and transform the gum::prm::PRMSystem into gum::BayesNet.
std::string _filename_
std::string _entityName_
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
std::string _classpath_
BayesNet< GUM_SCALAR > * _bn_
static std::string _getEntityName_(const std::string &filename)

References gum::BNReader< GUM_SCALAR >::BNReader(), O3prmBNReader(), _bn_, _classpath_, _entityName_, _filename_, and _getEntityName_().

Referenced by O3prmBNReader(), and ~O3prmBNReader().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~O3prmBNReader()

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

Definition at line 95 of file O3prmBNReader_tpl.h.

95 {
97 }

References O3prmBNReader().

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

193 {
194 system.instantiate();
196 system.groundedBN(factory);
197 _bn_->setProperty("name", _entityName_);
198 }

References _bn_, _entityName_, gum::prm::PRMSystem< GUM_SCALAR >::groundedBN(), and gum::prm::PRMSystem< GUM_SCALAR >::instantiate().

Referenced by proceed().

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

74 {
75 auto b = filename.find_last_of("/\\");
76 auto e = filename.find_last_of(".") - 1;
77 GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
78 return filename.substr(b + 1, e - b);
79 }

Referenced by O3prmBNReader().

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

67 {
68 auto res = classname.substr(0, 4);
69 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
70 return res;
71 }

Referenced by proceed().

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

58 {
59 auto res = path + name; // path ends up with a "."
60 if (toRemove != "") {
61 if (res.substr(0, toRemove.size()) == toRemove) { res = res.substr(toRemove.size()); }
62 }
63 return res;
64 }

Referenced by proceed().

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

99{ return _errors_.error(i).column; }
ErrorsContainer _errors_

References _errors_.

◆ errIsError()

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

type of ith error or warning

Definition at line 102 of file O3prmBNReader.h.

102{ return _errors_.error(i).is_error; }

References _errors_.

◆ errLine()

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

publishing Errors API

of errors

Definition at line 96 of file O3prmBNReader.h.

96{ return _errors_.error(i).line; }

References _errors_.

◆ errMsg()

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

message of ith error or warning

Definition at line 105 of file O3prmBNReader.h.

105{ return _errors_.error(i).msg; }

References _errors_.

◆ errors()

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

publishing Errors API

of errors

Definition at line 91 of file O3prmBNReader.h.

91{ return _errors_.error_count; }

References _errors_.

Referenced by proceed().

Here is the caller graph for this function:

◆ proceed()

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

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

Definition at line 103 of file O3prmBNReader_tpl.h.

103 {
105 if (_classpath_ != "") { reader.addClassPath(_classpath_); }
106 reader.readFile(_filename_);
108 _errors_ = reader.errorsContainer();
109
110
111 if (errors() == 0) {
113 if (prm->isSystem(_entityName_)) {
114 _generateBN_(prm->getSystem(_entityName_));
115 } else if (prm->isClass(_entityName_)) {
116 ParseError warn(false,
117 "No system '" + _entityName_
118 + "' found but class found. Generating unnamed instance.",
120 0);
121 _errors_.add(warn);
125 s.add(i);
127 instanceName += "."; // to be removed in _getVariableName_
128 } else if (prm->classes().size() == 1) {
129 const std::string& entityName = (*prm->classes().begin())->name();
130 ParseError warn(false,
131 "Unique class '" + entityName + "' found. Generating unnamed instance.",
133 0);
134 _errors_.add(warn);
135
139 s.add(i);
141
142 // force the name of the BN to be the name of the class instead of the name
143 // of the file
144 _bn_->setProperty("name", entityName);
145 instanceName += "."; // to be removed in _getVariableName_
146 } else {
147 ParseError err(true,
148 "Neither system nor class '" + _entityName_ + "' and more than one class.",
150 0);
151 _errors_.add(err);
152 }
153
154 // renaming variables in th BN
156 for (auto node: _bn_->nodes()) {
157 // keeping the complete name in description
158 const std::string& nn = _bn_->variable(node).name();
159 _bn_->variable(node).setDescription(nn);
160
161 // trying to simplify the
162 auto start = nn.find_first_of('(');
163 auto end = nn.find_first_of(')');
164 if (0 < start && start < end && end < nn.size()) {
165 auto path = nn.substr(0, start);
166 auto type = nn.substr(start + 1, end - start - 1);
167 auto name = nn.substr(end + 1, std::string::npos);
168
170
172 // forcing newName to be unique
173 int num = 0;
174 while (names.contains(newName)) {
176 }
177
178 names.insert(newName);
179 _bn_->changeVariableName(node, newName);
180 } else {
181 ParseError warn(false, "Name " + nn + " cannot be simplified.", _filename_, 0);
182 _errors_.add(warn);
183 }
184 }
185 }
186
187 delete prm;
188
189 return errors();
190 }
Size errors()
publishing Errors API
static std::string _getInstanceName_(const std::string &classname)
void _generateBN_(prm::PRMSystem< GUM_SCALAR > &system)
static std::string _getVariableName_(const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")

References _bn_, _classpath_, _entityName_, _errors_, _filename_, _generateBN_(), _getInstanceName_(), _getVariableName_(), gum::prm::PRMSystem< GUM_SCALAR >::add(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::addClassPath(), gum::Set< Key >::contains(), errors(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::errorsContainer(), gum::Set< Key >::insert(), gum::prm::o3prm::O3prmReader< GUM_SCALAR >::prm(), and gum::prm::o3prm::O3prmReader< GUM_SCALAR >::readFile().

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

108{ _errors_.elegantErrors(o); }

References _errors_.

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

111 {
112 _errors_.elegantErrorsAndWarnings(o);
113 }

References _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 116 of file O3prmBNReader.h.

116{ _errors_.syntheticResults(o); }

References _errors_.

◆ warnings()

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

of errors

Definition at line 94 of file O3prmBNReader.h.

94{ return _errors_.warning_count; }

References _errors_.

Member Data Documentation

◆ _bn_

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

Definition at line 126 of file O3prmBNReader.h.

Referenced by O3prmBNReader(), _generateBN_(), and proceed().

◆ _classpath_

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

Definition at line 123 of file O3prmBNReader.h.

Referenced by O3prmBNReader(), and proceed().

◆ _entityName_

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

Definition at line 124 of file O3prmBNReader.h.

Referenced by O3prmBNReader(), _generateBN_(), and proceed().

◆ _errors_

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

◆ _filename_

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

Definition at line 121 of file O3prmBNReader.h.

Referenced by O3prmBNReader(), and proceed().


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