aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
UAIBNWriter_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 # include <agrum/BN/io/UAI/UAIBNWriter.h>
25 
26 namespace gum {
27 
28  /*
29  * Default constructor.
30  */
31  template < typename GUM_SCALAR >
32  INLINE UAIBNWriter< GUM_SCALAR >::UAIBNWriter() {
34  }
35 
36  /*
37  * Destructor.
38  */
39  template < typename GUM_SCALAR >
42  }
43 
44  /*
45  * Writes a bayes net in the given ouput stream.
46  *
47  * @param output The output stream.
48  * @param bn The bayes net writen in the stream.
49  * @throws IOError Raised if an I/O error occurs.
50  */
51  template < typename GUM_SCALAR >
53  const IBayesNet< GUM_SCALAR >& bn) {
54  if (!output.good()) {
55  GUM_ERROR(IOError, "Stream states flags are not all unset.");
56  }
57 
58  output << preambule__(bn) << std::endl;
59 
60  for (auto node: bn.nodes())
61  output << cptBloc__(bn, node) << std::endl;
62 
63  output << std::endl;
64 
65  output.flush();
66 
67  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
68  }
69 
70  /*
71  * Writes a bayes net in the file referenced by filePath.
72  * If the file doesn't exists, it is created.
73  * If the file exists, it's content will be erased.
74  *
75  * @param filePath The path to the file used to write the bayes net.
76  * @param bn The bayes net writen in the file.
77  * @throw IOError Raised if an I/O error occurs.
78  */
79  template < typename GUM_SCALAR >
81  const IBayesNet< GUM_SCALAR >& bn) {
83 
84  write(output, bn);
85 
86  output.close();
87 
88  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
89  }
90 
91  template < typename GUM_SCALAR >
95 
96  str << "BAYES" << std::endl;
97 
98  str << bn.size() << std::endl;
99 
100  for (auto node: bn.nodes())
101  str << bn.variable(node).domainSize() << " ";
102  str << std::endl;
103 
104  str << bn.size() << std::endl; // number of potentials
105 
106  for (auto node: bn.nodes()) {
107  const auto& p = bn.cpt(node);
108  str << p.nbrDim() << " ";
109  // P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
110  // in order to print it at last
111  NodeId first = 0;
112  bool isFirst = true;
113  for (auto k: p.variablesSequence()) {
114  if (isFirst) {
115  isFirst = false;
116  first = bn.idFromName(k->name());
117  } else {
118  str << bn.idFromName(k->name()) << " ";
119  }
120  }
121  str << first << " # " << bn.variable(node).name() << std::endl;
122  }
123  str << std::endl;
124 
125  return str.str();
126  }
127  template < typename GUM_SCALAR >
128  INLINE std::string
130  NodeId node) {
132 
133  const auto& p = bn.cpt(node);
134  str << p.domainSize();
135  Instantiation I(p);
136  for (I.setFirst(); !I.end(); ++I) {
137  if (I.val(0) == 0) str << std::endl << " ";
138  str << p[I] << " ";
139  }
140  str << std::endl;
141 
142  return str.str();
143  }
144 
145 } /* namespace gum */
146 
147 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669