23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 36 template <
typename GUM_SCALAR >
38 GUM_CONSTRUCTOR(BIFWriter);
42 template <
typename GUM_SCALAR >
44 GUM_DESTRUCTOR(BIFWriter);
53 template <
typename GUM_SCALAR >
55 const IBayesNet< GUM_SCALAR >& bn) {
57 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
60 output << __header(bn) << std::endl;
62 for (
const auto node : bn.nodes()) {
63 output << __variableBloc(bn.variable(node)) << std::endl;
66 for (
const auto node : bn.nodes()) {
67 const Potential< GUM_SCALAR >& proba = bn.cpt(node);
68 output << __variableCPT(proba);
75 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
85 template <
typename GUM_SCALAR >
87 const IBayesNet< GUM_SCALAR >& bn) {
88 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
91 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
94 output << __header(bn) << std::endl;
96 for (
const auto node : bn.nodes()) {
97 output << __variableBloc(bn.variable(node)) << std::endl;
100 for (
const auto node : bn.nodes()) {
101 const Potential< GUM_SCALAR >& proba = bn.cpt(node);
102 output << __variableCPT(proba);
110 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
114 template <
typename GUM_SCALAR >
117 std::stringstream str;
118 std::string tab =
" ";
120 if (cpt.nbrDim() == 1) {
121 Instantiation inst(cpt);
122 str <<
"probability (" << cpt.variable(0).name() <<
") {" << std::endl;
123 str << tab <<
"default";
125 for (inst.setFirst(); !inst.end(); ++inst) {
126 str <<
" " << cpt[inst];
129 str <<
";" << std::endl <<
"}" << std::endl;
130 }
else if (cpt.domainSize() > 1) {
131 Instantiation inst(cpt);
132 Instantiation condVars;
133 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
134 str <<
"probability (" << (varsSeq[(
Idx)0])->name() <<
" | ";
136 for (
Idx i = 1; i < varsSeq.size() - 1; i++) {
137 str << varsSeq[i]->name() <<
", ";
138 condVars << *(varsSeq[i]);
141 str << varsSeq[varsSeq.size() - 1]->name() <<
") {" << std::endl;
143 condVars << *(varsSeq[varsSeq.size() - 1]);
145 for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
146 str << tab <<
"(" << __variablesLabels(varsSeq, inst) <<
")";
149 for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
150 str <<
" " << cpt[inst];
153 str <<
";" << std::endl;
155 inst.unsetOverflow();
158 str <<
"}" << std::endl;
165 template <
typename GUM_SCALAR >
168 std::stringstream str;
169 std::string tab =
" ";
170 str <<
"network \"" << bn.propertyWithDefault(
"name",
"unnamedBN") <<
"\" {" 172 str <<
"// written by aGrUM " << GUM_VERSION << std::endl;
173 str <<
"}" << std::endl;
178 template <
typename GUM_SCALAR >
181 std::stringstream str;
182 std::string tab =
" ";
183 str <<
"variable " << var.name() <<
" {" << std::endl;
184 str << tab <<
"type discrete[" << var.domainSize() <<
"] {";
186 for (
Idx i = 0; i < var.domainSize() - 1; i++) {
187 str << var.label(i) <<
", ";
190 str << var.label(var.domainSize() - 1) <<
"};" << std::endl;
192 str <<
"}" << std::endl;
197 template <
typename GUM_SCALAR >
199 const Sequence< const DiscreteVariable* >& varsSeq,
200 const Instantiation& inst) {
201 std::stringstream str;
202 const DiscreteVariable* varPtr =
nullptr;
204 for (
Idx i = 1; i < varsSeq.size() - 1; i++) {
206 str << varPtr->label(inst.val(*varPtr)) <<
", ";
209 varPtr = varsSeq[varsSeq.size() - 1];
211 str << varPtr->label(inst.val(*varPtr));
217 #endif // DOXYGEN_SHOULD_SKIP_THIS ~BIFWriter() final
Destructor.
std::string __variableBloc(const DiscreteVariable &var)
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BIF format.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __variableCPT(const Potential< GUM_SCALAR > &cpt)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __variablesLabels(const Sequence< const DiscreteVariable * > &varsSeq, const Instantiation &inst)
std::string __header(const IBayesNet< GUM_SCALAR > &bn)
Size Idx
Type for indexes.
BIFWriter()
Default constructor.
#define GUM_ERROR(type, msg)