aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
netWriter_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 
24 // to ease parsing in IDE
25 # include <agrum/BN/io/net/netWriter.h>
26 
27 namespace gum {
28  /* =========================================================================*/
29  /* === GUM_BN_WRITER === */
30  /* =========================================================================*/
31  // Default constructor.
32  template < typename GUM_SCALAR >
33  INLINE NetWriter< GUM_SCALAR >::NetWriter() {
35  }
36 
37  // Default destructor.
38  template < typename GUM_SCALAR >
41  }
42 
43  //
44  // Writes a Bayesian network in the output stream using the BN format.
45  //
46  // @param ouput The output stream.
47  // @param bn The Bayesian network writen in output.
48  // @throws Raised if an I/O error occurs.
49  template < typename GUM_SCALAR >
51  const IBayesNet< GUM_SCALAR >& bn) {
52  if (!output.good()) GUM_ERROR(IOError, "Stream states flags are not all unset.")
53 
54  output << _header_(bn) << std::endl;
55 
56  for (auto node: bn.nodes())
58 
59  for (auto node: bn.nodes())
61 
62  output << std::endl;
63 
64  output.flush();
65 
66  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed.") }
67  }
68 
69  // Writes a Bayesian network in the referenced file using the BN format.
70  // If the file doesn't exists, it is created.
71  // If the file exists, it's content will be erased.
72  //
73  // @param filePath The path to the file used to write the Bayesian network.
74  // @param bn The Bayesian network writed in the file.
75  // @throws Raised if an I/O error occurs.
76  template < typename GUM_SCALAR >
78  const IBayesNet< GUM_SCALAR >& bn) {
80 
81  if (!output.good()) { GUM_ERROR(IOError, "Stream states flags are not all unset.") }
82 
83  output << _header_(bn) << std::endl;
84 
85  for (auto node: bn.nodes())
87 
88  for (auto node: bn.nodes())
90 
91  output << std::endl;
92 
93  output.flush();
94  output.close();
95 
96  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed.") }
97  }
98 
99  // Returns a bloc defining a variable's CPT in the BN format.
100  template < typename GUM_SCALAR >
103  std::string tab = " "; // poor tabulation
104 
106  if (cpt.nbrDim() == 1) {
107  str << "potential (" << cpt.variable(0).name() << ") {" << std::endl << tab << "data = ( ";
108 
109  for (inst.setFirst(); !inst.end(); ++inst) {
110  str << " " << cpt[inst];
111  }
112 
113  str << ");";
114  } else { // cpt.domainSize() > 1
116 
118  for (Idx i = 1; i < varsSeq.size(); i++)
119  conds.add(*varsSeq[varsSeq.size() - i]);
120 
121  str << "potential ( " << (varsSeq[(Idx)0])->name() << " | ";
122  for (Idx i = 1; i < varsSeq.size(); i++)
123  str << varsSeq[i]->name() << " ";
124  str << ") {" << std::endl << tab << "data = \n";
125 
126  std::string comment;
127  conds.setFirst();
128  while (true) {
129  str << tab << "(";
130  for (Idx i = 0; i < conds.nbrDim(); i++) {
131  if (conds.val(i) != 0) break;
132  str << "(";
133  }
134 
135  inst.setVals(conds);
136  for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
137  str << tab << cpt[inst];
138 
139  comment = tab + "% ";
140  for (Idx i = 0; i < conds.nbrDim(); i++) {
141  comment += conds.variable(i).name() + "=" + conds.variable(i).label(conds.val(i)) + tab;
142  }
143 
144  ++conds;
145  if (conds.end()) {
146  for (Idx i = 0; i < inst.nbrDim(); i++) {
147  str << ")";
148  }
149  str << ";" << comment;
150  break;
151  } else {
152  for (Idx i = 0; i < conds.nbrDim(); i++) {
153  str << ")";
154  if (conds.val(i) != 0) break;
155  }
156  str << comment << "\n";
157  }
158  }
159  }
160  str << "\n}\n" << std::endl;
161  return str.str();
162  }
163 
164  // Returns the header of the BN file.
165  template < typename GUM_SCALAR >
168  std::string tab = " "; // poor tabulation
169  str << std::endl << "net {" << std::endl;
170  str << " name = " << bn.propertyWithDefault("name", "unnamedBN") << ";" << std::endl;
171  str << " software = \"aGrUM " << GUM_VERSION << "\";" << std::endl;
172  str << " node_size = (50 50);" << std::endl;
173  str << "}" << std::endl;
174  return str.str();
175  }
176 
177  // Returns a bloc defining a variable in the BN format.
178  template < typename GUM_SCALAR >
181  std::string tab = " "; // poor tabulation
182  str << "node " << var.name() << " {" << std::endl;
183  str << tab << "states = (";
184 
185  for (Idx i = 0; i < var.domainSize(); i++) {
186  str << var.label(i) << " ";
187  }
188 
189  str << ");" << std::endl;
190  str << tab << "label = \"" << var.name() << "\";" << std::endl;
191  str << tab << "ID = \"" << var.name() << "\";" << std::endl;
192 
193  str << "}" << std::endl;
194 
195  return str.str();
196  }
197 } /* namespace gum */
198 
199 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643