43#ifndef DOXYGEN_SHOULD_SKIP_THIS
55 template <
typename GUM_SCALAR >
61 template <
typename GUM_SCALAR >
72 template <
typename GUM_SCALAR >
75 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : stream not writable.")
77 output << _header_(bn) << std::endl;
79 for (auto node: bn.nodes())
80 output << _variableBloc_(bn.variable(node)) << std::endl;
82 for (auto node: bn.nodes())
83 output << _variableCPT_(bn.cpt(node));
89 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
99 template <
typename GUM_SCALAR >
102 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
104 _doWrite(output, bn);
107 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
111 template <
typename GUM_SCALAR >
113 std::stringstream str;
114 std::string tab =
" ";
117 if (cpt.nbrDim() == 1) {
118 str <<
"potential (" << cpt.variable(0).name() <<
") {" << std::endl << tab <<
"data = ( ";
120 for (inst.setFirst(); !inst.end(); ++inst) {
121 str << std::format(
" {}", cpt[inst]);
130 for (
Idx i = 1; i < varsSeq.size(); i++)
131 conds.
add(*varsSeq[varsSeq.size() - i]);
133 str <<
"potential ( " << (varsSeq[
static_cast< Idx >(0)])->name() <<
" | ";
134 for (
Idx i = 1; i < varsSeq.size(); i++)
135 str << varsSeq[i]->name() <<
" ";
136 str <<
") {" << std::endl << tab <<
"data = \n";
142 for (
Idx i = 0; i < conds.nbrDim(); i++) {
143 if (conds.val(i) != 0)
break;
148 for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
149 str << std::format(
" {}{}", tab, cpt[inst]);
151 comment = tab +
"% ";
152 for (
Idx i = 0; i < conds.nbrDim(); i++) {
153 comment += conds.variable(i).name() +
"=" + conds.variable(i).label(conds.val(i)) + tab;
158 for (
Idx i = 0; i < inst.nbrDim(); i++) {
161 str <<
";" << comment;
164 for (
Idx i = 0; i < conds.nbrDim(); i++) {
166 if (conds.val(i) != 0)
break;
168 str << comment <<
"\n";
172 str <<
"\n}\n" << std::endl;
177 template <
typename GUM_SCALAR >
179 std::stringstream str;
180 std::string tab =
" ";
181 str << std::endl <<
"net {" << std::endl;
182 str <<
" name = " << bn.propertyWithDefault(
"name",
"unnamedBN") <<
";" << std::endl;
183 str <<
" software = \"aGrUM " << GUM_VERSION <<
"\";" << std::endl;
184 str <<
" node_size = (50 50);" << std::endl;
185 str <<
"}" << std::endl;
190 template <
typename GUM_SCALAR >
192 std::stringstream str;
193 std::string tab =
" ";
194 str <<
"node " << var.name() <<
" {" << std::endl;
195 str << tab <<
"states = (";
197 for (
Idx i = 0; i < var.domainSize(); i++) {
198 str << var.label(i) <<
" ";
201 str <<
");" << std::endl;
202 str << tab <<
"label = \"" << var.name() <<
"\";" << std::endl;
203 str << tab <<
"ID = \"" << var.name() <<
"\";" << std::endl;
205 str <<
"}" << std::endl;
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.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
Writes a IBayesNet in the BN format.
static std::string _header_(const IBayesNet< GUM_SCALAR > &bn)
NetWriter()
Default constructor.
std::string _variableCPT_(const Tensor< GUM_SCALAR > &cpt)
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
~NetWriter() override
Destructor.
std::string _variableBloc_(const DiscreteVariable &var)
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
Definition of class for BN file output manipulation.