43#ifndef DOXYGEN_SHOULD_SKIP_THIS
51 template <
typename GUM_SCALAR >
59 template <
typename GUM_SCALAR >
71 template <
typename GUM_SCALAR >
74 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
76 output << _heading_() << std::endl;
77 output <<
"<!-- Variables -->" << std::endl;
79 for (
const auto node: infdiag.nodes()) {
82 if (infdiag.isChanceNode(node)) nodeType = 2;
83 else if (infdiag.isUtilityNode(node)) nodeType = 3;
85 output << _variableBloc_(infdiag.variable(node), nodeType) << std::endl;
88 output <<
"<!-- Probability distributions -->" << std::endl;
90 for (
const auto node: infdiag.nodes())
91 output << _variableDefinition_(node, infdiag);
94 output << _documentend_();
97 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
109 template <
typename GUM_SCALAR >
112 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
114 write(output, infdiag);
118 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
124 template <
typename GUM_SCALAR >
126 std::stringstream str;
129 str <<
"<?xml version=\"1.0\" ?>" << std::endl;
135 str << std::endl <<
"<BIF VERSION=\"0.3\">" << std::endl;
138 str <<
"<NETWORK>" << std::endl;
146 template <
typename GUM_SCALAR >
159 std::stringstream str;
162 str <<
"<VARIABLE TYPE=\"";
165 case 1 : str <<
"decision";
break;
167 case 2 : str <<
"nature";
break;
169 case 3 : str <<
"utility";
break;
174 str <<
"\">" << std::endl;
177 str <<
"\t<NAME>" << var.name() <<
"</NAME>" << std::endl;
178 str <<
"\t<PROPERTY>description = " << var.description() <<
"</PROPERTY>" << std::endl;
179 str <<
"\t<PROPERTY>fast = " << var.toFast() <<
"</PROPERTY>" << std::endl;
182 str <<
"<!-- OUTCOME are not used in pyAgrum BIFXML (see fast property) but are kept for "
185 for (
Idx i = 0; i < var.domainSize(); i++)
186 str <<
"\t<OUTCOME>" << var.label(i) <<
"</OUTCOME>" << std::endl;
189 str <<
"</VARIABLE>" << std::endl;
197 template <
typename GUM_SCALAR >
206 std::stringstream str;
208 if (!((infdiag.isDecisionNode(varNodeId)) && (infdiag.parents(varNodeId).empty()))) {
210 str <<
"<DEFINITION>" << std::endl;
213 str <<
"\t<FOR>" << infdiag.variable(varNodeId).name() <<
"</FOR>";
215 str <<
"<!--" << infdiag.variable(varNodeId).name() <<
" | ";
216 for (
const auto n: infdiag.parents(varNodeId))
217 str << infdiag.variable(n).name() <<
",";
221 if (infdiag.isDecisionNode(varNodeId)) {
225 for (
const auto par: infdiag.parents(varNodeId))
226 parentList.
pushBack(infdiag.variable(par).name());
228 for (
auto parentListIte = parentList.rbegin(); parentListIte != parentList.rend();
230 str <<
"\t<GIVEN>" << (*parentListIte) <<
"</GIVEN>" << std::endl;
231 }
else if (infdiag.isChanceNode(varNodeId))
232 for (
Idx i = infdiag.cpt(varNodeId).nbrDim(); i > 1;
234 str <<
"\t<GIVEN>" << infdiag.cpt(varNodeId).variable(i - 1).name() <<
"</GIVEN>"
236 else if (infdiag.isUtilityNode(varNodeId))
237 for (
Idx i = infdiag.utility(varNodeId).nbrDim(); i > 1;
239 str <<
"\t<GIVEN>" << infdiag.utility(varNodeId).variable(i - 1).name() <<
"</GIVEN>"
243 if (infdiag.isChanceNode(varNodeId)) {
247 for (inst.setFirst(); !inst.end(); inst.inc())
248 str << infdiag.cpt(varNodeId)[inst] <<
" ";
250 str <<
"</TABLE>" << std::endl;
251 }
else if (infdiag.isUtilityNode(varNodeId)) {
256 for (inst.setFirst(); !inst.end(); inst.inc())
257 str << infdiag.utility(varNodeId)[inst] <<
" ";
259 str <<
"</TABLE>" << std::endl;
263 str <<
"</DEFINITION>" << std::endl;
272 template <
typename GUM_SCALAR >
274 std::stringstream str;
276 str <<
"</NETWORK>" << std::endl;
277 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 ouput 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