47#ifndef DOXYGEN_SHOULD_SKIP_THIS
55 template < GUM_Numeric GUM_SCALAR >
63 template < GUM_Numeric GUM_SCALAR >
75 template < GUM_Numeric GUM_SCALAR >
78 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
80 output << _heading_() << std::endl;
81 output <<
"<!-- Variables -->" << std::endl;
83 for (
const auto node: infdiag.nodes()) {
86 if (infdiag.isChanceNode(node)) nodeType = 2;
87 else if (infdiag.isUtilityNode(node)) nodeType = 3;
89 output << _variableBloc_(infdiag.variable(node), nodeType) << std::endl;
92 output <<
"<!-- Probability distributions -->" << std::endl;
94 for (
const auto node: infdiag.nodes())
95 output << _variableDefinition_(node, infdiag);
98 output << _documentend_();
101 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
113 template < GUM_Numeric GUM_SCALAR >
116 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
118 write(output, infdiag);
122 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
128 template < GUM_Numeric GUM_SCALAR >
130 std::stringstream str;
133 str <<
"<?xml version=\"1.0\" ?>" << std::endl;
139 str << std::endl <<
"<BIF VERSION=\"0.3\">" << std::endl;
142 str <<
"<NETWORK>" << std::endl;
150 template < GUM_Numeric GUM_SCALAR >
163 std::stringstream str;
166 str <<
"<VARIABLE TYPE=\"";
169 case 1 : str <<
"decision";
break;
171 case 2 : str <<
"nature";
break;
173 case 3 : str <<
"utility";
break;
178 str <<
"\">" << std::endl;
181 str << std::format(
"\t<NAME>{}</NAME>\n", var.name());
182 str << std::format(
"\t<PROPERTY>description = {}</PROPERTY>\n", var.description());
183 str << std::format(
"\t<PROPERTY>fast = {}</PROPERTY>\n", var.toFast());
186 str <<
"<!-- OUTCOME are not used in pyAgrum BIFXML (see fast property) but are kept for "
189 for (
Idx i = 0; i < var.domainSize(); i++)
190 str << std::format(
"\t<OUTCOME>{}</OUTCOME>\n", var.label(i));
193 str <<
"</VARIABLE>" << std::endl;
201 template < GUM_Numeric GUM_SCALAR >
210 std::stringstream str;
212 if (!((infdiag.isDecisionNode(varNodeId)) && (infdiag.parents(varNodeId).empty()))) {
214 str <<
"<DEFINITION>" << std::endl;
217 str << std::format(
"\t<FOR>{}</FOR>", infdiag.variable(varNodeId).name());
219 str << std::format(
"<!--{} | ", infdiag.variable(varNodeId).name());
220 for (
const auto n: infdiag.parents(varNodeId))
221 str << infdiag.variable(n).name() <<
",";
225 if (infdiag.isDecisionNode(varNodeId)) {
229 for (
const auto par: infdiag.parents(varNodeId))
230 parentList.
pushBack(infdiag.variable(par).name());
232 for (
auto parentListIte = parentList.rbegin(); parentListIte != parentList.rend();
234 str << std::format(
"\t<GIVEN>{}</GIVEN>\n", *parentListIte);
235 }
else if (infdiag.isChanceNode(varNodeId))
236 for (
Idx i = infdiag.cpt(varNodeId).nbrDim(); i > 1;
238 str << std::format(
"\t<GIVEN>{}</GIVEN>\n",
239 infdiag.cpt(varNodeId).variable(i - 1).name());
240 else if (infdiag.isUtilityNode(varNodeId))
241 for (
Idx i = infdiag.utility(varNodeId).nbrDim(); i > 1;
243 str << std::format(
"\t<GIVEN>{}</GIVEN>\n",
244 infdiag.utility(varNodeId).variable(i - 1).name());
247 if (infdiag.isChanceNode(varNodeId)) {
251 for (inst.setFirst(); !inst.end(); inst.inc())
252 str << infdiag.cpt(varNodeId)[inst] <<
" ";
254 str <<
"</TABLE>" << std::endl;
255 }
else if (infdiag.isUtilityNode(varNodeId)) {
260 for (inst.setFirst(); !inst.end(); inst.inc())
261 str << infdiag.utility(varNodeId)[inst] <<
" ";
263 str <<
"</TABLE>" << std::endl;
267 str <<
"</DEFINITION>" << std::endl;
276 template < GUM_Numeric GUM_SCALAR >
278 std::stringstream str;
280 str <<
"</NETWORK>" << std::endl;
281 str <<
"</BIF>" << std::endl;
Definition file for BIF XML exportation class.
Writes an influence diagram in a XML files with BIF format.
virtual ~BIFXMLIDWriter()
Destructor.
std::string _heading_()
Returns the header of the BIF file.
std::string _variableBloc_(const DiscreteVariable &var, int nodeType)
Returns a bloc defining a variable in the BIF format.
BIFXMLIDWriter()
Default constructor.
std::string _variableDefinition_(const NodeId &varNodeId, const InfluenceDiagram< GUM_SCALAR > &infdiag)
Returns a bloc defining a variable's table (if she has) in the BIF format.
std::string _documentend_()
Returns the end of the BIF file.
virtual void write(std::ostream &output, const InfluenceDiagram< GUM_SCALAR > &infdiag)
Writes an influence diagram in the given output stream.
Base class for discrete random variable.
Class representing an Influence Diagram.
Class for assigning/browsing values to tuples of discrete variables.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
#define GUM_ERROR(type, msg)
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities