23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 32 template <
typename GUM_SCALAR >
34 GUM_CONSTRUCTOR(BIFXMLBNWriter);
40 template <
typename GUM_SCALAR >
42 GUM_DESTRUCTOR(BIFXMLBNWriter);
52 template <
typename GUM_SCALAR >
55 const IBayesNet< GUM_SCALAR >& bn) {
57 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
60 output << __heading(bn) << std::endl;
62 output <<
"<!-- Variables -->" << std::endl;
64 for (
auto node : bn.nodes())
65 output << __variableBloc(bn.variable(node)) << std::endl;
67 output <<
"<!-- Probability distributions -->" << std::endl;
69 for (
auto node : bn.nodes())
70 output << __variableDefinition(node, bn);
74 output << __documentend();
78 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
90 template <
typename GUM_SCALAR >
93 const IBayesNet< GUM_SCALAR >& bn) {
94 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
100 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
106 template <
typename GUM_SCALAR >
109 std::stringstream str;
112 str <<
"<?xml version=\"1.0\" ?>" << std::endl;
138 str << std::endl <<
"<BIF VERSION=\"0.3\">" << std::endl;
141 str <<
"<NETWORK>" << std::endl;
142 str <<
"<NAME>" << bn.propertyWithDefault(
"name",
"unnamedBN") <<
"</NAME>" 144 str <<
"<PROPERTY>software aGrUM</PROPERTY>" << std::endl;
152 template <
typename GUM_SCALAR >
162 std::stringstream str;
165 str <<
"<VARIABLE TYPE=\"nature\">" << std::endl;
168 str <<
"\t<NAME>" << var.name() <<
"</NAME>" << std::endl;
169 str <<
"\t<PROPERTY>" << var.description() <<
"</PROPERTY>" << std::endl;
172 for (
Idx i = 0; i < var.domainSize(); i++)
173 str <<
"\t<OUTCOME>" << var.label(i) <<
"</OUTCOME>" << std::endl;
176 str <<
"</VARIABLE>" << std::endl;
184 template <
typename GUM_SCALAR >
186 const NodeId& varNodeId,
const IBayesNet< GUM_SCALAR >& bn) {
192 std::stringstream str;
195 str <<
"<DEFINITION>" << std::endl;
198 str <<
"\t<FOR>" << bn.variable(varNodeId).name() <<
"</FOR>" << std::endl;
204 const Potential< GUM_SCALAR >& cpt = bn.cpt(varNodeId);
207 for (
Idx i = 1; i < cpt.nbrDim(); i++)
208 str <<
"\t<GIVEN>" << cpt.variable(i).name() <<
"</GIVEN>" << std::endl;
211 inst << cpt.variable(0);
213 for (
Idx i = cpt.nbrDim() - 1; i > 0; i--)
214 inst << cpt.variable(i);
218 for (inst.setFirst(); !inst.end(); inst.inc()) {
219 if (inst.val(0) == 0)
220 str << std::endl <<
"\t\t";
227 str << std::endl <<
"\t</TABLE>" << std::endl;
230 str <<
"</DEFINITION>" << std::endl;
238 template <
typename GUM_SCALAR >
240 std::stringstream str;
242 str <<
"</NETWORK>" << std::endl;
243 str <<
"</BIF>" << std::endl;
250 #endif // DOXYGEN_SHOULD_SKIP_THIS BIFXMLBNWriter()
Default constructor.
std::string __heading(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)