45#ifndef DOXYGEN_SHOULD_SKIP_THIS
51# include <agrum/base/external/json/json.hpp>
52using json = nlohmann::json;
53using ordered_json = nlohmann::ordered_json;
56 template < GUM_Numeric GUM_SCALAR >
59 template < GUM_Numeric GUM_SCALAR >
60 GumIDWriter< GUM_SCALAR >::GumIDWriter(
bool binary,
int indent) : IDWriter< GUM_SCALAR >() {
62 _indent_ = (indent < -1) ? -1 : indent;
63 GUM_CONSTRUCTOR(GumIDWriter);
66 template < GUM_Numeric GUM_SCALAR >
67 GumIDWriter< GUM_SCALAR >::~GumIDWriter() {
68 GUM_DESTRUCTOR(GumIDWriter);
71 template < GUM_Numeric GUM_SCALAR >
72 void GumIDWriter< GUM_SCALAR >::write(std::ostream& output,
73 const InfluenceDiagram< GUM_SCALAR >&
id) {
74 if (!output.good())
GUM_ERROR(
IOError,
"Input/Output error : stream not writable.")
77 content[
"type"] =
"ID";
78 content[
"GumJsonVersion"] =
"1.0";
81 content[
"chance"] = ordered_json::array();
82 content[
"utility"] = ordered_json::array();
83 content[
"decision"] = ordered_json::array();
84 for (const auto& node:
id.nodes()) {
85 const auto fast =
id.variable(node).toFast();
86 if (
id.isChanceNode(node)) {
87 content[
"chance"].push_back(fast);
88 }
else if (
id.isUtilityNode(node)) {
89 content[
"utility"].push_back(fast);
91 content[
"decision"].push_back(fast);
96 content[
"parents"] = ordered_json::object();
97 for (
const auto& node:
id.nodes()) {
98 ordered_json parentList = ordered_json::array();
99 const auto& name =
id.variable(node).name();
100 if (
id.isChanceNode(node)) {
101 const auto& cpt =
id.cpt(node);
102 for (Idx i = 1; i < cpt.nbrDim(); i++)
103 parentList.push_back(cpt.variable(i).name());
104 }
else if (
id.isUtilityNode(node)) {
105 const auto& ut =
id.utility(node);
106 for (Idx i = 1; i < ut.nbrDim(); i++)
107 parentList.push_back(ut.variable(i).name());
110 for (
const auto& p:
id.parents(node))
111 parentList.push_back(
id.variable(p).name());
113 content[
"parents"][name] = parentList;
117 for (
const auto& node:
id.nodes()) {
118 if (!
id.isChanceNode(node))
continue;
120 const auto& cpt =
id.cpt(node);
121 Instantiation I(cpt);
122 for (I.setFirst(); !I.end(); ++I)
123 vals.push_back(cpt[I]);
124 content[
"cpt"][
id.variable(node).name()] = vals;
128 for (
const auto& node:
id.nodes()) {
129 if (!
id.isUtilityNode(node))
continue;
131 const auto& ut =
id.utility(node);
133 for (I.setFirst(); !I.end(); ++I)
134 vals.push_back(ut[I]);
135 content[
"reward"][
id.variable(node).name()] = vals;
139 for (
const auto& prop:
id.properties())
140 content[
"properties"][prop] =
id.property(prop);
145 output << content.dump(_indent_);
149 GUM_ERROR(
IOError,
"Writing in the ostream failed. Check if the stream is writable.")
153 template < GUM_Numeric GUM_SCALAR >
154 void GumIDWriter< GUM_SCALAR >::write(std::string_view filePath,
155 InfluenceDiagram< GUM_SCALAR >&
id) {
157 write(filePath,
static_cast< const InfluenceDiagram< GUM_SCALAR >&
>(
id));
160 template < GUM_Numeric GUM_SCALAR >
161 void GumIDWriter< GUM_SCALAR >::write(std::string_view filePath,
162 const InfluenceDiagram< GUM_SCALAR >&
id) {
163 std::ofstream output(std::string(filePath),
164 _binary_ ? (std::ios_base::trunc | std::ios::binary)
165 : std::ios_base::trunc);
171 template < GUM_Numeric GUM_SCALAR >
172 std::string GumIDWriter< GUM_SCALAR >::toString(
const InfluenceDiagram< GUM_SCALAR >&
id) {
173 std::ostringstream oss;
Shared binary I/O helpers for GUM (jgum/bgum) serialization.
Definition of class for GUM (json) file output for Influence Diagrams.
Writes an InfluenceDiagram in the GUM json format.
GumIDWriter(bool binary=false)
Default constructor.
Exception : input/output problem.
#define GUM_ERROR(type, msg)
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).