Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions phlex/app/load_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,45 @@ namespace phlex::experimental {
}
}

namespace detail {
boost::json::object adjust_config(std::string const& label, boost::json::object raw_config)
{
raw_config["module_label"] = label;

// Automatically specify the 'pymodule' Phlex plugin if the 'py' parameter is specified
if (auto const* py = raw_config.if_contains("py")) {
if (auto const* cpp = raw_config.if_contains("cpp")) {
std::string msg = fmt::format("Both 'cpp' and 'py' parameters specified for {}", label);
if (auto const* cpp_value = cpp->if_string()) {
msg += fmt::format("\n - cpp: {}", *cpp_value);
}
if (auto const* py_value = py->if_string()) {
msg += fmt::format("\n - py: {}", *py_value);
}
throw std::runtime_error(msg);
}
raw_config["cpp"] = "pymodule";
}

return raw_config;
}
}

void load_module(framework_graph& g, std::string const& label, boost::json::object raw_config)
{
auto const& spec = value_to<std::string>(raw_config.at("plugin"));
auto const adjusted_config = detail::adjust_config(label, std::move(raw_config));

auto const& spec = value_to<std::string>(adjusted_config.at("cpp"));
auto& creator =
create_module.emplace_back(plugin_loader<detail::module_creator_t>(spec, "create_module"));
raw_config["module_label"] = label;

configuration const config{raw_config};
configuration const config{adjusted_config};
creator(g.module_proxy(config), config);
}

