21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 template <
typename GUM_SCALAR >
32 GUM_CONSTRUCTOR(BIFXMLIDWriter);
38 template <
typename GUM_SCALAR >
40 GUM_DESTRUCTOR(BIFXMLIDWriter);
50 template <
typename GUM_SCALAR >
52 std::ostream& output,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
54 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
57 output << __heading() << std::endl;
58 output <<
"<!-- Variables -->" << std::endl;
60 for (
const auto node : infdiag.nodes()) {
63 if (infdiag.isChanceNode(node))
65 else if (infdiag.isUtilityNode(node))
68 output << __variableBloc(infdiag.variable(node), nodeType) << std::endl;
71 output <<
"<!-- Probability distributions -->" << std::endl;
73 for (
const auto node : infdiag.nodes())
74 output << __variableDefinition(node, infdiag);
77 output << __documentend();
80 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
92 template <
typename GUM_SCALAR >
94 std::string filePath,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
95 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
97 write(output, infdiag);
101 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
107 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;
149 template <
typename GUM_SCALAR >
160 std::stringstream str;
163 str <<
"<VARIABLE TYPE=\"";
166 case 1: str <<
"decision";
break;
168 case 2: str <<
"nature";
break;
170 case 3: str <<
"utility";
break;
175 str <<
"\">" << std::endl;
178 str <<
"\t<NAME>" << var.name() <<
"</NAME>" << std::endl;
179 str <<
"\t<PROPERTY>" << var.description() <<
"</PROPERTY>" << std::endl;
183 for (
Idx i = 0; i < var.domainSize(); i++)
184 str <<
"\t<OUTCOME>" << var.label(i) <<
"</OUTCOME>" << std::endl;
187 str <<
"</VARIABLE>" << std::endl;
195 template <
typename GUM_SCALAR >
197 const NodeId& varNodeId,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
203 std::stringstream str;
205 if (!((infdiag.isDecisionNode(varNodeId))
206 && (infdiag.parents(varNodeId).empty()))) {
208 str <<
"<DEFINITION>" << std::endl;
211 str <<
"\t<FOR>" << infdiag.variable(varNodeId).name() <<
"</FOR>" 217 for (
const auto par : infdiag.parents(varNodeId))
218 parentList.
pushBack(infdiag.variable(par).name());
221 parentListIte != parentList.
rend();
223 str <<
"\t<GIVEN>" << (*parentListIte) <<
"</GIVEN>" << std::endl;
225 if (infdiag.isChanceNode(varNodeId)) {
226 Instantiation inst(infdiag.cpt(varNodeId));
229 for (inst.setFirst(); !inst.end(); inst.inc())
230 str << infdiag.cpt(varNodeId)[inst] <<
" ";
232 str <<
"</TABLE>" << std::endl;
233 }
else if (infdiag.isUtilityNode(varNodeId)) {
235 Instantiation inst(infdiag.utility(varNodeId));
238 for (inst.setFirst(); !inst.end(); inst.inc())
239 str << infdiag.utility(varNodeId)[inst] <<
" ";
241 str <<
"</TABLE>" << std::endl;
245 str <<
"</DEFINITION>" << std::endl;
254 template <
typename GUM_SCALAR >
256 std::stringstream str;
258 str <<
"</NETWORK>" << std::endl;
259 str <<
"</BIF>" << std::endl;
266 #endif // DOXYGEN_SHOULD_SKIP_THIS
gum is the global namespace for all aGrUM entities
ListIterator< std::string > iterator
std::string __documentend()
Returns the end of the BIF file.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
BIFXMLIDWriter()
Default constructor.
Definition file for BIF XML exportation class.
virtual void write(std::ostream &output, const InfluenceDiagram< GUM_SCALAR > &infdiag)
Writes an influence diagram in the given ouput stream.
virtual ~BIFXMLIDWriter()
Destructor.
const iterator & rend() noexcept
Returns an unsafe iterator pointing just before the beginning of the List.
iterator rbegin()
Returns an unsafe iterator pointing to the last element of the List.
Size NodeId
Type for node ids.
std::string __variableBloc(const DiscreteVariable &var, int nodeType)
Returns a bloc defining a variable in the BIF format.
#define GUM_ERROR(type, msg)
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 __heading()
Returns the header of the BIF file.