45#ifndef DOXYGEN_SHOULD_SKIP_THIS
51# include <agrum/base/external/json/json.hpp>
52using json = nlohmann::json;
55 template < GUM_Numeric GUM_SCALAR >
57 std::string_view filename,
58 bool binary) :
IDReader< GUM_SCALAR >(id, filename) {
59 GUM_CONSTRUCTOR(GumIDReader)
61 _streamName_ = filename;
66 template < GUM_Numeric GUM_SCALAR >
67 GumIDReader< GUM_SCALAR >::GumIDReader(InfluenceDiagram< GUM_SCALAR >*
id) :
68 IDReader< GUM_SCALAR >(id,
"") {
69 GUM_CONSTRUCTOR(GumIDReader)
76 template < GUM_Numeric GUM_SCALAR >
77 GumIDReader< GUM_SCALAR >::~GumIDReader() {
78 GUM_DESTRUCTOR(GumIDReader)
81 template < GUM_Numeric GUM_SCALAR >
82 template <
typename JsonType >
83 Size GumIDReader< GUM_SCALAR >::_proceedFromJson_(
const JsonType& content) {
86 if (content.contains(
"type") && content[
"type"].template get< std::string >() !=
"ID") {
87 addError(
"Invalid GUM file format: expected 'ID' type, got '"
88 + content[
"type"].
template get< std::string >() +
"'",
95 if (!content.contains(
"chance") || !content.contains(
"utility") || !content.contains(
"decision")
96 || !content.contains(
"parents")) {
98 "Invalid GUM file format: missing 'chance', 'utility', 'decision' or 'parents' sections",
106 InfluenceDiagram< GUM_SCALAR > tmp;
109 for (
const auto& node: content[
"chance"]) {
110 tmp.addChanceNode(node.template get< std::string >());
113 for (
const auto& node: content[
"utility"]) {
114 tmp.addUtilityNode(node.template get< std::string >());
117 for (
const auto& node: content[
"decision"]) {
118 tmp.addDecisionNode(node.template get< std::string >());
122 tmp.beginTopologyTransformation();
123 for (
const auto& entry: content[
"parents"].items()) {
124 const auto& nodeName = entry.key();
125 for (
const auto& p: entry.value()) {
126 tmp.addArc(p.template get< std::string >(), nodeName);
129 tmp.endTopologyTransformation();
132 if (content.contains(
"cpt")) {
133 for (
const auto& entry: content[
"cpt"].items()) {
134 tmp.cpt(entry.key()).fillWith(entry.value().template get< std::vector< double > >());
139 if (content.contains(
"reward")) {
140 for (
const auto& entry: content[
"reward"].items()) {
141 tmp.utility(entry.key()).fillWith(entry.value().template get< std::vector< double > >());
146 if (content.contains(
"properties")) {
147 for (
const auto& prop: content[
"properties"].items()) {
148 tmp.setProperty(prop.key(), prop.value().template get< std::string >());
152 *_id_ = std::move(tmp);
161 template < GUM_Numeric GUM_SCALAR >
162 Size GumIDReader< GUM_SCALAR >::proceed() {
163 if (_parseDone_) {
return 0; }
164 if (_streamName_.empty()) {
166 "GumIDReader was constructed without a filename: use proceedFromString() instead "
171 std::ifstream file(_streamName_, _binary_ ? std::ios::binary : std::ios::in);
172 if (!file.is_open()) {
173 addException(
"No such file " + _streamName_, _streamName_);
178 = _binary_ ? json::from_msgpack(
_readVector_(file)) : json::parse(file, nullptr, false);
180 if (content.is_discarded()) {
181 addException(
"Error parsing file", _streamName_);
184 return _proceedFromJson_(content);
185 }
catch (
const std::exception& e) {
186 addException(std::string(
"Error reading binary file: ") + e.what(), _streamName_);
191 template < GUM_Numeric GUM_SCALAR >
192 Size GumIDReader< GUM_SCALAR >::proceedFromString(std::string_view content) {
193 if (_parseDone_) {
return 0; }
194 const auto j = json::parse(content,
nullptr,
false);
195 if (j.is_discarded()) {
196 addException(
"Invalid JSON string", _streamName_);
199 return _proceedFromJson_(j);
202 template < GUM_Numeric GUM_SCALAR >
203 void GumIDReader< GUM_SCALAR >::showElegantErrorsAndWarnings(std::ostream& stream)
const {
204 if (_parseDone_ || count() > 0) {
205 elegantErrorsAndWarnings(stream);
211 template < GUM_Numeric GUM_SCALAR >
212 void GumIDReader< GUM_SCALAR >::showErrorCounts(std::ostream& stream)
const {
213 if (_parseDone_ || count() > 0) {
214 syntheticResults(stream);
Shared binary I/O helpers for GUM (jgum/bgum) serialization.
Definition of class for reading an InfluenceDiagram from a GUM (json) file.
Base class for all aGrUM's exceptions.
GUM_NODISCARD std::string errorContent() const
Returns the message content.
GumIDReader(InfluenceDiagram< GUM_SCALAR > *id, std::string_view filename, bool binary=false)
Constructor.
Pure virtual class for importing an ID from a file.
Class representing an Influence Diagram.
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities
std::vector< uint8_t > _readVector_(std::istream &is)
Reads a length-prefixed byte vector from a binary stream (bgum format).