43#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template <
typename GUM_SCALAR >
62 template <
typename GUM_SCALAR >
73 template <
typename GUM_SCALAR >
76 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
78 output << _header_(bn) << std::endl;
80 for (
const auto node: bn.nodes()) {
81 output << _variableBloc_(bn.variable(node)) << std::endl;
84 for (
const auto node: bn.nodes()) {
86 output << _variableCPT_(proba);
93 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
103 template <
typename GUM_SCALAR >
106 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
108 _doWrite(output, bn);
111 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
115 template <
typename GUM_SCALAR >
117 std::stringstream str;
118 std::string tab =
" ";
120 if (cpt.nbrDim() == 1) {
122 str <<
"probability (" << this->_onlyValidCharsInName(cpt.variable(0).name()) <<
") {"
124 str << tab <<
"table";
126 for (inst.setFirst(); !inst.end(); ++inst) {
127 str << std::format(
" {}", cpt[inst]);
130 str <<
";" << std::endl <<
"}" << std::endl;
131 }
else if (cpt.domainSize() > 1) {
135 str <<
"probability (" << this->_onlyValidCharsInName((varsSeq[(
Idx)0])->name()) <<
" | ";
137 for (
Idx i = 1; i < varsSeq.size(); i++) {
138 if (i > 1) str <<
", ";
139 str << this->_onlyValidCharsInName(varsSeq[i]->name());
140 condVars << *(varsSeq[i]);
142 str <<
") {" << std::endl;
144 for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
145 str << tab <<
"(" << _variablesLabels_(varsSeq, inst) <<
")";
148 for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
149 str << std::format(
" {}", cpt[inst]);
152 str <<
";" << std::endl;
154 inst.unsetOverflow();
157 str <<
"}" << std::endl;
164 template <
typename GUM_SCALAR >
166 std::stringstream str;
167 std::string tab =
" ";
168 str <<
"network \"" << bn.propertyWithDefault(
"name",
"unnamedBN") <<
"\" {" << std::endl;
169 str <<
"// written by aGrUM " << GUM_VERSION << std::endl;
170 str <<
"}" << std::endl;
175 template <
typename GUM_SCALAR >
177 std::stringstream str;
178 std::string tab =
" ";
179 str <<
"variable " << this->_onlyValidCharsInName(var.name()) <<
" {" << std::endl;
180 str << tab <<
"type discrete[" << var.domainSize() <<
"] {";
182 for (
Idx i = 0; i < var.domainSize(); i++) {
183 if (i > 0) str <<
", ";
184 str << this->_onlyValidCharsInName(var.label(i));
187 str <<
"};" << std::endl;
189 str <<
"}" << std::endl;
194 template <
typename GUM_SCALAR >
198 std::stringstream str;
201 for (
Idx i = 1; i < varsSeq.size(); i++) {
202 if (i > 1) str <<
", ";
204 str << this->_onlyValidCharsInName(varPtr->label(inst.val(*varPtr)));
209 template <
typename GUM_SCALAR >
211 this->_validCharInNamesCheck(bn);
Definition of class for BIF file output manipulation.
Writes a IBayesNet in the BIF format.
std::string _variablesLabels_(const Sequence< const DiscreteVariable * > &varsSeq, const Instantiation &inst)
BIFWriter()
Default constructor.
std::string _variableCPT_(const Tensor< GUM_SCALAR > &cpt)
std::string _variableBloc_(const DiscreteVariable &var)
void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn) final
Check whether the BN is syntactically correct for BIF format.
~BIFWriter() override
Destructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BIF format.
std::string _header_(const IBayesNet< GUM_SCALAR > &bn)
Base class for discrete random variable.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
The generic class for storing (ordered) sequences of objects.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
#define GUM_ERROR(type, msg)
Size Idx
Type for indexes.
gum is the global namespace for all aGrUM entities