23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 32 template <
typename GUM_SCALAR >
34 GUM_CONSTRUCTOR(BIFXMLIDWriter);
40 template <
typename GUM_SCALAR >
42 GUM_DESTRUCTOR(BIFXMLIDWriter);
52 template <
typename GUM_SCALAR >
54 std::ostream& output,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
56 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
59 output << __heading() << std::endl;
60 output <<
"<!-- Variables -->" << std::endl;
62 for (
const auto node : infdiag.nodes()) {
65 if (infdiag.isChanceNode(node))
67 else if (infdiag.isUtilityNode(node))
70 output << __variableBloc(infdiag.variable(node), nodeType) << std::endl;
73 output <<
"<!-- Probability distributions -->" << std::endl;
75 for (
const auto node : infdiag.nodes())
76 output << __variableDefinition(node, infdiag);
79 output << __documentend();
82 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
94 template <
typename GUM_SCALAR >
96 std::string filePath,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
97 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
99 write(output, infdiag);
103 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
109 template <
typename GUM_SCALAR >
111 std::stringstream str;
114 str <<
"<?xml version=\"1.0\" ?>" << std::endl;
140 str << std::endl <<
"<BIF VERSION=\"0.3\">" << std::endl;
143 str <<
"<NETWORK>" << std::endl;
151 template <
typename GUM_SCALAR >
162 std::stringstream str;
165 str <<
"<VARIABLE TYPE=\"";
168 case 1: str <<
"decision";
break;
170 case 2: str <<
"nature";
break;
172 case 3: str <<
"utility";
break;
177 str <<
"\">" << std::endl;
180 str <<
"\t<NAME>" << var.name() <<
"</NAME>" << std::endl;
181 str <<
"\t<PROPERTY>" << var.description() <<
"</PROPERTY>" << std::endl;
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 >
199 const NodeId& varNodeId,
const InfluenceDiagram< GUM_SCALAR >& infdiag) {
205 std::stringstream str;
207 if (!((infdiag.isDecisionNode(varNodeId))
208 && (infdiag.parents(varNodeId).empty()))) {
210 str <<
"<DEFINITION>" << std::endl;
213 str <<
"\t<FOR>" << infdiag.variable(varNodeId).name() <<
"</FOR>" 219 for (
const auto par : infdiag.parents(varNodeId))
220 parentList.
pushBack(infdiag.variable(par).name());
223 parentListIte != parentList.
rend();
225 str <<
"\t<GIVEN>" << (*parentListIte) <<
"</GIVEN>" << std::endl;
227 if (infdiag.isChanceNode(varNodeId)) {
228 Instantiation inst(infdiag.cpt(varNodeId));
231 for (inst.setFirst(); !inst.end(); inst.inc())
232 str << infdiag.cpt(varNodeId)[inst] <<
" ";
234 str <<
"</TABLE>" << std::endl;
235 }
else if (infdiag.isUtilityNode(varNodeId)) {
237 Instantiation inst(infdiag.utility(varNodeId));
240 for (inst.setFirst(); !inst.end(); inst.inc())
241 str << infdiag.utility(varNodeId)[inst] <<
" ";
243 str <<
"</TABLE>" << std::endl;
247 str <<
"</DEFINITION>" << std::endl;
256 template <
typename GUM_SCALAR >
258 std::stringstream str;
260 str <<
"</NETWORK>" << std::endl;
261 str <<
"</BIF>" << std::endl;
268 #endif // DOXYGEN_SHOULD_SKIP_THIS
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.