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 _indent_ = (indent < -1) ? -1 : indent;
63 GUM_CONSTRUCTOR(GumBNWriter);
67 template < GUM_Numeric GUM_SCALAR >
68 GumBNWriter< GUM_SCALAR >::~GumBNWriter() {
69 GUM_DESTRUCTOR(GumBNWriter);
78 template < GUM_Numeric GUM_SCALAR >
79 void GumBNWriter< GUM_SCALAR >::_doWrite(std::ostream& output,
80 const IBayesNet< GUM_SCALAR >& bn) {
81 if (!output.good())
GUM_ERROR(
IOError,
"Input/Output error : stream not writable.");
84 content[
"type"] =
"BN";
85 content[
"GumJsonVersion"] =
"1.0";
88 content[
"nodes"] = ordered_json::array();
89 for (
const auto& node: bn.nodes()) {
90 content[
"nodes"].push_back(bn.variable(node).toFast());
93 content[
"parents"] = ordered_json::object();
94 auto& parentsObj = content[
"parents"].template get_ref< ordered_json::object_t& >();
95 parentsObj.reserve(bn.size());
96 for (
const auto& node: bn.nodes()) {
97 ordered_json parentList = ordered_json::array();
98 const auto& cpt = bn.cpt(node);
99 for (Idx i = 1; i < cpt.nbrDim(); i++)
100 parentList.push_back(cpt.variable(i).name());
101 parentsObj.emplace_back(bn.variable(node).name(), std::move(parentList));
104 content[
"cpt"] = ordered_json::object();
105 auto& cptObj = content[
"cpt"].template get_ref< ordered_json::object_t& >();
106 cptObj.reserve(bn.size());
107 for (
const auto& node: bn.nodes()) {
108 const auto& cpt = bn.cpt(node);
113 std::string name = agg->aggregatorName();
114 const auto bracket = name.find(
'[');
115 if (bracket != std::string::npos) name.erase(bracket);
117 ordered_json aggJson;
118 aggJson[
"kind"] =
"aggregator";
119 aggJson[
"name"] = name;
120 if (
const auto value = agg->value()) aggJson[
"value"] = *value;
122 cptObj.emplace_back(bn.variable(node).name(), std::move(aggJson));
126 if (
const auto* ici =
dynamic_cast< const MultiDimICIModel< GUM_SCALAR >*
>(cpt.content())) {
127 ordered_json weights = ordered_json::object();
128 for (Idx i = 1; i < cpt.nbrDim(); i++)
129 weights[cpt.variable(i).name()] = ici->causalWeight(cpt.variable(i));
131 ordered_json iciJson;
132 iciJson[
"kind"] =
"ici";
133 iciJson[
"name"] = ici->name();
134 iciJson[
"externalWeight"] = ici->externalWeight();
135 iciJson[
"causalWeights"] = weights;
137 cptObj.emplace_back(bn.variable(node).name(), std::move(iciJson));
142 Instantiation I(cpt);
143 for (I.setFirst(); !I.end(); ++I) {
144 cptValues.push_back(cpt[I]);
146 cptObj.emplace_back(bn.variable(node).name(), std::move(cptValues));
149 for (
const auto& prop: bn.properties()) {
150 content[
"properties"][prop] = bn.property(prop);
159 output << content.dump(_indent_);
163 GUM_ERROR(
IOError,
"Writing in the ostream failed. Check if the stream is writable.")
174 template < GUM_Numeric GUM_SCALAR >
175 void GumBNWriter< GUM_SCALAR >::_doWrite(std::string_view filePath,
176 const IBayesNet< GUM_SCALAR >& bn) {
177 std::ofstream output(std::string(filePath),
178 _binary_ ? (std::ios_base::trunc | std::ios::binary)
179 : std::ios_base::trunc);
181 _doWrite(output, bn);
Definition of classe for GUM (json) file output manipulation.
Shared binary I/O helpers for GUM (jgum/bgum) serialization.
virtual class for writing a BN to a file.
GumBNWriter(bool binary=false, int indent=-1)
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
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).