SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
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::Reader reader;
52 
53  reader.parse(JsonSchema::GaussianPK, schema);
54  validator.Parse(schema, path);
55 
56  // Validate inputs.
57  validator.Validate(json, path);
58  if(validator.HasErrors())
59  throw BuildException(validator.GetErrors());
60 
61  return new GaussianPK(
62  json["mu"].asDouble(),
63  json["sigma"].asDouble()
64  );
65  }
66 
67 
69 
74  RationalSwitchPK* RationalSwitchPK::Build(const Json::Value& json, const std::string& path)
75  {
76  Json::ObjectRequirement validator;
77  Json::Value schema;
78  Json::Reader reader;
79 
80  reader.parse(JsonSchema::RationalSwitchPK, schema);
81  validator.Parse(schema, path);
82 
83  // Validate inputs.
84  validator.Validate(json, path);
85  if(validator.HasErrors())
86  throw BuildException(validator.GetErrors());
87  return new RationalSwitchPK(
88  json["d0"].asDouble(),
89  json["r0"].asDouble(),
90  json["n"].asInt(),
91  json["m"].asInt()
92  );
93  }
94 }
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.
RationalSwitchPK(double d0, double r0, int n, int m)
Constructor.
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.
GaussianPK(double mu, double sigma)
Constructor.
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.