aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
UAIMNWriter_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) 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/MN/io/UAI/UAIMNWriter.h>
25 
26 namespace gum {
27 
28  /*
29  * Default constructor.
30  */
31  template < typename GUM_SCALAR >
32  INLINE UAIMNWriter< GUM_SCALAR >::UAIMNWriter() {
34  }
35 
36  /*
37  * Destructor.
38  */
39  template < typename GUM_SCALAR >
42  }
43 
44  /*
45  * Writes a Markov net in the given ouput stream.
46  *
47  * @param output The output stream.
48  * @param MN The Markov net writen in the stream.
49  * @throws IOError Raised if an I/O error occurs.
50  */
51  template < typename GUM_SCALAR >
53  const IMarkovNet< GUM_SCALAR >& MN) {
54  if (!output.good()) { GUM_ERROR(IOError, "Stream states flags are not all unset.") }
55 
56  output << _preambule_(MN) << std::endl;
57 
58  for (const auto& kv: MN.factors())
60 
61  output << std::endl;
62 
63  output.flush();
64 
65  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
66  }
67 
68  /*
69  * Writes a Markov net in the file referenced by filePath.
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 Markov net.
74  * @param MN The Markov net writen in the file.
75  * @throw IOError Raised if an I/O error occurs.
76  */
77  template < typename GUM_SCALAR >
79  const IMarkovNet< GUM_SCALAR >& MN) {
81 
82  write(output, MN);
83 
84  output.close();
85 
86  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
87  }
88 
89  template < typename GUM_SCALAR >
92 
93  str << "MARKOV" << std::endl;
94 
95  str << MN.size() << " # nbr Nodes" << std::endl;
96 
97  for (auto node: MN.nodes())
98  str << MN.variable(node).domainSize() << " ";
99  str << std::endl;
100 
101  str << MN.factors().size() << " # nbr Factors " << std::endl; // number of cliques
102 
103  for (const auto& kv: MN.factors()) {
104  const auto& nodeset = kv.first;
105  str << nodeset.size() << " ";
106  for (auto k: nodeset) {
107  str << k << " ";
108  }
109  str << std::endl;
110  }
111 
112  return str.str();
113  }
114 
115  template < typename GUM_SCALAR >
116  INLINE std::string
118  const Potential< GUM_SCALAR >& clikpot) {
120 
121  str << clikpot.domainSize() << " # {";
122 
123  for (Idx k = 0; k < clikpot.nbrDim(); k++) {
125  if (k == clikpot.nbrDim() - 1)
126  str << "}";
127  else
128  str << ", ";
129  }
131  for (I.setFirst(); !I.end(); ++I) {
132  if (I.val(0) == 0) str << std::endl << " ";
133  str << clikpot[I] << " ";
134  }
135  str << std::endl;
136 
137  return str.str();
138  }
139 
140 } /* namespace gum */
141 
142 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643