Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
parameters.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
22
23#include <ql/errors.hpp>
24
25using QuantLib::Date;
26using std::string;
27using std::vector;
28
29namespace ore {
30namespace analytics {
31
32bool Parameters::hasGroup(const string& groupName) const { return (data_.find(groupName) != data_.end()); }
33
34bool Parameters::has(const string& groupName, const string& paramName) const {
35 QL_REQUIRE(hasGroup(groupName), "param group '" << groupName << "' not found");
36 auto it = data_.find(groupName);
37 return (it->second.find(paramName) != it->second.end());
38}
39
40string Parameters::get(const string& groupName, const string& paramName, bool fail) const {
41 if (fail) {
42 QL_REQUIRE(has(groupName, paramName), "parameter " << paramName << " not found in param group " << groupName);
43 auto it = data_.find(groupName);
44 return it->second.find(paramName)->second;
45 } else {
46 if (!hasGroup(groupName) || !has(groupName,paramName))
47 return "";
48 else {
49 auto it = data_.find(groupName);
50 return it->second.find(paramName)->second;
51 }
52 }
53}
54
55const map<string, string>& Parameters::data(const string& groupName) const {
56 auto it = data_.find(groupName);
57 QL_REQUIRE(it != data_.end(), "param group '" << groupName << "' not found");
58 return it->second;
59}
60
61const map<string, string>& Parameters::markets() const {
62 return data("markets");
63}
64
65void Parameters::fromFile(const string& fileName) {
66 LOG("load ORE configuration from " << fileName);
67 clear();
68 XMLDocument doc(fileName);
69 fromXML(doc.getFirstNode("ORE"));
70 LOG("load ORE configuration from " << fileName << " done.");
71}
72
73void Parameters::clear() { data_.clear(); }
74
76 XMLUtils::checkNode(node, "ORE");
77
78 XMLNode* setupNode = XMLUtils::getChildNode(node, "Setup");
79 QL_REQUIRE(setupNode, "node Setup not found in parameter file");
80 map<string, string> setupMap;
81 for (XMLNode* child = XMLUtils::getChildNode(setupNode); child; child = XMLUtils::getNextSibling(child)) {
82 string key = XMLUtils::getAttribute(child, "name");
83 string value = XMLUtils::getNodeValue(child);
84 setupMap[key] = value;
85 }
86 data_["setup"] = setupMap;
87
88 XMLNode* loggingNode = XMLUtils::getChildNode(node, "Logging");
89 if (loggingNode) {
90 map<string, string> loggingMap;
91 for (XMLNode* child = XMLUtils::getChildNode(loggingNode); child; child = XMLUtils::getNextSibling(child)) {
92 string key = XMLUtils::getAttribute(child, "name");
93 string value = XMLUtils::getNodeValue(child);
94 loggingMap[key] = value;
95 }
96 data_["logging"] = loggingMap;
97 }
98
99 XMLNode* marketsNode = XMLUtils::getChildNode(node, "Markets");
100 if (marketsNode) {
101 map<string, string> marketsMap;
102 for (XMLNode* child = XMLUtils::getChildNode(marketsNode); child; child = XMLUtils::getNextSibling(child)) {
103 string key = XMLUtils::getAttribute(child, "name");
104 string value = XMLUtils::getNodeValue(child);
105 marketsMap[key] = value;
106 }
107 data_["markets"] = marketsMap;
108 }
109
110 XMLNode* analyticsNode = XMLUtils::getChildNode(node, "Analytics");
111 if (analyticsNode) {
112 for (XMLNode* child = XMLUtils::getChildNode(analyticsNode); child; child = XMLUtils::getNextSibling(child)) {
113 string groupName = XMLUtils::getAttribute(child, "type");
114 map<string, string> analyticsMap;
115 for (XMLNode* paramNode = XMLUtils::getChildNode(child); paramNode;
116 paramNode = XMLUtils::getNextSibling(paramNode)) {
117 string key = XMLUtils::getAttribute(paramNode, "name");
118 string value = XMLUtils::getNodeValue(paramNode);
119 analyticsMap[key] = value;
120 }
121 data_[groupName] = analyticsMap;
122 }
123 }
124}
125
127 XMLNode* node = doc.allocNode("ORE");
128 QL_FAIL("Parameters::toXML not implemented yet");
129 return node;
130}
131
133 LOG("Parameters:");
134 for (auto p : data_)
135 for (auto pp : p.second)
136 LOG("group = " << p.first << " : " << pp.first << " = " << pp.second);
137}
138} // namespace analytics
139} // namespace ore
void fromFile(const string &)
Definition: parameters.cpp:65
bool has(const string &groupName, const string &paramName) const
Definition: parameters.cpp:34
string get(const string &groupName, const string &paramName, bool fail=true) const
Definition: parameters.cpp:40
map< string, map< string, string > > data_
Definition: parameters.hpp:58
const map< string, string > & data(const string &groupName) const
Definition: parameters.cpp:55
const map< string, string > & markets() const
Definition: parameters.cpp:61
virtual void fromXML(XMLNode *node) override
Definition: parameters.cpp:75
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: parameters.cpp:126
bool hasGroup(const string &groupName) const
Definition: parameters.cpp:32
XMLNode * allocNode(const string &nodeName)
XMLNode * getFirstNode(const string &name) const
static string getAttribute(XMLNode *node, const string &attrName)
static void checkNode(XMLNode *n, const string &expectedName)
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static string getNodeValue(XMLNode *node)
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
SafeStack< ValueType > value
#define LOG(text)
Open Risk Engine setup and analytics choice.