aGrUM  0.14.2
BIFXMLBNReader_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 namespace gum {
25  /*
26  * Constructor
27  * A reader is created to reading a defined file.
28  * Note that an BN as to be created before and given in parameter.
29  */
30  template < typename GUM_SCALAR >
31  INLINE
32  BIFXMLBNReader< GUM_SCALAR >::BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn,
33  const std::string& filePath) :
34  BNReader< GUM_SCALAR >(bn, filePath) {
35  GUM_CONSTRUCTOR(BIFXMLBNReader);
36  __bn = bn;
37  __filePath = filePath;
38  }
39 
40  /*
41  * Default destructor.
42  */
43  template < typename GUM_SCALAR >
45  GUM_DESTRUCTOR(BIFXMLBNReader);
46  }
47 
48  /*
49  * Reads the bayes net from the file referenced by filePath given at the
50  * creation
51  * of class
52  * @return Returns the number of error during the parsing (0 if none).
53  */
54  template < typename GUM_SCALAR >
56  try {
57  // Loading file
58  std::string status = "Loading File ...";
59  GUM_EMIT2(onProceed, 0, status);
60 
62  xmlDoc.LoadFile();
63 
64  if (xmlDoc.NoChildren()) {
65  GUM_ERROR(IOError,
66  ": Loading fail, please check the file for any syntax error.");
67  }
68 
69  // Finding BIF element
70  status = "File loaded. Now looking for BIF element ...";
71  GUM_EMIT2(onProceed, 4, status);
72 
73  ticpp::Element* bifElement = xmlDoc.FirstChildElement("BIF");
74 
75  // Finding network element
76  status = "BIF Element reached. Now searching network ...";
77  GUM_EMIT2(onProceed, 7, status);
78 
79  ticpp::Element* networkElement = bifElement->FirstChildElement("NETWORK");
80 
81  // Finding id variables
82  status = "Network found. Now proceedind variables instanciation...";
83  GUM_EMIT2(onProceed, 10, status);
84 
85  __parsingVariables(networkElement);
86 
87  // Filling diagram
88  status = "All variables have been instancied. Now filling up diagram...";
89  GUM_EMIT2(onProceed, 55, status);
90 
91  __fillingBN(networkElement);
92 
93  status = "Instanciation of network completed";
94  GUM_EMIT2(onProceed, 100, status);
95 
96  return 0;
97  } catch (ticpp::Exception& tinyexception) {
98  GUM_ERROR(IOError, tinyexception.what());
99  return 1;
100  }
101  }
102 
103  template < typename GUM_SCALAR >
105  ticpp::Element* parentNetwork) {
106  // Counting the number of variable for the signal
107  int nbVar = 0;
108  ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
109 
110  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
111  nbVar++;
112 
113  // Iterating on variable element
114  int nbIte = 0;
115 
116  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
117  ticpp::Element* currentVar = varIte.Get();
118 
119  // Getting variable name
120  ticpp::Element* varNameElement = currentVar->FirstChildElement("NAME");
121  std::string varName = varNameElement->GetTextOrDefault("");
122 
123  // Getting variable description
124  ticpp::Element* varDescrElement = currentVar->FirstChildElement("PROPERTY");
125  std::string varDescription = varDescrElement->GetTextOrDefault("");
126 
127  // Instanciation de la variable
128  auto newVar = new LabelizedVariable(varName, varDescription, 0);
129 
130  // Getting variable outcomes
131  ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
132 
133  for (varOutComesIte = varOutComesIte.begin(currentVar);
134  varOutComesIte != varOutComesIte.end();
135  ++varOutComesIte)
136  newVar->addLabel(varOutComesIte->GetTextOrDefault(""));
137 
138  // Add the variable to the bn and then delete newVar (add makes a copy)
139  __bn->add(*newVar);
140  delete (newVar);
141 
142  // Emitting progress.
143  std::string status =
144  "Network found. Now proceedind variables instanciation...";
145  int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
146  GUM_EMIT2(onProceed, progress, status);
147  nbIte++;
148  }
149  }
150 
151  template < typename GUM_SCALAR >
153  // Counting the number of variable for the signal
154  int nbDef = 0;
155  ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
156 
157  for (definitionIte = definitionIte.begin(parentNetwork);
158  definitionIte != definitionIte.end();
159  ++definitionIte)
160  nbDef++;
161 
162  // Iterating on definition nodes
163  int nbIte = 0;
164 
165  for (definitionIte = definitionIte.begin(parentNetwork);
166  definitionIte != definitionIte.end();
167  ++definitionIte) {
168  ticpp::Element* currentVar = definitionIte.Get();
169 
170  // Considered Node
171  std::string currentVarName =
172  currentVar->FirstChildElement("FOR")->GetTextOrDefault("");
173  NodeId currentVarId = __bn->idFromName(currentVarName);
174 
175  // Get Node's parents
176  ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
177  List< NodeId > parentList;
178 
179  for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end();
180  ++givenIte) {
181  std::string parentNode = givenIte->GetTextOrDefault("");
182  NodeId parentId = __bn->idFromName(parentNode);
183  parentList.pushBack(parentId);
184  }
185 
186  for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
187  parentListIte != parentList.rendSafe();
188  --parentListIte)
189  __bn->addArc(*parentListIte, currentVarId);
190 
191  // Recuperating tables values
192  ticpp::Element* tableElement = currentVar->FirstChildElement("TABLE");
193  std::istringstream issTableString(tableElement->GetTextOrDefault(""));
194  std::list< GUM_SCALAR > tablelist;
195  GUM_SCALAR value;
196 
197  while (!issTableString.eof()) {
198  issTableString >> value;
199  tablelist.push_back(value);
200  }
201 
202  std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
203 
204  // Filling tables
205  __bn->cpt(currentVarId).fillWith(tablevector);
206 
207  // Emitting progress.
208  std::string status =
209  "All variables have been instancied. Now filling up diagram...";
210  int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
211  GUM_EMIT2(onProceed, progress, status);
212  nbIte++;
213  }
214  }
215 
216 } /* namespace gum */
217 
218 #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:383
classe for import of bayes net from a XML file written with BIF Format
gum is the global namespace for all aGrUM entities
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:40
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:45
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:97
~BIFXMLBNReader() final
Default destructor.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52