Fawkes API  Fawkes Development Version
Plan.cpp
1 
2 /****************************************************************************
3  * Plan
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 "Plan.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 Plan::Plan(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 Plan::Plan(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 Plan::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 Plan::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 (goal_id_) {
84  rapidjson::Value v_goal_id;
85  v_goal_id.SetString(*goal_id_, allocator);
86  v.AddMember("goal-id", v_goal_id, allocator);
87  }
88  if (cost_) {
89  rapidjson::Value v_cost;
90  v_cost.SetFloat(*cost_);
91  v.AddMember("cost", v_cost, allocator);
92  }
93  rapidjson::Value v_actions(rapidjson::kArrayType);
94  v_actions.Reserve(actions_.size(), allocator);
95  for (const auto &e : actions_) {
96  rapidjson::Value v(rapidjson::kObjectType);
97  e->to_json_value(d, v);
98  v_actions.PushBack(v, allocator);
99  }
100  v.AddMember("actions", v_actions, allocator);
101 }
102 
103 void
104 Plan::from_json(const std::string &json)
105 {
106  rapidjson::Document d;
107  d.Parse(json);
108 
109  from_json_value(d);
110 }
111 
112 void
113 Plan::from_json_value(const rapidjson::Value &d)
114 {
115  if (d.HasMember("kind") && d["kind"].IsString()) {
116  kind_ = d["kind"].GetString();
117  }
118  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
119  apiVersion_ = d["apiVersion"].GetString();
120  }
121  if (d.HasMember("id") && d["id"].IsString()) {
122  id_ = d["id"].GetString();
123  }
124  if (d.HasMember("goal-id") && d["goal-id"].IsString()) {
125  goal_id_ = d["goal-id"].GetString();
126  }
127  if (d.HasMember("cost") && d["cost"].IsFloat()) {
128  cost_ = d["cost"].GetFloat();
129  }
130  if (d.HasMember("actions") && d["actions"].IsArray()) {
131  const rapidjson::Value &a = d["actions"];
132  actions_ = std::vector<std::shared_ptr<PlanAction>>{};
133  ;
134  actions_.reserve(a.Size());
135  for (auto &v : a.GetArray()) {
136  std::shared_ptr<PlanAction> nv{new PlanAction()};
137  nv->from_json_value(v);
138  actions_.push_back(std::move(nv));
139  }
140  }
141 }
142 
143 void
144 Plan::validate(bool subcall) const
145 {
146  std::vector<std::string> missing;
147  if (!kind_)
148  missing.push_back("kind");
149  if (!apiVersion_)
150  missing.push_back("apiVersion");
151  if (!id_)
152  missing.push_back("id");
153  if (!goal_id_)
154  missing.push_back("goal-id");
155  for (size_t i = 0; i < actions_.size(); ++i) {
156  if (!actions_[i]) {
157  missing.push_back("actions[" + std::to_string(i) + "]");
158  } else {
159  try {
160  actions_[i]->validate(true);
161  } catch (std::vector<std::string> &subcall_missing) {
162  for (const auto &s : subcall_missing) {
163  missing.push_back("actions[" + std::to_string(i) + "]." + s);
164  }
165  }
166  }
167  }
168 
169  if (!missing.empty()) {
170  if (subcall) {
171  throw missing;
172  } else {
173  std::ostringstream s;
174  s << "Plan is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
175  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
176  s << missing[i];
177  if (i < (missing.size() - 1)) {
178  s << ", ";
179  }
180  }
181  throw std::runtime_error(s.str());
182  }
183  }
184 }
PlanAction representation for JSON transfer.
Definition: PlanAction.h:32
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: PlanAction.cpp:149
Plan()
Constructor.
Definition: Plan.cpp:23
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Plan.cpp:61
virtual ~Plan()
Destructor.
Definition: Plan.cpp:37
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Plan.cpp:42
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Plan.cpp:104
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Plan.cpp:144
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Plan.cpp:113