45#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template < GUM_Numeric GUM_SCALAR >
58 std::string_view filePath) :
59 BNReader< GUM_SCALAR >(bn, filePath) {
60 GUM_CONSTRUCTOR(BIFXMLBNReader);
62 _filePath_ = filePath;
68 template < GUM_Numeric GUM_SCALAR >
69 BIFXMLBNReader< GUM_SCALAR >::BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn, std::istream& stream) :
70 BNReader< GUM_SCALAR >(bn,
"<stream>") {
71 GUM_CONSTRUCTOR(BIFXMLBNReader);
73 _isFromStream_ =
true;
75 = std::string(std::istreambuf_iterator< char >(stream), std::istreambuf_iterator< char >());
81 template < GUM_Numeric GUM_SCALAR >
82 BIFXMLBNReader< GUM_SCALAR >::~BIFXMLBNReader() {
83 GUM_DESTRUCTOR(BIFXMLBNReader);
92 template < GUM_Numeric GUM_SCALAR >
93 Size BIFXMLBNReader< GUM_SCALAR >::proceed() {
96 std::string status = _isFromStream_ ?
"Parsing XML content ..." :
"Loading File ...";
100 if (_isFromStream_) {
101 xmlDoc.
Parse(_xmlContent_);
108 GUM_ERROR(
IOError,
": Loading fail, please check the file for any syntax error.")
112 status =
"File loaded. Now looking for BIF element ...";
118 status =
"BIF Element reached. Now searching network ...";
125 if (nameElement !=
nullptr) { _bn_->setProperty(
"name", nameElement->
GetTextOrDefault(
"")); }
128 status =
"Network found. Now proceeding variables instantiation...";
131 _parsingVariables_(networkElement);
134 status =
"All variables have been instantiated. Now filling up diagram...";
137 _fillingBN_(networkElement);
139 status =
"Instantiation of network completed";
146 template < GUM_Numeric GUM_SCALAR >
147 void BIFXMLBNReader< GUM_SCALAR >::_parsingVariables_(
ticpp::Element* parentNetwork) {
151 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
157 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
164 std::string description =
"";
165 std::string fast =
"";
168 for (varPropertiesIte = varPropertiesIte.begin(currentVar);
169 varPropertiesIte != varPropertiesIte.end();
170 ++varPropertiesIte) {
171 const auto pair =
gum::split(varPropertiesIte->GetTextOrDefault(
""),
"=");
172 if (pair.size() == 2) {
176 if (property ==
"description") {
178 }
else if (property ==
"fast") {
187 auto newVar =
new LabelizedVariable(varName, description, 0);
192 for (varOutComesIte = varOutComesIte.begin(currentVar);
193 varOutComesIte != varOutComesIte.end();
195 newVar->addLabel(varOutComesIte->GetTextOrDefault(
""));
202 newVar->setDescription(description);
204 if (newVar->name() != varName) {
206 "Variable name (" << varName <<
") and fast syntax (" << fast
207 <<
") are not compatible. Please check the syntax.")
214 std::string status =
"Network found. Now proceedind variables instanciation...";
215 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
221 template < GUM_Numeric GUM_SCALAR >
224 void BIFXMLBNReader< GUM_SCALAR >::_fillingBN_(
ticpp::Element* parentNetwork) {
229 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
236 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
242 NodeId currentVarId = _bn_->idFromName(currentVarName);
246 List< NodeId > parentList;
248 for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end(); ++givenIte) {
249 std::string parentNode = givenIte->GetTextOrDefault(
"");
250 NodeId parentId = _bn_->idFromName(parentNode);
251 parentList.pushBack(parentId);
254 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
255 parentListIte != parentList.rendSafe();
257 _bn_->addArc(*parentListIte, currentVarId);
262 std::list< GUM_SCALAR > tablelist;
265 while (!issTableString.eof()) {
266 issTableString >> value;
267 tablelist.push_back(value);
270 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
273 _bn_->cpt(currentVarId).fillWith(tablevector);
276 std::string status =
"All variables have been instancied. Now filling up diagram...";
277 int progress = (int)((
float)nbIte / (
float)nbDef * 45) + 55;
classe for import of bayes net from a XML file written with BIF Format
BIFXMLBNReader(BayesNet< GUM_SCALAR > *bn, std::string_view filePath)
Constructor A reader is created to reading a defined file.
Pure virtual class for reading a BN from a file.
Class representing a Bayesian network.
Exception : input/output problem.
Wrapper around TiXmlDocument.
void LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
void Parse(const std::string &xml, bool throwIfParseError=true, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given xml data.
Wrapper around TiXmlElement.
std::string GetTextOrDefault(const std::string &defaultValue) const
Gets the text of an Element, if it doesn't exist it will return the defaultValue.
This is a ticpp exception class.
const char * what() const
Override std::exception::what() to return m_details.
Iterator for conveniently stepping through Nodes and Attributes.
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
bool NoChildren() const
Check if this node has no children.
#define GUM_ERROR(type, msg)
std::string toLower(std::string_view str)
Returns the lowercase version of str.
std::vector< std::string > split(std::string_view str, std::string_view delim)
Split str using the delimiter.
std::string trim_copy(std::string_view s)
trim from both ends (copying)
gum is the global namespace for all aGrUM entities
std::unique_ptr< DiscreteVariable > fastVariable(std::string var_description, Size default_domain_size)
Create a pointer on a Discrete Variable from a "fast" syntax.
#define GUM_EMIT2(signal, arg1, arg2)
Utilities for manipulating strings.