43#ifndef DOXYGEN_SHOULD_SKIP_THIS
59 template <
typename GUM_SCALAR >
61 const std::string& filePath) :
62 IDReader< GUM_SCALAR >(infdiag, filePath) {
63 GUM_CONSTRUCTOR(BIFXMLIDReader);
65 _filePath_ = filePath;
71 template <
typename GUM_SCALAR >
72 INLINE BIFXMLIDReader< GUM_SCALAR >::~BIFXMLIDReader() {
73 GUM_DESTRUCTOR(BIFXMLIDReader);
82 template <
typename GUM_SCALAR >
83 void BIFXMLIDReader< GUM_SCALAR >::proceed() {
86 std::string status =
"Loading File ...";
92 if (xmlDoc.NoChildren()) {
93 GUM_ERROR(
IOError,
": Loading fail, please check the file for any syntax error.")
97 status =
"File loaded. Now looking for BIF element ...";
103 status =
"BIF Element reached. Now searching network ...";
109 status =
"Network found. Now proceeding variables instanciation...";
112 _parsingVariables_(networkElement);
115 status =
"All variables have been instancied. Now filling up diagram...";
118 _fillingDiagram_(networkElement);
120 status =
"Instanciation of network completed";
125 template <
typename GUM_SCALAR >
126 void BIFXMLIDReader< GUM_SCALAR >::_parsingVariables_(
ticpp::Element* parentNetwork) {
131 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
137 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
144 std::string description =
"";
145 std::string fast =
"";
149 for (varPropertiesIte = varPropertiesIte.begin(currentVar);
150 varPropertiesIte != varPropertiesIte.end();
151 ++varPropertiesIte) {
152 const auto pair =
gum::split(varPropertiesIte->GetTextOrDefault(
""),
"=");
153 if (pair.size() == 2) {
157 if (property ==
"description") {
159 }
else if (property ==
"fast") {
165 const auto nodeType = currentVar->
GetAttribute< std::string >(
"TYPE");
169 auto newVar =
new LabelizedVariable(varName, description, 0);
174 for (varOutComesIte = varOutComesIte.begin(currentVar);
175 varOutComesIte != varOutComesIte.end();
177 newVar->addLabel(varOutComesIte->GetTextOrDefault(
""));
181 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
182 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
183 else _infdiag_->addChanceNode(*newVar);
187 newVar->setDescription(description);
189 if (newVar->name() != varName) {
191 "Variable name (" << varName <<
") and fast syntax (" << fast
192 <<
") are not compatible. Please check the syntax.")
196 if (nodeType ==
"decision") _infdiag_->addDecisionNode(*newVar);
197 else if (nodeType ==
"utility") _infdiag_->addUtilityNode(*newVar);
198 else _infdiag_->addChanceNode(*newVar);
203 std::string status =
"Network found. Now proceedind variables instanciation...";
204 int progress = (int)((
float)nbIte / (
float)nbVar * 45) + 10;
210 template <
typename GUM_SCALAR >
211 void BIFXMLIDReader< GUM_SCALAR >::_fillingDiagram_(
ticpp::Element* parentNetwork) {
216 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
223 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
229 NodeId currentVarId = _infdiag_->idFromName(currentVarName);
233 List< NodeId > parentList;
235 for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end(); ++givenIte) {
236 std::string parentNode = givenIte->GetTextOrDefault(
"");
237 NodeId parentId = _infdiag_->idFromName(parentNode);
238 parentList.pushBack(parentId);
241 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
242 parentListIte != parentList.rendSafe();
244 _infdiag_->addArc(*parentListIte, currentVarId);
247 if (!_infdiag_->isDecisionNode(currentVarId)) {
250 std::list< GUM_SCALAR > tablelist;
253 while (!issTableString.eof()) {
254 issTableString >> value;
255 tablelist.push_back(value);
258 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
261 if (_infdiag_->isChanceNode(currentVarId)) {
262 const Tensor< GUM_SCALAR >* table = &_infdiag_->cpt(currentVarId);
263 table->populate(tablevector);
264 }
else if (_infdiag_->isUtilityNode(currentVarId)) {
265 const Tensor< GUM_SCALAR >* table = &_infdiag_->utility(currentVarId);
266 table->populate(tablevector);
271 std::string status =
"All variables have been instancied. Now filling up diagram...";
272 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, const std::string &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 str)
Returns the lowercase version of str.
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
std::string trim_copy(const std::string &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.