Skip to content

Commit 4df9728

Browse files
authored
Merge pull request #31 from FAIRDataPipeline/dev
Dev
2 parents f3b12da + 40dbde4 commit 4df9728

File tree

13 files changed

+541
-99
lines changed

13 files changed

+541
-99
lines changed

.github/workflows/fdp_cpp_api.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,27 @@ jobs:
109109
cmake -Bbuild -DFDPAPI_BUILD_TESTS=ON
110110
- name: Compile FDP-Cpp-API
111111
run: cmake --build build --config=Release
112+
Build_GCC_4_8:
113+
name: Build GCC 4.8
114+
runs-on: ubuntu-18.04
115+
steps:
116+
- uses: actions/checkout@v2
117+
- uses: actions/setup-python@v2
118+
with:
119+
python-version: "3.9"
120+
architecture: "x64"
121+
- name: Set up GCC
122+
uses: egor-tensin/setup-gcc@v1
123+
with:
124+
version: 4.8
125+
platform: x64
126+
- name: Install Dependencies
127+
run: |
128+
sudo apt update
129+
sudo apt install -y lcov libjsoncpp-dev curl libcurl4-openssl-dev libyaml-cpp-dev gcovr
130+
- name: Configure Library
131+
run: |
132+
cmake -Bbuild
133+
- name: Build Library
134+
run: |
135+
cmake --build build

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ build/
55
test/data_store/
66
data_store/
77
.fair
8-
venv/
8+
venv/
9+
test/data/temp/

include/fdp/exceptions.hxx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,37 @@
77
namespace FairDataPipeline {
88
class config_parsing_error : public std::runtime_error {
99
public:
10-
config_parsing_error(const std::string& message)
11-
: std::runtime_error(message) {}
10+
using std::runtime_error::runtime_error;
1211
};
1312

1413
class rest_apiquery_error : public std::runtime_error {
1514
public:
16-
rest_apiquery_error(const std::string& message)
17-
: std::runtime_error(message) {}
15+
using std::runtime_error::runtime_error;
1816
};
1917

2018
class json_parse_error : public std::runtime_error {
2119
public:
22-
json_parse_error(const std::string& message) : std::runtime_error(message) {}
20+
using std::runtime_error::runtime_error;
2321
};
2422

2523
class validation_error : public std::runtime_error {
2624
public:
27-
validation_error(const std::string& message) : std::runtime_error(message) {}
25+
using std::runtime_error::runtime_error;
2826
};
2927

3028
class sync_error : public std::runtime_error {
3129
public:
32-
sync_error(const std::string& message) : std::runtime_error(message) {}
30+
using std::runtime_error::runtime_error;
3331
};
3432

3533
class write_error : public std::runtime_error {
3634
public:
37-
write_error(const std::string& message) : std::runtime_error(message) {}
35+
using std::runtime_error::runtime_error;
36+
};
37+
38+
class toml_error : public std::runtime_error {
39+
public:
40+
using std::runtime_error::runtime_error;
3841
};
3942

4043
}; // namespace FairDataPipeline

include/fdp/objects/distribution.hxx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef __FDP_DISTRIBUTION_HXX__
2+
#define __FDP_DISTRIBUTION_HXX__
3+
4+
#include <string>
5+
#include <ghc/filesystem.hpp>
6+
7+
namespace FairDataPipeline {
8+
/**
9+
* @brief Class for API objects
10+
*
11+
*/
12+
class Distribution {
13+
14+
public:
15+
Distribution(const std::string &name, double mu, double sigma):
16+
_name(name),
17+
_mu(mu),
18+
_sigma(sigma),
19+
_component("0"){};
20+
Distribution(const std::string &name, double mu, double sigma, const std::string &component):
21+
_name(name),
22+
_mu(mu),
23+
_sigma(sigma),
24+
_component(component){};
25+
explicit Distribution(const std::string &file_name);
26+
Distribution(const std::string &file_name, const std::string &component);
27+
virtual ~Distribution() = default;
28+
std::string get_name() const {return _name;}
29+
double get_mu() const {return _mu;}
30+
double get_sigma() const {return _sigma;}
31+
std::string get_component() const {return _component;}
32+
33+
std::string write_to_toml(std::string &file_name);
34+
std::string write_to_toml(std::string &component, std::string &file_name);
35+
36+
private:
37+
std::string _name;
38+
double _mu;
39+
double _sigma;
40+
std::string _component;
41+
42+
protected:
43+
void read_from_toml(const ghc::filesystem::path &file_path, const std::string &component);
44+
virtual bool isEqual(const Distribution& dist) const;
45+
friend bool operator==(const Distribution& lhs, const Distribution& rhs){
46+
return typeid(lhs) == typeid(rhs) && lhs.isEqual(rhs);
47+
}
48+
};
49+
50+
}; // namespace FairDataPipeline
51+
52+
#endif

