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_);
103 xmlDoc = XmlDocument(_filePath_);
107 if (xmlDoc.noChildren()) {
108 GUM_ERROR(
IOError,
": Loading fail, please check the file for any syntax error.")
112 status =
"File loaded. Now looking for BIF element ...";
115 XmlElement bifElement = xmlDoc.firstChildElement(
"BIF");
118 status =
"BIF Element reached. Now searching network ...";
121 XmlElement networkElement = bifElement.firstChildElement(
"NETWORK");
124 XmlElement nameElement = networkElement.firstChildElement(
"NAME",
false);
125 if (!nameElement.isNull()) { _bn_->setProperty(
"name", nameElement.textOrDefault(
"")); }
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_(XmlElement parentNetwork) {
149 for (
const auto& _: parentNetwork.children(
"VARIABLE")) {
156 for (
const auto& currentVar: parentNetwork.children(
"VARIABLE")) {
158 XmlElement varNameElement = currentVar.firstChildElement(
"NAME");
159 std::string varName = varNameElement.textOrDefault(
"");
161 std::string description =
"";
162 std::string fast =
"";
164 for (
const auto& property: currentVar.children(
"PROPERTY")) {
165 const auto pair =
gum::split(property.textOrDefault(
""),
"=");
166 if (pair.size() == 2) {
170 if (propertyName ==
"description") {
172 }
else if (propertyName ==
"fast") {
181 auto newVar =
new LabelizedVariable(varName, description, 0);
184 for (
const auto& outcome: currentVar.children(
"OUTCOME"))
185 newVar->addLabel(outcome.textOrDefault(
""));
192 newVar->setDescription(description);
194 if (newVar->name() != varName) {
196 "Variable name (" << varName <<
") and fast syntax (" << fast
197 <<
") are not compatible. Please check the syntax.")
204 std::string status =
"Network found. Now proceedind variables instanciation...";
205 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
211 template < GUM_Numeric GUM_SCALAR >
214 void BIFXMLBNReader< GUM_SCALAR >::_fillingBN_(XmlElement parentNetwork) {
217 for (
const auto& _: parentNetwork.children(
"DEFINITION")) {
224 for (
const auto& currentVar: parentNetwork.children(
"DEFINITION")) {
226 std::string currentVarName = currentVar.firstChildElement(
"FOR").textOrDefault(
"");
227 NodeId currentVarId = _bn_->idFromName(currentVarName);
230 List< NodeId > parentList;
232 for (
const auto& given: currentVar.children(
"GIVEN")) {
233 std::string parentNode = given.textOrDefault(
"");
234 NodeId parentId = _bn_->idFromName(parentNode);
235 parentList.pushBack(parentId);
238 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
239 parentListIte != parentList.rendSafe();
241 _bn_->addArc(*parentListIte, currentVarId);
244 XmlElement tableElement = currentVar.firstChildElement(
"TABLE");
245 std::istringstream issTableString(tableElement.textOrDefault(
""));
246 std::list< GUM_SCALAR > tablelist;
249 while (!issTableString.eof()) {
250 issTableString >> value;
251 tablelist.push_back(value);
254 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
257 _bn_->cpt(currentVarId).fillWith(tablevector);
260 std::string status =
"All variables have been instancied. Now filling up diagram...";
261 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.
GUM_NODISCARD const char * what() const noexcept override
Exception : input/output problem.
#define GUM_ERROR(type, msg)
GUM_SHARED_PUBLIC std::string toLower(std::string_view str)
Returns the lowercase version of str.
GUM_SHARED_PUBLIC std::string trim_copy(std::string_view s)
trim from both ends (copying)
std::vector< std::string > split(std::string_view str, std::string_view delim)
Split str using the delimiter.
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.