aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLBNReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 BIFXMLBNReader< GUM_SCALAR >::BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn,
33  const std::string& filePath) :
36  _bn_ = bn;
38  }
39 
40  /*
41  * Default destructor.
42  */
43  template < typename GUM_SCALAR >
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 ...";
60 
62  xmlDoc.LoadFile();
63 
64  if (xmlDoc.NoChildren()) {
65  GUM_ERROR(IOError, ": Loading fail, please check the file for any syntax error.")
66  }
67 
68  // Finding BIF element
69  status = "File loaded. Now looking for BIF element ...";
71 
73 
74  // Finding network element
75  status = "BIF Element reached. Now searching network ...";
77 
79 
80  // Finding id variables
81  status = "Network found. Now proceedind variables instanciation...";
83 
85 
86  // Filling diagram
87  status = "All variables have been instancied. Now filling up diagram...";
89 
91 
92  status = "Instanciation of network completed";
93  GUM_EMIT2(onProceed, 100, status);
94 
95  return 0;
97  }
98 
99  template < typename GUM_SCALAR >
101  // Counting the number of variable for the signal
102  int nbVar = 0;
103  ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
104 
106  nbVar++;
107 
108  // Iterating on variable element
109  int nbIte = 0;
110 
111  for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
113 
114  // Getting variable name
117 
118  // Getting variable description
121 
122  // Instanciation de la variable
124 
125  // Getting variable outcomes
126  ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
127 
130  ++varOutComesIte)
132 
133  // Add the variable to the bn and then delete newVar (add makes a copy)
134  _bn_->add(*newVar);
135  delete (newVar);
136 
137  // Emitting progress.
138  std::string status = "Network found. Now proceedind variables instanciation...";
139  int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
141  nbIte++;
142  }
143  }
144 
145  template < typename GUM_SCALAR >
147  // Counting the number of variable for the signal
148  int nbDef = 0;
149  ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
150 
152  ++definitionIte)
153  nbDef++;
154 
155  // Iterating on definition nodes
156  int nbIte = 0;
157 
159  ++definitionIte) {
161 
162  // Considered Node
165 
166  // Get Node's parents
167  ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
169 
174  }
175 
178  --parentListIte)
180 
181  // Recuperating tables values
186 
187  while (!issTableString.eof()) {
190  }
191 
193 
194  // Filling tables
196 
197  // Emitting progress.
198  std::string status = "All variables have been instancied. Now filling up diagram...";
199  int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
201  nbIte++;
202  }
203  }
204 
205 } /* namespace gum */
206 
207 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643