Fawkes API  Fawkes Development Version
DomainPrecondition.cpp
1 
2 /****************************************************************************
3  * DomainPrecondition
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 "DomainPrecondition.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 
28 {
29  from_json(json);
30 }
31 
32 DomainPrecondition::DomainPrecondition(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 DomainPrecondition::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 DomainPrecondition::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 (name_) {
79  rapidjson::Value v_name;
80  v_name.SetString(*name_, allocator);
81  v.AddMember("name", v_name, 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 (grounded_) {
89  rapidjson::Value v_grounded;
90  v_grounded.SetBool(*grounded_);
91  v.AddMember("grounded", v_grounded, allocator);
92  }
93  if (is_satisfied_) {
94  rapidjson::Value v_is_satisfied;
95  v_is_satisfied.SetBool(*is_satisfied_);
96  v.AddMember("is-satisfied", v_is_satisfied, allocator);
97  }
98 }
99 
100 void
101 DomainPrecondition::from_json(const std::string &json)
102 {
103  rapidjson::Document d;
104  d.Parse(json);
105 
106  from_json_value(d);
107 }
108 
109 void
110 DomainPrecondition::from_json_value(const rapidjson::Value &d)
111 {
112  if (d.HasMember("kind") && d["kind"].IsString()) {
113  kind_ = d["kind"].GetString();
114  }
115  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
116  apiVersion_ = d["apiVersion"].GetString();
117  }
118  if (d.HasMember("name") && d["name"].IsString()) {
119  name_ = d["name"].GetString();
120  }
121  if (d.HasMember("type") && d["type"].IsString()) {
122  type_ = d["type"].GetString();
123  }
124  if (d.HasMember("grounded") && d["grounded"].IsBool()) {
125  grounded_ = d["grounded"].GetBool();
126  }
127  if (d.HasMember("is-satisfied") && d["is-satisfied"].IsBool()) {
128  is_satisfied_ = d["is-satisfied"].GetBool();
129  }
130 }
131 
132 void
133 DomainPrecondition::validate(bool subcall) const
134 {
135  std::vector<std::string> missing;
136  if (!kind_)
137  missing.push_back("kind");
138  if (!apiVersion_)
139  missing.push_back("apiVersion");
140  if (!name_)
141  missing.push_back("name");
142  if (!type_)
143  missing.push_back("type");
144  if (!grounded_)
145  missing.push_back("grounded");
146  if (!is_satisfied_)
147  missing.push_back("is-satisfied");
148 
149  if (!missing.empty()) {
150  if (subcall) {
151  throw missing;
152  } else {
153  std::ostringstream s;
154  s << "DomainPrecondition is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
155  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
156  s << missing[i];
157  if (i < (missing.size() - 1)) {
158  s << ", ";
159  }
160  }
161  throw std::runtime_error(s.str());
162  }
163  }
164 }
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.
DomainPrecondition()
Constructor.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
virtual ~DomainPrecondition()
Destructor.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
virtual std::string to_json(bool pretty=false) const
Render object to JSON.