45#ifndef DOXYGEN_SHOULD_SKIP_THIS
53# include <agrum/base/external/json/json.hpp>
54using json = nlohmann::json;
55using ordered_json = nlohmann::ordered_json;
59 template < GUM_Numeric GUM_SCALAR >
62 template < GUM_Numeric GUM_SCALAR >
63 GumBNWriter< GUM_SCALAR >::GumBNWriter(
bool binary,
int indent) : BNWriter< GUM_SCALAR >() {
65 _indent_ = (indent < -1) ? -1 : indent;
66 GUM_CONSTRUCTOR(GumBNWriter);
70 template < GUM_Numeric GUM_SCALAR >
71 GumBNWriter< GUM_SCALAR >::~GumBNWriter() {
72 GUM_DESTRUCTOR(GumBNWriter);
81 template < GUM_Numeric GUM_SCALAR >
82 void GumBNWriter< GUM_SCALAR >::_doWrite(std::ostream& output,
83 const IBayesNet< GUM_SCALAR >& bn) {
84 if (!output.good())
GUM_ERROR(
IOError,
"Input/Output error : stream not writable.");
87 content[
"type"] =
"BN";
88 content[
"GumJsonVersion"] =
"1.0";
91 content[
"nodes"] = ordered_json::array();
92 for (
const auto& node: bn.nodes()) {
93 content[
"nodes"].push_back(bn.variable(node).toFast());
96 content[
"parents"] = ordered_json::object();
97 auto& parentsObj = content[
"parents"].template get_ref< ordered_json::object_t& >();
98 parentsObj.reserve(bn.size());
99 for (
const auto& node: bn.nodes()) {
100 ordered_json parentList = ordered_json::array();
101 const auto& cpt = bn.cpt(node);
102 for (Idx i = 1; i < cpt.nbrDim(); i++)
103 parentList.push_back(cpt.variable(i).name());
104 parentsObj.emplace_back(bn.variable(node).name(), std::move(parentList));
107 content[
"cpt"] = ordered_json::object();
108 auto& cptObj = content[
"cpt"].template get_ref< ordered_json::object_t& >();
109 cptObj.reserve(bn.size());
110 for (
const auto& node: bn.nodes()) {
111 const auto& cpt = bn.cpt(node);
116 std::string name = agg->aggregatorName();
117 const auto bracket = name.find(
'[');
118 if (bracket != std::string::npos) name.erase(bracket);
120 ordered_json aggJson;
121 aggJson[
"kind"] =
"aggregator";
122 aggJson[
"name"] = name;
123 if (
const auto value = agg->value()) aggJson[
"value"] = *value;
125 cptObj.emplace_back(bn.variable(node).name(), std::move(aggJson));
129 if (
const auto* ici =
dynamic_cast< const MultiDimICIModel< GUM_SCALAR >*
>(cpt.content())) {
130 ordered_json weights = ordered_json::object();
131 for (Idx i = 1; i < cpt.nbrDim(); i++)
132 weights[cpt.variable(i).name()] = ici->causalWeight(cpt.variable(i));
134 ordered_json iciJson;
135 iciJson[
"kind"] =
"ici";
136 iciJson[
"name"] = ici->name();
137 iciJson[
"externalWeight"] = ici->externalWeight();
138 iciJson[
"causalWeights"] = weights;
140 cptObj.emplace_back(bn.variable(node).name(), std::move(iciJson));
145 Instantiation I(cpt);
146 for (I.setFirst(); !I.end(); ++I) {
147 cptValues.push_back(cpt[I]);
149 cptObj.emplace_back(bn.variable(node).name(), std::move(cptValues));
152 for (
const auto& prop: bn.properties()) {
153 content[
"properties"][prop] = bn.property(prop);
162 output << content.dump(_indent_);
166 GUM_ERROR(
IOError,
"Writing in the ostream failed. Check if the stream is writable.")
177 template < GUM_Numeric GUM_SCALAR >
178 void GumBNWriter< GUM_SCALAR >::_doWrite(std::string_view filePath,
179 const IBayesNet< GUM_SCALAR >& bn) {
180 std::ofstream output(std::string(filePath),
181 _binary_ ? (std::ios_base::trunc | std::ios::binary)
182 : std::ios_base::trunc);
184 _doWrite(output, bn);
Definition of classe for GUM (json) file output manipulation.
Shared binary I/O helpers for GUM (jgum/bgum) serialization.
Writes a IBayesNet in the GUM json format.
GumBNWriter(bool binary=false)
Default constructor.
Exception : input/output problem.
#define GUM_ERROR(type, msg)
Abstract base class for all multi dimensionnal Causal Independency models.
gum is the global namespace for all aGrUM entities
int defaultJgumIndent()
The indentation used by the GUM writers (jgum text format) when none is given: the value of the key j...
GUM_SHARED_PUBLIC void _writeVector_(std::ostream &os, const std::vector< uint8_t > &vec)
Writes a length-prefixed byte vector to a binary stream (bgum format).