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 ...";
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 ...";
105 status =
"BIF Element reached. Now searching 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_(
ticpp::Element* parentNetwork) {
134 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
140 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
147 std::string description =
"";
148 std::string fast =
"";
152 for (varPropertiesIte = varPropertiesIte.begin(currentVar);
153 varPropertiesIte != varPropertiesIte.end();
154 ++varPropertiesIte) {
155 const auto pair =
gum::split(varPropertiesIte->GetTextOrDefault(
""),
"=");
156 if (pair.size() == 2) {
160 if (property ==
"description") {
162 }
else if (property ==
"fast") {
168 const auto nodeType = currentVar->
GetAttribute< std::string >(
"TYPE");
172 auto newVar =
new LabelizedVariable(varName, description, 0);
177 for (varOutComesIte = varOutComesIte.begin(currentVar);
178 varOutComesIte != varOutComesIte.end();
180 newVar->addLabel(varOutComesIte->GetTextOrDefault(
""));
184 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
185 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
186 else _infdiag_->addChanceNode(*newVar);
190 newVar->setDescription(description);
192 if (newVar->name() != varName) {
194 "Variable name (" << varName <<
") and fast syntax (" << fast
195 <<
") are not compatible. Please check the syntax.")
199 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
200 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
201 else _infdiag_->addChanceNode(*newVar);
206 std::string status =
"Network found. Now proceedind variables instanciation...";
207 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
213 template < GUM_Numeric GUM_SCALAR >
214 void BIFXMLIDReader< GUM_SCALAR >::_fillingDiagram_(
ticpp::Element* parentNetwork) {
219 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
226 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
232 NodeId currentVarId = _infdiag_->idFromName(currentVarName);
236 List< NodeId > parentList;
238 for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end(); ++givenIte) {
239 std::string parentNode = givenIte->GetTextOrDefault(
"");
240 NodeId parentId = _infdiag_->idFromName(parentNode);
241 parentList.pushBack(parentId);
244 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
245 parentListIte != parentList.rendSafe();
247 _infdiag_->addArc(*parentListIte, currentVarId);
250 if (!_infdiag_->isDecisionNode(currentVarId)) {
253 std::list< GUM_SCALAR > tablelist;
256 while (!issTableString.eof()) {
257 issTableString >> value;
258 tablelist.push_back(value);
261 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
264 if (_infdiag_->isChanceNode(currentVarId)) {
265 const Tensor< GUM_SCALAR >* table = &_infdiag_->cpt(currentVarId);
266 table->populate(tablevector);
267 }
else if (_infdiag_->isUtilityNode(currentVarId)) {
268 const Tensor< GUM_SCALAR >* table = &_infdiag_->utility(currentVarId);
269 table->populate(tablevector);
274 std::string status =
"All variables have been instancied. Now filling up diagram...";
275 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.
Pure virtual class for importing an ID from a file.
Exception : input/output problem.
Class representing an Influence Diagram.
Wrapper around TiXmlDocument.
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.
T GetAttribute(const std::string &name, bool throwIfNotFound=true) const
Returns an attribute of name from an element.
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.
#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.