include/fdp/registry/data_io.hxx renamed to include/fdp/utilities/data_io.hxx

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "fdp/objects/metadata.hxx"
2727
#include "fdp/utilities/logging.hxx"
2828
#include "fdp/utilities/semver.hxx"
29+
#include "fdp/exceptions.hxx"
2930

3031
namespace FairDataPipeline {
3132

@@ -44,64 +45,53 @@ namespace FairDataPipeline {
4445
* @snippet `test/test_filesystem.cxx TestTOMLPERead
4546
*
4647
*****************************************************************************/
47-
double read_point_estimate_from_toml(const ghc::filesystem::path var_address);
48+
double read_point_estimate_from_toml(const ghc::filesystem::path file_path);
49+
double read_point_estimate_from_toml(const ghc::filesystem::path file_path, const std::string &component);
50+
toml::value read_parameter_from_toml(const ghc::filesystem::path file_path,
51+
const std::string &parameter);
52+
toml::value read_parameter_from_toml(const ghc::filesystem::path file_path,
53+
const std::string &parameter,
54+
const std::string &component);
55+
toml::value read_data_from_toml(const ghc::filesystem::path file_path);
56+
toml::value read_component_from_toml(const ghc::filesystem::path file_path, const std::string &component);
57+
58+
ghc::filesystem::path write_toml_data(ghc::filesystem::path file_path,
59+
const std::string &component,
60+
const toml::value &data);
4861

4962
/**
5063
* @brief Create an estimate
5164
*
5265
* @tparam T
5366
* @param value
54-
* @param data_product
55-
* @param version_num
56-
* @param config
67+
* @param file_path
5768
* @return ghc::filesystem::path
5869
*/
5970
template <typename T>
60-
ghc::filesystem::path create_estimate(T &value,
61-
const ghc::filesystem::path &data_product,
62-
const Versioning::version &version_num,
63-
const Config *config
71+
ghc::filesystem::path write_toml_parameter(T &value,
72+
const std::string &parameter,
73+
const std::string &component,
74+
const ghc::filesystem::path file_path
6475
) {
65-
const std::string param_name_ = data_product.stem().string();
66-
const std::string namespace_ = config->get_default_output_namespace();
67-
const ghc::filesystem::path data_store_ = config->get_data_store();
68-
const toml::value data_{
69-
{param_name_, {{"type", "point-estimate"}, {"value", value}}}};
70-
71-
const ghc::filesystem::path output_filename_ =
72-
data_store_ / namespace_ / data_product.parent_path() /
73-
std::string(version_num.to_string() + ".toml");
74-
std::ofstream toml_out_;
75-
76-
if (!ghc::filesystem::exists(output_filename_.parent_path())) {
77-
ghc::filesystem::create_directories(output_filename_.parent_path());
78-
}
79-
80-
toml_out_.open(output_filename_.string());
8176

82-
if (!toml_out_) {
83-
throw std::runtime_error("Failed to open TOML file for writing");
84-
}
85-
86-
toml_out_ << toml::format(data_);
87-
88-
toml_out_.close();
77+
const toml::value data_{
78+
{component, {{"type", parameter}, {"value", value}}}};
79+
return write_toml_data(file_path, component, data_);
80+
81+
}
8982

90-
91-
auto the_logger = logger::get_logger();
92-
the_logger->debug()
93-
<< "FileSystem:CreateEstimate: Wrote point estimate to '" << output_filename_.string() << "'";
9483

95-
return output_filename_;
84+
template <typename T>
85+
ghc::filesystem::path
86+
write_point_estimate(T &value, const std::string &component,
87+
const ghc::filesystem::path file_path) {
88+
return write_toml_parameter(value, "point-estimate",
89+
component, file_path);
9690
}
9791

98-
/**
99-
* @brief Get the first key of a given toml value
100-
*
101-
* @param data_table
102-
* @return std::string
103-
*/
104-
std::string get_first_key_(const toml::value data_table);
92+
std::string get_first_component(const ghc::filesystem::path &file_path);
93+
94+
bool component_exists(const ghc::filesystem::path &file_path, const std::string &component);
10595

10696
}; // namespace FairDataPipeline
10797

include/fdp/utilities/logging.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace FairDataPipeline {
159159
Logger* _logger;
160160
enum LOG_LEVEL _msg_lvl;
161161

162-
std::ostringstream _oss;
162+
std::ostringstream _oss{""};
163163
};
164164

165165
static sptr create( enum LOG_LEVEL lvl, Sink::sptr sink, std::string name="" );

src/fdp.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "fdp/objects/config.hxx"
1010
#include "fdp/objects/metadata.hxx"
1111
#include "fdp/registry/api.hxx"
12-
#include "fdp/registry/data_io.hxx"
12+
#include "fdp/utilities/data_io.hxx"
1313
#include "fdp/utilities/logging.hxx"
1414

1515
namespace FairDataPipeline {

src/objects/distribution.cxx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "fdp/objects/distribution.hxx"
2+
#include "fdp/exceptions.hxx"
3+
#include "fdp/utilities/data_io.hxx"
4+
5+
#include <vector>
6+
7+
namespace FairDataPipeline {
8+
Distribution::Distribution(const std::string &file_name) {
9+
ghc::filesystem::path file_name_(file_name);
10+
_component = get_first_component(ghc::filesystem::path(file_name_));
11+
read_from_toml(file_name_, _component);
12+
}
13+
Distribution::Distribution(const std::string &file_name,
14+
const std::string &component)
15+
: _component(component) {
16+
ghc::filesystem::path file_name_(file_name);
17+
read_from_toml(file_name_, _component);
18+
}
19+
20+
void Distribution::read_from_toml(const ghc::filesystem::path &file_path,
21+
const std::string &component) {
22+
auto data_ = read_component_from_toml(file_path, component);
23+
if (!data_.contains("type")) {
24+
throw toml_error("Error Toml file: " + file_path.string() +
25+
" does not contain a type tag");
26+
}
27+
28+
if (data_.at("type").as_string() != "distribution") {
29+
throw toml_error("Error component: " + component +
30+
" does not contain a distribution");
31+
}
32+
33+
std::vector<std::string> required_keys_{"distribution", "mu", "sigma"};
34+
35+
for (const auto &key : required_keys_) {
36+
if (!data_.contains(key)) {
37+
throw toml_error("Error component: " + component +
38+
" does not contain a " + key);
39+
}
40+
}
41+
42+
_name = data_.at("distribution").as_string();
43+
44+
if (data_.at("mu").is_floating()) {
45+
_mu = data_.at("mu").as_floating();
46+
} else if (data_.is_integer()) {
47+
_mu = static_cast<double>(data_.at("mu").as_integer());
48+
} else {
49+
throw toml_error("Error mu value is not a number");
50+
}
51+
52+
if (data_.at("sigma").is_floating()) {
53+
_sigma = data_.at("sigma").as_floating();
54+
} else if (data_.is_integer()) {
55+
_sigma = static_cast<double>(data_.at("sigma").as_integer());
56+
} else {
57+
throw toml_error("Error sigma value is not a number");
58+
}
59+
}
60+
61+
std::string Distribution::write_to_toml(std::string &file_name) {
62+
return write_to_toml(_component, file_name);
63+
}
64+
65+
std::string Distribution::write_to_toml(std::string &component,
66+
std::string &file_name) {
67+
const toml::value data_{{component,
68+
{{"type", "distribution"},
69+
{"distribution", _name},
70+
{"mu", _mu},
71+
{"sigma", _sigma}}}};
72+
73+
return write_toml_data(ghc::filesystem::path(file_name), component, data_)
74+
.string();
75+
}
76+
77+
bool Distribution::isEqual(const Distribution &dist) const {
78+
return this->_component == dist._component && this->_mu == dist._mu &&
79+
this->_name == dist._name && this->_sigma == dist._sigma;
80+
}
81+
82+
}; // namespace FairDataPipeline

src/registry/data_io.cxx

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)