45#ifndef DOXYGEN_SHOULD_SKIP_THIS
61 template < GUM_Numeric GUM_SCALAR >
63 std::string_view filePath) :
64 IDReader< GUM_SCALAR >(infdiag, filePath) {
65 GUM_CONSTRUCTOR(BIFXMLIDReader);
67 _filePath_ = filePath;
73 template < GUM_Numeric GUM_SCALAR >
74 BIFXMLIDReader< GUM_SCALAR >::~BIFXMLIDReader() {
75 GUM_DESTRUCTOR(BIFXMLIDReader);
84 template < GUM_Numeric GUM_SCALAR >
85 Size BIFXMLIDReader< GUM_SCALAR >::proceed() {
88 std::string status =
"Loading File ...";
91 XmlDocument xmlDoc(_filePath_);
94 if (xmlDoc.noChildren()) {
95 GUM_ERROR(
IOError,
": Loading fail, please check the file for any syntax error.")
99 status =
"File loaded. Now looking for BIF element ...";
102 XmlElement bifElement = xmlDoc.firstChildElement(
"BIF");
105 status =
"BIF Element reached. Now searching network ...";
108 XmlElement networkElement = bifElement.firstChildElement(
"NETWORK");
111 status =
"Network found. Now proceeding variables instanciation...";
114 _parsingVariables_(networkElement);
117 status =
"All variables have been instancied. Now filling up diagram...";
120 _fillingDiagram_(networkElement);
122 status =
"Instanciation of network completed";
128 template < GUM_Numeric GUM_SCALAR >
129 void BIFXMLIDReader< GUM_SCALAR >::_parsingVariables_(XmlElement parentNetwork) {
132 for (
const auto& _: parentNetwork.children(
"VARIABLE")) {
139 for (
const auto& currentVar: parentNetwork.children(
"VARIABLE")) {
141 XmlElement varNameElement = currentVar.firstChildElement(
"NAME");
142 std::string varName = varNameElement.textOrDefault(
"");
144 std::string description =
"";
145 std::string fast =
"";
148 for (
const auto& property: currentVar.children(
"PROPERTY")) {
149 const auto pair =
gum::split(property.textOrDefault(
""),
"=");
150 if (pair.size() == 2) {
154 if (propertyName ==
"description") {
156 }
else if (propertyName ==
"fast") {
162 const auto nodeType = currentVar.attribute(
"TYPE");
166 auto newVar =
new LabelizedVariable(varName, description, 0);
169 for (
const auto& outcome: currentVar.children(
"OUTCOME")) {
170 newVar->addLabel(outcome.textOrDefault(
""));
174 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
175 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
176 else _infdiag_->addChanceNode(*newVar);
180 newVar->setDescription(description);
182 if (newVar->name() != varName) {
184 "Variable name (" << varName <<
") and fast syntax (" << fast
185 <<
") are not compatible. Please check the syntax.")
189 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
190 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
191 else _infdiag_->addChanceNode(*newVar);
196 std::string status =
"Network found. Now proceedind variables instanciation...";
197 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
203 template < GUM_Numeric GUM_SCALAR >
204 void BIFXMLIDReader< GUM_SCALAR >::_fillingDiagram_(XmlElement parentNetwork) {
207 for (
const auto& _: parentNetwork.children(
"DEFINITION")) {
214 for (
const auto& currentVar: parentNetwork.children(
"DEFINITION")) {
216 std::string currentVarName = currentVar.firstChildElement(
"FOR").textOrDefault(
"");
217 NodeId currentVarId = _infdiag_->idFromName(currentVarName);
220 List< NodeId > parentList;
222 for (
const auto& given: currentVar.children(
"GIVEN")) {
223 std::string parentNode = given.textOrDefault(
"");
224 NodeId parentId = _infdiag_->idFromName(parentNode);
225 parentList.pushBack(parentId);
228 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
229 parentListIte != parentList.rendSafe();
231 _infdiag_->addArc(*parentListIte, currentVarId);
234 if (!_infdiag_->isDecisionNode(currentVarId)) {
235 XmlElement tableElement = currentVar.firstChildElement(
"TABLE");
236 std::istringstream issTableString(tableElement.textOrDefault(
""));
237 std::list< GUM_SCALAR > tablelist;
240 while (!issTableString.eof()) {
241 issTableString >> value;
242 tablelist.push_back(value);
245 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
248 if (_infdiag_->isChanceNode(currentVarId)) {
249 const Tensor< GUM_SCALAR >* table = &_infdiag_->cpt(currentVarId);
250 table->populate(tablevector);
251 }
else if (_infdiag_->isUtilityNode(currentVarId)) {
252 const Tensor< GUM_SCALAR >* table = &_infdiag_->utility(currentVarId);
253 table->populate(tablevector);
258 std::string status =
"All variables have been instancied. Now filling up diagram...";
259 int progress = (int)((
float)nbIte / (
float)nbDef * 45) + 55;
classe for import of Influence Diagram from a XML file written with BIF Format
BIFXMLIDReader(InfluenceDiagram< GUM_SCALAR > *infdiag, std::string_view filePath)
Constructor A reader is created to reading a defined file.
GUM_NODISCARD const char * what() const noexcept override
Pure virtual class for importing an ID from a file.
Exception : input/output problem.
Class representing an Influence Diagram.
#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.