ParameterSet.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 #include <momemta/ParameterSet.h>
20 
21 #include <momemta/Unused.h>
22 
23 #include <lua/LazyTable.h>
24 #include <lua/utils.h>
25 
26 ParameterSet::ParameterSet(const std::string& module_type, const std::string& module_name) {
27  m_set.emplace("@type", Element(module_type));
28  m_set.emplace("@name", Element(module_name));
29 }
30 
31 const momemta::any& ParameterSet::rawGet(const std::string& name) const {
32  auto value = m_set.find(name);
33  if (value == m_set.end())
34  throw not_found_error("Parameter '" + name + "' not found.");
35 
36  return value->second.value;
37 }
38 
39 bool ParameterSet::lazy() const {
40  return false;
41 }
42 
43 bool ParameterSet::exists(const std::string& name) const {
44  auto value = m_set.find(name);
45  return (value != m_set.end());
46 }
47 
48 void ParameterSet::create(const std::string& name, const momemta::any& value) {
49  m_set.emplace(name, Element(value, false));
50 }
51 
52 void ParameterSet::setInternal(const std::string& name, Element& element, const momemta::any& value) {
53  UNUSED(name);
54 
55  element.value = value;
56  element.lazy = false;
57 }
58 
59 void ParameterSet::freeze() {
60  if (frozen)
61  return;
62 
63  frozen = true;
64 
65  for (auto& p: m_set) {
66  auto& element = p.second;
67  try {
68  if (element.lazy) {
69  element.lazy = false;
70  if (element.value.type() == typeid(lua::LazyFunction)) {
71  element.value = momemta::any_cast<lua::LazyFunction>(element.value)();
72  } else if (element.value.type() == typeid(lua::LazyTableField)) {
73  element.value = momemta::any_cast<lua::LazyTableField>(element.value)();
74  }
75  } else {
76  // Recursion
77  if (element.value.type() == typeid(ParameterSet)) {
78  ParameterSet& s = momemta::any_cast<ParameterSet&>(element.value);
79  s.freeze();
80  } else if (element.value.type() == typeid(std::vector<ParameterSet>)) {
81  std::vector<ParameterSet>& v = momemta::any_cast<std::vector<ParameterSet>&>(element.value);
82  for (auto& c: v)
83  c.freeze();
84  }
85  }
86  } catch(...) {
87  LOG(fatal) << "Exception while trying to parse parameter " << getModuleType() << "." << getModuleName() << "::" << p.first;
88  std::rethrow_exception(std::current_exception());
89  }
90  }
91 }
92 
93 void ParameterSet::setGlobalParameters(const ParameterSet& parameters) {
94  m_set.emplace("@global_parameters", Element(parameters, false));
95 }
96 
98  return new ParameterSet(*this);
99 }
100 
101 std::vector<std::string> ParameterSet::getNames() const {
102  std::vector<std::string> names;
103  for (const auto& it: m_set) {
104  names.push_back(it.first);
105  }
106 
107  return names;
108 }
109 
110 void ParameterSet::remove(const std::string& name) {
111  m_set.erase(name);
112 }
Lazy table field in lua (delayed table access)
Definition: LazyTable.h:35
A small wrapper around a momemta::any value.
Definition: ParameterSet.h:259
virtual void create(const std::string &name, const momemta::any &value)
Add a new element to the ParameterSet.
Definition: ParameterSet.cc:48
A class encapsulating a lua table.
Definition: ParameterSet.h:82
virtual bool lazy() const
A flag indicating if this ParameterSet lazy loads its fields or not.
Definition: ParameterSet.cc:39
virtual ParameterSet * clone() const
Clone this ParameterSet.
Definition: ParameterSet.cc:97
Lazy function in lua (delayed function evaluation)
Definition: utils.h:72
const momemta::any & rawGet(const std::string &name) const
Definition: ParameterSet.cc:31