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