23 #ifndef DOXYGEN_SHOULD_SKIP_THIS 27 # define O3PRM_INDENT " " 33 template <
typename GUM_SCALAR >
35 GUM_CONSTRUCTOR(O3prmBNWriter);
41 template <
typename GUM_SCALAR >
43 GUM_DESTRUCTOR(O3prmBNWriter);
53 template <
typename GUM_SCALAR >
56 const IBayesNet< GUM_SCALAR >& bn) {
58 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
61 output <<
"class BayesNet {" << std::endl;
63 for (
auto node : bn.nodes()) {
64 output << __extractAttribute(bn, node) << std::endl;
67 output <<
"}" << std::endl;
73 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
76 template <
typename GUM_SCALAR >
78 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
79 std::stringstream str;
81 str << __extractType(bn, node) <<
" ";
82 str << __extractName(bn, node) <<
" ";
83 if (bn.parents(node).size() > 0) {
84 str <<
"dependson " << __extractParents(bn, node) <<
" ";
86 str <<
" {" << __extractCPT(bn, node) <<
"};" << std::endl;
90 template <
typename GUM_SCALAR >
92 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
93 std::stringstream str;
94 auto var = &(bn.variable(node));
95 for (
auto parent : bn.cpt(node).variablesSequence()) {
96 if (var != parent) { str << parent->name() <<
", "; }
98 return str.str().substr(0, str.str().size() - 2);
101 template <
typename GUM_SCALAR >
105 std::stringstream str;
107 Instantiation inst(bn.cpt(node));
110 if (inst.nbrDim() == 1) {
112 for (inst.setFirst(); !inst.end(); inst.inc()) {
118 str << bn.cpt(node)[inst];
123 for (
auto var = inst.variablesSequence().rbegin();
124 var != inst.variablesSequence().rend();
129 auto currentval = inst.val(0) + 1;
130 for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
137 if (currentval != inst.val(0)) {
138 str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
139 currentval = inst.val(0);
141 str << bn.cpt(node)[inst];
143 str << std::endl << O3PRM_INDENT;
150 template <
typename GUM_SCALAR >
154 switch (bn.variable(node).varType()) {
156 auto double_var =
dynamic_cast< const DiscretizedVariable< double >*
>(
157 &(bn.variable(node)));
158 if (double_var !=
nullptr) {
159 return __extractDiscretizedType< DiscretizedVariable< double > >(
162 auto float_var =
dynamic_cast< const DiscretizedVariable< float >*
>(
163 &(bn.variable(node)));
164 if (float_var !=
nullptr) {
165 return __extractDiscretizedType< DiscretizedVariable< float > >(
170 "DiscretizedVariable ticks are neither doubles or floats");
173 return __extractRangeType(bn, node);
176 return __extractLabelizedType(bn, node);
181 template <
typename GUM_SCALAR >
183 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
184 const auto& var =
static_cast< const RangeVariable&
>(bn.variable(node));
185 std::stringstream str;
186 str <<
"int (" << var.minVal() <<
", " << var.maxVal() <<
")";
190 template <
typename GUM_SCALAR >
192 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
193 std::stringstream str;
195 for (
auto l : bn.variable(node).labels()) {
198 return str.str().substr(0, str.str().size() - 2) +
")";
201 template <
typename GUM_SCALAR >
202 template <
typename VARTYPE >
205 std::stringstream str;
206 if (var->ticks().size() >= 3) {
207 str <<
"real(" << var->ticks()[0];
208 for (
size_t i = 1; i < var->ticks().size(); ++i) {
209 str <<
", " << var->ticks()[i];
214 GUM_ERROR(InvalidArgument,
"discretized variable does not have enough ticks");
217 template <
typename GUM_SCALAR >
221 if (!bn.variable(node).name().empty()) {
222 return bn.variable(node).name();
224 std::stringstream str;
239 template <
typename GUM_SCALAR >
242 const IBayesNet< GUM_SCALAR >& bn) {
243 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
249 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
254 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __extractCPT(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
O3prmBNWriter()
Default constructor.
std::string __extractRangeType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractLabelizedType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractName(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractAttribute(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractDiscretizedType(const VARTYPE *var)
virtual ~O3prmBNWriter()
Destructor.
Size NodeId
Type for node ids.
std::string __extractParents(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
#define GUM_ERROR(type, msg)
virtual void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.