45#ifndef DOXYGEN_SHOULD_SKIP_THIS
49 template < GUM_Numeric GUM_SCALAR >
51 BNReader< GUM_SCALAR >(bn, filename) {
52 GUM_CONSTRUCTOR(UAIBNReader);
54 _streamName_ = filename;
60 _scanner_ =
new UAIBN::Scanner(_streamName_.c_str());
61 _parser_ =
new UAIBN::Parser(_scanner_);
62 }
catch (
IOError const&) { _ioerror_ =
true; }
65 template < GUM_Numeric GUM_SCALAR >
66 UAIBNReader< GUM_SCALAR >::~UAIBNReader() {
67 GUM_DESTRUCTOR(UAIBNReader);
71 if (_parser_)
delete (_parser_);
73 if (_scanner_)
delete (_scanner_);
77 template < GUM_Numeric GUM_SCALAR >
78 UAIBN::Scanner& UAIBNReader< GUM_SCALAR >::scanner() {
79 if (_ioerror_) {
GUM_ERROR(gum::IOError,
"No such file " + streamName()) }
84 template < GUM_Numeric GUM_SCALAR >
85 const std::string& UAIBNReader< GUM_SCALAR >::streamName()
const {
89 template < GUM_Numeric GUM_SCALAR >
90 bool UAIBNReader< GUM_SCALAR >::trace()
const {
91 return _traceScanning_;
94 template < GUM_Numeric GUM_SCALAR >
95 void UAIBNReader< GUM_SCALAR >::trace(
bool b) {
97 scanner().setTrace(b);
100 template < GUM_Numeric GUM_SCALAR >
101 Size UAIBNReader< GUM_SCALAR >::proceed() {
102 if (_ioerror_) {
GUM_ERROR(gum::IOError,
"No such file " + streamName()) }
108 buildFromQuartets(_parser_->getQuartets());
111 return 1 + _parser_->errors().error_count;
115 return (_parser_->errors().error_count);
118 template < GUM_Numeric GUM_SCALAR >
119 void UAIBNReader< GUM_SCALAR >::buildFromQuartets(
120 std::vector< std::tuple< float, int, int, int > > quartets) {
122 Size max = quartets.size();
124 _addWarning_(1, 1,
"Empty BayesNet");
128 auto isInt = [&]() ->
bool {
return (std::get< 0 >(quartets[current]) == -1); };
129 auto lig = [&]() ->
int {
return std::get< 2 >(quartets[current]); };
130 auto col = [&]() ->
int {
return std::get< 3 >(quartets[current]); };
132 auto getInt = [&]() ->
int {
133 if (!isInt()) this->_addFatalError_(lig(), col(),
"int expected");
134 return std::get< 1 >(quartets[current]);
136 auto getVal = [&]() -> GUM_SCALAR {
137 return (isInt()) ? (std::get< 1 >(quartets[current])) : (
std::get< 0 >(quartets[current]));
139 auto incCurrent = [&]() {
141 if (current >= max) this->_addFatalError_(lig(), col(),
"Not enough data in UAI file");
145 Size nbrNode = (Size)getInt();
147 for (NodeId i = 0; i < nbrNode; i++) {
150 if (mod < 2) _addError_(lig(), col(),
"Number of modalities should be greater than 2.");
155 Size nbrPot = (Size)getInt();
156 if (nbrPot != nbrNode)
157 _addWarning_(lig(), col(),
"Number of CPTs should be the same as number of nodes");
160 for (NodeId i = 0; i < nbrPot; i++) {
162 Size nbrPar = (Size)getInt();
163 if (nbrPar == 0) _addError_(lig(), col(),
"0 is not possible here");
165 std::vector< NodeId > papas;
166 for (NodeId j = 1; j < nbrPar; j++) {
168 NodeId papa = (NodeId)getInt();
169 if (papa >= nbrNode) _addError_(lig(), col(),
"Not enough variables in the BayesNet");
170 papas.push_back(papa);
174 NodeId nodePot = (Size)getInt();
175 if (nodePot >= nbrNode) _addError_(lig(), col(),
"Not enough variables in the BayesNet");
176 if (s.contains(nodePot)) _addError_(lig(), col(),
"Parents already defined");
179 for (
const auto papa: papas) {
180 _bn_->addArc(papa, nodePot);
184 std::vector< GUM_SCALAR > v;
185 for (NodeId i = 0; i < nbrPot; i++) {
187 Size nbrParam = (Size)getInt();
188 if (nbrParam != _bn_->cpt(i).domainSize())
189 _addFatalError_(lig(), col(),
"Size does not fit between parents and parameters");
190 for (Idx j = 0; j < nbrParam; j++) {
192 v.push_back(getVal());
194 _bn_->cpt(i).fillWith(v);
198 if (current != max - 1) _addError_(lig(), col(),
"Too many data in this file");
203 template < GUM_Numeric GUM_SCALAR >
204 Idx UAIBNReader< GUM_SCALAR >::errLine(Idx i) {
205 if (_parseDone_)
return _parser_->errors().error(i).line;
209 template < GUM_Numeric GUM_SCALAR >
210 Idx UAIBNReader< GUM_SCALAR >::errCol(Idx i) {
211 if (_parseDone_)
return _parser_->errors().error(i).column;
215 template < GUM_Numeric GUM_SCALAR >
216 bool UAIBNReader< GUM_SCALAR >::errIsError(Idx i) {
217 if (_parseDone_)
return _parser_->errors().error(i).is_error;
221 template < GUM_Numeric GUM_SCALAR >
222 std::string UAIBNReader< GUM_SCALAR >::errMsg(Idx i) {
223 if (_parseDone_)
return _parser_->errors().error(i).msg;
227 template < GUM_Numeric GUM_SCALAR >
228 void UAIBNReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
229 if (_parseDone_) _parser_->errors().elegantErrors(o);
233 template < GUM_Numeric GUM_SCALAR >
234 void UAIBNReader< GUM_SCALAR >::showElegantErrorsAndWarnings(std::ostream& o) {
235 if (_parseDone_) _parser_->errors().elegantErrorsAndWarnings(o);
239 template < GUM_Numeric GUM_SCALAR >
240 void UAIBNReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
241 if (_parseDone_) _parser_->errors().simpleErrorsAndWarnings(o);
245 template < GUM_Numeric GUM_SCALAR >
246 void UAIBNReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
247 if (_parseDone_) _parser_->errors().syntheticResults(o);
251 template < GUM_Numeric GUM_SCALAR >
252 Size UAIBNReader< GUM_SCALAR >::errors() {
253 return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
256 template < GUM_Numeric GUM_SCALAR >
257 Size UAIBNReader< GUM_SCALAR >::warnings() {
258 return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
261 template < GUM_Numeric GUM_SCALAR >
262 void UAIBNReader< GUM_SCALAR >::_addFatalError_(Idx lig, Idx col, std::string_view s) {
263 _parser_->errors().addError(s, _streamName_, lig, col);
267 template < GUM_Numeric GUM_SCALAR >
268 void UAIBNReader< GUM_SCALAR >::_addError_(Idx lig, Idx col, std::string_view s) {
269 _parser_->errors().addError(s, _streamName_, lig, col);
272 template < GUM_Numeric GUM_SCALAR >
273 void UAIBNReader< GUM_SCALAR >::_addWarning_(Idx lig, Idx col, std::string_view s) {
274 _parser_->errors().addWarning(s, _streamName_, lig, col);
Definition file for UAI exportation class.
Pure virtual class for reading a BN from a file.
Class representing a Bayesian network.
Base class for all aGrUM's exceptions.
Exception : input/output problem.
Exception : operation not allowed.
UAIBNReader(BayesNet< GUM_SCALAR > *bn, std::string_view filename)
Constructor A reader is defined for reading a defined file.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities