SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Private Attributes | List of all members
Json::ObjectRequirement Class Reference

Requirements on an object. More...

#include <ObjectRequirement.h>

Inheritance diagram for Json::ObjectRequirement:
Inheritance graph
[legend]

Public Member Functions

 ObjectRequirement ()
 Constructor.
 
 ~ObjectRequirement ()
 Destructor.
 
virtual void ClearErrors () override
 Clear errors on all Requirements.
 
virtual void ClearNotices () override
 Clear notices on all Requirements.
 
virtual void Reset () override
 Reset Requirement.
 
virtual void Parse (Value json, const std::string &path) override
 Parse JSON value to generate Requirement(s). More...
 
virtual void Validate (const Value &json, const std::string &path) override
 Validate JSON value. More...
 
- Public Member Functions inherited from Json::Requirement
bool HasErrors ()
 Check if errors have occured. More...
 
std::vector< std::string > GetErrors ()
 Get list of error messages. More...
 
virtual bool HasNotices ()
 Check if notices have been queued. More...
 
std::vector< std::string > GetNotices ()
 Get list of notices. More...
 
virtual ~Requirement ()
 Destructor.
 

Private Attributes

std::map< std::string, std::unique_ptr< Requirement > > properties_
 Map of properties the object needs to have.
 
std::map< std::string, std::unique_ptr< Requirement > > patternProps_
 Map of patterns the object needs to match.
 
RequireList extended_
 List of requirements.
 
std::unique_ptr< DependencyRequirementdependency_
 Dependency requirement.
 
std::vector< std::string > required_
 List of requirements.
 
bool moreProps_
 If True, more properties need to be set.
 
bool setMin_
 If True lower bound is active.
 
bool setMax_
 If True upper bound is active.
 
unsigned int min_
 Lower bound.
 
unsigned int max_
 Upper bound.
 

Additional Inherited Members

- Protected Member Functions inherited from Json::Requirement
void PushError (const std::string &error)
 Add error to list of error messages. More...
 
void PushNotice (const std::string &notice)
 Add message to list of notices. More...
 

Detailed Description

Requirements on an object.

Definition at line 46 of file ObjectRequirement.h.

Member Function Documentation

◆ Parse()

virtual void Json::ObjectRequirement::Parse ( Value  json,
const std::string &  path 
)
inlineoverridevirtual

Parse JSON value to generate Requirement(s).

Parameters
jsonJSON input value.
pathPath for JSON path specification.

Implements Json::Requirement.

Definition at line 142 of file ObjectRequirement.h.

143  {
144  Reset();
145  RequirementLoader loader;
146 
147  // Additional properties.
148  if(json.isMember("additionalProperties"))
149  {
150  if(json["additionalProperties"].isBool())
151  moreProps_ = json["additionalProperties"].asBool();
152  else if(json["additionalProperties"].isObject())
153  json["properties"]["additionalProperties"] = json["additionalProperties"];
154  }
155 
156  // properties.
157  if(json.isMember("properties") && json["properties"].isObject())
158  {
159  auto& props = json["properties"];
160  auto names = props.getMemberNames();
161  int i = 0;
162  for(auto& prop : props)
163  {
164  if(auto property = loader.LoadRequirement(prop))
165  {
166  properties_[names[i]] = std::move(property);
167  properties_[names[i]]->Parse(prop, path + "/" + names[i]);
168  }
169 
170  ++i;
171  }
172  }
173 
174  // Pattern properties. TODO: eliminate redundant code!!
175  if(json.isMember("patternProperties") && json["patternProperties"].isObject())
176  {
177  auto& props = json["patternProperties"];
178  auto names = props.getMemberNames();
179  int i = 0;
180  for(auto& prop : props)
181  {
182  if(prop.isObject())
183  {
184  if(auto property = loader.LoadRequirement(prop))
185  {
186  patternProps_[names[i]] = std::move(property);
187  patternProps_[names[i]]->Parse(prop, path + "/" + names[i]);
188  }
189  }
190 
191  ++i;
192  }
193  }
194 
195  // Required properties.
196  if(json.isMember("required") && json["required"].isArray())
197  {
198  for(auto& requirement : json["required"])
199  required_.push_back(requirement.asString());
200  }
201 
202  // Min property count.
203  if(json.isMember("minProperties") && json["minProperties"].isUInt())
204  {
205  setMin_ = true;
206  min_ = json["minProperties"].asInt();
207  }
208 
209  // Max property count.
210  if(json.isMember("maxProperties") && json["maxProperties"].isUInt())
211  {
212  setMax_ = true;
213  max_ = json["maxProperties"].asInt();
214  }
215 
216  // Dependencies
217  if(json.isMember("dependencies") && json["dependencies"].isObject())
218  {
219  dependency_ = std::unique_ptr<DependencyRequirement>(new DependencyRequirement());
220  dependency_->Parse(json["dependencies"], path);
221  }
222 
223  // Extended properties.
224  for(auto& prop : json)
225  {
226  if(auto req = loader.LoadExtended(prop))
227  {
228  extended_.push_back(std::move(req));
229  extended_.back()->Parse(prop, path);
230  }
231  }
232  }
unsigned int max_
Upper bound.
RequireList extended_
List of requirements.
virtual void Reset() override
Reset Requirement.
std::map< std::string, std::unique_ptr< Requirement > > patternProps_
Map of patterns the object needs to match.
std::vector< std::string > required_
List of requirements.
std::map< std::string, std::unique_ptr< Requirement > > properties_
Map of properties the object needs to have.
unsigned int min_
Lower bound.
std::unique_ptr< DependencyRequirement > dependency_
Dependency requirement.
bool setMax_
If True upper bound is active.
bool moreProps_
If True, more properties need to be set.
bool setMin_
If True lower bound is active.

