21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 34 template <
typename GUM_SCALAR >
36 GUM_CONSTRUCTOR(BIFWriter);
40 template <
typename GUM_SCALAR >
42 GUM_DESTRUCTOR(BIFWriter);
51 template <
typename GUM_SCALAR >
53 const IBayesNet< GUM_SCALAR >& bn) {
55 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
58 output << __header(bn) << std::endl;
60 for (
const auto node : bn.nodes()) {
61 output << __variableBloc(bn.variable(node)) << std::endl;
64 for (
const auto node : bn.nodes()) {
65 const Potential< GUM_SCALAR >& proba = bn.cpt(node);
66 output << __variableCPT(proba);
73 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
83 template <
typename GUM_SCALAR >
85 const IBayesNet< GUM_SCALAR >& bn) {
86 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
89 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
92 output << __header(bn) << std::endl;
94 for (
const auto node : bn.nodes()) {
95 output << __variableBloc(bn.variable(node)) << std::endl;
98 for (
const auto node : bn.nodes()) {
99 const Potential< GUM_SCALAR >& proba = bn.cpt(node);
100 output << __variableCPT(proba);
108 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
112 template <
typename GUM_SCALAR >
115 std::stringstream str;
116 std::string tab =
" ";
118 if (cpt.nbrDim() == 1) {
119 Instantiation inst(cpt);
120 str <<
"probability (" << cpt.variable(0).name() <<
") {" << std::endl;
121 str << tab <<
"default";
123 for (inst.setFirst(); !inst.end(); ++inst) {
124 str <<
" " << cpt[inst];
127 str <<
";" << std::endl <<
"}" << std::endl;
128 }
else if (cpt.domainSize() > 1) {
129 Instantiation inst(cpt);
130 Instantiation condVars;
131 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
132 str <<
"probability (" << (varsSeq[(
Idx)0])->name() <<
" | ";
134 for (
Idx i = 1; i < varsSeq.size() - 1; i++) {
135 str << varsSeq[i]->name() <<
", ";
136 condVars << *(varsSeq[i]);
139 str << varsSeq[varsSeq.size() - 1]->name() <<
") {" << std::endl;
141 condVars << *(varsSeq[varsSeq.size() - 1]);
143 for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
144 str << tab <<
"(" << __variablesLabels(varsSeq, inst) <<
")";
147 for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
148 str <<
" " << cpt[inst];
151 str <<
";" << std::endl;
153 inst.unsetOverflow();
156 str <<
"}" << std::endl;
163 template <
typename GUM_SCALAR >
166 std::stringstream str;
167 std::string tab =
" ";
168 str <<
"network \"" << bn.propertyWithDefault(
"name",
"unnamedBN") <<
"\" {" 170 str <<
"// written by aGrUM " << GUM_VERSION << std::endl;
171 str <<
"}" << std::endl;
176 template <
typename GUM_SCALAR >
179 std::stringstream str;
180 std::string tab =
" ";
181 str <<
"variable " << var.name() <<
" {" << std::endl;
182 str << tab <<
"type discrete[" << var.domainSize() <<
"] {";
184 for (
Idx i = 0; i < var.domainSize() - 1; i++) {
185 str << var.label(i) <<
", ";
188 str << var.label(var.domainSize() - 1) <<
"};" << std::endl;
190 str <<
"}" << std::endl;
195 template <
typename GUM_SCALAR >
197 const Sequence< const DiscreteVariable* >& varsSeq,
198 const Instantiation& inst) {
199 std::stringstream str;
200 const DiscreteVariable* varPtr =
nullptr;
202 for (
Idx i = 1; i < varsSeq.size() - 1; i++) {
204 str << varPtr->label(inst.val(*varPtr)) <<
", ";
207 varPtr = varsSeq[varsSeq.size() - 1];
209 str << varPtr->label(inst.val(*varPtr));
215 #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.
gum is the global namespace for all aGrUM entities
std::string __variableCPT(const Potential< GUM_SCALAR > &cpt)
Definition of classe for BIF file output manipulation.
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)