SSAGES  0.9.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, nullptr);
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, nullptr);
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 }
Requirements on an object.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Exception to be thrown when building the Driver fails.
Gaussian Function.
static GaussianPK * Build(const Json::Value &json, const std::string &path)
Build GaussianPK from JSON value.
GaussianPK(double mu, double sigma)
Constructor.
Pairwise kernel base class.
static PairwiseKernel * Build(const Json::Value &json, const std::string &path)
Build PairwiseKernel from JSON value.
Rational Switching Function.
RationalSwitchPK(double d0, double r0, int n, int m)
Constructor.
static RationalSwitchPK * Build(const Json::Value &json, const std::string &path)
Build RationalSwitchPK from JSON value.