21 #ifndef DOXYGEN_SHOULD_SKIP_THIS 25 # define O3PRM_INDENT " " 31 template <
typename GUM_SCALAR >
33 GUM_CONSTRUCTOR(O3prmBNWriter);
39 template <
typename GUM_SCALAR >
41 GUM_DESTRUCTOR(O3prmBNWriter);
51 template <
typename GUM_SCALAR >
54 const IBayesNet< GUM_SCALAR >& bn) {
56 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
59 output <<
"class BayesNet {" << std::endl;
61 for (
auto node : bn.nodes()) {
62 output << __extractAttribute(bn, node) << std::endl;
65 output <<
"}" << std::endl;
71 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
74 template <
typename GUM_SCALAR >
76 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
77 std::stringstream str;
79 str << __extractType(bn, node) <<
" ";
80 str << __extractName(bn, node) <<
" ";
81 if (bn.parents(node).size() > 0) {
82 str <<
"dependson " << __extractParents(bn, node) <<
" ";
84 str <<
" {" << __extractCPT(bn, node) <<
"};" << std::endl;
88 template <
typename GUM_SCALAR >
90 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
91 std::stringstream str;
92 auto var = &(bn.variable(node));
93 for (
auto parent : bn.cpt(node).variablesSequence()) {
94 if (var != parent) { str << parent->name() <<
", "; }
96 return str.str().substr(0, str.str().size() - 2);
99 template <
typename GUM_SCALAR >
103 std::stringstream str;
105 Instantiation inst(bn.cpt(node));
108 if (inst.nbrDim() == 1) {
110 for (inst.setFirst(); !inst.end(); inst.inc()) {
116 str << bn.cpt(node)[inst];
121 for (
auto var = inst.variablesSequence().rbegin();
122 var != inst.variablesSequence().rend();
127 auto currentval = inst.val(0) + 1;
128 for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
135 if (currentval != inst.val(0)) {
136 str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
137 currentval = inst.val(0);
139 str << bn.cpt(node)[inst];
141 str << std::endl << O3PRM_INDENT;
148 template <
typename GUM_SCALAR >
152 switch (bn.variable(node).varType()) {
154 auto double_var =
dynamic_cast< const DiscretizedVariable< double >*
>(
155 &(bn.variable(node)));
156 if (double_var !=
nullptr) {
157 return __extractDiscretizedType< DiscretizedVariable< double > >(
160 auto float_var =
dynamic_cast< const DiscretizedVariable< float >*
>(
161 &(bn.variable(node)));
162 if (float_var !=
nullptr) {
163 return __extractDiscretizedType< DiscretizedVariable< float > >(
168 "DiscretizedVariable ticks are neither doubles or floats");
171 return __extractRangeType(bn, node);
173 default: {
return __extractLabelizedType(bn, node); }
177 template <
typename GUM_SCALAR >
179 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
180 const auto& var =
static_cast< const RangeVariable&
>(bn.variable(node));
181 std::stringstream str;
182 str <<
"int (" << var.minVal() <<
", " << var.maxVal() <<
")";
186 template <
typename GUM_SCALAR >
188 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
189 std::stringstream str;
191 for (
auto l : bn.variable(node).labels()) {
194 return str.str().substr(0, str.str().size() - 2) +
")";
197 template <
typename GUM_SCALAR >
198 template <
typename VARTYPE >
201 std::stringstream str;
202 if (var->ticks().size() >= 3) {
203 str <<
"real(" << var->ticks()[0];
204 for (
size_t i = 1; i < var->ticks().size(); ++i) {
205 str <<
", " << var->ticks()[i];
210 GUM_ERROR(InvalidArgument,
"discretized variable does not have enough ticks");
213 template <
typename GUM_SCALAR >
217 if (!bn.variable(node).name().empty()) {
218 return bn.variable(node).name();
220 std::stringstream str;
235 template <
typename GUM_SCALAR >
238 const IBayesNet< GUM_SCALAR >& bn) {
239 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
245 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
250 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
gum is the global namespace for all aGrUM entities
Definition file for BIF XML exportation class.
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.