aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
BIFXMLBNReader_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
47
48namespace gum {
49 /*
50 * Constructor
51 * A reader is created to reading a defined file.
52 * Note that an BN as to be created before and given in parameter.
53 */
54 template < typename GUM_SCALAR >
56 const std::string& filePath) :
57 BNReader< GUM_SCALAR >(bn, filePath) {
58 GUM_CONSTRUCTOR(BIFXMLBNReader);
59 _bn_ = bn;
60 _filePath_ = filePath;
61 }
62
63 /*
64 * Default destructor.
65 */
66 template < typename GUM_SCALAR >
67 INLINE BIFXMLBNReader< GUM_SCALAR >::~BIFXMLBNReader() {
68 GUM_DESTRUCTOR(BIFXMLBNReader);
69 }
70
71 /*
72 * Reads the bayes net from the file referenced by filePath given at the
73 * creation
74 * of class
75 * @return Returns the number of errors during the parsing (0 if none).
76 */
77 template < typename GUM_SCALAR >
78 Size BIFXMLBNReader< GUM_SCALAR >::proceed() {
79 try {
80 // Loading file
81 std::string status = "Loading File ...";
82 GUM_EMIT2(onProceed, 0, status);
83
84 ticpp::Document xmlDoc(_filePath_);
85 xmlDoc.LoadFile();
86
87 if (xmlDoc.NoChildren()) {
88 GUM_ERROR(IOError, ": Loading fail, please check the file for any syntax error.")
89 }
90
91 // Finding BIF element
92 status = "File loaded. Now looking for BIF element ...";
93 GUM_EMIT2(onProceed, 4, status);
94
95 ticpp::Element* bifElement = xmlDoc.FirstChildElement("BIF");
96
97 // Finding network element
98 status = "BIF Element reached. Now searching network ...";
99 GUM_EMIT2(onProceed, 7, status);
100
101 ticpp::Element* networkElement = bifElement->FirstChildElement("NETWORK");
102
103 // Finding id variables
104 status = "Network found. Now proceeding variables instantiation...";
105 GUM_EMIT2(onProceed, 10, status);
106
107 _parsingVariables_(networkElement);
108
109 // Filling diagram
110 status = "All variables have been instantiated. Now filling up diagram...";
111 GUM_EMIT2(onProceed, 55, status);
112
113 _fillingBN_(networkElement);
114
115 status = "Instantiation of network completed";
116 GUM_EMIT2(onProceed, 100, status);
117
118 return 0;
119 } catch (ticpp::Exception& tinyexception) { GUM_ERROR(IOError, tinyexception.what()) }
120 }
121
122 template < typename GUM_SCALAR >
123 void BIFXMLBNReader< GUM_SCALAR >::_parsingVariables_(ticpp::Element* parentNetwork) {
124 int nbVar = 0;
125 ticpp::Iterator< ticpp::Element > varIte("VARIABLE");
126
127 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte)
128 nbVar++;
129
130 // Iterating on variable element
131 int nbIte = 0;
132
133 for (varIte = varIte.begin(parentNetwork); varIte != varIte.end(); ++varIte) {
134 ticpp::Element* currentVar = varIte.Get();
135
136 // Getting variable name
137 ticpp::Element* varNameElement = currentVar->FirstChildElement("NAME");
138 std::string varName = varNameElement->GetTextOrDefault("");
139
140 std::string description = "";
141 std::string fast = "";
142 // Getting variable description and/or fast syntax
143 ticpp::Iterator< ticpp::Element > varPropertiesIte("PROPERTY");
144 for (varPropertiesIte = varPropertiesIte.begin(currentVar);
145 varPropertiesIte != varPropertiesIte.end();
146 ++varPropertiesIte) {
147 const auto pair = gum::split(varPropertiesIte->GetTextOrDefault(""), "=");
148 if (pair.size() == 2) {
149 const auto property = gum::toLower(gum::trim_copy(pair[0]));
150 const auto value = gum::trim_copy(pair[1]);
151 // check for descritpion and fast
152 if (property == "description") {
153 description = value;
154 } else if (property == "fast") {
155 fast = value;
156 }
157 }
158 }
159
160 if (fast == "") {
161 // if no fast syntax, we create a variable with the default}
162 // Instanciation de la variable
163 auto newVar = new LabelizedVariable(varName, description, 0);
164
165 // Getting variable outcomes
166 ticpp::Iterator< ticpp::Element > varOutComesIte("OUTCOME");
167
168 for (varOutComesIte = varOutComesIte.begin(currentVar);
169 varOutComesIte != varOutComesIte.end();
170 ++varOutComesIte)
171 newVar->addLabel(varOutComesIte->GetTextOrDefault(""));
172
173 // Add the variable to the bn and then delete newVar (add makes a copy)
174 _bn_->add(*newVar);
175 delete newVar;
176 } else {
177 auto newVar = gum::fastVariable(fast, 2);
178 newVar->setDescription(description);
179 // we could check if varName is OK
180 if (newVar->name() != varName) {
182 "Variable name (" << varName << ") and fast syntax (" << fast
183 << ") are not compatible. Please check the syntax.")
184 }
185 // Add the variable to the bn and then delete newVar (add makes a copy)
186 _bn_->add(*newVar);
187 }
188
189 // Emitting progress.
190 std::string status = "Network found. Now proceedind variables instanciation...";
191 int progress = (int)((float)nbIte / (float)nbVar * 45) + 10;
192 GUM_EMIT2(onProceed, progress, status);
193 nbIte++;
194 }
195 }
196
197 template < typename GUM_SCALAR >
198
199
200 void BIFXMLBNReader< GUM_SCALAR >::_fillingBN_(ticpp::Element* parentNetwork) {
201 // Counting the number of variable for the signal
202 int nbDef = 0;
203 ticpp::Iterator< ticpp::Element > definitionIte("DEFINITION");
204
205 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
206 ++definitionIte)
207 nbDef++;
208
209 // Iterating on definition nodes
210 int nbIte = 0;
211
212 for (definitionIte = definitionIte.begin(parentNetwork); definitionIte != definitionIte.end();
213 ++definitionIte) {
214 ticpp::Element* currentVar = definitionIte.Get();
215
216 // Considered Node
217 std::string currentVarName = currentVar->FirstChildElement("FOR")->GetTextOrDefault("");
218 NodeId currentVarId = _bn_->idFromName(currentVarName);
219
220 // Get Node's parents
221 ticpp::Iterator< ticpp::Element > givenIte("GIVEN");
222 List< NodeId > parentList;
223
224 for (givenIte = givenIte.begin(currentVar); givenIte != givenIte.end(); ++givenIte) {
225 std::string parentNode = givenIte->GetTextOrDefault("");
226 NodeId parentId = _bn_->idFromName(parentNode);
227 parentList.pushBack(parentId);
228 }
229
230 for (List< NodeId >::iterator_safe parentListIte = parentList.rbeginSafe();
231 parentListIte != parentList.rendSafe();
232 --parentListIte)
233 _bn_->addArc(*parentListIte, currentVarId);
234
235 // Recuperating tables values
236 ticpp::Element* tableElement = currentVar->FirstChildElement("TABLE");
237 std::istringstream issTableString(tableElement->GetTextOrDefault(""));
238 std::list< GUM_SCALAR > tablelist;
239 GUM_SCALAR value;
240
241 while (!issTableString.eof()) {
242 issTableString >> value;
243 tablelist.push_back(value);
244 }
245
246 std::vector< GUM_SCALAR > tablevector(tablelist.begin(), tablelist.end());
247
248 // Filling tables
249 _bn_->cpt(currentVarId).fillWith(tablevector);
250
251 // Emitting progress.
252 std::string status = "All variables have been instancied. Now filling up diagram...";
253 int progress = (int)((float)nbIte / (float)nbDef * 45) + 55;
254 GUM_EMIT2(onProceed, progress, status);
255 nbIte++;
256 }
257 }
258} /* namespace gum */
259
260#endif // DOXYGEN_SHOULD_SKIP_THIS
classe for import of bayes net from a XML file written with BIF Format
BIFXMLBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filePath)
Constructor A reader is created to reading a defined file.
Pure virtual class for reading a BN from a file.
Definition BNReader.h:76
Class representing a Bayesian network.
Definition BayesNet.h:93
Exception : input/output problem.
Wrapper around TiXmlDocument.
Definition ticpp.h:1409
Wrapper around TiXmlElement.
Definition ticpp.h:1500
std::string GetTextOrDefault(const std::string &defaultValue) const
Gets the text of an Element, if it doesn't exist it will return the defaultValue.
Definition ticpp.h:1636
This is a ticpp exception class.
Definition ticpp.h:74
const char * what() const
Override std::exception::what() to return m_details.
Definition ticpp.cpp:920
Iterator for conveniently stepping through Nodes and Attributes.
Definition ticpp.h:1112
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
Definition ticpp.cpp:501
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::string toLower(std::string str)
Returns the lowercase version of str.
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
std::string trim_copy(const std::string &s)
trim from both ends (copying)
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::unique_ptr< DiscreteVariable > fastVariable(std::string var_description, Size default_domain_size)
Create a pointer on a Discrete Variable from a "fast" syntax.
#define GUM_EMIT2(signal, arg1, arg2)
Definition signaler2.h:61
Utilities for manipulating strings.