23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 template <
typename GUM_SCALAR >
35 GUM_CONSTRUCTOR(NetWriter);
39 template <
typename GUM_SCALAR >
41 GUM_DESTRUCTOR(NetWriter);
50 template <
typename GUM_SCALAR >
52 const IBayesNet< GUM_SCALAR >& bn) {
54 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
56 output << __header(bn) << std::endl;
58 for (
auto node : bn.nodes())
59 output << __variableBloc(bn.variable(node)) << std::endl;
61 for (
auto node : bn.nodes())
62 output << __variableCPT(bn.cpt(node));
68 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
78 template <
typename GUM_SCALAR >
80 const IBayesNet< GUM_SCALAR >& bn) {
81 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
84 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
87 output << __header(bn) << std::endl;
89 for (
auto node : bn.nodes())
90 output << __variableBloc(bn.variable(node)) << std::endl;
92 for (
auto node : bn.nodes())
93 output << __variableCPT(bn.cpt(node));
100 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
104 template <
typename GUM_SCALAR >
107 std::stringstream str;
108 std::string tab =
" ";
110 Instantiation inst(cpt);
111 if (cpt.nbrDim() == 1) {
112 str <<
"potential (" << cpt.variable(0).name() <<
") {" << std::endl
113 << tab <<
"data = ( ";
115 for (inst.setFirst(); !inst.end(); ++inst) {
116 str <<
" " << cpt[inst];
121 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
124 for (
Idx i = 1; i < varsSeq.size(); i++)
125 conds.add(*varsSeq[i]);
127 str <<
"potential ( " << (varsSeq[(
Idx)0])->name() <<
" | ";
128 for (
Idx i = 1; i < varsSeq.size(); i++)
129 str << varsSeq[i]->name() <<
" ";
130 str <<
") {" << std::endl << tab <<
"data = \n";
136 for (
Idx i = 0; i < conds.nbrDim(); i++) {
137 if (conds.val(i) != 0)
break;
142 for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
143 str << tab << cpt[inst];
145 comment = tab +
"% ";
146 for (
Idx i = 0; i < conds.nbrDim(); i++) {
147 comment += conds.variable(i).name() +
"=" 148 + conds.variable(i).label(conds.val(i)) + tab;
153 for (
Idx i = 0; i < inst.nbrDim(); i++) {
156 str <<
";" << comment;
159 for (
Idx i = 0; i < conds.nbrDim(); i++) {
161 if (conds.val(i) != 0)
break;
163 str << comment <<
"\n";
167 str <<
"\n}\n" << std::endl;
172 template <
typename GUM_SCALAR >
175 std::stringstream str;
176 std::string tab =
" ";
177 str << std::endl <<
"net {" << std::endl;
178 str <<
" name = " << bn.propertyWithDefault(
"name",
"unnamedBN") <<
";" 180 str <<
" software = \"aGrUM " << GUM_VERSION <<
"\";" << std::endl;
181 str <<
" node_size = (50 50);" << std::endl;
182 str <<
"}" << std::endl;
187 template <
typename GUM_SCALAR >
190 std::stringstream str;
191 std::string tab =
" ";
192 str <<
"node " << var.name() <<
" {" << std::endl;
193 str << tab <<
"states = (";
195 for (
Idx i = 0; i < var.domainSize(); i++) {
196 str << var.label(i) <<
" ";
199 str <<
");" << std::endl;
200 str << tab <<
"label = \"" << var.name() <<
"\";" << std::endl;
201 str << tab <<
"ID = \"" << var.name() <<
"\";" << std::endl;
203 str <<
"}" << std::endl;
209 #endif // DOXYGEN_SHOULD_SKIP_THIS NetWriter()
Default constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __header(const IBayesNet< GUM_SCALAR > &bn)
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
~NetWriter() final
Destructor.
std::string __variableCPT(const Potential< GUM_SCALAR > &cpt)
Size Idx
Type for indexes.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __variableBloc(const DiscreteVariable &var)
#define GUM_ERROR(type, msg)