aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
netWriter_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 
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())
53  GUM_ERROR(IOError, "Stream states flags are not all unset.");
54 
55  output << header__(bn) << std::endl;
56 
57  for (auto node: bn.nodes())
59 
60  for (auto node: bn.nodes())
62 
63  output << std::endl;
64 
65  output.flush();
66 
67  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
68  }
69 
70  // Writes a Bayesian network in the referenced file using the BN format.
71  // If the file doesn't exists, it is created.
72  // If the file exists, it's content will be erased.
73  //
74  // @param filePath The path to the file used to write the Bayesian network.
75  // @param bn The Bayesian network writed in the file.
76  // @throws Raised if an I/O error occurs.
77  template < typename GUM_SCALAR >
79  const IBayesNet< GUM_SCALAR >& bn) {
81 
82  if (!output.good()) {
83  GUM_ERROR(IOError, "Stream states flags are not all unset.");
84  }
85 
86  output << header__(bn) << std::endl;
87 
88  for (auto node: bn.nodes())
90 
91  for (auto node: bn.nodes())
93 
94  output << std::endl;
95 
96  output.flush();
97  output.close();
98 
99  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
100  }
101 
102  // Returns a bloc defining a variable's CPT in the BN format.
103  template < typename GUM_SCALAR >
104  INLINE std::string
107  std::string tab = " "; // poor tabulation
108 
110  if (cpt.nbrDim() == 1) {
111  str << "potential (" << cpt.variable(0).name() << ") {" << std::endl
112  << tab << "data = ( ";
113 
114  for (inst.setFirst(); !inst.end(); ++inst) {
115  str << " " << cpt[inst];
116  }
117 
118  str << ");";
119  } else { // cpt.domainSize() > 1
121 
123  for (Idx i = 1; i < varsSeq.size(); i++)
124  conds.add(*varsSeq[varsSeq.size() - i]);
125 
126  str << "potential ( " << (varsSeq[(Idx)0])->name() << " | ";
127  for (Idx i = 1; i < varsSeq.size(); i++)
128  str << varsSeq[i]->name() << " ";
129  str << ") {" << std::endl << tab << "data = \n";
130 
131  std::string comment;
132  conds.setFirst();
133  while (true) {
134  str << tab << "(";
135  for (Idx i = 0; i < conds.nbrDim(); i++) {
136  if (conds.val(i) != 0) break;
137  str << "(";
138  }
139 
140  inst.setVals(conds);
141  for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
142  str << tab << cpt[inst];
143 
144  comment = tab + "% ";
145  for (Idx i = 0; i < conds.nbrDim(); i++) {
146  comment += conds.variable(i).name() + "="
147  + conds.variable(i).label(conds.val(i)) + tab;
148  }
149 
150  ++conds;
151  if (conds.end()) {
152  for (Idx i = 0; i < inst.nbrDim(); i++) {
153  str << ")";
154  }
155  str << ";" << comment;
156  break;
157  } else {
158  for (Idx i = 0; i < conds.nbrDim(); i++) {
159  str << ")";
160  if (conds.val(i) != 0) break;
161  }
162  str << comment << "\n";
163  }
164  }
165  }
166  str << "\n}\n" << std::endl;
167  return str.str();
168  }
169 
170  // Returns the header of the BN file.
171  template < typename GUM_SCALAR >
172  INLINE std::string
175  std::string tab = " "; // poor tabulation
176  str << std::endl << "net {" << std::endl;
177  str << " name = " << bn.propertyWithDefault("name", "unnamedBN") << ";"
178  << std::endl;
179  str << " software = \"aGrUM " << GUM_VERSION << "\";" << std::endl;
180  str << " node_size = (50 50);" << std::endl;
181  str << "}" << std::endl;
182  return str.str();
183  }
184 
185  // Returns a bloc defining a variable in the BN format.
186  template < typename GUM_SCALAR >
187  INLINE std::string
190  std::string tab = " "; // poor tabulation
191  str << "node " << var.name() << " {" << std::endl;
192  str << tab << "states = (";
193 
194  for (Idx i = 0; i < var.domainSize(); i++) {
195  str << var.label(i) << " ";
196  }
197 
198  str << ");" << std::endl;
199  str << tab << "label = \"" << var.name() << "\";" << std::endl;
200  str << tab << "ID = \"" << var.name() << "\";" << std::endl;
201 
202  str << "}" << std::endl;
203 
204  return str.str();
205  }
206 } /* namespace gum */
207 
208 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669