aGrUM  0.13.2
DSLWriter_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 // to ease automatic parser
29 # include <agrum/BN/IBayesNet.h>
31 
32 namespace gum {
33  /* =========================================================================*/
34  /* === GUM_DSL_WRITTER === */
35  /* =========================================================================*/
36  // Default constructor.
37  template < typename GUM_SCALAR >
39  GUM_CONSTRUCTOR(DSLWriter);
40  }
41 
42  // Default destructor.
43  template < typename GUM_SCALAR >
45  GUM_DESTRUCTOR(DSLWriter);
46  }
47 
53  template < typename GUM_SCALAR >
54  void DSLWriter< GUM_SCALAR >::write(std::ostream& output,
55  const IBayesNet< GUM_SCALAR >& bn) {
56  if (!output.good()) {
57  GUM_ERROR(IOError, "Stream states flags are not all unset.");
58  }
59 
60  output << "net " << bn.propertyWithDefault("name", "unnamedBN") << std::endl
61  << "{" << std::endl;
62 
63  output << "// property softwar aGrUM " << GUM_VERSION << std::endl
64  << std::endl;
65 
66  for (auto node : bn.topologicalOrder()) {
67  output << __variableBloc(bn, bn.variable(node));
68  }
69 
70  output << "};";
71 
72  output.flush();
73 
74  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
75  }
76 
84  template < typename GUM_SCALAR >
85  void DSLWriter< GUM_SCALAR >::write(const std::string& filePath,
86  const IBayesNet< GUM_SCALAR >& bn) {
87  std::filebuf fb;
88  fb.open(filePath.c_str(), std::ios::out);
89  std::ostream output(&fb);
90 
91  write(output, bn);
92 
93  fb.close();
94  }
95 
99  template < typename GUM_SCALAR >
100  std::string
101  DSLWriter< GUM_SCALAR >::__variableBloc(const IBayesNet< GUM_SCALAR >& bn,
102  const DiscreteVariable& var) {
103  NodeId id;
104  std::ostringstream oss;
105 
106  id = bn.idFromName(var.name());
107 
108  oss << "\tnode " << var.name() << "\n\t{\n";
109 
110  oss << "\t\tTYPE = CPT;\n";
111 
112  oss << "\t\tHEADER =\n\t\t{\n";
113  oss << "\t\t\tID = " << var.name() << ";\n";
114  oss << "\t\t\tNAME = \"" << var.name() << "\";\n";
115  oss << "\t\t};\n";
116 
117  oss << "\t\tPARENTS = (";
118  const Sequence< const DiscreteVariable* >& tmp_vars =
119  bn.cpt(id).variablesSequence();
120 
121  for (Idx i = tmp_vars.size() - 1; i > 0; i--) {
122  if (i < tmp_vars.size() - 1) oss << ", ";
123 
124  oss << tmp_vars[i]->name();
125  }
126 
127  oss << ");\n";
128 
129  oss << "\t\tDEFINITION =\n\t\t{\n";
130 
132  oss << "\t\t\tNAMESTATES = (";
133 
134  for (Idx i = 0; i < var.domainSize(); i++) {
135  if (i != 0) oss << ", ";
136 
137  oss << var.label(i);
138  }
139 
140  oss << ");\n";
141 
143 
145  oss << "\t\t\tPROBABILITIES = (";
146  Idx i = 0;
147 
148  Instantiation iter(*bn.cpt(id).content());
149  for (iter.setFirst(); i < bn.cpt(id).domainSize(); ++iter, ++i) {
150  if (i != 0) oss << ", ";
151  oss << bn.cpt(id)[iter];
152  }
153 
154  oss << ");\n";
155 
157 
158  oss << "\t\t};\n";
159 
160  oss << "\t};\n\n";
161 
162  return oss.str();
163  }
164 
165 } /* namespace gum */
166 
167 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
~DSLWriter() final
Destructor.
Class representing Bayesian networks.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the DSL format.
std::string __variableBloc(const IBayesNet< GUM_SCALAR > &bn, const DiscreteVariable &var)
DSLWriter()
Default constructor.
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66