References dependency_, extended_, Json::RequirementLoader::LoadExtended(), Json::RequirementLoader::LoadRequirement(), max_, min_, moreProps_, patternProps_, properties_, required_, Reset(), setMax_, and setMin_.

Referenced by SSAGES::ResourceHandler::Build(), SSAGES::Logger::Build(), SSAGES::ABF::Build(), SSAGES::ANN::Build(), SSAGES::BFS::Build(), SSAGES::CFF::Build(), SSAGES::ForwardFlux::Build(), SSAGES::Meta::Build(), SSAGES::StringMethod::Build(), SSAGES::Umbrella::Build(), SSAGES::AlphaRMSDCV::Build(), SSAGES::AngleCV::Build(), SSAGES::ANNCV::Build(), SSAGES::AntiBetaRMSDCV::Build(), SSAGES::BoxVolumeCV::Build(), SSAGES::GyrationTensorCV::Build(), SSAGES::PairwiseCV::Build(), SSAGES::ParallelBetaRMSDCV::Build(), SSAGES::ParticleCoordinateCV::Build(), SSAGES::ParticlePositionCV::Build(), SSAGES::ParticleSeparationCV::Build(), SSAGES::RMSDCV::Build(), SSAGES::RouseModeCV::Build(), SSAGES::TorsionalCV::Build(), SSAGES::GaussianPK::Build(), SSAGES::RationalSwitchPK::Build(), SSAGES::Chebyshev::Build(), SSAGES::Legendre::Build(), SSAGES::Fourier::Build(), SSAGES::Grid< T >::BuildGrid(), and SSAGES::Method::BuildMethod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Validate()

virtual void Json::ObjectRequirement::Validate ( const Value &  json,
const std::string &  path 
)
inlineoverridevirtual

Validate JSON value.

Parameters
jsonJSON value to be validated.
pathPath for JSON path specification.

Implements Json::Requirement.

Definition at line 239 of file ObjectRequirement.h.

