ConfigurationReader.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #include <lua.hpp>
21 
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>
27 
28 #include <ExecutionPath.h>
29 #include <lua/LazyTable.h>
30 #include <lua/ParameterSetParser.h>
31 #include <lua/utils.h>
32 #include <strings/StringPiece.h>
33 
34 ConfigurationReader::ConfigurationReader(const std::string& file) :
36  // Empty
37 }
38 
39 ConfigurationReader::ConfigurationReader(const std::string& from, const ParameterSet& parameters) {
40 
41  lua_state = lua::init_runtime(this);
42 
43  lua::inject_parameters(lua_state.get(), parameters);
44 
45  momemta::StringPiece from_view = from;
46  bool from_string = from_view.Consume("!");
47 
48  if (from_string) {
49  LOG(debug) << "Parsing LUA configuration from string";
50 
51  // Parse 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;
56  }
57 
58  } else {
59  LOG(debug) << "Parsing LUA configuration from " << from;
60 
61  // Parse file
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;
66  }
67  }
68 
69  // FIXME: Find a better way of doing that
70 
71  // Read global parameters from global variable named 'parameters'
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.";
76  ParameterSetParser::parse(*configuration.global_parameters, lua_state.get(), -1);
77  }
78  lua_pop(lua_state.get(), 1);
79 
80  // Read cuba configuration
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.";
85  ParameterSetParser::parse(*configuration.cuba_configuration, lua_state.get(), -1);
86  }
87  lua_pop(lua_state.get(), 1);
88 
89  for (auto& m: configuration.modules) {
90  LOG(debug) << "Module declared: " << m.type << "::" << m.name;
91 
92  lua_getglobal(lua_state.get(), m.type.c_str());
93  lua_getfield(lua_state.get(), -1, m.name.c_str());
94 
95  m.parameters.reset(new ParameterSet());
96  ParameterSetParser::parse(*m.parameters, lua_state.get(), -1);
97 
98  lua_pop(lua_state.get(), 2);
99  }
100 }
101 
102 void ConfigurationReader::onModuleDeclared(const std::string& type, const std::string& name) {
104  module.name = name;
105  module.type = type;
106 
107  configuration.modules.push_back(module);
108 }
109 
111  configuration.integrands.push_back(tag);
112 }
113 
115  configuration.paths.push_back(std::make_shared<ExecutionPath>(path));
116 }
117 
119  configuration.n_dimensions++;
120 }
121 
122 void ConfigurationReader::onNewInputDeclared(const std::string& name) {
123  configuration.inputs.push_back(name);
124 }
125 
126 ParameterSet& ConfigurationReader::getGlobalParameters() {
127  return *configuration.global_parameters;
128 }
129 
130 ParameterSet& ConfigurationReader::getCubaConfiguration() {
131  return *configuration.cuba_configuration;
132 }
133 
135  return configuration.freeze();
136 }
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.
Definition: LazyTable.h:62
std::string name
Name of the module (user-defined from the configuration file)
Definition: Configuration.h:44
std::shared_ptr< lua_State > init_runtime(ILuaCallback *callback)
Initialize the lua runtime.
Definition: utils.cc:554
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.
Definition: utils.cc:81
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.
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
A class encapsulating a lua table.
Definition: ParameterSet.h:82
< Thrown if the configuration file is not valid
Definition: utils.h:24
A frozen snapshot of the configuration file.
Definition: Configuration.h:36
A module declaration, defined from the configuration file.
Definition: Configuration.h:43
void inject_parameters(lua_State *L, const ParameterSet &parameters)
Inject parameters into the current lua state.
Definition: utils.cc:572
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.