45#ifndef DOXYGEN_SHOULD_SKIP_THIS
57 template < GUM_Numeric GUM_SCALAR >
63 template < GUM_Numeric GUM_SCALAR >
74 template < GUM_Numeric GUM_SCALAR >
76 if (!output.good())
GUM_ERROR(IOError,
"Input/Output error : stream not writable.")
78 output << _header_(bn) << std::endl;
80 for (auto node: bn.nodes())
81 output << _variableBloc_(bn.variable(node)) << std::endl;
83 for (auto node: bn.nodes())
84 output << _variableCPT_(bn.cpt(node));
90 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
100 template < GUM_Numeric GUM_SCALAR >
103 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
105 _doWrite(output, bn);
108 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
112 template < GUM_Numeric GUM_SCALAR >
114 std::stringstream str;
115 std::string tab =
" ";
118 if (cpt.nbrDim() == 1) {
119 str << std::format(
"potential ({}) {{\n{}data = ( ", cpt.variable(0).name(), tab);
121 for (inst.setFirst(); !inst.end(); ++inst) {
122 str << std::format(
" {}", cpt[inst]);
131 for (
Idx i = 1; i < varsSeq.size(); i++)
132 conds.
add(*varsSeq[varsSeq.size() - i]);
134 str << std::format(
"potential ( {} | ", (varsSeq[
static_cast< Idx >(0)])->name());
135 for (
Idx i = 1; i < varsSeq.size(); i++)
136 str << varsSeq[i]->name() <<
" ";
137 str <<
") {" << std::endl << tab <<
"data = \n";
143 for (
Idx i = 0; i < conds.nbrDim(); i++) {
144 if (conds.val(i) != 0)
break;
149 for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
150 str << std::format(
" {}{}", tab, cpt[inst]);
152 comment = tab +
"% ";
153 for (
Idx i = 0; i < conds.nbrDim(); i++) {
154 comment += conds.variable(i).name() +
"=" + conds.variable(i).label(conds.val(i)) + tab;
159 for (
Idx i = 0; i < inst.nbrDim(); i++) {
162 str <<
";" << comment;
165 for (
Idx i = 0; i < conds.nbrDim(); i++) {
167 if (conds.val(i) != 0)
break;
169 str << comment <<
"\n";
173 str <<
"\n}\n" << std::endl;
178 template < GUM_Numeric GUM_SCALAR >
180 std::stringstream str;
181 std::string tab =
" ";
182 str << std::endl <<
"net {" << std::endl;
183 str << std::format(
" name = {};\n", bn.propertyWithDefault(
"name",
"unnamedBN"));
184 str << std::format(
" software = \"aGrUM {}\";\n", GUM_VERSION);
185 str <<
" node_size = (50 50);" << std::endl;
186 str <<
"}" << std::endl;
191 template < GUM_Numeric GUM_SCALAR >
193 std::stringstream str;
194 std::string tab =
" ";
195 str << std::format(
"node {} {{\n", var.name());
196 str << tab <<
"states = (";
198 for (
Idx i = 0; i < var.domainSize(); i++) {
199 str << var.label(i) <<
" ";
202 str <<
");" << std::endl;
203 str << std::format(
"{}label = \"{}\";\n", tab, var.name());
204 str << std::format(
"{}ID = \"{}\";\n", tab, var.name());
206 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.