23#include <boost/make_shared.hpp>
24#include <boost/algorithm/string/join.hpp>
26#include <ql/errors.hpp>
32std::string getParameter(
const std::map<std::string, std::string>& m,
const std::string& p,
33 const std::vector<std::string>& qs,
const bool mandatory,
const std::string& defaultValue) {
35 for (
auto const& q : qs) {
37 auto r = m.find(p +
"_" + q);
49 QL_FAIL(
"parameter " << p <<
" not found (qualifier list was \"" << boost::algorithm::join(qs,
", ") <<
"\")");
56 const bool mandatory,
const std::string& defaultValue)
const {
61 const bool mandatory,
const std::string& defaultValue)
const {
62 return getParameter(
modelParameters_, p, qualifiers, mandatory, defaultValue);
66 const bool allowOverwrite) {
67 boost::unique_lock<boost::shared_mutex> lock(
mutex_);
69 auto key = make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
71 [&key](std::function<QuantLib::ext::shared_ptr<EngineBuilder>()>& b) {
73 return key == std::make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
76 "EngineBuilderFactory::addEngineBuilder(" << tmp->model() <<
"/" << tmp->engine() <<
"/"
77 << boost::algorithm::join(tmp->tradeTypes(),
",")
78 <<
"): builder for given key already exists.");
84 const std::function<QuantLib::ext::shared_ptr<EngineBuilder>(
const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
85 const std::vector<Date>& grid)>& builder,
86 const bool allowOverwrite) {
87 boost::unique_lock<boost::shared_mutex> lock(
mutex_);
88 auto tmp = builder(
nullptr, {});
89 auto key = make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
90 auto it = std::remove_if(
92 [&key](std::function<QuantLib::ext::shared_ptr<EngineBuilder>(
const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
93 const std::vector<Date>& grid)>& b) {
94 auto tmp = b(nullptr, {});
95 return key == std::make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
97 QL_REQUIRE(it == amcEngineBuilderBuilders_.end() || allowOverwrite,
98 "EngineBuilderFactory::addAmcEngineBuilder(" << tmp->model() <<
"/" << tmp->engine() <<
"/"
99 << boost::algorithm::join(tmp->tradeTypes(),
",")
100 <<
"): builder for given key already exists.");
101 amcEngineBuilderBuilders_.erase(it, amcEngineBuilderBuilders_.end());
102 amcEngineBuilderBuilders_.push_back(builder);
106 const std::function<QuantLib::ext::shared_ptr<EngineBuilder>(
const QuantLib::ext::shared_ptr<ore::data::ModelCG>& model,
107 const std::vector<Date>& grid)>& builder,
108 const bool allowOverwrite) {
109 boost::unique_lock<boost::shared_mutex> lock(
mutex_);
110 auto tmp = builder(
nullptr, {});
111 auto key = make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
112 auto it = std::remove_if(
114 [&key](std::function<QuantLib::ext::shared_ptr<EngineBuilder>(
const QuantLib::ext::shared_ptr<ore::data::ModelCG>& model,
115 const std::vector<Date>& grid)>& b) {
116 auto tmp = b(nullptr, {});
117 return key == std::make_tuple(tmp->model(), tmp->engine(), tmp->tradeTypes());
119 QL_REQUIRE(it == amcCgEngineBuilderBuilders_.end() || allowOverwrite,
120 "EngineBuilderFactory::addAmcCgEngineBuilder(" << tmp->model() <<
"/" << tmp->engine() <<
"/"
121 << boost::algorithm::join(tmp->tradeTypes(),
",")
122 <<
"): builder for given key already exists.");
123 amcCgEngineBuilderBuilders_.erase(it, amcCgEngineBuilderBuilders_.end());
124 amcCgEngineBuilderBuilders_.push_back(builder);
128 const bool allowOverwrite) {
129 boost::unique_lock<boost::shared_mutex> lock(
mutex_);
130 auto key = builder()->legType();
133 [&key](std::function<QuantLib::ext::shared_ptr<LegBuilder>()>& b) { return key == b()->legType(); });
135 "EngineBuilderFactory::addLegBuilder(" << key <<
"): builder for given key already exists.");
141 boost::shared_lock<boost::shared_mutex> lock(
mutex_);
142 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> result;
144 result.push_back(b());
148std::vector<QuantLib::ext::shared_ptr<EngineBuilder>>
150 const std::vector<Date>& grid)
const {
151 boost::shared_lock<boost::shared_mutex> lock(
mutex_);
152 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> result;
154 result.push_back(b(cam, grid));
158std::vector<QuantLib::ext::shared_ptr<EngineBuilder>>
160 const std::vector<Date>& grid)
const {
161 boost::shared_lock<boost::shared_mutex> lock(
mutex_);
162 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> result;
164 result.push_back(b(model, grid));
169 boost::shared_lock<boost::shared_mutex> lock(
mutex_);
170 std::vector<QuantLib::ext::shared_ptr<LegBuilder>> result;
172 result.push_back(b());
177 const map<MarketContext, string>& configurations,
178 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
180 const std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> extraEngineBuilders,
181 const bool allowOverwrite)
182 : market_(market), engineData_(engineData), configurations_(configurations), referenceData_(referenceData),
183 iborFallbackConfig_(iborFallbackConfig) {
184 LOG(
"Building EngineFactory");
191 const string& modelName =
builder->model();
192 const string& engineName =
builder->engine();
193 auto key = make_tuple(modelName, engineName,
builder->tradeTypes());
197 "EngineFactory: duplicate engine builder for (" << modelName <<
"/" << engineName <<
"/"
198 << boost::algorithm::join(
builder->tradeTypes(),
",")
199 <<
") - this is an internal error.");
205 "No Pricing Engine configuration was provided for trade type " << tradeType);
208 const string& model =
engineData_->model(tradeType);
209 const string& engine =
engineData_->engine(tradeType);
210 typedef pair<tuple<string, string, set<string>>, QuantLib::ext::shared_ptr<EngineBuilder>> map_type;
211 auto pred = [&model, &engine, &tradeType](
const map_type& v) ->
bool {
212 const set<string>&
types = std::get<2>(v.first);
213 return std::get<0>(v.first) == model && std::get<1>(v.first) == engine &&
217 QL_REQUIRE(it !=
builders_.end(),
"No EngineBuilder for " << model <<
"/" << engine <<
"/" << tradeType);
219 "Ambiguous EngineBuilder for " << model <<
"/" << engine <<
"/" << tradeType);
221 QuantLib::ext::shared_ptr<EngineBuilder>
builder = it->second;
222 string effectiveTradeType = tradeType;
223 if(
auto db = QuantLib::ext::dynamic_pointer_cast<DelegatingEngineBuilder>(
builder))
224 effectiveTradeType = db->effectiveTradeType();
236 "EngineFactory duplicate leg builder for '" <<
legBuilder->legType()
237 <<
"' - this is an internal error.");
242 QL_REQUIRE(it !=
legBuilders_.end(),
"No LegBuilder for " << legType);
247 set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>> res;
249 res.insert(b.second->modelBuilders().begin(), b.second->modelBuilders().end());
255 for(
auto const& b: EngineBuilderFactory::instance().generateEngineBuilders())
257 for(
auto const& b: EngineBuilderFactory::instance().generateLegBuilders())
262 const std::vector<QuantLib::ext::shared_ptr<LegBuilder>> extraLegBuilders,
263 const bool allowOverwrite) {
265 if (extraEngineBuilders.size() > 0) {
266 DLOG(
"adding " << extraEngineBuilders.size() <<
" extra engine builders");
267 for (
auto eb : extraEngineBuilders)
270 if (extraLegBuilders.size() > 0) {
271 DLOG(
"adding " << extraLegBuilders.size() <<
" extra leg builders");
272 for (
auto elb : extraLegBuilders)
std::vector< std::function< QuantLib::ext::shared_ptr< LegBuilder >()> > legBuilderBuilders_
void addEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >()> &builder, const bool allowOverwrite=false)
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateAmcEngineBuilders(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid) const
void addAmcEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid)> &builder, const bool allowOverwrite=false)
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateEngineBuilders() const
std::vector< QuantLib::ext::shared_ptr< LegBuilder > > generateLegBuilders() const
void addAmcCgEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid)> &builder, const bool allowOverwrite=false)
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >()> > engineBuilderBuilders_
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid)> > amcEngineBuilderBuilders_
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateAmcCgEngineBuilders(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid) const
boost::shared_mutex mutex_
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid)> > amcCgEngineBuilderBuilders_
void addLegBuilder(const std::function< QuantLib::ext::shared_ptr< LegBuilder >()> &builder, const bool allowOverwrite=false)
map< string, string > engineParameters_
std::string modelParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
map< string, string > modelParameters_
QuantLib::ext::shared_ptr< Market > market_
QuantLib::ext::shared_ptr< EngineBuilder > builder(const string &tradeType)
Get a builder by trade type.
map< string, QuantLib::ext::shared_ptr< LegBuilder > > legBuilders_
QuantLib::ext::shared_ptr< LegBuilder > legBuilder(const string &legType)
Get a leg builder by leg type.
QuantLib::ext::shared_ptr< EngineData > engineData_
set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders() const
return model builders
void addDefaultBuilders()
Add a set of default engine and leg builders.
EngineFactory(const QuantLib::ext::shared_ptr< EngineData > &data, const QuantLib::ext::shared_ptr< Market > &market, const map< MarketContext, string > &configurations=std::map< MarketContext, string >(), const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData=nullptr, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig(), const std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > extraEngineBuilders={}, const bool allowOverwrite=false)
Create an engine factory.
map< MarketContext, string > configurations_
void addExtraBuilders(const std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > extraEngineBuilders, const std::vector< QuantLib::ext::shared_ptr< LegBuilder > > extraLegBuilders, const bool allowOverwrite=false)
Add a set of default engine and leg builders, overwrite existing builders with same key if specified.
map< tuple< string, string, set< string > >, QuantLib::ext::shared_ptr< EngineBuilder > > builders_
void registerBuilder(const QuantLib::ext::shared_ptr< EngineBuilder > &builder, const bool allowOverwrite=false)
Register a builder with the factory.
void registerLegBuilder(const QuantLib::ext::shared_ptr< LegBuilder > &legBuilder, const bool allowOverwrite=false)
Register a leg builder with the factory.
Classes and functions for log message handling.
#define LOG(text)
Logging Macro (Level = Notice)
#define DLOG(text)
Logging Macro (Level = Debug)
boost::bimap< std::string, TRS::FundingData::NotionalType > types
Serializable Credit Default Swap.