21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 template <
typename GUM_SCALAR >
32 GUM_CONSTRUCTOR(BIFXMLBNWriter);
38 template <
typename GUM_SCALAR >
40 GUM_DESTRUCTOR(BIFXMLBNWriter);
50 template <
typename GUM_SCALAR >
53 const IBayesNet< GUM_SCALAR >& bn) {
55 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
58 output << __heading(bn) << std::endl;
60 output <<
"<!-- Variables -->" << std::endl;
62 for (
auto node : bn.nodes())
63 output << __variableBloc(bn.variable(node)) << std::endl;
65 output <<
"<!-- Probability distributions -->" << std::endl;
67 for (
auto node : bn.nodes())
68 output << __variableDefinition(node, bn);
72 output << __documentend();
76 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
88 template <
typename GUM_SCALAR >
91 const IBayesNet< GUM_SCALAR >& bn) {
92 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
98 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
104 template <
typename GUM_SCALAR >
107 std::stringstream str;
110 str <<
"<?xml version=\"1.0\" ?>" << std::endl;
136 str << std::endl <<
"<BIF VERSION=\"0.3\">" << std::endl;
139 str <<
"<NETWORK>" << std::endl;
140 str <<
"<NAME>" << bn.propertyWithDefault(
"name",
"unnamedBN") <<
"</NAME>" 142 str <<
"<PROPERTY>software aGrUM</PROPERTY>" << std::endl;
150 template <
typename GUM_SCALAR >
160 std::stringstream str;
163 str <<
"<VARIABLE TYPE=\"nature\">" << std::endl;
166 str <<
"\t<NAME>" << var.name() <<
"</NAME>" << std::endl;
167 str <<
"\t<PROPERTY>" << var.description() <<
"</PROPERTY>" << std::endl;
170 for (
Idx i = 0; i < var.domainSize(); i++)
171 str <<
"\t<OUTCOME>" << var.label(i) <<
"</OUTCOME>" << std::endl;
174 str <<
"</VARIABLE>" << std::endl;
182 template <
typename GUM_SCALAR >
184 const NodeId& varNodeId,
const IBayesNet< GUM_SCALAR >& bn) {
190 std::stringstream str;
193 str <<
"<DEFINITION>" << std::endl;
196 str <<
"\t<FOR>" << bn.variable(varNodeId).name() <<
"</FOR>" << std::endl;
202 const Potential< GUM_SCALAR >& cpt = bn.cpt(varNodeId);
205 for (
Idx i = 1; i < cpt.nbrDim(); i++)
206 str <<
"\t<GIVEN>" << cpt.variable(i).name() <<
"</GIVEN>" << std::endl;
209 inst << cpt.variable(0);
211 for (
Idx i = cpt.nbrDim() - 1; i > 0; i--)
212 inst << cpt.variable(i);
216 for (inst.setFirst(); !inst.end(); inst.inc()) {
217 if (inst.val(0) == 0)
218 str << std::endl <<
"\t\t";
225 str << std::endl <<
"\t</TABLE>" << std::endl;
228 str <<
"</DEFINITION>" << std::endl;
236 template <
typename GUM_SCALAR >
238 std::stringstream str;
240 str <<
"</NETWORK>" << std::endl;
241 str <<
"</BIF>" << std::endl;
248 #endif // DOXYGEN_SHOULD_SKIP_THIS BIFXMLBNWriter()
Default constructor.
std::string __heading(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
gum is the global namespace for all aGrUM entities
std::string __variableBloc(const DiscreteVariable &var)
Returns a bloc defining a variable in the BIF format.
std::string __documentend()
Returns the end of the BIF file.
std::string __variableDefinition(const NodeId &varNodeId, const IBayesNet< GUM_SCALAR > &bn)
Returns a bloc defining a variable's table (if she has) in the BIF format.
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
~BIFXMLBNWriter() final
Destructor.
Definition file for BIF XML exportation class.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)