aGrUM  0.14.2
BIFXMLIDReader_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
22 
24 # include <fstream>
25 # include <iostream>
26 # include <sstream>
27 
28 namespace gum {
29  /*
30  * Constructor
31  * A reader is created to reading a defined file.
32  * Note that an ID as to be created before and given in parameter.
33  */
34  template < typename GUM_SCALAR >
36  InfluenceDiagram< GUM_SCALAR >* infdiag, const std::string& filePath) :
37  IDReader< GUM_SCALAR >(infdiag, filePath) {
38  GUM_CONSTRUCTOR(BIFXMLIDReader);
39  __infdiag = infdiag;
40  __filePath = filePath;
41  }
42 
43  /*
44  * Default destructor.
45  */
46  template < typename GUM_SCALAR >
48  GUM_DESTRUCTOR(BIFXMLIDReader);
49  }
50 
51  /*
52  * Reads the influence diagram from the file referenced by filePath given at
53  * the
54  * creation of class
55  * @return Returns the number of error during the parsing (0 if none).
56  */
57  template < typename GUM_SCALAR >
59  try {
60  // Loading file
61  std::string status = "Loading File ...";
62  GUM_EMIT2(onProceed, 0, status);
63 
65  xmlDoc.LoadFile();
66 
67  if (xmlDoc.NoChildren()) {
68  GUM_ERROR(IOError,
69  ": Loading fail, please check the file for any syntax error.");
70  }
71 
72  // Finding BIF element
73  status = "File loaded. Now looking for BIF element ...";
74  GUM_EMIT2(onProceed, 4, status);
75 
76  ticpp::Element* bifElement = xmlDoc.FirstChildElement("BIF");
77 
78  // Finding network element
79  status = "BIF Element reached. Now searching network ...";
80  GUM_EMIT2(onProceed, 7, status);
81 
82  ticpp::Element* networkElement = bifElement->FirstChildElement("NETWORK");
83 
84  // Finding id variables
85  status = "Network found. Now proceeding variables instanciation...";
86  GUM_EMIT2(onProceed, 10, status);
87 
88  __parsingVariables(networkElement);
89 
90  // Filling diagram
91  status = "All variables have been instancied. Now filling up diagram...";
92  GUM_EMIT2(onProceed, 55, status);
93 
94  __fillingDiagram(networkElement);
95 
96  status = "Instanciation of network completed";
97  GUM_EMIT2(onProceed, 100, status);
98 
99  } catch (ticpp::Exception& tinyexception) {
100  GUM_ERROR(IOError, tinyexception.what());
101  }
102  }
103 
104  template < typename GUM_SCALAR >
106  ticpp::Element* parentNetwork) {
107  // Counting the number of variable for the signal
108  int nbVar = 0;
109  ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
110 
111  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
112  nbVar++;
113 
114  // Iterating on variable element
115  int nbIte = 0;
116 
117  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
118  ticpp::Element* currentVar = varIte.Get();
119 
120  // Getting variable name
121  ticpp::Element* varNameElement = currentVar->FirstChildElement("NAME");
122  std::string varName = varNameElement->GetTextOrDefault("");
123 
124  // Getting variable description
125  ticpp::Element* varDescrElement = currentVar->FirstChildElement("PROPERTY");
126  std::string varDescription = varDescrElement->GetTextOrDefault("");
127 
128  // Instanciation de la variable
129  LabelizedVariable newVar(varName, varDescription, 0);
130 
131  // Getting variable outcomes
132  ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
133 
134  for (varOutComesIte = varOutComesIte.begin(currentVar);
135  varOutComesIte != varOutComesIte.end();
136  ++varOutComesIte)
137  newVar.addLabel(varOutComesIte->GetTextOrDefault(""));
138 
139  // Getting variable type
140  std::string nodeType = currentVar->GetAttribute< std::string >("TYPE");
141 
142  // Add the variable to the id
143  if (nodeType.compare("decision") == 0)
144  __infdiag->addDecisionNode(newVar);
145  else if (nodeType.compare("utility") == 0)
146  __infdiag->addUtilityNode(newVar);
147  else
148  __infdiag->addChanceNode(newVar);
149 
150  // Emitting progress.
151  std::string status =
152  "Network found. Now proceedind variables instanciation...";
153  int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
154  GUM_EMIT2(onProceed, progress, status);
155  nbIte++;
156  }
157  }
158 
159  template < typename GUM_SCALAR >
161  ticpp::Element* parentNetwork) {
162  // Counting the number of variable for the signal
163  int nbDef = 0;
164  ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
165 
166  for (definitionIte = definitionIte.begin(parentNetwork);
167  definitionIte != definitionIte.end();
168  ++definitionIte)
169  nbDef++;
170 
171  // Iterating on definition nodes
172  int nbIte = 0;
173 
174  for (definitionIte = definitionIte.begin(parentNetwork);
175  definitionIte != definitionIte.end();
176  ++definitionIte) {
177  ticpp::Element* currentVar = definitionIte.Get();
178 
179  // Considered Node
180  std::string currentVarName =
181  currentVar->FirstChildElement("FOR")->GetTextOrDefault("");
182  NodeId currentVarId = __infdiag->idFromName(currentVarName);
183 
184  // Get Node's parents
185  ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
186  List< NodeId > parentList;
187 
188  for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end();
189  ++givenIte) {
190  std::string parentNode = givenIte->GetTextOrDefault("");
191  NodeId parentId = __infdiag->idFromName(parentNode);
192  parentList.pushBack(parentId);
193  }
194 
195  for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
196  parentListIte != parentList.rendSafe();
197  --parentListIte)
198  __infdiag->addArc(*parentListIte, currentVarId);
199 
200  // Recuperating tables values
201  if (!__infdiag->isDecisionNode(currentVarId)) {
202  ticpp::Element* tableElement = currentVar->FirstChildElement("TABLE");
203  std::istringstream issTableString(tableElement->GetTextOrDefault(""));
204  std::list< GUM_SCALAR > tablelist;
205  GUM_SCALAR value;
206 
207  while (!issTableString.eof()) {
208  issTableString >> value;
209  tablelist.push_back(value);
210  }
211 
212  std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
213 
214  // Filling tables
215  if (__infdiag->isChanceNode(currentVarId)) {
216  const Potential< GUM_SCALAR >* table = &__infdiag->cpt(currentVarId);
217  table->populate(tablevector);
218  } else if (__infdiag->isUtilityNode(currentVarId)) {
219  const Potential< GUM_SCALAR >* table = &__infdiag->utility(currentVarId);
220  table->populate(tablevector);
221  }
222  }
223 
224  // Emitting progress.
225  std::string status =
226  "All variables have been instancied. Now filling up diagram...";
227  int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
228  GUM_EMIT2(onProceed, progress, status);
229  nbIte++;
230  }
231  }
232 
233 } /* namespace gum */
234 
235 #endif // DOXYGEN_SHOULD_SKIP_THIS
Wrapper around TiXmlElement.
Definition: ticpp.h:1493
std::string __filePath
the path to the xml filePath
classe for import of Influence Diagram from a XML file written with BIF Format
gum::Signaler2< int, std::string > onProceed
Signaler used to indicates how many percent of the Xml files have been parsed yet.
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
ListIteratorSafe< Val > iterator_safe
Types for STL compliance.
Definition: list.h:383
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void __parsingVariables(ticpp::Element *parentNetwork)
Parsing xml element containing data on variables.
void __fillingDiagram(ticpp::Element *parentNetwork)
fill the diagram
BIFXMLIDReader(InfluenceDiagram< GUM_SCALAR > *infdiag, const std::string &filePath)
Constructor A reader is created to reading a defined file.
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
InfluenceDiagram< GUM_SCALAR > * __infdiag
An handle to the influence diagram in which will be load the content of the xml filePath.
~BIFXMLIDReader()
Default destructor.
Wrapper around TiXmlDocument.
Definition: ticpp.h:1404
T GetAttribute(const std::string &name, bool throwIfNotFound=true) const
Returns an attribute of name from an element.
Definition: ticpp.h:1789
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
virtual void proceed()
Reads the influence diagram from the file referenced by filePath given at the creation of class...
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52