Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DefaultInterpreterPortsFactory.cpp
Go to the documentation of this file.
2
7#include "JsonParser.h"
8
9#include <filesystem>
10#include <sstream>
11#include <stdexcept>
12#include <system_error>
13
14namespace {
15
16namespace fs = std::filesystem;
17
18bool same_mapping_file(const std::string& lhs, const std::string& rhs)
19{
20 if (lhs == rhs) {
21 return true;
22 }
23
24 std::error_code ec;
25 const fs::path lhs_path(lhs);
26 const fs::path rhs_path(rhs);
27
28 if (fs::exists(lhs_path, ec) && fs::exists(rhs_path, ec)) {
29 ec.clear();
30 if (fs::equivalent(lhs_path, rhs_path, ec) && !ec) {
31 return true;
32 }
33 }
34
35 return fs::absolute(lhs_path).lexically_normal() == fs::absolute(rhs_path).lexically_normal();
36}
37
38void validate_no_mapping_collision(const IMappingDatabasePort& smDB,
39 const IMappingDatabasePort& modelDB,
40 const std::string& modelName,
41 const std::string& modelJsonPath,
42 const std::string& smJsonPath)
43{
44 if (same_mapping_file(modelJsonPath, smJsonPath)) {
45 return;
46 }
47
48 const auto smParams = smDB.getParams();
49 const auto modelParams = modelDB.getParams();
50
51 for (const auto& [name, _] : modelParams) {
52 if (smParams.contains(name)) {
53 std::ostringstream oss;
54 oss << "MARTY mapping collision for key '" << name << "' while loading model '"
55 << modelName << "'. The key exists in both the read-only SM mapping '"
56 << smJsonPath << "' and the user/model mapping '" << modelJsonPath
57 << "'. Rename the BSM key or remove the duplicate entry.";
58 throw std::runtime_error(oss.str());
59 }
60 }
61}
62
63std::string resolve_sm_mapping_path(const std::string& modelName,
64 const std::string& modelJsonPath,
65 const std::string& smJsonPath)
66{
67 if (modelName == "SM" || !same_mapping_file(modelJsonPath, smJsonPath)) {
68 return smJsonPath;
69 }
70
71 // Backward-compatibility guard: older call sites may still pass
72 // FileNameManager::getjsondbmodel() for both the model mapping and the SM
73 // mapping. When a BSM mapping is configured, that makes both databases load
74 // the BSM JSON and SM parameters such as G_F become unavailable. In that
75 // case, recover the read-only SM mapping from the same mapping directory.
76 std::error_code ec;
77 const fs::path candidate = fs::path(modelJsonPath).parent_path() / "sm.json";
78 if (fs::exists(candidate, ec) && !ec && !same_mapping_file(candidate.string(), modelJsonPath)) {
79 return candidate.string();
80 }
81
82 return smJsonPath;
83}
84
85} // namespace
86
88 std::shared_ptr<IMappingAdapterFactory> adapterFactory,
89 std::shared_ptr<IParamMappingSource> loader
90)
91: adapterFactory_(std::move(adapterFactory))
92, loader_(std::move(loader))
93{}
94
95std::unique_ptr<IParameterResolver>
97 const std::string& modelJsonPath,
98 const std::string& smJsonPath) const
99{
100 std::shared_ptr<IMappingAdapterFactory> adapter =
101 adapterFactory_ ? adapterFactory_
102 : std::make_shared<DefaultMappingAdapterFactory>();
103
104 std::shared_ptr<IParamMappingSource> src =
105 loader_ ? loader_
106 : std::make_shared<JsonParamMappingAdapter>(std::make_shared<JSONParser>());
107
108 const std::string resolvedSmJsonPath = resolve_sm_mapping_path(modelName, modelJsonPath, smJsonPath);
109
110 auto modelDB = MappingDatabaseProxy::fromFactory(*adapter, modelName, modelJsonPath, src);
111 auto smDB = MappingDatabaseProxy::fromFactory(*adapter, "SM", resolvedSmJsonPath, src);
112
113 validate_no_mapping_collision(*smDB, *modelDB, modelName, modelJsonPath, resolvedSmJsonPath);
114
115 return std::make_unique<ParameterResolverProxy>(smDB, modelDB);
116}
Declares the default factory for interpreter ports.
Factory for creating default mapping database adapters.
Adapter to load parameter mappings from a JSON/YAML-like DBNode tree.
Lightweight JSON parser/serializer for DBNode trees.
Declares a proxy implementation of IMappingDatabasePort.
Declares a proxy implementation of IParameterResolver.
DefaultInterpreterPortsFactory(std::shared_ptr< IMappingAdapterFactory > adapterFactory=nullptr, std::shared_ptr< IParamMappingSource > loader=nullptr)
std::unique_ptr< IParameterResolver > makeResolver(const std::string &modelName, const std::string &modelJsonPath, const std::string &smJsonPath) const override
Creates a new IParameterResolver for the given model.
Interface for accessing a parameter-mapping database.
virtual std::unordered_map< std::string, InterpretedParam > getParams() const =0
Returns all parameters stored in the database.
static std::shared_ptr< MappingDatabaseProxy > fromFactory(const IMappingAdapterFactory &factory, const std::string &instanceName, const std::string &jsonFilePath, std::shared_ptr< IParamMappingSource > loader=nullptr)
Factory helper to create a proxy with a given adapter factory.
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353