Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: JWiredFactoryGenerator #399

Merged
merged 4 commits into from
Jan 7, 2025
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
56 changes: 56 additions & 0 deletions src/libraries/JANA/Components/JWiredFactoryGeneratorT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2025, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.
// Created by Nathan Brei

#pragma once

#include <JANA/JFactorySet.h>
#include <JANA/JFactoryGenerator.h>
#include <JANA/Services/JWiringService.h>


namespace jana::components {

template<class FactoryT>
class JWiredFactoryGeneratorT : public JFactoryGenerator {

public:
using FactoryConfigType = typename FactoryT::ConfigType;

explicit JWiredFactoryGeneratorT() = default;

void GenerateFactories(JFactorySet *factory_set) override {

auto wiring_svc = GetApplication()->template GetService<jana::services::JWiringService>();
const auto& type_name = JTypeInfo::demangle<FactoryT>();
for (const auto* wiring : wiring_svc->GetWirings(GetPluginName(), type_name)) {

FactoryT *factory = new FactoryT;
factory->SetApplication(GetApplication());
factory->SetPluginName(this->GetPluginName());
factory->SetTypeName(type_name);

// Set the parameter values on the factory. This way, the values in the wiring file
// show up as "defaults" and the values set on the command line show up as "overrides".
factory->ConfigureAllParameters(wiring->configs);

// Check that output levels in wiring file match the factory's level
for (auto output_level : wiring->output_levels) {
if (output_level != wiring->level) {
throw JException("JOmniFactories are constrained to a single output level");
}
}

factory->PreInit(wiring->prefix,
wiring->level,
wiring->input_names,
wiring->input_levels,
wiring->output_names);

factory_set->Add(factory);
}
}
};

} // namespace jana::components
using jana::components::JWiredFactoryGeneratorT;
9 changes: 6 additions & 3 deletions src/libraries/JANA/JApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ JApplication::JApplication(JParameterManager* params) {
ProvideService(m_execution_engine);
ProvideService(std::make_shared<JGlobalRootLock>());
ProvideService(std::make_shared<JTopologyBuilder>());
ProvideService(std::make_shared<jana::services::JWiringService>());

}


Expand Down Expand Up @@ -134,9 +136,6 @@ void JApplication::Initialize() {
LOG_WARN(m_logger) << oss.str() << LOG_END;
}

// Set up wiring
ProvideService(std::make_shared<jana::services::JWiringService>());

// Attach all plugins
plugin_loader->attach_plugins(component_manager.get());

Expand All @@ -152,6 +151,10 @@ void JApplication::Initialize() {

topology_builder->create_topology();
auto execution_engine = m_service_locator->get<JExecutionEngine>();

// Make sure that Init() is called on any remaining JServices
m_service_locator->wire_everything();

m_initialized = true;
// This needs to be at the end so that m_initialized==false while InitPlugin() is being called
}
Expand Down
1 change: 1 addition & 0 deletions src/libraries/JANA/Services/JServiceLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class JServiceLocator {
std::lock_guard<std::mutex> lock(mutex);
auto svc = std::dynamic_pointer_cast<JService>(t);
assert(svc != nullptr);
svc->SetTypeName(JTypeInfo::demangle<T>());
underlying[std::type_index(typeid(T))] = svc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/JANA/Services/JWiringService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void JWiringService::AddWirings(const toml::table& table, const std::string& sou
}
auto& f = *fac.as_table();

wiring->plugin_name = f["plugin_name"].value<std::string>().value();
wiring->plugin_name = f["plugin_name"].value<std::string>().value_or("");
wiring->type_name = f["type_name"].value<std::string>().value();
wiring->prefix = f["prefix"].value<std::string>().value();

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/JANA/Services/JWiringService.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JWiringService : public JService {
};

private:
Parameter<std::string> m_wirings_input_file {this, "jana:wiring_file", "wiring.toml",
Parameter<std::string> m_wirings_input_file {this, "jana:wiring_file", "",
"Path to TOML file containing wiring definitions"};

Parameter<bool> m_strict_inheritance {this, "jana:wiring_strictness", true,
Expand Down
84 changes: 84 additions & 0 deletions src/programs/unit_tests/Services/JWiringServiceTests.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include "JANA/Components/JOmniFactory.h"
#include "JANA/Components/JWiredFactoryGeneratorT.h"
#include "JANA/JException.h"
#include "JANA/Utils/JEventLevel.h"
#include <JANA/Services/JWiringService.h>
Expand Down Expand Up @@ -179,3 +181,85 @@ TEST_CASE("WiringTests_FakeFacGen") {
REQUIRE(final_wirings[2]->configs["x"] == "27"); // from facgen only

}

struct Cluster { double x,y,E; };

struct WiredOmniFac : jana::components::JOmniFactory<WiredOmniFac> {
Input<Cluster> m_protoclusters_in {this};
Output<Cluster> m_clusters_out {this};

Parameter<int> m_x {this, "x", 1, "x"};
Parameter<std::string> m_y {this, "y", "silent", "y" };

void Configure() {
}

void ChangeRun(int32_t /*run_nr*/) {
}

void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) {

for (auto protocluster : *m_protoclusters_in) {
m_clusters_out().push_back(new Cluster {protocluster->x, protocluster->y, protocluster->E + 1});
}
}
};

static constexpr std::string_view realfacgen_wiring = R"(
[[factory]]
type_name = "WiredOmniFac"
prefix = "myfac"
input_names = ["usual_input"]
output_names = ["usual_output"]

[factory.configs]
x = "22"
y = "verbose"

[[factory]]
type_name = "WiredOmniFac"
prefix = "myfac_modified"
input_names = ["different_input"]
output_names = ["different_output"]

[factory.configs]
x = "100"
y = "silent"

)";

TEST_CASE("WiringTests_RealFacGen") {

JApplication app;

auto wiring_svc = app.GetService<jana::services::JWiringService>();
toml::table table = toml::parse(realfacgen_wiring);
wiring_svc->AddWirings(table, "testcase");

auto gen = new jana::components::JWiredFactoryGeneratorT<WiredOmniFac>;
app.Add(gen);
app.Initialize();

auto& summary = app.GetComponentSummary();
jout << summary;
auto vf = summary.FindComponents("myfac");
REQUIRE(vf.size() == 1);
REQUIRE(vf.at(0)->GetOutputs().at(0)->GetName() == "usual_output");

auto ef = summary.FindComponents("myfac_modified");
REQUIRE(ef.size() == 1);
REQUIRE(ef.at(0)->GetOutputs().at(0)->GetName() == "different_output");

// Check that parameter values propagated from wiring file to parameter manager
REQUIRE(app.GetParameterValue<int>("myfac:x") == 22);
REQUIRE(app.GetParameterValue<std::string>("myfac:y") == "verbose");
REQUIRE(app.GetParameterValue<int>("myfac_modified:x") == 100);
REQUIRE(app.GetParameterValue<std::string>("myfac_modified:y") == "silent");

//app.GetJParameterManager()->PrintParameters(2, 0);
//auto event = std::make_shared<JEvent>(&app);
//auto facs = event->GetFactorySet()->GetAllMultifactories();

}


Loading