-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create infrastructure to allow alternative module types to be loaded
Provide API to allow alternative module implementation to be loaded based on the name of the type of the module given in the configuration.
- Loading branch information
Showing
7 changed files
with
282 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef FWCore_Framework_ModuleTypeResolverBase_h | ||
#define FWCore_Framework_ModuleTypeResolverBase_h | ||
// -*- C++ -*- | ||
// | ||
// Package: FWCore/Framework | ||
// Class : ModuleTypeResolverBase | ||
// | ||
/**\class edm::ModuleTypeResolverBase ModuleTypeResolverBase.h "FWCore/Framework/interface/ModuleTypeResolverBase.h" | ||
Description: Base class for deriving alternative module types to use when loading | ||
Usage: | ||
This is meant to be used as part of a do...while loop. The condition should be the rturned int is not kDone and the | ||
type returned is not what you need. | ||
*/ | ||
// | ||
// Original Author: Chris Jones | ||
// Created: Wed, 27 Apr 2022 16:21:10 GMT | ||
// | ||
|
||
// system include files | ||
#include <string> | ||
|
||
// user include files | ||
|
||
// forward declarations | ||
namespace edm { | ||
class ModuleTypeResolverBase { | ||
public: | ||
static constexpr int kInitialIndex = 0; | ||
static constexpr int kLastIndex = -1; | ||
virtual ~ModuleTypeResolverBase() = default; | ||
|
||
/**This function is meant to be called multiple times with different values for index. The first call should set index | ||
to kInitialIndex. The int returned from the function is the new index to use on next call or is a value of kLastIndex which | ||
means no further calls should be made. The returned string is the next concrete type to be used when making a call. | ||
On subsequent call, the argument basename can be the same string as returned from the previous call to the function. | ||
**/ | ||
virtual std::pair<std::string, int> resolveType(std::string basename, int index) const = 0; | ||
}; | ||
} // namespace edm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#include "catch.hpp" | ||
|
||
#include "FWCore/Framework/src/Factory.h" | ||
#include "FWCore/Framework/interface/ModuleTypeResolverBase.h" | ||
#include "FWCore/Framework/interface/global/EDProducer.h" | ||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
#include "FWCore/Framework/interface/PreallocationConfiguration.h" | ||
#include "FWCore/PluginManager/interface/PluginManager.h" | ||
#include "FWCore/PluginManager/interface/standard.h" | ||
#include "DataFormats/Provenance/interface/ProductRegistry.h" | ||
|
||
#include <iostream> | ||
|
||
namespace edm::test { | ||
class SimpleTestTypeResolver : public edm::ModuleTypeResolverBase { | ||
public: | ||
SimpleTestTypeResolver() = default; | ||
std::pair<std::string, int> resolveType(std::string basename, int index) const final { | ||
return {basename,kLastIndex}; | ||
} | ||
}; | ||
|
||
class ComplexTestTypeResolver : public edm::ModuleTypeResolverBase { | ||
public: | ||
ComplexTestTypeResolver() = default; | ||
std::pair<std::string, int> resolveType(std::string basename, int index) const final { | ||
constexpr auto kGeneric = "generic::"; | ||
constexpr auto kOther = "edm::test::other::"; | ||
constexpr auto kCPU = "edm::test::cpu::"; | ||
if(index != kInitialIndex and index != kLastIndex) { | ||
basename.replace(basename.find(kOther), strlen(kOther), kCPU); | ||
return {basename, kLastIndex}; | ||
} | ||
if(index == kInitialIndex and basename.find(kGeneric) != std::string::npos) { | ||
basename.replace(basename.find(kGeneric), strlen(kGeneric), kOther); | ||
return {basename, kInitialIndex+1}; | ||
} | ||
return {basename,kLastIndex}; | ||
} | ||
}; | ||
|
||
class FactoryTestAProd : public edm::global::EDProducer<> { | ||
public: | ||
explicit FactoryTestAProd(edm::ParameterSet const&) {} | ||
void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {} | ||
}; | ||
|
||
namespace cpu { | ||
class FactoryTestAProd : public edm::global::EDProducer<> { | ||
public: | ||
explicit FactoryTestAProd(edm::ParameterSet const&) {} | ||
void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {} | ||
}; | ||
} | ||
namespace other { | ||
class FactoryTestAProd : public edm::global::EDProducer<> { | ||
public: | ||
explicit FactoryTestAProd(edm::ParameterSet const&) {} | ||
void produce(StreamID, edm::Event&, edm::EventSetup const&) const final {} | ||
}; | ||
} | ||
} // namespace edm::test | ||
|
||
DEFINE_FWK_MODULE(edm::test::FactoryTestAProd); | ||
namespace edm::test { | ||
using FactoryTestBProd = FactoryTestAProd; | ||
} | ||
DEFINE_FWK_MODULE(edm::test::FactoryTestBProd); | ||
DEFINE_FWK_MODULE(edm::test::cpu::FactoryTestAProd); | ||
DEFINE_FWK_MODULE(edm::test::other::FactoryTestAProd); | ||
namespace edm::test::cpu { | ||
using FactoryTestCProd = FactoryTestAProd; | ||
} | ||
DEFINE_FWK_MODULE(edm::test::cpu::FactoryTestCProd); | ||
|
||
static bool called = false; | ||
using namespace edm; | ||
TEST_CASE("test edm::Factory", "[Factory]") { | ||
signalslot::Signal<void(const ModuleDescription&)> pre; | ||
signalslot::Signal<void(const ModuleDescription&)> post; | ||
ProductRegistry prodReg; | ||
PreallocationConfiguration preallocConfig; | ||
std::shared_ptr<ProcessConfiguration const> procConfig = std::make_shared<ProcessConfiguration>(); | ||
if(not called) { | ||
edmplugin::PluginManager::configure(edmplugin::standard::config()); | ||
called = true; | ||
} | ||
|
||
SECTION("test missing plugin") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "DoesNotExistModule"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
|
||
CHECK_THROWS(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), nullptr, pre,post)); | ||
try { | ||
factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), nullptr, pre,post); | ||
}catch(cms::Exception const& iE) { | ||
//std::cout <<iE.what()<<"\n"; | ||
REQUIRE(std::string(iE.what()).find("DoesNotExistModule") !=std::string::npos); | ||
} | ||
} | ||
SECTION("test missing plugin with simple resolver") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "DoesNotExistModule"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
edm::test::SimpleTestTypeResolver resolver; | ||
CHECK_THROWS(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post)); | ||
try { | ||
factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post); | ||
}catch(cms::Exception const& iE) { | ||
//std::cout <<iE.what()<<"\n"; | ||
REQUIRE(std::string(iE.what()).find("DoesNotExistModule") !=std::string::npos); | ||
} | ||
} | ||
SECTION("test missing plugin with complex resolver") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "generic::DoesNotExistModule"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
edm::test::ComplexTestTypeResolver resolver; | ||
CHECK_THROWS(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post)); | ||
try { | ||
factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post); | ||
}catch(cms::Exception const& iE) { | ||
REQUIRE(std::string(iE.what()).find("generic::DoesNotExistModule") !=std::string::npos); | ||
REQUIRE(std::string(iE.what()).find("edm::test::other::DoesNotExistModule") !=std::string::npos); | ||
REQUIRE(std::string(iE.what()).find("edm::test::cpu::DoesNotExistModule") !=std::string::npos); | ||
} | ||
} | ||
|
||
SECTION("test found plugin") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestAProd"); | ||
pset.addParameter<std::string>("@module_label", "a"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
|
||
REQUIRE(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), nullptr, pre,post)); | ||
} | ||
SECTION("test found plugin with simple resolver") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "edm::test::FactoryTestBProd"); | ||
pset.addParameter<std::string>("@module_label", "b"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
edm::test::SimpleTestTypeResolver resolver; | ||
REQUIRE(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post)); | ||
} | ||
SECTION("test found plugin with complex resolver") { | ||
SECTION("find other") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "generic::FactoryTestAProd"); | ||
pset.addParameter<std::string>("@module_label", "gen"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
edm::test::ComplexTestTypeResolver resolver; | ||
REQUIRE(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post)); | ||
} | ||
SECTION("find cpu") { | ||
auto factory = Factory::get(); | ||
ParameterSet pset; | ||
pset.addParameter<std::string>("@module_type", "generic::FactoryTestCProd"); | ||
pset.addParameter<std::string>("@module_label", "cgen"); | ||
pset.addParameter<std::string>("@module_edm_type", "EDProducer"); | ||
edm::test::ComplexTestTypeResolver resolver; | ||
REQUIRE(factory->makeModule(MakeModuleParams(&pset,prodReg,&preallocConfig, procConfig), &resolver, pre,post)); | ||
} | ||
} | ||
|
||
|
||
} |