5 #include "boost/lexical_cast.hpp" 6 #include "boost/tokenizer.hpp" 7 #include "range/v3/algorithm/for_each.hpp" 8 #include "range/v3/view/remove_if.hpp" 9 #include "range/v3/view/reverse.hpp" 10 #include "range/v3/view/transform.hpp" 17 struct AlgorithmRepr {
23 if ( a.parent.name() != typ ) s <<
"/" << a.parent.name();
28 struct DataObjIDSorter {
54 m_algorithms = instantiateAndInitializeAlgorithms( m_producers );
58 ranges::for_each( m_algorithms | ranges::view::transform( [](
const auto& entry ) {
return entry.alg; } ) |
59 ranges::view::remove_if( [](
const auto* alg ) {
return alg->cardinality() == 0; } ),
61 this->warning() <<
"non-reentrant algorithm: " << AlgorithmRepr{*alg} <<
endmsg;
66 msg <<
"Available DataProducers: ";
68 msg, m_algorithms,
", ",
69 [](
auto& os,
const AlgEntry& e ) -> decltype(
auto ) {
return os << AlgorithmRepr{*e.
alg}; } );
74 m_dependencies = mapProducers( m_algorithms );
84 for (
AlgEntry& algEntry : m_algorithms ) {
85 ss = algEntry.alg->sysStart();
87 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
92 for (
AlgEntry& algEntry : m_cfnodes ) {
93 ss = algEntry.alg->sysStart();
95 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
107 for (
AlgEntry& algEntry : m_algorithms ) {
108 ss = algEntry.alg->sysStop();
110 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
115 for (
AlgEntry& algEntry : m_cfnodes ) {
116 ss = algEntry.alg->sysStop();
118 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
126 ranges::for_each( m_algorithms | ranges::view::transform( &AlgEntry::alg ),
128 m_algorithms.clear();
138 auto appMgr = service<IAlgManager>(
"ApplicationMgr" );
146 myIAlg = createAlgorithm( *
appMgr, theType, theName );
160 if ( sc.isFailure() ) {
174 debug() <<
"Data Dependencies for Algorithms:";
175 for (
const auto& entry : m_algorithms ) {
176 debug() <<
"\n " << entry.alg->name() <<
" :";
177 for (
const auto&
id : entry.alg->inputDataObjs() ) { debug() <<
"\n o INPUT " <<
id.key(); }
178 for (
const auto&
id : entry.alg->outputDataObjs() ) { debug() <<
"\n o OUTPUT " <<
id.key(); }
185 for (
AlgEntry& alg : algorithms ) {
186 const auto&
output = alg.alg->outputDataObjs();
187 if (
output.empty() ) {
continue; }
188 for (
auto id :
output ) {
189 if (
id.key().find(
":" ) != std::string::npos ) {
190 error() <<
" in Alg " << AlgorithmRepr{*alg.alg} <<
" alternatives are NOT allowed for outputs! id: " <<
id 194 auto r = producers.
emplace(
id, &alg );
196 if ( output.size() == 1 ) {
197 error() <<
"multiple algorithms declare " <<
id <<
" as output! -- IGNORING " << AlgorithmRepr{*alg.alg}
200 error() <<
"multiple algorithms declare " <<
id <<
" as output; given that " << AlgorithmRepr{*alg.alg}
201 <<
" produces multiple outputs ";
203 error() <<
" this could lead to clashes in case any of the other " 204 "items is ever requested" 212 for (
auto& algEntry : algorithms ) {
213 auto input = sortedDataObjIDColl( algEntry.alg->inputDataObjs() );
216 if (
id.key().find(
":" ) != std::string::npos ) {
217 warning() <<
" contains alternatives which require resolution...\n";
218 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.
key(), boost::char_separator<char>{
":"}};
221 if ( itok != tokens.end() ) {
222 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
223 id.updateKey( *itok );
224 warning() <<
"Please update input to not require alternatives, and " 225 "instead properly configure the dataloader" 228 error() <<
"failed to find alternate in global output list" 229 <<
" for id: " <<
id <<
" in Alg " << algEntry.alg <<
endmsg;
232 auto iproducer = producers.
find(
id );
233 if ( iproducer != producers.
end() ) {
234 algEntry.dependsOn.insert( iproducer->second );
256 for (
const auto& req : requested ) {
258 if (
id.key().find(
":" ) != std::string::npos ) {
259 warning() << req.
key() <<
" contains alternatives which require resolution...\n";
260 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.key(), boost::char_separator<char>{
":"}};
262 [&](
DataObjID t ) {
return m_dependencies.find( t ) != m_dependencies.end(); } );
263 if ( itok != tokens.end() ) {
264 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
265 id.updateKey( *itok );
266 warning() <<
"Please update input to not require alternatives, and " 267 "instead properly configure the dataloader" 270 error() <<
"failed to find alternate in global output list" 271 <<
" for id: " <<
id <<
endmsg;
274 auto i = m_dependencies.find(
id );
275 if ( i == m_dependencies.end() )
281 for (
auto current = deps.
begin(); current != deps.
end(); ++current ) {
283 [current](
auto& stopper ) {
return ( *current )->alg->name() == stopper; } ) ) {
286 for (
auto* entry : ( *current )->dependsOn ) {
297 return {
begin( range ),
end( range )};
308 if ( alg !=
end( m_cfnodes ) && alg->alg->type() != requested.
type() ) {
309 error() <<
"requested " << requested <<
" but have matching name with different type: " << alg->alg->
type()
312 if ( alg ==
end( m_cfnodes ) ) {
313 auto av = instantiateAndInitializeAlgorithms( {requested.
type() +
'/' + requested.
name()} );
314 assert( av.size() == 1 );
315 m_cfnodes.push_back(
std::move( av.front() ) );
316 alg =
std::next( m_cfnodes.rbegin() ).base();
318 assert( alg !=
end( m_cfnodes ) );
319 assert( alg->alg !=
nullptr );
321 [&requested](
auto& stopper ) {
return requested.
name() == stopper; } ) ==
std::end( stoppers ) ) {
322 result = algorithmsRequiredFor( alg->alg->inputDataObjs(), stoppers );
326 debug() <<
std::endl <<
"requested " << requested <<
" returning " <<
std::endl <<
" ";
328 debug(), result,
",\n ",
329 [](
auto& os,
const Gaudi::Algorithm* a ) -> decltype(
auto ) {
return os << AlgorithmRepr{*a}; } );
330 debug() << std::endl <<
endmsg;
Definition of the MsgStream class used to transmit messages.
StatusCode initialize() override
Define general base for Gaudi exception.
std::map< DataObjID, AlgEntry * > mapProducers(std::vector< AlgEntry > &algorithms) const
StatusCode finalize() override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
StatusCode start() override
const std::string & name() const override
The identifying name of the algorithm object.
::details::reverse_wrapper< T > reverse(T &&iterable)
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
StatusCode finalize() override
StatusCode start() override
StatusCode sysFinalize() override
System finalization.
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
StatusCode stop() override
#define DECLARE_COMPONENT(type)
Helper class to parse a string of format "type/name".
This class is used for returning status codes from appropriate routines.
std::vector< AlgEntry > instantiateAndInitializeAlgorithms(const std::vector< std::string > &names) const
const std::string & key() const
decltype(auto) range(Args &&...args)
Zips multiple containers together to form a single range.
StatusCode stop() override
The IAlgorithm is the interface implemented by the Algorithm base class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
const std::string & type() const
std::vector< Gaudi::Algorithm * > algorithmsRequiredFor(const DataObjIDColl &requested, const std::vector< std::string > &stoppers={}) const override
Base class from which all concrete algorithm classes should be derived.
virtual StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&alg, bool managed=false, bool checkIfExists=true )=0
Create an instance of a algorithm type that has been declared beforehand and assigns to it a name...
constexpr static const auto FAILURE
StatusCode initialize() override
AttribStringParser::Iterator begin(const AttribStringParser &parser)
const std::string & name() const
std::string fullKey() const
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
std::ostream & operator<<(std::ostream &str, const GaudiAlg::ID &id)
Operator overloading for ostream.