aGrUM  0.16.0
BIFXMLBNReader_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
25 
26 namespace gum {
27  /*
28  * Constructor
29  * A reader is created to reading a defined file.
30  * Note that an BN as to be created before and given in parameter.
31  */
32  template < typename GUM_SCALAR >
33  INLINE
34  BIFXMLBNReader< GUM_SCALAR >::BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn,
35  const std::string& filePath) :
36  BNReader< GUM_SCALAR >(bn, filePath) {
37  GUM_CONSTRUCTOR(BIFXMLBNReader);
38  __bn = bn;
39  __filePath = filePath;
40  }
41 
42  /*
43  * Default destructor.
44  */
45  template < typename GUM_SCALAR >
47  GUM_DESTRUCTOR(BIFXMLBNReader);
48  }
49 
50  /*
51  * Reads the bayes net from the file referenced by filePath given at the
52  * creation
53  * of class
54  * @return Returns the number of error during the parsing (0 if none).
55  */
56  template < typename GUM_SCALAR >
58  try {
59  // Loading file
60  std::string status = "Loading File ...";
61  GUM_EMIT2(onProceed, 0, status);
62 
64  xmlDoc.LoadFile();
65 
66  if (xmlDoc.NoChildren()) {
67  GUM_ERROR(IOError,
68  ": Loading fail, please check the file for any syntax error.");
69  }
70 
71  // Finding BIF element
72  status = "File loaded. Now looking for BIF element ...";
73  GUM_EMIT2(onProceed, 4, status);
74 
75  ticpp::Element* bifElement = xmlDoc.FirstChildElement("BIF");
76 
77  // Finding network element
78  status = "BIF Element reached. Now searching network ...";
79  GUM_EMIT2(onProceed, 7, status);
80 
81  ticpp::Element* networkElement = bifElement->FirstChildElement("NETWORK");
82 
83  // Finding id variables
84  status = "Network found. Now proceedind variables instanciation...";
85  GUM_EMIT2(onProceed, 10, status);
86 
87  __parsingVariables(networkElement);
88 
89  // Filling diagram
90  status = "All variables have been instancied. Now filling up diagram...";
91  GUM_EMIT2(onProceed, 55, status);
92 
93  __fillingBN(networkElement);
94 
95  status = "Instanciation of network completed";
96  GUM_EMIT2(onProceed, 100, status);
97 
98  return 0;
99  } catch (ticpp::Exception& tinyexception) {
100  GUM_ERROR(IOError, tinyexception.what());
101  return 1;
102  }
103  }
104 
105  template < typename GUM_SCALAR >
107  ticpp::Element* parentNetwork) {
108  // Counting the number of variable for the signal
109  int nbVar = 0;
110  ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
111 
112  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
113  nbVar++;
114 
115  // Iterating on variable element
116  int nbIte = 0;
117 
118  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
119  ticpp::Element* currentVar = varIte.Get();
120 
121  // Getting variable name
122  ticpp::Element* varNameElement = currentVar->FirstChildElement("NAME");
123  std::string varName = varNameElement->GetTextOrDefault("");
124 
125  // Getting variable description
126  ticpp::Element* varDescrElement = currentVar->FirstChildElement("PROPERTY");
127  std::string varDescription = varDescrElement->GetTextOrDefault("");
128 
129  // Instanciation de la variable
130  auto newVar = new LabelizedVariable(varName, varDescription, 0);
131 
132  // Getting variable outcomes
133  ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
134 
135  for (varOutComesIte = varOutComesIte.begin(currentVar);
136  varOutComesIte != varOutComesIte.end();
137  ++varOutComesIte)
138  newVar->addLabel(varOutComesIte->GetTextOrDefault(""));
139 
140  // Add the variable to the bn and then delete newVar (add makes a copy)
141  __bn->add(*newVar);
142  delete (newVar);
143 
144  // Emitting progress.
145  std::string status =
146  "Network found. Now proceedind variables instanciation...";
147  int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
148  GUM_EMIT2(onProceed, progress, status);
149  nbIte++;
150  }
151  }
152 
153  template < typename GUM_SCALAR >
155  // Counting the number of variable for the signal
156  int nbDef = 0;
157  ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
158 
159  for (definitionIte = definitionIte.begin(parentNetwork);
160  definitionIte != definitionIte.end();
161  ++definitionIte)
162  nbDef++;
163 
164  // Iterating on definition nodes
165  int nbIte = 0;
166 
167  for (definitionIte = definitionIte.begin(parentNetwork);
168  definitionIte != definitionIte.end();
169  ++definitionIte) {
170  ticpp::Element* currentVar = definitionIte.Get();
171 
172  // Considered Node
173  std::string currentVarName =
174  currentVar->FirstChildElement("FOR")->GetTextOrDefault("");
175  NodeId currentVarId = __bn->idFromName(currentVarName);
176 
177  // Get Node's parents
178  ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
179  List< NodeId > parentList;
180 
181  for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end();
182  ++givenIte) {
183  std::string parentNode = givenIte->GetTextOrDefault("");
184  NodeId parentId = __bn->idFromName(parentNode);
185  parentList.pushBack(parentId);
186  }
187 
188  for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
189  parentListIte != parentList.rendSafe();
190  --parentListIte)
191  __bn->addArc(*parentListIte, currentVarId);
192 
193  // Recuperating tables values
194  ticpp::Element* tableElement = currentVar->FirstChildElement("TABLE");
195  std::istringstream issTableString(tableElement->GetTextOrDefault(""));
196  std::list< GUM_SCALAR > tablelist;
197  GUM_SCALAR value;
198 
199  while (!issTableString.eof()) {
200  issTableString >> value;
201  tablelist.push_back(value);
202  }
203 
204  std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
205 
206  // Filling tables
207  __bn->cpt(currentVarId).fillWith(tablevector);
208 
209  // Emitting progress.
210  std::string status =
211  "All variables have been instancied. Now filling up diagram...";
212  int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
213  GUM_EMIT2(onProceed, progress, status);
214  nbIte++;
215  }
216  }
217 
218 } /* namespace gum */
219 
220 #endif // DOXYGEN_SHOULD_SKIP_THIS
void __parsingVariables(ticpp::Element *parentNetwork)
Parsing xml element containing data on variables.
Wrapper around TiXmlElement.
Definition: ticpp.h:1493
std::string GetTextOrDefault(const std::string &defaultValue) const
Gets the text of an Element, if it doesn&#39;t exist it will return the defaultValue. ...
Definition: ticpp.h:1626
BayesNet< GUM_SCALAR > * __bn
An handle to the bayes net in which will be load the content of the xml filePath. ...
std::string __filePath
the path to the xml filePath
ListIteratorSafe< Val > iterator_safe
Types for STL compliance.
Definition: list.h:386
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void __fillingBN(ticpp::Element *parentNetwork)
fill the diagram
const char * what() const
Override std::exception::what() to return m_details.
Definition: ticpp.cpp:920
This is a ticpp exception class.
Definition: ticpp.h:74
#define GUM_EMIT2(signal, arg1, arg2)
Definition: signaler2.h:42
gum::Signaler2< int, std::string > onProceed
Signaler used to indicates how many percent of the Xml files have been parsed yet.
Size proceed() final
Reads the bayes net from the file referenced by filePath given at the creation of class...
Wrapper around TiXmlDocument.
Definition: ticpp.h:1404
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
Definition: ticpp.cpp:501
Iterator for conveniently stepping through Nodes and Attributes.
Definition: ticpp.h:1112
BIFXMLBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filePath)
Constructor A reader is created to reading a defined file.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
~BIFXMLBNReader() final
Default destructor.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55