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