aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BIFXMLBNReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 # include <agrum/BN/io/BIFXML/BIFXMLBNReader.h>
24 
25 namespace gum {
26  /*
27  * Constructor
28  * A reader is created to reading a defined file.
29  * Note that an BN as to be created before and given in parameter.
30  */
31  template < typename GUM_SCALAR >
32  INLINE
33  BIFXMLBNReader< GUM_SCALAR >::BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn,
34  const std::string& filePath) :
37  bn__ = bn;
39  }
40 
41  /*
42  * Default destructor.
43  */
44  template < typename GUM_SCALAR >
47  }
48 
49  /*
50  * Reads the bayes net from the file referenced by filePath given at the
51  * creation
52  * of class
53  * @return Returns the number of error during the parsing (0 if none).
54  */
55  template < typename GUM_SCALAR >
57  try {
58  // Loading file
59  std::string status = "Loading File ...";
61 
63  xmlDoc.LoadFile();
64 
65  if (xmlDoc.NoChildren()) {
67  ": Loading fail, please check the file for any syntax error.");
68  }
69 
70  // Finding BIF element
71  status = "File loaded. Now looking for BIF element ...";
73 
75 
76  // Finding network element
77  status = "BIF Element reached. Now searching network ...";
79 
81 
82  // Finding id variables
83  status = "Network found. Now proceedind variables instanciation...";
85 
87 
88  // Filling diagram
89  status = "All variables have been instancied. Now filling up diagram...";
91 
93 
94  status = "Instanciation of network completed";
95  GUM_EMIT2(onProceed, 100, status);
96 
97  return 0;
98  } catch (ticpp::Exception& tinyexception) {
100  return 1;
101  }
102  }
103 
104  template < typename GUM_SCALAR >
107  // Counting the number of variable for the signal
108  int nbVar = 0;
109  ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
110 
112  nbVar++;
113 
114  // Iterating on variable element
115  int nbIte = 0;
116 
117  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
119 
120  // Getting variable name
123 
124  // Getting variable description
127 
128  // Instanciation de la variable
130 
131  // Getting variable outcomes
132  ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
133 
136  ++varOutComesIte)
138 
139  // Add the variable to the bn and then delete newVar (add makes a copy)
140  bn__->add(*newVar);
141  delete (newVar);
142 
143  // Emitting progress.
144  std::string status
145  = "Network found. Now proceedind variables instanciation...";
146  int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
148  nbIte++;
149  }
150  }
151 
152  template < typename GUM_SCALAR >
154  // Counting the number of variable for the signal
155  int nbDef = 0;
156  ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
157 
160  ++definitionIte)
161  nbDef++;
162 
163  // Iterating on definition nodes
164  int nbIte = 0;
165 
168  ++definitionIte) {
170 
171  // Considered Node
175 
176  // Get Node's parents
177  ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
179 
181  ++givenIte) {
185  }
186 
189  --parentListIte)
191 
192  // Recuperating tables values
197 
198  while (!issTableString.eof()) {
201  }
202 
204 
205  // Filling tables
207 
208  // Emitting progress.
209  std::string status
210  = "All variables have been instancied. Now filling up diagram...";
211  int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
213  nbIte++;
214  }
215  }
216 
217 } /* namespace gum */
218 
219 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669