240  {
241  if(!json.isObject())
242  {
243  PushError(path + ": Must be of type \"object\"");
244  return;
245  }
246 
247  if(setMin_ && json.size() < min_)
248  PushError(path + ": Object must contain at least " + std::to_string(min_) + " properties");
249 
250  if(setMax_ && json.size() > max_)
251  PushError(path + ": Object must contain at most " + std::to_string(max_) + " properties");
252 
253 
254  // Check dependencies.
255  if(dependency_ != nullptr)
256  {
257  dependency_->Validate(json, path);
258  if(dependency_->HasErrors())
259  for(const auto& error : dependency_->GetErrors())
260  PushError(error);
261  if(dependency_->HasNotices())
262  for(const auto& notice : dependency_->GetNotices())
263  PushNotice(notice);
264  }
265 
266  // Copy so we can pop items off the list.
267  auto rprops = required_;
268 
269  auto names = json.getMemberNames();
270  int i = 0;
271  for(auto& prop : json)
272  {
273  Requirement* requirement = nullptr;
274  auto it = properties_.find(names[i]);
275  if(it != properties_.end())
276  requirement = it->second.get();
277  else if(patternProps_.size() != 0)
278  {
279  for(auto& pattern : patternProps_)
280  {
281  auto regex = std::regex(pattern.first, std::regex::ECMAScript);
282  if(std::regex_search(names[i], regex))
283  requirement = pattern.second.get();
284  }
285  }
286 
287  if(!requirement && properties_.find("additionalProperties") != properties_.end())
288  requirement = properties_["additionalProperties"].get();
289 
290  if(requirement)
291  {
292  requirement->Validate(prop, path + "/" + names[i]);
293  if(requirement->HasErrors())
294  for(const auto& error : requirement->GetErrors())
295  PushError(error);
296  if(requirement->HasNotices())
297  for(const auto& notice : requirement->GetNotices())
298  PushNotice(notice);
299  }
300  else if(!moreProps_)
301  PushError(path + ": Invalid property \"" + names[i] + "\" specified");
302 
303  rprops.erase(std::remove(rprops.begin(), rprops.end(),names[i]),rprops.end());
304  ++i;
305  }
306 
307  if(required_.size() && rprops.size() != 0)
308  {
309  std::string msg = std::accumulate(rprops.begin(), rprops.end(), std::string(),
310  [](const std::string& a, const std::string& b) -> std::string {
311  return a + (a.length() > 0 ? ", " : "") + b;
312  });
313  PushError(path + ": Missing properties: " + msg);
314  }
315 
316  // Validate extended.
317  for(auto& requirement : extended_)
318  {
319  requirement->Validate(json, path);
320  if(requirement->HasErrors())
321  for(const auto& error : requirement->GetErrors())
322  PushError(error);
323  if(requirement->HasNotices())
324  for(const auto& notice : requirement->GetNotices())
325  PushNotice(notice);
326  }
327  }
void PushNotice(const std::string &notice)
Add message to list of notices.
Definition: Requirement.h:62
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53

References dependency_, extended_, Json::Requirement::GetErrors(), Json::Requirement::GetNotices(), Json::Requirement::HasErrors(), Json::Requirement::HasNotices(), max_, min_, moreProps_, patternProps_, properties_, Json::Requirement::PushError(), Json::Requirement::PushNotice(), required_, setMax_, setMin_, and Json::Requirement::Validate().

Referenced by SSAGES::ResourceHandler::Build(), SSAGES::Logger::Build(), SSAGES::ABF::Build(), SSAGES::ANN::Build(), SSAGES::BFS::Build(), SSAGES::CFF::Build(), SSAGES::ForwardFlux::Build(), SSAGES::Meta::Build(), SSAGES::StringMethod::Build(), SSAGES::Umbrella::Build(), SSAGES::AlphaRMSDCV::Build(), SSAGES::AngleCV::Build(), SSAGES::ANNCV::Build(), SSAGES::AntiBetaRMSDCV::Build(), SSAGES::BoxVolumeCV::Build(), SSAGES::GyrationTensorCV::Build(), SSAGES::PairwiseCV::Build(), SSAGES::ParallelBetaRMSDCV::Build(), SSAGES::ParticleCoordinateCV::Build(), SSAGES::ParticlePositionCV::Build(), SSAGES::ParticleSeparationCV::Build(), SSAGES::RMSDCV::Build(), SSAGES::RouseModeCV::Build(), SSAGES::TorsionalCV::Build(), SSAGES::GaussianPK::Build(), SSAGES::RationalSwitchPK::Build(), SSAGES::Chebyshev::Build(), SSAGES::Legendre::Build(), SSAGES::Fourier::Build(), SSAGES::Grid< T >::BuildGrid(), and SSAGES::Method::BuildMethod().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: