aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DSLWriter_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 /** @file
23  * @brief Templates implementation of bns/io/gumBNWriter.h classes.
24  *
25  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 // to ease automatic parser
30 # include <agrum/BN/IBayesNet.h>
31 # include <agrum/BN/io/DSL/DSLWriter.h>
32 
33 namespace gum {
34  /* =========================================================================*/
35  /* === GUM_DSL_WRITER === */
36  /* =========================================================================*/
37  // Default constructor.
38  template < typename GUM_SCALAR >
39  INLINE DSLWriter< GUM_SCALAR >::DSLWriter() {
41  }
42 
43  // Default destructor.
44  template < typename GUM_SCALAR >
47  }
48 
49  /** Writes a Bayesian network in the output stream using the DSL format.
50  * @param ouput The output stream.
51  * @param bn The Bayesian network writen in output.
52  * @throws Raised if an I/O error occurs.
53  */
54  template < typename GUM_SCALAR >
56  const IBayesNet< GUM_SCALAR >& bn) {
57  if (!output.good()) {
58  GUM_ERROR(IOError, "Stream states flags are not all unset.");
59  }
60 
61  output << "net " << bn.propertyWithDefault("name", "unnamedBN") << std::endl
62  << "{" << std::endl;
63 
64  output << "// property softwar aGrUM " << GUM_VERSION << std::endl
65  << std::endl;
66 
67  for (auto node: bn.topologicalOrder()) {
69  }
70 
71  output << "};";
72 
73  output.flush();
74 
75  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
76  }
77 
78  /** Writes a Bayesian network in the referenced file using the DSL format.
79  * If the file doesn't exists, it is created.
80  * If the file exists, it's content will be erased.
81  * @param filePath The path to the file used to write the Bayesian network.
82  * @param bn The Bayesian network writed in the file.
83  * @throws Raised if an I/O error occurs.
84  */
85  template < typename GUM_SCALAR >
86  void DSLWriter< GUM_SCALAR >::write(const std::string& filePath,
87  const IBayesNet< GUM_SCALAR >& bn) {
88  std::filebuf fb;
89  fb.open(filePath.c_str(), std::ios::out);
90  std::ostream output(&fb);
91 
92  write(output, bn);
93 
94  fb.close();
95  }
96 
97  /**
98  * Returns a bloc defining a variable in the DSL format.
99  */
100  template < typename GUM_SCALAR >
101  std::string
103  const DiscreteVariable& var) {
104  NodeId id;
106 
107  id = bn.idFromName(var.name());
108 
109  oss << "\tnode " << var.name() << "\n\t{\n";
110 
111  oss << "\t\tTYPE = CPT;\n";
112 
113  oss << "\t\tHEADER =\n\t\t{\n";
114  oss << "\t\t\tID = " << var.name() << ";\n";
115  oss << "\t\t\tNAME = \"" << var.name() << "\";\n";
116  oss << "\t\t};\n";
117 
118  oss << "\t\tPARENTS = (";
119  const Sequence< const DiscreteVariable* >& tmp_vars
120  = bn.cpt(id).variablesSequence();
121 
122  for (Idx i = tmp_vars.size() - 1; i > 0; i--) {
123  if (i < tmp_vars.size() - 1) oss << ", ";
124 
125  oss << tmp_vars[i]->name();
126  }
127 
128  oss << ");\n";
129 
130  oss << "\t\tDEFINITION =\n\t\t{\n";
131 
132  ////////////////////////////
133  oss << "\t\t\tNAMESTATES = (";
134 
135  for (Idx i = 0; i < var.domainSize(); i++) {
136  if (i != 0) oss << ", ";
137 
138  oss << var.label(i);
139  }
140 
141  oss << ");\n";
142 
143  ////////////////////////////
144 
145  ////////////////////////////
146  oss << "\t\t\tPROBABILITIES = (";
147  Idx i = 0;
148 
150  for (iter.setFirst(); i < bn.cpt(id).domainSize(); ++iter, ++i) {
151  if (i != 0) oss << ", ";
152  oss << bn.cpt(id)[iter];
153  }
154 
155  oss << ");\n";
156 
157  ///////////////////////////
158 
159  oss << "\t\t};\n";
160 
161  oss << "\t};\n\n";
162 
163  return oss.str();
164  }
165 
166 } /* namespace gum */
167 
168 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669