Fawkes API  Fawkes Development Version
Goal.cpp
1 
2 /****************************************************************************
3  * Goal
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 "Goal.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 Goal::Goal(const std::string &json)
29 {
30  from_json(json);
31 }
32 
33 Goal::Goal(const rapidjson::Value &v)
34 {
35  from_json_value(v);
36 }
37 
39 {
40 }
41 
42 std::string
43 Goal::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 Goal::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 (sub_type_) {
90  rapidjson::Value v_sub_type;
91  v_sub_type.SetString(*sub_type_, allocator);
92  v.AddMember("sub-type", v_sub_type, allocator);
93  }
94  if (_class_) {
95  rapidjson::Value v__class;
96  v__class.SetString(*_class_, allocator);
97  v.AddMember("class", v__class, allocator);
98  }
99  if (mode_) {
100  rapidjson::Value v_mode;
101  v_mode.SetString(*mode_, allocator);
102  v.AddMember("mode", v_mode, allocator);
103  }
104  if (outcome_) {
105  rapidjson::Value v_outcome;
106  v_outcome.SetString(*outcome_, allocator);
107  v.AddMember("outcome", v_outcome, allocator);
108  }
109  rapidjson::Value v_error(rapidjson::kArrayType);
110  v_error.Reserve(error_.size(), allocator);
111  for (const auto &e : error_) {
112  rapidjson::Value v;
113  v.SetString(e, allocator);
114  v_error.PushBack(v, allocator);
115  }
116  v.AddMember("error", v_error, allocator);
117  if (message_) {
118  rapidjson::Value v_message;
119  v_message.SetString(*message_, allocator);
120  v.AddMember("message", v_message, allocator);
121  }
122  if (parent_) {
123  rapidjson::Value v_parent;
124  v_parent.SetString(*parent_, allocator);
125  v.AddMember("parent", v_parent, allocator);
126  }
127  if (priority_) {
128  rapidjson::Value v_priority;
129  v_priority.SetInt64(*priority_);
130  v.AddMember("priority", v_priority, allocator);
131  }
132  rapidjson::Value v_parameters(rapidjson::kArrayType);
133  v_parameters.Reserve(parameters_.size(), allocator);
134  for (const auto &e : parameters_) {
135  rapidjson::Value v;
136  v.SetString(e, allocator);
137  v_parameters.PushBack(v, allocator);
138  }
139  v.AddMember("parameters", v_parameters, allocator);
140  rapidjson::Value v_meta(rapidjson::kArrayType);
141  v_meta.Reserve(meta_.size(), allocator);
142  for (const auto &e : meta_) {
143  rapidjson::Value v;
144  v.SetString(e, allocator);
145  v_meta.PushBack(v, allocator);
146  }
147  v.AddMember("meta", v_meta, allocator);
148  rapidjson::Value v_plans(rapidjson::kArrayType);
149  v_plans.Reserve(plans_.size(), allocator);
150  for (const auto &e : plans_) {
151  rapidjson::Value v;
152  v.SetString(e, allocator);
153  v_plans.PushBack(v, allocator);
154  }
155  v.AddMember("plans", v_plans, allocator);
156  rapidjson::Value v_required_resources(rapidjson::kArrayType);
157  v_required_resources.Reserve(required_resources_.size(), allocator);
158  for (const auto &e : required_resources_) {
159  rapidjson::Value v;
160  v.SetString(e, allocator);
161  v_required_resources.PushBack(v, allocator);
162  }
163  v.AddMember("required-resources", v_required_resources, allocator);
164  rapidjson::Value v_acquired_resources(rapidjson::kArrayType);
165  v_acquired_resources.Reserve(acquired_resources_.size(), allocator);
166  for (const auto &e : acquired_resources_) {
167  rapidjson::Value v;
168  v.SetString(e, allocator);
169  v_acquired_resources.PushBack(v, allocator);
170  }
171  v.AddMember("acquired-resources", v_acquired_resources, allocator);
172 }
173 
174 void
175 Goal::from_json(const std::string &json)
176 {
177  rapidjson::Document d;
178  d.Parse(json);
179 
180  from_json_value(d);
181 }
182 
183 void
184 Goal::from_json_value(const rapidjson::Value &d)
185 {
186  if (d.HasMember("kind") && d["kind"].IsString()) {
187  kind_ = d["kind"].GetString();
188  }
189  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
190  apiVersion_ = d["apiVersion"].GetString();
191  }
192  if (d.HasMember("id") && d["id"].IsString()) {
193  id_ = d["id"].GetString();
194  }
195  if (d.HasMember("type") && d["type"].IsString()) {
196  type_ = d["type"].GetString();
197  }
198  if (d.HasMember("sub-type") && d["sub-type"].IsString()) {
199  sub_type_ = d["sub-type"].GetString();
200  }
201  if (d.HasMember("class") && d["class"].IsString()) {
202  _class_ = d["class"].GetString();
203  }
204  if (d.HasMember("mode") && d["mode"].IsString()) {
205  mode_ = d["mode"].GetString();
206  }
207  if (d.HasMember("outcome") && d["outcome"].IsString()) {
208  outcome_ = d["outcome"].GetString();
209  }
210  if (d.HasMember("error") && d["error"].IsArray()) {
211  const rapidjson::Value &a = d["error"];
212  error_ = std::vector<std::string>{};
213 
214  error_.reserve(a.Size());
215  for (auto &v : a.GetArray()) {
216  error_.push_back(v.GetString());
217  }
218  }
219  if (d.HasMember("message") && d["message"].IsString()) {
220  message_ = d["message"].GetString();
221  }
222  if (d.HasMember("parent") && d["parent"].IsString()) {
223  parent_ = d["parent"].GetString();
224  }
225  if (d.HasMember("priority") && d["priority"].IsInt64()) {
226  priority_ = d["priority"].GetInt64();
227  }
228  if (d.HasMember("parameters") && d["parameters"].IsArray()) {
229  const rapidjson::Value &a = d["parameters"];
230  parameters_ = std::vector<std::string>{};
231 
232  parameters_.reserve(a.Size());
233  for (auto &v : a.GetArray()) {
234  parameters_.push_back(v.GetString());
235  }
236  }
237  if (d.HasMember("meta") && d["meta"].IsArray()) {
238  const rapidjson::Value &a = d["meta"];
239  meta_ = std::vector<std::string>{};
240 
241  meta_.reserve(a.Size());
242  for (auto &v : a.GetArray()) {
243  meta_.push_back(v.GetString());
244  }
245  }
246  if (d.HasMember("plans") && d["plans"].IsArray()) {
247  const rapidjson::Value &a = d["plans"];
248  plans_ = std::vector<std::string>{};
249 
250  plans_.reserve(a.Size());
251  for (auto &v : a.GetArray()) {
252  plans_.push_back(v.GetString());
253  }
254  }
255  if (d.HasMember("required-resources") && d["required-resources"].IsArray()) {
256  const rapidjson::Value &a = d["required-resources"];
257  required_resources_ = std::vector<std::string>{};
258 
259  required_resources_.reserve(a.Size());
260  for (auto &v : a.GetArray()) {
261  required_resources_.push_back(v.GetString());
262  }
263  }
264  if (d.HasMember("acquired-resources") && d["acquired-resources"].IsArray()) {
265  const rapidjson::Value &a = d["acquired-resources"];
266  acquired_resources_ = std::vector<std::string>{};
267 
268  acquired_resources_.reserve(a.Size());
269  for (auto &v : a.GetArray()) {
270  acquired_resources_.push_back(v.GetString());
271  }
272  }
273 }
274 
275 void
276 Goal::validate(bool subcall) const
277 {
278  std::vector<std::string> missing;
279  if (!kind_) {
280  missing.push_back("kind");
281  }
282  if (!apiVersion_) {
283  missing.push_back("apiVersion");
284  }
285  if (!id_) {
286  missing.push_back("id");
287  }
288  if (!type_) {
289  missing.push_back("type");
290  }
291  if (!_class_) {
292  missing.push_back("class");
293  }
294  if (!mode_) {
295  missing.push_back("mode");
296  }
297 
298  if (!missing.empty()) {
299  if (subcall) {
300  throw missing;
301  } else {
302  std::string s =
303  std::accumulate(std::next(missing.begin()),
304  missing.end(),
305  missing.front(),
306  [](std::string &s, const std::string &n) { return s + ", " + n; });
307  throw std::runtime_error("Goal is missing " + s);
308  }
309  }
310 }
virtual ~Goal()
Destructor.
Definition: Goal.cpp:38
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Goal.cpp:276
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Goal.cpp:43
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Goal.cpp:175
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Goal.cpp:184
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Goal.cpp:62
Goal()
Constructor.
Definition: Goal.cpp:24