45#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template < GUM_Numeric GUM_SCALAR >
58 BNReader< GUM_SCALAR >(bn, filePath) {
59 GUM_CONSTRUCTOR(XDSLBNReader);
61 _filePath_ = filePath;
67 template < GUM_Numeric GUM_SCALAR >
68 XDSLBNReader< GUM_SCALAR >::~XDSLBNReader() {
69 GUM_DESTRUCTOR(XDSLBNReader);
78 template < GUM_Numeric GUM_SCALAR >
79 Size XDSLBNReader< GUM_SCALAR >::proceed() {
82 std::string status =
"Loading File ...";
85 XmlDocument xmlDoc(_filePath_);
88 if (xmlDoc.noChildren()) {
89 GUM_ERROR(
IOError,
": Loading fail, please check the file for any syntax error.")
93 status =
"File loaded. Now looking for DSL element ...";
96 XmlElement bifElement = xmlDoc.firstChildElement(
"smile");
97 std::string netName =
"unnamedBN";
98 bifElement.attribute(
"id", &netName,
false);
99 _bn_->setProperty(
"name", netName);
102 status =
"smile Element reached. Now searching network ...";
105 XmlElement nodesElement = bifElement.firstChildElement(
"nodes");
108 status =
"Network found. Now proceeding variables instantiation...";
111 _parsingCpts_(nodesElement);
114 status =
"All variables have been instantiated. Now filling up diagram...";
117 XmlElement extensionsElement
118 = bifElement.firstChildElement(
"extensions").firstChildElement(
"genie");
121 _parsingExtension_(extensionsElement);
122 status =
"All variables have been renamed. Now filling up diagram...";
126 status =
"Instantiation of network completed";
133 template < GUM_Numeric GUM_SCALAR >
134 Size XDSLBNReader< GUM_SCALAR >::_parsingCpts_(XmlElement cptsNetwork) {
136 Size nbVar = Size(0);
137 for (
const auto& _: cptsNetwork.children(
"cpt"))
140 std::string status =
"Network found. Now proceeding variables instantiation...";
146 for (
const auto& currentVar: cptsNetwork.children(
"cpt")) {
148 std::string varName = currentVar.attribute(
"id");
149 std::string varDescription = varName;
152 auto newVar =
new LabelizedVariable(varName, varDescription, 0);
155 for (
const auto& state: currentVar.children(
"state"))
156 newVar->addLabel(state.attribute(
"id"));
163 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
169 for (
const auto& currentVar: cptsNetwork.children(
"cpt")) {
170 std::string varName = currentVar.attribute(
"id");
172 auto parentsElt = currentVar.firstChildElement(
"parents",
false);
173 if (!parentsElt.isNull()) {
175 const auto& strvec =
split(parentsElt.textOrDefault(
""),
" ");
176 for (
auto rit = strvec.begin(); rit != strvec.end(); ++rit)
177 _bn_->addArc(*rit, varName);
180 std::istringstream issTableString(
181 currentVar.firstChildElement(
"probabilities").textOrDefault(
""));
182 std::vector< GUM_SCALAR > tablevector;
185 while (!issTableString.eof()) {
186 issTableString >> value;
187 tablevector.push_back(value);
191 _bn_->cpt(varName).fillWith(tablevector);
198 template < GUM_Numeric GUM_SCALAR >
199 void XDSLBNReader< GUM_SCALAR >::_parsingExtension_(XmlElement nodesNetwork) {
200 for (
const auto& currentVar: nodesNetwork.children(
"node")) {
201 std::string varName = currentVar.attribute(
"id");
202 std::string descName = currentVar.firstChildElement(
"name").textOrDefault(
"");
203 if (descName != varName) _bn_->changeVariableName(varName, descName);
classe for import of bayes net from a XML file written with BIF Format
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.
XDSLBNReader(BayesNet< GUM_SCALAR > *bn, std::string_view filePath)
Constructor A reader is created to reading a defined file.
#define GUM_ERROR(type, msg)
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
#define GUM_EMIT2(signal, arg1, arg2)
Utilities for manipulating strings.