void load_source(framework_graph& g, std::string const& label, boost::json::object raw_config)
{
auto const& spec = value_to<std::string>(raw_config.at("plugin"));
auto const& spec = value_to<std::string>(raw_config.at("cpp"));
auto& creator =
create_source.emplace_back(plugin_loader<detail::source_creator_t>(spec, "create_source"));

Expand All @@ -76,7 +101,7 @@ namespace phlex::experimental {
detail::next_index_t load_driver(boost::json::object const& raw_config)
{
configuration const config{raw_config};
auto const& spec = config.get<std::string>("plugin");
auto const& spec = config.get<std::string>("cpp");
create_driver = plugin_loader<detail::driver_creator_t>(spec, "create_driver");
return create_driver(config);
}
Expand Down
6 changes: 6 additions & 0 deletions phlex/app/load_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#include <functional>

namespace phlex::experimental {
namespace detail {
// Adjust_config adds the module_label as a parameter, and it checks if the 'py'
// parameter exists, inserting the 'cpp: "pymodule"' configuration if necessary.
boost::json::object adjust_config(std::string const& label, boost::json::object raw_config);
}

void load_module(framework_graph& g, std::string const& label, boost::json::object config);
void load_source(framework_graph& g, std::string const& label, boost::json::object config);
detail::next_index_t load_driver(boost::json::object const& config);
Expand Down
2 changes: 1 addition & 1 deletion plugins/python/src/pymodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ PHLEX_EXPERIMENTAL_REGISTER_ALGORITHMS(m, config)

PyGILRAII g;

std::string modname = config.get<std::string>("pyplugin");
std::string modname = config.get<std::string>("py");
PyObject* mod = PyImport_ImportModule(modname.c_str());
if (mod) {
PyObject* reg = PyObject_GetAttrString(mod, "PHLEX_EXPERIMENTAL_REGISTER_ALGORITHMS");
Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ include(CetTest)
cet_test_env(SPDLOG_LEVEL=debug)

cet_test(concepts SOURCE concepts.cpp LIBRARIES phlex::core)
cet_test(
adjust_config
USE_CATCH2_MAIN
SOURCE
adjust_config.cpp
LIBRARIES
run_phlex
Boost::json
)
cet_test(config_test USE_CATCH2_MAIN SOURCE configuration.cpp LIBRARIES
phlex::configuration
)
Expand Down
61 changes: 61 additions & 0 deletions test/adjust_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "phlex/app/load_module.hpp"

#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers_string.hpp"

#include "boost/json.hpp"

using namespace phlex::experimental::detail;

TEST_CASE("Adjust empty config", "[config]")
{
auto const config = adjust_config("empty", {});
CHECK(config.at("module_label").as_string() == "empty");
}

TEST_CASE("Adjust config with py only", "[config]")
{
boost::json::object obj;
obj["py"] = "my_python_module";

auto const config = adjust_config("", std::move(obj));
CHECK(config.at("py").as_string() == "my_python_module");
CHECK(config.at("cpp").as_string() == "pymodule");
}

TEST_CASE("Both py and cpp specified as strings", "[config]")
{
boost::json::object obj;
obj["py"] = "my_python_module";
obj["cpp"] = "my_other_python_phlex_module";

auto const err_msg = R"""(Both 'cpp' and 'py' parameters specified for malformed1
- cpp: my_other_python_phlex_module
- py: my_python_module)""";
CHECK_THROWS_WITH(adjust_config("malformed1", std::move(obj)),
Catch::Matchers::ContainsSubstring(err_msg));
}

TEST_CASE("Both py and cpp specified, py as string", "[config]")
{
boost::json::object obj;
obj["py"] = "my_python_module";
obj["cpp"] = 1;

auto const err_msg = R"""(Both 'cpp' and 'py' parameters specified for malformed2
- py: my_python_module)""";
CHECK_THROWS_WITH(adjust_config("malformed2", std::move(obj)),
Catch::Matchers::ContainsSubstring(err_msg));
}

TEST_CASE("Both py and cpp specified, cpp as string", "[config]")
{
boost::json::object obj;
obj["py"] = 2;
obj["cpp"] = "my_other_python_phlex_module";

auto const err_msg = R"""(Both 'cpp' and 'py' parameters specified for malformed3
- cpp: my_other_python_phlex_module)""";
CHECK_THROWS_WITH(adjust_config("malformed3", std::move(obj)),
Catch::Matchers::ContainsSubstring(err_msg));
}
6 changes: 3 additions & 3 deletions test/benchmarks/benchmark-01.jsonnet
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
a_creator: {
plugin: 'last_index',
cpp: 'last_index',
},
},
}
8 changes: 4 additions & 4 deletions test/benchmarks/benchmark-02.jsonnet
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
a1_creator: {
plugin: 'last_index',
cpp: 'last_index',
product_name: "a1"
},
a2_creator: {
plugin: 'last_index',
cpp: 'last_index',
product_name: "a2"
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/benchmarks/benchmark-03.jsonnet
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
read_id: {
plugin: 'read_id',
cpp: 'read_id',
},
},
}
8 changes: 4 additions & 4 deletions test/benchmarks/benchmark-04.jsonnet
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
a_creator: {
plugin: 'last_index',
cpp: 'last_index',
},
read_index: {
plugin: 'read_index',
cpp: 'read_index',
consumes: { product: 'a', layer: "event" }
},
},
Expand Down
10 changes: 5 additions & 5 deletions test/benchmarks/benchmark-05.jsonnet
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
b_creator: {
plugin: 'last_index',
cpp: 'last_index',
produces: 'b',
},
c_creator: {
plugin: 'last_index',
cpp: 'last_index',
produces: 'c',
},
d: {
plugin: 'verify_difference',
cpp: 'verify_difference',
expected: 0
},
},
Expand Down
12 changes: 6 additions & 6 deletions test/benchmarks/benchmark-06.jsonnet
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
a_creator: {
plugin: 'last_index',
cpp: 'last_index',
},
b_creator: {
plugin: 'plus_one',
cpp: 'plus_one',
},
c_creator: {
plugin: 'plus_101',
cpp: 'plus_101',
},
d: {
plugin: 'verify_difference',
cpp: 'verify_difference',
},
},
}
12 changes: 6 additions & 6 deletions test/benchmarks/benchmark-07.jsonnet
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: 100000 }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
even_filter: {
plugin: 'accept_even_ids',
cpp: 'accept_even_ids',
input: { product: 'id', layer: 'event' },
},
b_creator: {
plugin: 'last_index',
cpp: 'last_index',
when: ['even_filter:accept_even_ids'],
produces: 'b',
},
c_creator: {
plugin: 'last_index',
cpp: 'last_index',
when: ['even_filter:accept_even_ids'],
produces: 'c',
},
d: {
plugin: 'verify_difference',
cpp: 'verify_difference',
expected: 0
},
},
Expand Down
12 changes: 6 additions & 6 deletions test/benchmarks/benchmark-08.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ local max_number = 100000;

{
driver: {
plugin: 'generate_layers',
cpp: 'generate_layers',
layers: {
event: { total: max_number }
}
},
sources: {
provider: {
plugin: 'benchmarks_provider'
cpp: 'benchmarks_provider'
}
},
modules: {
a_creator: {
plugin: 'last_index',
cpp: 'last_index',
produces: 'a',
},
even_filter: {
plugin: 'accept_even_numbers',
cpp: 'accept_even_numbers',
consumes: { product: 'a', layer: 'event' }
},
fibonacci_filter: {
plugin: 'accept_fibonacci_numbers',
cpp: 'accept_fibonacci_numbers',
consumes: { product: 'a', layer: "event" },
max_number: max_number,
},
d: {
plugin: 'verify_even_fibonacci_numbers',
cpp: 'verify_even_fibonacci_numbers',
when: ['even_filter:accept_even_numbers', 'fibonacci_filter:accept'],
consumes: { product: 'a', layer: "event" },
max_number: max_number,
Expand Down
Loading
Loading