Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
simmbasicnamemapper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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#include <ql/errors.hpp>
23
24using namespace QuantLib;
25
29using std::map;
30using std::string;
31
32namespace ore {
33namespace analytics {
34
35string SimmBasicNameMapper::qualifier(const string& externalName) const {
36 // Returns mapping if there is one found else returns the externalName
37 auto m = mapping_.find(externalName);
38 if (m != mapping_.end()) {
39 Date vt = validToDate(externalName);
40 Date vf = validFromDate(externalName);
41 Date today = Settings::instance().evaluationDate();
42 if (vt >= today && vf <= today)
43 return m->second;
44 else {
45 ALOG("Name mapping for external name " << externalName << " is expired");
46 return externalName;
47 }
48 } else {
49 return externalName;
50 }
51}
52
53string SimmBasicNameMapper::validTo(const string& externalName) const {
54 // Returns mapping expiry date string
55 auto v = validTo_.find(externalName);
56 if (v != validTo_.end())
57 return v->second;
58 else
59 return {};
60}
61
62string SimmBasicNameMapper::validFrom(const string& externalName) const {
63 // Returns mapping start date string
64 auto v = validFrom_.find(externalName);
65 if (v != validFrom_.end())
66 return v->second;
67 else
68 return {};
69}
70
71Date SimmBasicNameMapper::validToDate(const string& externalName) const {
72 // Returns mapping expiry date
73 string valid = validTo_.at(externalName);
74 if (!valid.empty())
75 return ore::data::parseDate(valid);
76 else
77 return Date::maxDate();
78}
79
80Date SimmBasicNameMapper::validFromDate(const string& externalName) const {
81 // Returns mapping start date
82 string valid = validFrom_.at(externalName);
83 if (!valid.empty())
84 return ore::data::parseDate(valid);
85 else
86 return Date::minDate();
87}
88
89bool SimmBasicNameMapper::hasQualifier(const string& externalName) const { return mapping_.count(externalName) > 0; }
90
91bool SimmBasicNameMapper::hasValidQualifier(const std::string& externalName,
92 const QuantLib::Date& referenceDate) const {
95}
96
97std::string SimmBasicNameMapper::externalName(const std::string& qualifier) const {
98 for (auto it = mapping_.begin(); it != mapping_.end(); ++it)
99 if (it->second == qualifier)
100 return it->first;
101 return qualifier;
102}
103
105 XMLUtils::checkNode(node, "SIMMNameMappings");
106
107 // Every time a call to fromXML is made, the map is cleared
108 mapping_.clear();
109
110 LOG("Start parsing SIMMNameMappings");
111
112 for (XMLNode* mappingNode = XMLUtils::getChildNode(node, "Mapping"); mappingNode;
113 mappingNode = XMLUtils::getNextSibling(mappingNode, "Mapping")) {
114
115 string validTo = XMLUtils::getChildValue(mappingNode, "ValidTo", false);
116 string validFrom = XMLUtils::getChildValue(mappingNode, "ValidFrom", false);
117 string name = XMLUtils::getChildValue(mappingNode, "Name", false);
118 string qualifier = XMLUtils::getChildValue(mappingNode, "Qualifier", false);
119 if (name == "" || qualifier == "") {
120 ALOG("skip name mapping for name '" << name << "' and qualifier '" << qualifier << "'");
121 continue;
122 }
123
124 if (validTo != "") {
125 // Check whether the provided validTo attribute is a valid date, ALERT and ignore if it is not
126 try {
128 } catch(std::exception&) {
129 ALOG("Cannot parse name mapping validTo " << validTo << " for qualifier " << qualifier << ", ignore");
130 validTo = "";
131 }
132 }
133
134 if (validFrom != "") {
135 // Check whether the provided validTo attribute is a valid date, ALERT and ignore if it is not
136 try {
138 } catch(std::exception&) {
139 ALOG("Cannot parse name mapping validFrom " << validFrom << " for qualifier " << qualifier << ", ignore");
140 validFrom = "";
141 }
142 }
143
147 }
148
149 LOG("Finished parsing SIMMNameMappings");
150}
151
153
154 LOG("Start toXML for SIMM name mappings");
155
156 XMLNode* node = doc.allocNode("SIMMNameMappings");
157 for (const auto& kv : mapping_) {
158 XMLNode* mappingNode = doc.allocNode("Mapping");
159 string vt = validTo(kv.first);
160 if (vt != "")
161 XMLUtils::addChild(doc, mappingNode, "ValidTo", vt);
162 string vf = validFrom(kv.first);
163 if (vf != "")
164 XMLUtils::addChild(doc, mappingNode, "ValidFrom", vf);
165 XMLUtils::addChild(doc, mappingNode, "Name", kv.first);
166 XMLUtils::addChild(doc, mappingNode, "Qualifier", kv.second);
167 XMLUtils::appendNode(node, mappingNode);
168 }
169
170 LOG("Finished toXML for SIMM name mappings");
171
172 return node;
173}
174
175void SimmBasicNameMapper::addMapping(const string& externalName, const string& qualifier, const std::string& validFrom,
176 const std::string& validTo) {
177 // Just overwrite if name already exists
181 if (validTo != "") {
182 try {
184 } catch (std::exception&) {
185 ALOG("Cannot parse name mapping validTo " << validTo << " for qualifier " << qualifier << ", ignore");
187 }
188 }
189 if (validFrom != "") {
190 try {
192 } catch (std::exception&) {
193 ALOG("Cannot parse name mapping validTo " << validFrom << " for qualifier " << qualifier << ", ignore");
195 }
196 }
197}
198
199} // namespace analytics
200} // namespace ore
std::string validFrom(const std::string &externalName) const
Qualifier mapping start date (ISO date), may be blank and interpreted as QL::minDate()
std::string validTo(const std::string &externalName) const
Qualifier mapping expiry (ISO date), may be blank and interpreted as QL::maxDate()
QuantLib::Date validToDate(const std::string &externalName) const
Qualifier mapping expiry (QuantLib date)
std::map< std::string, std::string > mapping_
void fromXML(ore::data::XMLNode *node) override
QuantLib::Date validFromDate(const std::string &externalName) const
Qualifier mapping startDate (QuantLib date)
std::string externalName(const std::string &qualifier) const override
reverse lookup on qualifier
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
void addMapping(const std::string &externalName, const std::string &qualifier, const std::string &validFrom="", const std::string &validTo="")
Add a single mapping.
bool hasQualifier(const std::string &externalName) const override
Has qualifier.
std::map< std::string, std::string > validFrom_
std::string qualifier(const std::string &externalName) const override
bool hasValidQualifier(const std::string &externalName, const QuantLib::Date &referenceDate) const
Has qualifier that is valid w.r.t. given reference date.
std::map< std::string, std::string > validTo_
XMLNode * allocNode(const string &nodeName)
static void checkNode(XMLNode *n, const string &expectedName)
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
Date parseDate(const string &s)
#define LOG(text)
#define ALOG(text)
Date referenceDate
Basic SIMM class for mapping names to SIMM qualifiers.
string name