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