Fawkes API  Fawkes Development Version
PDDLFormula.cpp
1 
2 /****************************************************************************
3  * PDDLFormula
4  * (auto-generated, do not modify directly)
5  *
6  * CLIPS Executive REST API.
7  * Enables access to goals, plans, and all items in the domain model.
8  *
9  * API Contact: Tim Niemueller <niemueller@kbsg.rwth-aachen.de>
10  * API Version: v1beta1
11  * API License: Apache 2.0
12  ****************************************************************************/
13 
14 #include "PDDLFormula.h"
15 
16 #include <rapidjson/document.h>
17 #include <rapidjson/prettywriter.h>
18 #include <rapidjson/stringbuffer.h>
19 #include <rapidjson/writer.h>
20 
21 #include <numeric>
22 #include <sstream>
23 
25 {
26 }
27 
28 PDDLFormula::PDDLFormula(const std::string &json)
29 {
30  from_json(json);
31 }
32 
33 PDDLFormula::PDDLFormula(const rapidjson::Value &v)
34 {
35  from_json_value(v);
36 }
37 
39 {
40 }
41 
42 std::string
43 PDDLFormula::to_json(bool pretty) const
44 {
45  rapidjson::Document d;
46 
47  to_json_value(d, d);
48 
49  rapidjson::StringBuffer buffer;
50  if (pretty) {
51  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
52  d.Accept(writer);
53  } else {
54  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
55  d.Accept(writer);
56  }
57 
58  return buffer.GetString();
59 }
60 
61 void
62 PDDLFormula::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
63 {
64  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
65  v.SetObject();
66  // Avoid unused variable warnings
67  (void)allocator;
68 
69  if (kind_) {
70  rapidjson::Value v_kind;
71  v_kind.SetString(*kind_, allocator);
72  v.AddMember("kind", v_kind, allocator);
73  }
74  if (apiVersion_) {
75  rapidjson::Value v_apiVersion;
76  v_apiVersion.SetString(*apiVersion_, allocator);
77  v.AddMember("apiVersion", v_apiVersion, allocator);
78  }
79  if (id_) {
80  rapidjson::Value v_id;
81  v_id.SetString(*id_, allocator);
82  v.AddMember("id", v_id, allocator);
83  }
84  if (type_) {
85  rapidjson::Value v_type;
86  v_type.SetString(*type_, allocator);
87  v.AddMember("type", v_type, allocator);
88  }
89  if (part_of_) {
90  rapidjson::Value v_part_of;
91  v_part_of.SetString(*part_of_, allocator);
92  v.AddMember("part-of", v_part_of, allocator);
93  }
94 }
95 
96 void
97 PDDLFormula::from_json(const std::string &json)
98 {
99  rapidjson::Document d;
100  d.Parse(json);
101 
102  from_json_value(d);
103 }
104 
105 void
106 PDDLFormula::from_json_value(const rapidjson::Value &d)
107 {
108  if (d.HasMember("kind") && d["kind"].IsString()) {
109  kind_ = d["kind"].GetString();
110  }
111  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
112  apiVersion_ = d["apiVersion"].GetString();
113  }
114  if (d.HasMember("id") && d["id"].IsString()) {
115  id_ = d["id"].GetString();
116  }
117  if (d.HasMember("type") && d["type"].IsString()) {
118  type_ = d["type"].GetString();
119  }
120  if (d.HasMember("part-of") && d["part-of"].IsString()) {
121  part_of_ = d["part-of"].GetString();
122  }
123 }
124 
125 void
126 PDDLFormula::validate(bool subcall) const
127 {
128  std::vector<std::string> missing;
129  if (!kind_) {
130  missing.push_back("kind");
131  }
132  if (!apiVersion_) {
133  missing.push_back("apiVersion");
134  }
135  if (!id_) {
136  missing.push_back("id");
137  }
138  if (!type_) {
139  missing.push_back("type");
140  }
141  if (!part_of_) {
142  missing.push_back("part-of");
143  }
144 
145  if (!missing.empty()) {
146  if (subcall) {
147  throw missing;
148  } else {
149  std::string s =
150  std::accumulate(std::next(missing.begin()),
151  missing.end(),
152  missing.front(),
153  [](std::string &s, const std::string &n) { return s + ", " + n; });
154  throw std::runtime_error("PDDLFormula is missing " + s);
155  }
156  }
157 }
virtual ~PDDLFormula()
Destructor.
Definition: PDDLFormula.cpp:38
PDDLFormula()
Constructor.
Definition: PDDLFormula.cpp:24
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: PDDLFormula.cpp:97
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: PDDLFormula.cpp:62
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: PDDLFormula.cpp:43