45#ifndef DOXYGEN_SHOULD_SKIP_THIS
49# define O3PRM_INDENT " "
55 template < GUM_Numeric GUM_SCALAR >
63 template < GUM_Numeric GUM_SCALAR >
75 template < GUM_Numeric GUM_SCALAR >
78 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
79 std::string bnName = bn.propertyWithDefault(
"name",
"");
80 if (bnName.empty()) bnName =
"bayesnet";
82 output <<
"class " << bnName <<
" {" << std::endl;
84 for (
auto node: bn.nodes()) {
85 output << _extractAttribute_(bn, node) << std::endl;
88 output <<
"}" << std::endl;
94 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
97 template < GUM_Numeric GUM_SCALAR >
100 std::stringstream str;
102 str << _extractType_(bn, node) <<
" ";
103 str << _extractName_(bn, node) <<
" ";
104 if (bn.parents(node).size() > 0) { str <<
"dependson " << _extractParents_(bn, node) <<
" "; }
105 str <<
" {" << _extractCPT_(bn, node) <<
"};" << std::endl;
109 template < GUM_Numeric 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 < GUM_Numeric 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 < GUM_Numeric 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 < GUM_Numeric GUM_SCALAR >
187 const auto& var =
static_cast< const RangeVariable&
>(bn.variable(node));
188 return std::format(
"int ({}, {})", var.minVal(), var.maxVal());
191 template < GUM_Numeric GUM_SCALAR >
194 std::stringstream str;
196 for (
auto l: bn.variable(node).labels()) {
199 return str.str().substr(0, str.str().size() - 2) +
")";
202 template < GUM_Numeric GUM_SCALAR >
203 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 < GUM_Numeric GUM_SCALAR >
220 if (!bn.variable(node).name().empty()) {
221 return bn.variable(node).name();
223 return std::to_string(node);
236 template < GUM_Numeric GUM_SCALAR >
239 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
241 _doWrite(output, bn);
245 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 a Bayes net in the given output 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