aGrUM 3.1.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 () override
Size proceed () override
 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_ (std::string_view path, std::string_view type, std::string_view name, std::string_view toRemove="")
static std::string _getEntityName_ (std::string_view filename)
static std::string _getInstanceName_ (std::string_view classname)

Private Attributes

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

Detailed Description

template<GUM_Numeric 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 75 of file O3prmBNReader.h.

Constructor & Destructor Documentation

◆ O3prmBNReader()

template<GUM_Numeric 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 84 of file O3prmBNReader_tpl.h.

87 :
90 _bn_ = bn;
94 }
BNReader(BayesNet< GUM_SCALAR > *bn, std::string_view 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="")
static std::string _getEntityName_(std::string_view filename)
std::string _classpath_
BayesNet< GUM_SCALAR > * _bn_

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<GUM_Numeric GUM_SCALAR>
gum::O3prmBNReader< GUM_SCALAR >::~O3prmBNReader ( )
override

Definition at line 97 of file O3prmBNReader_tpl.h.

97 {
99 }

References O3prmBNReader().

Here is the call graph for this function:

Member Function Documentation

◆ _generateBN_()

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
std::string gum::O3prmBNReader< GUM_SCALAR >::_getEntityName_ ( std::string_view filename)
staticprivate

Definition at line 76 of file O3prmBNReader_tpl.h.

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

Referenced by O3prmBNReader().

Here is the caller graph for this function:

◆ _getInstanceName_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::O3prmBNReader< GUM_SCALAR >::_getInstanceName_ ( std::string_view classname)
staticprivate

Definition at line 69 of file O3prmBNReader_tpl.h.

69 {
70 std::string res{classname.substr(0, 4)};
71 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
72 return res;
73 }

Referenced by proceed().

Here is the caller graph for this function:

◆ _getVariableName_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::O3prmBNReader< GUM_SCALAR >::_getVariableName_ ( std::string_view path,
std::string_view type,
std::string_view name,
std::string_view toRemove = "" )
staticprivate

Definition at line 56 of file O3prmBNReader_tpl.h.

59 {
61 res.append(name); // path ends up with a "."
62 if (!toRemove.empty()) {
63 if (res.substr(0, toRemove.size()) == toRemove) { res = res.substr(toRemove.size()); }
64 }
65 return res;
66 }

Referenced by proceed().

Here is the caller graph for this function:

◆ errCol()

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

col of ith error or warning

Definition at line 216 of file O3prmBNReader_tpl.h.

216 {
217 return _errors_.error(i).column;
218 }
ErrorsContainer _errors_

References _errors_.

◆ errIsError()

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

type of ith error or warning

Definition at line 221 of file O3prmBNReader_tpl.h.

221 {
222 return _errors_.error(i).is_error;
223 }

References _errors_.

◆ errLine()

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

publishing Errors API

of errors

Definition at line 211 of file O3prmBNReader_tpl.h.

211 {
212 return _errors_.error(i).line;
213 }

References _errors_.

◆ errMsg()

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

message of ith error or warning

Definition at line 226 of file O3prmBNReader_tpl.h.

226 {
227 return _errors_.error(i).msg;
228 }

References _errors_.

◆ errors()

template<GUM_Numeric GUM_SCALAR>
Size gum::O3prmBNReader< GUM_SCALAR >::errors ( )

publishing Errors API

of errors

Definition at line 201 of file O3prmBNReader_tpl.h.

201 {
202 return _errors_.error_count;
203 }

References _errors_.

Referenced by proceed().

Here is the caller graph for this function:

◆ proceed()

template<GUM_Numeric GUM_SCALAR>
Size gum::O3prmBNReader< GUM_SCALAR >::proceed ( )
override

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

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

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<GUM_Numeric GUM_SCALAR>
void gum::O3prmBNReader< GUM_SCALAR >::showElegantErrors ( std::ostream & o = std::cerr)

send on std::cerr the list of errors

Definition at line 231 of file O3prmBNReader_tpl.h.

231 {
232 _errors_.elegantErrors(o);
233 }

References _errors_.

◆ showElegantErrorsAndWarnings()

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

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

Definition at line 236 of file O3prmBNReader_tpl.h.

236 {
237 _errors_.elegantErrorsAndWarnings(o);
238 }

References _errors_.

◆ showErrorCounts()

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

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

Definition at line 241 of file O3prmBNReader_tpl.h.

241 {
242 _errors_.syntheticResults(o);
243 }

References _errors_.

◆ warnings()

template<GUM_Numeric GUM_SCALAR>
Size gum::O3prmBNReader< GUM_SCALAR >::warnings ( )

of errors

Definition at line 206 of file O3prmBNReader_tpl.h.

206 {
207 return _errors_.warning_count;
208 }

References _errors_.

Member Data Documentation

◆ _bn_

template<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
ErrorsContainer gum::O3prmBNReader< GUM_SCALAR >::_errors_
private

◆ _filename_

template<GUM_Numeric 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: