aGrUM  0.16.0
UAIWriter_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
26 
27 namespace gum {
28 
29  /*
30  * Default constructor.
31  */
32  template < typename GUM_SCALAR >
34  GUM_CONSTRUCTOR(UAIWriter);
35  }
36 
37  /*
38  * Destructor.
39  */
40  template < typename GUM_SCALAR >
42  GUM_DESTRUCTOR(UAIWriter);
43  }
44 
45  /*
46  * Writes a bayes net in the given ouput stream.
47  *
48  * @param output The output stream.
49  * @param bn The bayes net writen in the stream.
50  * @throws IOError Raised if an I/O error occurs.
51  */
52  template < typename GUM_SCALAR >
53  INLINE void UAIWriter< GUM_SCALAR >::write(std::ostream& output,
54  const IBayesNet< GUM_SCALAR >& bn) {
55  if (!output.good()) {
56  GUM_ERROR(IOError, "Stream states flags are not all unset.");
57  }
58 
59  output << __preambule(bn) << std::endl;
60 
61  for (auto node : bn.nodes())
62  output << __cptBloc(bn, node) << std::endl;
63 
64  output << std::endl;
65 
66  output.flush();
67 
68  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
69  }
70 
71  /*
72  * Writes a bayes net in the file referenced by filePath.
73  * If the file doesn't exists, it is created.
74  * If the file exists, it's content will be erased.
75  *
76  * @param filePath The path to the file used to write the bayes net.
77  * @param bn The bayes net writen in the file.
78  * @throw IOError Raised if an I/O error occurs.
79  */
80  template < typename GUM_SCALAR >
81  INLINE void UAIWriter< GUM_SCALAR >::write(const std::string& filePath,
82  const IBayesNet< GUM_SCALAR >& bn) {
83  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
84 
85  write(output, bn);
86 
87  output.close();
88 
89  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
90  }
91 
92  template < typename GUM_SCALAR >
93  INLINE std::string
94  UAIWriter< GUM_SCALAR >::__preambule(const IBayesNet< GUM_SCALAR >& bn) {
95  std::stringstream str;
96 
97  str << "BAYES" << std::endl;
98 
99  str << bn.size() << std::endl;
100 
101  for (auto node : bn.nodes())
102  str << bn.variable(node).domainSize() << " ";
103  str << std::endl;
104 
105  str << bn.size() << std::endl; // number of potentials
106 
107  for (auto node : bn.nodes()) {
108  const auto& p = bn.cpt(node);
109  str << p.nbrDim() << " ";
110  // P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
111  // in order to print it at last
112  NodeId first = 0;
113  bool isFirst = true;
114  for (auto k : p.variablesSequence()) {
115  if (isFirst) {
116  isFirst = false;
117  first = bn.idFromName(k->name());
118  } else {
119  str << bn.idFromName(k->name()) << " ";
120  }
121  }
122  str << first << " # " << bn.variable(node).name() << std::endl;
123  }
124  str << std::endl;
125 
126  return str.str();
127  }
128  template < typename GUM_SCALAR >
129  INLINE std::string
130  UAIWriter< GUM_SCALAR >::__cptBloc(const IBayesNet< GUM_SCALAR >& bn,
131  NodeId node) {
132  std::stringstream str;
133 
134  const auto& p = bn.cpt(node);
135  str << p.domainSize();
136  Instantiation I(p);
137  for (I.setFirst(); !I.end(); ++I) {
138  if (I.val(0) == 0) str << std::endl << " ";
139  str << p[I] << " ";
140  }
141  str << std::endl;
142 
143  return str.str();
144  }
145 
146 } /* namespace gum */
147 
148 #endif // DOXYGEN_SHOULD_SKIP_THIS
~UAIWriter() final
Destructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::string __cptBloc(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
UAIWriter()
Default constructor.
std::string __preambule(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55