-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCDataFrameAnalysisConfigReader.h
More file actions
233 lines (208 loc) · 8.58 KB
/
Copy pathCDataFrameAnalysisConfigReader.h
File metadata and controls
233 lines (208 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the following additional limitation. Functionality enabled by the
* files subject to the Elastic License 2.0 may only be used in production when
* invoked by an Elasticsearch process with a license key installed that permits
* use of machine learning features. You may not use this file except in
* compliance with the Elastic License 2.0 and the foregoing additional
* limitation.
*/
#ifndef INCLUDED_ml_api_CDataFrameAnalysisConfigReader_h
#define INCLUDED_ml_api_CDataFrameAnalysisConfigReader_h
#include <core/CLogger.h>
#include <api/ImportExport.h>
#include <boost/json.hpp>
#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
namespace json = boost::json;
namespace ml {
namespace api {
class CDataFrameAnalysisParameters;
//! \brief Reads and validates parameters for a data frame analysis.
//!
//! DESCRIPTION:\n
//! This wraps up extracting parameter values from a JSON configuration object.
//! It supports predefining a set of expected parameters. It is expected that
//! there will be one static reader object per analysis type.
//!
//! The read method extracts all the parameters the JSON contains and returns
//! a collection to get them by name.
//!
//! Expected usage is:
//! \code
//! static const CDataFrameAnalysisConfigReader reader{[] {
//! CDataFrameAnalysisConfigReader theReader;
//! theReader.addParameter("foo", CDataFrameAnalysisConfigReader::E_OptionalParameter);
//! theReader.addParameter("bar", CDataFrameAnalysisConfigReader::E_RequiredParameter);
//! }()};
//!
//! auto parameters = reader.read(json);
//! bool foo{parameters["foo"].fallback(true)};
//! double bar{parameters["bar"].as<double>()};
//! \endcode
//!
//! IMPLEMENTATION DECISIONS:\n
//! Not understanding a parameter is likely to result in the wrong analysis being
//! performed so any errors are treated as fatal causing the process to terminate
//! immediately. For the same reason this treats unexpected parameters as an error.
//! It is the calling code's responsibility to ask for the right type for a parameter.
//! If a parameter is known to exist, i.e. it is required, then it can be accessed
//! from the read value by calling as<T>(), for type T, otherwise use the fallback
//! method and provide the default value for the case it is missing.
class API_EXPORT CDataFrameAnalysisConfigReader {
public:
using TStrIntMap = std::map<std::string, int>;
enum ERequirement { E_OptionalParameter, E_RequiredParameter };
//! \brief A single parameter which has been read.
class API_EXPORT CParameter {
public:
explicit CParameter(const std::string& name) : m_Name{name} {}
CParameter(const std::string& name, const json::value& value, const TStrIntMap& permittedValues);
//! Get the name of the parameter.
const std::string& name() const { return m_Name; }
//! Get the parameter of type T.
template<typename T>
T as() const {
if (m_Value == nullptr) {
HANDLE_FATAL(<< "Input error: expected value for '" << m_Name
<< "'. Please report this problem.");
}
return this->fallback(T{});
}
//! Get a name value pair parameter.
std::pair<std::string, double> as(const std::string& name,
const std::string& value) const {
if (m_Value == nullptr) {
HANDLE_FATAL(<< "Input error: expected value for '" << m_Name
<< "'. Please report this problem.");
}
return this->fallback(name, value, std::make_pair("", 0.0));
}
//! Get the JSON object.
const json::value* jsonObject() { return m_Value; }
//! Get a boolean parameter.
bool fallback(bool value) const;
//! Get an unsigned integer parameter.
std::size_t fallback(std::size_t value) const;
//! Get a signed integer parameter.
std::ptrdiff_t fallback(std::ptrdiff_t value) const;
//! Get a floating point parameter.
double fallback(double value) const;
//! Get a string parameter.
std::string fallback(const std::string& value) const;
//! Get a (name, value) pair parameter.
std::pair<std::string, double>
fallback(const std::string& name,
const std::string& value,
const std::pair<std::string, double>& fallback) const;
//! Get an array of (name, value) pair objects.
std::vector<std::pair<std::string, double>>
fallback(const std::string& name,
const std::string& value,
const std::vector<std::pair<std::string, double>>& fallback) const;
//! Get an enum point parameter.
template<typename ENUM>
ENUM fallback(ENUM value) const {
static_assert(std::is_enum<ENUM>::value, "ENUM must be an enumeration");
if (m_Value == nullptr) {
return value;
}
if (m_Value->is_string() == false) {
this->handleFatal();
return value;
}
auto pos = m_PermittedValues->find(std::string{m_Value->as_string()});
if (pos == m_PermittedValues->end()) {
this->handleFatal();
return value;
}
return static_cast<ENUM>(pos->second);
}
//! Get an array of objects of type T.
template<typename T>
std::vector<T> fallback(const std::vector<T>& fallback) const {
if (m_Value == nullptr) {
return fallback;
}
if (m_Value->is_array() == false) {
// Try parsing as a single value.
return {this->as<T>()};
}
std::vector<T> result;
json::array arr = m_Value->as_array();
result.reserve(arr.size());
CParameter element{m_Name, SArrayElementTag{}};
for (std::size_t i = 0; i < arr.size(); ++i) {
element.m_Value = &(arr)[static_cast<int>(i)];
result.push_back(element.as<T>());
}
return result;
}
private:
struct SArrayElementTag {};
private:
CParameter(const std::string& name, SArrayElementTag);
void handleFatal() const;
private:
std::string m_Name;
const json::value* m_Value = nullptr;
const TStrIntMap* m_PermittedValues = nullptr;
bool m_ArrayElement = false;
};
public:
//! Register a parameter.
//!
//! \param[in] name The parameter name.
//! \param[in] requirement Is the parameter required or optional.
//! \param[in] permittedValues The permitted values for an enumeration.
void addParameter(const std::string& name,
ERequirement requirement,
TStrIntMap permittedValues = TStrIntMap{});
//! Extract the parameters from a JSON object.
CDataFrameAnalysisParameters read(const json::value& json) const;
private:
//! Reads a parameter from the JSON configuration object.
class API_EXPORT CParameterReader {
public:
CParameterReader(const std::string& name, ERequirement requirement, TStrIntMap permittedValues);
const std::string& name() const { return m_Name; }
bool required() const { return m_Requirement == E_RequiredParameter; }
CParameter readFrom(const json::object& json) const {
return {m_Name, json.at(m_Name), m_PermittedValues};
}
private:
std::string m_Name;
ERequirement m_Requirement;
TStrIntMap m_PermittedValues;
};
private:
std::vector<CParameterReader> m_ParameterReaders;
};
//! \brief A collection of all the data frame analysis parameters which have been read.
class API_EXPORT CDataFrameAnalysisParameters {
public:
using TParameter = CDataFrameAnalysisConfigReader::CParameter;
public:
//! Add \p parameter.
void add(const CDataFrameAnalysisConfigReader::CParameter& parameter) {
m_ParameterValues.push_back(parameter);
}
//! Get the parameter called \p name.
TParameter operator[](const std::string& name) const {
for (const auto& value : m_ParameterValues) {
if (name == value.name()) {
return value;
}
}
return TParameter{name};
}
private:
std::vector<TParameter> m_ParameterValues;
};
}
}
#endif // INCLUDED_ml_api_CDataFrameAnalysisConfigReader_h