SSAGES  0.8.3
Software Suite for Advanced General Ensemble Simulations
PairwiseKernel.cpp
1 
22 #include <stdexcept>
23 #include "Validator/ObjectRequirement.h"
24 #include "Drivers/DriverException.h"
25 #include "PairwiseKernel.h"
26 #include "schema.h"
27 
28 namespace SSAGES
29 {
30  PairwiseKernel* PairwiseKernel::Build(const Json::Value& json, const std::string& path)
31  {
32  auto type = json.get("type", "none").asString();
33  if(type == "gaussian")
34  return GaussianPK::Build(json, path);
35  else if(type == "rationalswitch")
36  return RationalSwitchPK::Build(json, path);
37  else
38  throw std::invalid_argument("Invalid pairwise kernel type \"" + type + "\".");
39  }
40 
42 
47  GaussianPK* GaussianPK::Build(const Json::Value& json, const std::string& path)
48  {
49  Json::ObjectRequirement validator;
50  Json::Value schema;
51  Json::CharReaderBuilder rbuilder;
52  Json::CharReader* reader = rbuilder.newCharReader();
53 
54  reader->parse(JsonSchema::GaussianPK.c_str(),
55  JsonSchema::GaussianPK.c_str() + JsonSchema::GaussianPK.size(),
56  &schema, NULL);
57  validator.Parse(schema, path);
58 
59  // Validate inputs.
60  validator.Validate(json, path);
61  if(validator.HasErrors())
62  throw BuildException(validator.GetErrors());
63 
64  return new GaussianPK(
65  json["mu"].asDouble(),
66  json["sigma"].asDouble()
67  );
68  }
69 
70 
72 
77  RationalSwitchPK* RationalSwitchPK::Build(const Json::Value& json, const std::string& path)
78  {
79  Json::ObjectRequirement validator;
80  Json::Value schema;
81  Json::CharReaderBuilder rbuilder;
82  Json::CharReader* reader = rbuilder.newCharReader();
83 
84  reader->parse(JsonSchema::RationalSwitchPK.c_str(),
85  JsonSchema::RationalSwitchPK.c_str() + JsonSchema::RationalSwitchPK.size(),
86  &schema, NULL);
87  validator.Parse(schema, path);
88 
89  // Validate inputs.
90  validator.Validate(json, path);
91  if(validator.HasErrors())
92  throw BuildException(validator.GetErrors());
93  return new RationalSwitchPK(
94  json["d0"].asDouble(),
95  json["r0"].asDouble(),
96  json["n"].asInt(),
97  json["m"].asInt()
98  );
99  }
100 }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
static RationalSwitchPK * Build(const Json::Value &json, const std::string &path)
Build RationalSwitchPK from JSON value.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
Exception to be thrown when building the Driver fails.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
static GaussianPK * Build(const Json::Value &json, const std::string &path)
Build GaussianPK from JSON value.
Requirements on an object.
Pairwise kernel base class.
Gaussian Function.
Rational Switching Function.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
static PairwiseKernel * Build(const Json::Value &json, const std::string &path)
Build PairwiseKernel from JSON value.