43#ifndef DOXYGEN_SHOULD_SKIP_THIS
47 template <
typename GUM_SCALAR >
49 BNReader< GUM_SCALAR >(bn, filename) {
50 GUM_CONSTRUCTOR(UAIBNReader);
52 _streamName_ = filename;
58 _scanner_ =
new UAIBN::Scanner(_streamName_.c_str());
59 _parser_ =
new UAIBN::Parser(_scanner_);
60 }
catch (
IOError const&) { _ioerror_ =
true; }
63 template <
typename GUM_SCALAR >
64 UAIBNReader< GUM_SCALAR >::~UAIBNReader() {
65 GUM_DESTRUCTOR(UAIBNReader);
69 if (_parser_)
delete (_parser_);
71 if (_scanner_)
delete (_scanner_);
75 template <
typename GUM_SCALAR >
76 INLINE UAIBN::Scanner& UAIBNReader< GUM_SCALAR >::scanner() {
77 if (_ioerror_) {
GUM_ERROR(gum::IOError,
"No such file " + streamName()) }
82 template <
typename GUM_SCALAR >
83 INLINE
const std::string& UAIBNReader< GUM_SCALAR >::streamName()
const {
87 template <
typename GUM_SCALAR >
88 INLINE
bool UAIBNReader< GUM_SCALAR >::trace()
const {
89 return _traceScanning_;
92 template <
typename GUM_SCALAR >
93 INLINE
void UAIBNReader< GUM_SCALAR >::trace(
bool b) {
95 scanner().setTrace(b);
98 template <
typename GUM_SCALAR >
99 Size UAIBNReader< GUM_SCALAR >::proceed() {
100 if (_ioerror_) {
GUM_ERROR(gum::IOError,
"No such file " + streamName()) }
106 buildFromQuartets(_parser_->getQuartets());
109 return 1 + _parser_->errors().error_count;
113 return (_parser_->errors().error_count);
116 template <
typename GUM_SCALAR >
117 void UAIBNReader< GUM_SCALAR >::buildFromQuartets(
118 std::vector< std::tuple< float, int, int, int > > quartets) {
120 Size max = quartets.size();
122 _addWarning_(1, 1,
"Empty BayesNet");
126 auto isInt = [&]() ->
bool {
return (std::get< 0 >(quartets[current]) == -1); };
127 auto lig = [&]() ->
int {
return std::get< 2 >(quartets[current]); };
128 auto col = [&]() ->
int {
return std::get< 3 >(quartets[current]); };
130 auto getInt = [&]() ->
int {
131 if (!isInt()) this->_addFatalError_(lig(), col(),
"int expected");
132 return std::get< 1 >(quartets[current]);
134 auto getVal = [&]() -> GUM_SCALAR {
135 return (isInt()) ? (std::get< 1 >(quartets[current])) : (
std::get< 0 >(quartets[current]));
137 auto incCurrent = [&]() {
139 if (current >= max) this->_addFatalError_(lig(), col(),
"Not enough data in UAI file");
143 Size nbrNode = (Size)getInt();
145 for (NodeId i = 0; i < nbrNode; i++) {
148 if (mod < 2) _addError_(lig(), col(),
"Number of modalities should be greater than 2.");
153 Size nbrPot = (Size)getInt();
154 if (nbrPot != nbrNode)
155 _addWarning_(lig(), col(),
"Number of CPTs should be the same as number of nodes");
158 for (NodeId i = 0; i < nbrPot; i++) {
160 Size nbrPar = (Size)getInt();
161 if (nbrPar == 0) _addError_(lig(), col(),
"0 is not possible here");
163 std::vector< NodeId > papas;
164 for (NodeId j = 1; j < nbrPar; j++) {
166 NodeId papa = (NodeId)getInt();
167 if (papa >= nbrNode) _addError_(lig(), col(),
"Not enough variables in the BayesNet");
168 papas.push_back(papa);
172 NodeId nodePot = (Size)getInt();
173 if (nodePot >= nbrNode) _addError_(lig(), col(),
"Not enough variables in the BayesNet");
174 if (s.contains(nodePot)) _addError_(lig(), col(),
"Parents already defined");
177 for (
const auto papa: papas) {
178 _bn_->addArc(papa, nodePot);
182 std::vector< GUM_SCALAR > v;
183 for (NodeId i = 0; i < nbrPot; i++) {
185 Size nbrParam = (Size)getInt();
186 if (nbrParam != _bn_->cpt(i).domainSize())
187 _addFatalError_(lig(), col(),
"Size does not fit between parents and parameters");
188 for (Idx j = 0; j < nbrParam; j++) {
190 v.push_back(getVal());
192 _bn_->cpt(i).fillWith(v);
196 if (current != max - 1) _addError_(lig(), col(),
"Too many data in this file");
201 template <
typename GUM_SCALAR >
202 INLINE Idx UAIBNReader< GUM_SCALAR >::errLine(Idx i) {
203 if (_parseDone_)
return _parser_->errors().error(i).line;
207 template <
typename GUM_SCALAR >
208 INLINE Idx UAIBNReader< GUM_SCALAR >::errCol(Idx i) {
209 if (_parseDone_)
return _parser_->errors().error(i).column;
213 template <
typename GUM_SCALAR >
214 INLINE
bool UAIBNReader< GUM_SCALAR >::errIsError(Idx i) {
215 if (_parseDone_)
return _parser_->errors().error(i).is_error;
219 template <
typename GUM_SCALAR >
220 INLINE std::string UAIBNReader< GUM_SCALAR >::errMsg(Idx i) {
221 if (_parseDone_)
return _parser_->errors().error(i).msg;
225 template <
typename GUM_SCALAR >
226 INLINE
void UAIBNReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
227 if (_parseDone_) _parser_->errors().elegantErrors(o);
231 template <
typename GUM_SCALAR >
232 INLINE
void UAIBNReader< GUM_SCALAR >::showElegantErrorsAndWarnings(std::ostream& o) {
233 if (_parseDone_) _parser_->errors().elegantErrorsAndWarnings(o);
237 template <
typename GUM_SCALAR >
238 INLINE
void UAIBNReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
239 if (_parseDone_) _parser_->errors().simpleErrorsAndWarnings(o);
243 template <
typename GUM_SCALAR >
244 INLINE
void UAIBNReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
245 if (_parseDone_) _parser_->errors().syntheticResults(o);
249 template <
typename GUM_SCALAR >
250 INLINE Size UAIBNReader< GUM_SCALAR >::errors() {
251 return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
254 template <
typename GUM_SCALAR >
255 INLINE Size UAIBNReader< GUM_SCALAR >::warnings() {
256 return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
259 template <
typename GUM_SCALAR >
260 INLINE
void UAIBNReader< GUM_SCALAR >::_addFatalError_(Idx lig, Idx col,
const std::string& s) {
261 _parser_->errors().addError(s, _streamName_, lig, col);
265 template <
typename GUM_SCALAR >
266 INLINE
void UAIBNReader< GUM_SCALAR >::_addError_(Idx lig, Idx col,
const std::string& s) {
267 _parser_->errors().addError(s, _streamName_, lig, col);
270 template <
typename GUM_SCALAR >
271 INLINE
void UAIBNReader< GUM_SCALAR >::_addWarning_(Idx lig, Idx col,
const std::string& s) {
272 _parser_->errors().addWarning(s, _streamName_, lig, col);
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, const std::string &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