43#ifndef DOXYGEN_SHOULD_SKIP_THIS
47# define O3PRM_INDENT " "
53 template <
typename GUM_SCALAR >
61 template <
typename GUM_SCALAR >
73 template <
typename GUM_SCALAR >
76 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
77 std::string bnName = bn.propertyWithDefault(
"name",
"");
78 if (bnName.empty()) bnName =
"bayesnet";
80 output <<
"class " << bnName <<
" {" << std::endl;
82 for (
auto node: bn.nodes()) {
83 output << _extractAttribute_(bn, node) << std::endl;
86 output <<
"}" << std::endl;
92 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
95 template <
typename GUM_SCALAR >
99 std::stringstream str;
101 str << _extractType_(bn, node) <<
" ";
102 str << _extractName_(bn, node) <<
" ";
103 if (bn.parents(node).size() > 0) { str <<
"dependson " << _extractParents_(bn, node) <<
" "; }
104 str <<
" {" << _extractCPT_(bn, node) <<
"};" << std::endl;
108 template <
typename GUM_SCALAR >
112 std::stringstream str;
113 auto var = &(bn.variable(node));
114 for (
auto parent: bn.cpt(node).variablesSequence()) {
115 if (var != parent) { str << parent->name() <<
", "; }
117 return str.str().substr(0, str.str().size() - 2);
120 template <
typename GUM_SCALAR >
123 std::stringstream str;
128 if (inst.nbrDim() == 1) {
130 for (inst.setFirst(); !inst.end(); inst.inc()) {
136 str << bn.cpt(node)[inst];
141 for (
auto var = inst.variablesSequence().rbegin(); var != inst.variablesSequence().rend();
146 auto currentval = inst.val(0) + 1;
147 for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
154 if (currentval != inst.val(0)) {
155 str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
156 currentval = inst.val(0);
158 str << bn.cpt(node)[inst];
160 str << std::endl << O3PRM_INDENT;
167 template <
typename GUM_SCALAR >
170 switch (bn.variable(node).varType()) {
173 return _extractDiscretizedType_< DiscretizedVariable< double > >(double_var);
176 return _extractRangeType_(bn, node);
179 return _extractLabelizedType_(bn, node);
184 template <
typename GUM_SCALAR >
188 const auto& var =
static_cast< const RangeVariable&
>(bn.variable(node));
189 std::stringstream str;
190 str <<
"int (" << var.minVal() <<
", " << var.maxVal() <<
")";
194 template <
typename GUM_SCALAR >
198 std::stringstream str;
200 for (
auto l: bn.variable(node).labels()) {
203 return str.str().substr(0, str.str().size() - 2) +
")";
206 template <
typename GUM_SCALAR >
207 template <
typename VARTYPE >
209 std::stringstream str;
210 if (var->ticks().size() >= 3) {
211 str <<
"real(" << var->ticks()[0];
212 for (
size_t i = 1; i < var->ticks().size(); ++i) {
213 str <<
", " << var->ticks()[i];
218 GUM_ERROR(InvalidArgument,
"discretized variable does not have enough ticks")
221 template <
typename GUM_SCALAR >
224 if (!bn.variable(node).name().empty()) {
225 return bn.variable(node).name();
227 std::stringstream str;
242 template <
typename GUM_SCALAR >
245 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
247 _doWrite(output, bn);
251 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
Definition file for BIF XML exportation class.
Class for discretized random variable.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
void setFirst()
Assign the first values to the tuple of the Instantiation.
<agrum/PRM/o3prm/O3prmBNWriter.h>
std::string _extractRangeType_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
std::string _extractDiscretizedType_(const VARTYPE *var)
std::string _extractParents_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string _extractCPT_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
~O3prmBNWriter() override
Destructor.
std::string _extractLabelizedType_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string _extractType_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string _extractAttribute_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string _extractName_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
O3prmBNWriter()
Default constructor.
Defines a discrete random variable over an integer interval.
#define GUM_ERROR(type, msg)
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities