22 #include <momemta/any.h> 23 #include <momemta/Logging.h> 24 #include <momemta/ConfigurationReader.h> 25 #include <momemta/ModuleFactory.h> 26 #include <momemta/ParameterSet.h> 28 #include <ExecutionPath.h> 29 #include <lua/LazyTable.h> 30 #include <lua/ParameterSetParser.h> 31 #include <lua/utils.h> 32 #include <strings/StringPiece.h> 34 ConfigurationReader::ConfigurationReader(
const std::string& file) :
39 ConfigurationReader::ConfigurationReader(
const std::string& from,
const ParameterSet& parameters) {
46 bool from_string = from_view.Consume(
"!");
49 LOG(debug) <<
"Parsing LUA configuration from string";
52 if (luaL_dostring(lua_state.get(), from_view.data())) {
53 std::string error = lua_tostring(lua_state.get(), -1);
54 LOG(fatal) <<
"Failed to parse configuration string: " << error;
59 LOG(debug) <<
"Parsing LUA configuration from " << from;
62 if (luaL_dofile(lua_state.get(), from.c_str())) {
63 std::string error = lua_tostring(lua_state.get(), -1);
64 LOG(fatal) <<
"Failed to parse configuration file: " << error;
72 configuration.global_parameters.reset(
new lua::LazyTable(lua_state,
"parameters"));
73 int type = lua_getglobal(lua_state.get(),
"parameters");
74 if (type == LUA_TTABLE) {
75 LOG(debug) <<
"Parsing global parameters.";
78 lua_pop(lua_state.get(), 1);
81 configuration.cuba_configuration.reset(
new lua::LazyTable(lua_state,
"cuba"));
82 type = lua_getglobal(lua_state.get(),
"cuba");
83 if (type == LUA_TTABLE) {
84 LOG(debug) <<
"Parsing cuba configuration.";
87 lua_pop(lua_state.get(), 1);
89 for (
auto& m: configuration.modules) {
90 LOG(debug) <<
"Module declared: " << m.type <<
"::" << m.name;
92 lua_getglobal(lua_state.get(), m.type.c_str());
93 lua_getfield(lua_state.get(), -1, m.name.c_str());
98 lua_pop(lua_state.get(), 2);
107 configuration.modules.push_back(module);
111 configuration.integrands.push_back(tag);
115 configuration.paths.push_back(std::make_shared<ExecutionPath>(path));
119 configuration.n_dimensions++;
123 configuration.inputs.push_back(name);
126 ParameterSet& ConfigurationReader::getGlobalParameters() {
127 return *configuration.global_parameters;
130 ParameterSet& ConfigurationReader::getCubaConfiguration() {
131 return *configuration.cuba_configuration;
135 return configuration.freeze();
virtual void onNewPath(const ExecutionPath &path) override
A new path is declared in the configuration file.
A specialization of ParameterSet for lazy loading of lua tables.
std::string name
Name of the module (user-defined from the configuration file)
std::shared_ptr< lua_State > init_runtime(ILuaCallback *callback)
Initialize the lua runtime.
A lua configuration file parser.
Configuration freeze() const
Freeze the configuration.
virtual void onModuleDeclared(const std::string &type, const std::string &name) override
A module is declared in the configuration file.
Type type(lua_State *L, int index)
Extract the type of a lua value.
virtual void addIntegrationDimension() override
A new integration dimension is requested in the configuration file.
static void parse(ParameterSet &p, lua_State *L, int index)
Convert a lua table to a ParameterSet.
A class encapsulating a lua table.
< Thrown if the configuration file is not valid
A frozen snapshot of the configuration file.
A module declaration, defined from the configuration file.
void inject_parameters(lua_State *L, const ParameterSet ¶meters)
Inject parameters into the current lua state.
virtual void onNewInputDeclared(const std::string &name) override
The configuration file declared a new input.
virtual void onIntegrandDeclared(const InputTag &tag) override
The integrand was defined in the configuration file.