45#ifndef DOXYGEN_SHOULD_SKIP_THIS
58 template < GUM_Numeric GUM_SCALAR >
64 template < GUM_Numeric GUM_SCALAR >
75 template < GUM_Numeric GUM_SCALAR >
77 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
79 output << _header_(bn) << std::endl;
81 for (
const auto node: bn.nodes()) {
82 output << _variableBloc_(bn.variable(node)) << std::endl;
85 for (
const auto node: bn.nodes()) {
87 output << _variableCPT_(proba);
94 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
104 template < GUM_Numeric GUM_SCALAR >
107 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
109 _doWrite(output, bn);
112 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
116 template < GUM_Numeric GUM_SCALAR >
118 std::stringstream str;
119 std::string tab =
" ";
121 if (cpt.nbrDim() == 1) {
123 str << std::format(
"probability ({}) {{\n",
124 this->_onlyValidCharsInName(cpt.variable(0).name()));
125 str << tab <<
"table";
127 for (inst.setFirst(); !inst.end(); ++inst) {
128 str << std::format(
" {}", cpt[inst]);
131 str <<
";" << std::endl <<
"}" << std::endl;
132 }
else if (cpt.domainSize() > 1) {
136 str << std::format(
"probability ({} | ",
137 this->_onlyValidCharsInName((varsSeq[(
Idx)0])->name()));
139 for (
Idx i = 1; i < varsSeq.size(); i++) {
140 if (i > 1) str <<
", ";
141 str << this->_onlyValidCharsInName(varsSeq[i]->name());
142 condVars << *(varsSeq[i]);
144 str <<
") {" << std::endl;
146 for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
147 str << std::format(
"{}({})", tab, _variablesLabels_(varsSeq, inst));
150 for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
151 str << std::format(
" {}", cpt[inst]);
154 str <<
";" << std::endl;
156 inst.unsetOverflow();
159 str <<
"}" << std::endl;
166 template < GUM_Numeric GUM_SCALAR >
168 std::stringstream str;
169 std::string tab =
" ";
170 str << std::format(
"network \"{}\" {{\n", bn.propertyWithDefault(
"name",
"unnamedBN"));
171 str << std::format(
"// written by aGrUM {}\n", GUM_VERSION);
172 str <<
"}" << std::endl;
177 template < GUM_Numeric GUM_SCALAR >
179 std::stringstream str;
180 std::string tab =
" ";
181 str << std::format(
"variable {} {{\n", this->_onlyValidCharsInName(var.name()));
182 str << std::format(
"{}type discrete[{}] {{", tab, var.domainSize());
184 for (
Idx i = 0; i < var.domainSize(); i++) {
185 if (i > 0) str <<
", ";
186 str << this->_onlyValidCharsInName(var.label(i));
189 str <<
"};" << std::endl;
191 str <<
"}" << std::endl;
196 template < GUM_Numeric GUM_SCALAR >
200 std::stringstream str;
203 for (
Idx i = 1; i < varsSeq.size(); i++) {
204 if (i > 1) str <<
", ";
206 str << this->_onlyValidCharsInName(varPtr->label(inst.val(*varPtr)));
211 template < GUM_Numeric GUM_SCALAR >
213 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