Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
enginedata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
22
23using namespace QuantLib;
24
25namespace ore {
26namespace data {
27
29 modelParams_.clear();
30 model_.clear();
31 engine_.clear();
32 engineParams_.clear();
33 globalParams_.clear();
34}
35
37 XMLUtils::checkNode(root, "PricingEngines");
38
39 // Get global parameters if there are any
40 if (XMLNode* node = XMLUtils::getChildNode(root, "GlobalParameters")) {
41 DLOG("Processing the GlobalParameters node");
42 globalParams_ = XMLUtils::getChildrenAttributesAndValues(node, "Parameter", "name", false);
43 }
44
45 for (XMLNode* node = XMLUtils::getChildNode(root, "Product"); node;
46 node = XMLUtils::getNextSibling(node, "Product")) {
47 std::string productName = XMLUtils::getAttribute(node, "type");
48
49 model_[productName] = XMLUtils::getChildValue(node, "Model");
50 DLOG("EngineData product=" << productName << " model=" << model_[productName]);
51
52 XMLNode* modelParams = XMLUtils::getChildNode(node, "ModelParameters");
53 map<string, string> modelParamMap;
54 for (XMLNode* paramNode = XMLUtils::getChildNode(modelParams, "Parameter"); paramNode;
55 paramNode = XMLUtils::getNextSibling(paramNode, "Parameter")) {
56 string paramName = XMLUtils::getAttribute(paramNode, "name");
57 string paramValue = XMLUtils::getNodeValue(paramNode);
58 modelParamMap[paramName] = paramValue;
59 DLOG("EngineData product=" << productName << " paramName=" << paramName << " paramValue=" << paramValue);
60 }
61 modelParams_[productName] = modelParamMap;
62
63 engine_[productName] = XMLUtils::getChildValue(node, "Engine");
64 DLOG("EngineData product=" << productName << " engine=" << engine_[productName]);
65
66 XMLNode* engineParams = XMLUtils::getChildNode(node, "EngineParameters");
67 map<string, string> engineParamMap;
68 for (XMLNode* paramNode = XMLUtils::getChildNode(engineParams, "Parameter"); paramNode;
69 paramNode = XMLUtils::getNextSibling(paramNode, "Parameter")) {
70 string paramName = XMLUtils::getAttribute(paramNode, "name");
71 string paramValue = XMLUtils::getNodeValue(paramNode);
72 engineParamMap[paramName] = paramValue;
73 DLOG("EngineData product=" << productName << " paramName=" << paramName << " paramValue=" << paramValue);
74 }
75 engineParams_[productName] = engineParamMap;
76 }
77}
78
80
81 XMLNode* pricingEnginesNode = doc.allocNode("PricingEngines");
82
83 // Add global parameters to XML
84 XMLNode* globalParamsNode = XMLUtils::addChild(doc, pricingEnginesNode, "GlobalParameters");
85 for (const auto& kv : globalParams_) {
86 XMLNode* n = doc.allocNode("Parameter", kv.second);
87 XMLUtils::addAttribute(doc, n, "name", kv.first);
88 XMLUtils::appendNode(globalParamsNode, n);
89 TLOG("Added pair [" << kv.first << "," << kv.second << "] to the GlobalParameters node");
90 }
91
92 for (auto modelIterator = model_.begin(); modelIterator != model_.end(); modelIterator++) {
93
94 XMLNode* productNode = XMLUtils::addChild(doc, pricingEnginesNode, "Product");
95 XMLUtils::addAttribute(doc, productNode, "type", modelIterator->first);
96 auto engineIterator = engine_.find(modelIterator->first);
97 XMLUtils::addChild(doc, productNode, "Model", modelIterator->second);
98 XMLUtils::addChild(doc, productNode, "Engine", engineIterator->second);
99 XMLNode* modelParametersNode = XMLUtils::addChild(doc, productNode, "ModelParameters");
100 for (auto modelParamsIterator = modelParams_.find(modelIterator->first)->second.begin();
101 modelParamsIterator != modelParams_.find(modelIterator->first)->second.end(); modelParamsIterator++) {
102 XMLNode* parameterNode = doc.allocNode("Parameter", modelParamsIterator->second);
103 XMLUtils::appendNode(modelParametersNode, parameterNode);
104 XMLUtils::addAttribute(doc, parameterNode, "name", modelParamsIterator->first);
105 }
106 XMLNode* engineParametersNode = XMLUtils::addChild(doc, productNode, "EngineParameters");
107 for (auto engineParamsIterator = engineParams_.find(modelIterator->first)->second.begin();
108 engineParamsIterator != engineParams_.find(modelIterator->first)->second.end(); engineParamsIterator++) {
109 XMLNode* parameterNode = doc.allocNode("Parameter", engineParamsIterator->second);
110 XMLUtils::appendNode(engineParametersNode, parameterNode);
111 XMLUtils::addAttribute(doc, parameterNode, "name", engineParamsIterator->first);
112 }
113 }
114 return pricingEnginesNode;
115}
116
117// we assume all the maps have the same keys
118bool EngineData::hasProduct(const std::string& productName) { return (model_.find(productName) != model_.end()); }
119
120vector<string> EngineData::products() const {
121 vector<string> res;
122 for (auto it : model_)
123 res.push_back(it.first);
124 return res;
125}
126
127bool operator==(const EngineData& lhs, const EngineData& rhs) {
128 vector<string> products = lhs.products();
129 // this assumes they are both sorted the same
130 if (products != rhs.products())
131 return false;
132 // now loop over the products and check everything
133 for (auto product : products) {
134 if (lhs.model(product) != rhs.model(product) || lhs.modelParameters(product) != rhs.modelParameters(product) ||
135 lhs.engine(product) != rhs.engine(product) ||
136 lhs.engineParameters(product) != rhs.engineParameters(product))
137 return false;
138 }
139 return true;
140}
141
142bool operator!=(const EngineData& lhs, const EngineData& rhs) { return !(lhs == rhs); }
143
144} // namespace data
145} // namespace ore
Pricing engine description.
Definition: enginedata.hpp:41
map< string, map< string, string > > engineParams_
Definition: enginedata.hpp:83
map< string, string > model_
Definition: enginedata.hpp:80
std::map< std::string, std::string > globalParams_
Definition: enginedata.hpp:84
map< string, string > engine_
Definition: enginedata.hpp:82
bool hasProduct(const string &productName)
Definition: enginedata.cpp:118
vector< string > products() const
Return all products.
Definition: enginedata.cpp:120
virtual void fromXML(XMLNode *node) override
Definition: enginedata.cpp:36
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: enginedata.cpp:79
const map< string, string > & modelParameters(const string &productName) const
Definition: enginedata.hpp:50
void clear()
Clear all data.
Definition: enginedata.cpp:28
const map< string, string > & engineParameters(const string &productName) const
Definition: enginedata.hpp:52
const string & model(const string &productName) const
Definition: enginedata.hpp:49
map< string, map< string, string > > modelParams_
Definition: enginedata.hpp:81
const string & engine(const string &productName) const
Definition: enginedata.hpp:51
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void addAttribute(XMLDocument &doc, XMLNode *node, const string &attrName, const string &attrValue)
Definition: xmlutils.cpp:412
static string getAttribute(XMLNode *node, const string &attrName)
Definition: xmlutils.cpp:419
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static map< string, string > getChildrenAttributesAndValues(XMLNode *parent, const string &names, const string &attributeName, bool mandatory=false)
Definition: xmlutils.cpp:365
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
A class to hold pricing engine parameters.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define TLOG(text)
Logging Macro (Level = Data)
Definition: log.hpp:556
bool operator!=(const Filter &a, const Filter &b)
bool operator==(const Dividend &d1, const Dividend &d)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
XML utility functions.