21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 31 template <
typename GUM_SCALAR >
33 GUM_CONSTRUCTOR(NetWriter);
37 template <
typename GUM_SCALAR >
39 GUM_DESTRUCTOR(NetWriter);
48 template <
typename GUM_SCALAR >
50 const IBayesNet< GUM_SCALAR >& bn) {
52 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
54 output << __header(bn) << std::endl;
56 for (
auto node : bn.nodes())
57 output << __variableBloc(bn.variable(node)) << std::endl;
59 for (
auto node : bn.nodes())
60 output << __variableCPT(bn.cpt(node));
66 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
76 template <
typename GUM_SCALAR >
78 const IBayesNet< GUM_SCALAR >& bn) {
79 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
82 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
85 output << __header(bn) << std::endl;
87 for (
auto node : bn.nodes())
88 output << __variableBloc(bn.variable(node)) << std::endl;
90 for (
auto node : bn.nodes())
91 output << __variableCPT(bn.cpt(node));
98 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
102 template <
typename GUM_SCALAR >
105 std::stringstream str;
106 std::string tab =
" ";
108 Instantiation inst(cpt);
109 if (cpt.nbrDim() == 1) {
110 str <<
"potential (" << cpt.variable(0).name() <<
") {" << std::endl
111 << tab <<
"data = ( ";
113 for (inst.setFirst(); !inst.end(); ++inst) {
114 str <<
" " << cpt[inst];
119 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
122 for (
Idx i = 1; i < varsSeq.size(); i++)
123 conds.add(*varsSeq[i]);
125 str <<
"potential ( " << (varsSeq[(
Idx)0])->name() <<
" | ";
126 for (
Idx i = 1; i < varsSeq.size(); i++)
127 str << varsSeq[i]->name() <<
" ";
128 str <<
") {" << std::endl << tab <<
"data = \n";
134 for (
Idx i = 0; i < conds.nbrDim(); i++) {
135 if (conds.val(i) != 0)
break;
140 for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
141 str << tab << cpt[inst];
143 comment = tab +
"% ";
144 for (
Idx i = 0; i < conds.nbrDim(); i++) {
145 comment += conds.variable(i).name() +
"=" 146 + conds.variable(i).label(conds.val(i)) + tab;
151 for (
Idx i = 0; i < inst.nbrDim(); i++) {
154 str <<
";" << comment;
157 for (
Idx i = 0; i < conds.nbrDim(); i++) {
159 if (conds.val(i) != 0)
break;
161 str << comment <<
"\n";
165 str <<
"\n}\n" << std::endl;
170 template <
typename GUM_SCALAR >
173 std::stringstream str;
174 std::string tab =
" ";
175 str << std::endl <<
"net {" << std::endl;
176 str <<
" name = " << bn.propertyWithDefault(
"name",
"unnamedBN") <<
";" 178 str <<
" software = \"aGrUM " << GUM_VERSION <<
"\";" << std::endl;
179 str <<
" node_size = (50 50);" << std::endl;
180 str <<
"}" << std::endl;
185 template <
typename GUM_SCALAR >
188 std::stringstream str;
189 std::string tab =
" ";
190 str <<
"node " << var.name() <<
" {" << std::endl;
191 str << tab <<
"states = (";
193 for (
Idx i = 0; i < var.domainSize(); i++) {
194 str << var.label(i) <<
" ";
197 str <<
");" << std::endl;
198 str << tab <<
"label = \"" << var.name() <<
"\";" << std::endl;
199 str << tab <<
"ID = \"" << var.name() <<
"\";" << std::endl;
201 str <<
"}" << std::endl;
207 #endif // DOXYGEN_SHOULD_SKIP_THIS NetWriter()
Default constructor.
gum is the global namespace for all aGrUM entities
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.
Definition of classe for BN file output manipulation.
std::string __variableBloc(const DiscreteVariable &var)
#define GUM_ERROR(type, msg)