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.");
60 std::string bnName = bn.propertyWithDefault(
"name",
"");
61 if (bnName ==
"") bnName =
"bayesnet";
63 output <<
"class " << bnName <<
" {" << std::endl;
65 for (
auto node: bn.nodes()) {
66 output << __extractAttribute(bn, node) << std::endl;
69 output <<
"}" << std::endl;
75 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
78 template <
typename GUM_SCALAR >
80 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
81 std::stringstream str;
83 str << __extractType(bn, node) <<
" ";
84 str << __extractName(bn, node) <<
" ";
85 if (bn.parents(node).size() > 0) {
86 str <<
"dependson " << __extractParents(bn, node) <<
" ";
88 str <<
" {" << __extractCPT(bn, node) <<
"};" << std::endl;
92 template <
typename GUM_SCALAR >
94 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
95 std::stringstream str;
96 auto var = &(bn.variable(node));
97 for (
auto parent: bn.cpt(node).variablesSequence()) {
98 if (var != parent) { str << parent->name() <<
", "; }
100 return str.str().substr(0, str.str().size() - 2);
103 template <
typename GUM_SCALAR >
107 std::stringstream str;
109 Instantiation inst(bn.cpt(node));
112 if (inst.nbrDim() == 1) {
114 for (inst.setFirst(); !inst.end(); inst.inc()) {
120 str << bn.cpt(node)[inst];
125 for (
auto var = inst.variablesSequence().rbegin();
126 var != inst.variablesSequence().rend();
131 auto currentval = inst.val(0) + 1;
132 for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
139 if (currentval != inst.val(0)) {
140 str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
141 currentval = inst.val(0);
143 str << bn.cpt(node)[inst];
145 str << std::endl << O3PRM_INDENT;
152 template <
typename GUM_SCALAR >
156 switch (bn.variable(node).varType()) {
158 auto double_var =
dynamic_cast< const DiscretizedVariable< double >*
>(
159 &(bn.variable(node)));
160 if (double_var !=
nullptr) {
161 return __extractDiscretizedType< DiscretizedVariable< double > >(
164 auto float_var =
dynamic_cast< const DiscretizedVariable< float >*
>(
165 &(bn.variable(node)));
166 if (float_var !=
nullptr) {
167 return __extractDiscretizedType< DiscretizedVariable< float > >(
172 "DiscretizedVariable ticks are neither doubles or floats");
175 return __extractRangeType(bn, node);
178 return __extractLabelizedType(bn, node);
183 template <
typename GUM_SCALAR >
185 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
186 const auto& var =
static_cast< const RangeVariable&
>(bn.variable(node));
187 std::stringstream str;
188 str <<
"int (" << var.minVal() <<
", " << var.maxVal() <<
")";
192 template <
typename GUM_SCALAR >
194 const IBayesNet< GUM_SCALAR >& bn,
NodeId node) {
195 std::stringstream str;
197 for (
auto l: bn.variable(node).labels()) {
200 return str.str().substr(0, str.str().size() - 2) +
")";
203 template <
typename GUM_SCALAR >
204 template <
typename VARTYPE >
207 std::stringstream str;
208 if (var->ticks().size() >= 3) {
209 str <<
"real(" << var->ticks()[0];
210 for (
size_t i = 1; i < var->ticks().size(); ++i) {
211 str <<
", " << var->ticks()[i];
216 GUM_ERROR(InvalidArgument,
"discretized variable does not have enough ticks");
219 template <
typename GUM_SCALAR >
223 if (!bn.variable(node).name().empty()) {
224 return bn.variable(node).name();
226 std::stringstream str;
241 template <
typename GUM_SCALAR >
244 const IBayesNet< GUM_SCALAR >& bn) {
245 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
251 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed."); }
256 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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.