6 #include "boost/lexical_cast.hpp" 7 #include "boost/tokenizer.hpp" 8 #include "range/v3/algorithm/for_each.hpp" 9 #include "range/v3/view/remove_if.hpp" 10 #include "range/v3/view/reverse.hpp" 11 #include "range/v3/view/transform.hpp" 18 struct AlgorithmRepr {
25 if ( a.parent.name() != typ ) s <<
"/" << a.parent.name();
30 struct DataObjIDSorter {
59 m_algorithms = instantiateAndInitializeAlgorithms( m_producers );
63 ranges::for_each( m_algorithms | ranges::view::transform( [](
const auto& entry ) {
return entry.alg; } ) |
64 ranges::view::remove_if( [](
const auto* alg ) {
return alg->cardinality() == 0; } ),
66 this->warning() <<
"non-reentrant algorithm: " << AlgorithmRepr{*alg} <<
endmsg;
71 msg <<
"Available DataProducers: ";
73 auto ) {
return os << AlgorithmRepr{*e.
alg}; } );
78 m_dependencies = mapProducers( m_algorithms );
89 for (
AlgEntry& algEntry : m_algorithms ) {
90 ss = algEntry.alg->sysStart();
92 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
97 for (
AlgEntry& algEntry : m_cfnodes ) {
98 ss = algEntry.alg->sysStart();
100 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
113 for (
AlgEntry& algEntry : m_algorithms ) {
114 ss = algEntry.alg->sysStop();
116 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
121 for (
AlgEntry& algEntry : m_cfnodes ) {
122 ss = algEntry.alg->sysStop();
124 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
133 ranges::for_each( m_algorithms | ranges::view::transform( &AlgEntry::alg ),
135 m_algorithms.clear();
146 auto appMgr = service<IAlgManager>(
"ApplicationMgr" );
154 myIAlg = createAlgorithm( *
appMgr, theType, theName );
168 if ( sc.isFailure() ) {
183 debug() <<
"Data Dependencies for Algorithms:";
184 for (
const auto& entry : m_algorithms ) {
185 debug() <<
"\n " << entry.alg->name() <<
" :";
186 for (
const auto&
id : entry.alg->inputDataObjs() ) {
187 debug() <<
"\n o INPUT " <<
id.key();
189 for (
const auto&
id : entry.alg->outputDataObjs() ) {
190 debug() <<
"\n o OUTPUT " <<
id.key();
198 for (
AlgEntry& alg : algorithms ) {
199 const auto&
output = alg.alg->outputDataObjs();
203 for (
auto id :
output ) {
204 if (
id.key().find(
":" ) != std::string::npos ) {
205 error() <<
" in Alg " << AlgorithmRepr{*alg.alg} <<
" alternatives are NOT allowed for outputs! id: " <<
id 209 auto r = producers.
emplace(
id, &alg );
211 if ( output.size() == 1 ) {
212 error() <<
"multiple algorithms declare " <<
id <<
" as output! -- IGNORING " << AlgorithmRepr{*alg.alg}
215 error() <<
"multiple algorithms declare " <<
id <<
" as output; given that " << AlgorithmRepr{*alg.alg}
216 <<
" produces multiple outputs ";
218 error() <<
" this could lead to clashes in case any of the other " 219 "items is ever requested" 227 for (
auto& algEntry : algorithms ) {
228 auto input = sortedDataObjIDColl( algEntry.alg->inputDataObjs() );
231 if (
id.key().find(
":" ) != std::string::npos ) {
232 warning() <<
" contains alternatives which require resolution...\n";
233 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.
key(), boost::char_separator<char>{
":"}};
236 if ( itok != tokens.end() ) {
237 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
238 id.updateKey( *itok );
239 warning() <<
"Please update input to not require alternatives, and " 240 "instead properly configure the dataloader" 243 error() <<
"failed to find alternate in global output list" 244 <<
" for id: " <<
id <<
" in Alg " << algEntry.alg <<
endmsg;
247 auto iproducer = producers.
find(
id );
248 if ( iproducer != producers.
end() ) {
249 algEntry.dependsOn.insert( iproducer->second );
271 for (
const auto& req : requested ) {
273 if (
id.key().find(
":" ) != std::string::npos ) {
274 warning() << req.
key() <<
" contains alternatives which require resolution...\n";
275 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.key(), boost::char_separator<char>{
":"}};
277 [&](
DataObjID t ) {
return m_dependencies.find( t ) != m_dependencies.end(); } );
278 if ( itok != tokens.end() ) {
279 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
280 id.updateKey( *itok );
281 warning() <<
"Please update input to not require alternatives, and " 282 "instead properly configure the dataloader" 285 error() <<
"failed to find alternate in global output list" 286 <<
" for id: " <<
id <<
endmsg;
289 auto i = m_dependencies.find(
id );
290 if ( i == m_dependencies.end() )
296 for (
auto current = deps.
begin(); current != deps.
end(); ++current ) {
298 [current](
auto& stopper ) {
return ( *current )->alg->name() == stopper; } ) ) {
301 for (
auto* entry : ( *current )->dependsOn ) {
312 return {
begin( range ),
end( range )};
323 if ( alg !=
end( m_cfnodes ) && alg->alg->type() != requested.
type() ) {
324 error() <<
"requested " << requested <<
" but have matching name with different type: " << alg->alg->
type()
327 if ( alg ==
end( m_cfnodes ) ) {
328 auto av = instantiateAndInitializeAlgorithms( {requested.
type() +
'/' + requested.
name()} );
329 assert( av.size() == 1 );
330 m_cfnodes.push_back(
std::move( av.front() ) );
331 alg =
std::next( m_cfnodes.rbegin() ).base();
333 assert( alg !=
end( m_cfnodes ) );
334 assert( alg->alg !=
nullptr );
336 [&requested](
auto& stopper ) {
return requested.
name() == stopper; } ) ==
std::end( stoppers ) ) {
337 result = algorithmsRequiredFor( alg->alg->inputDataObjs(), stoppers );
341 debug() <<
std::endl <<
"requested " << requested <<
" returning " <<
std::endl <<
" ";
343 auto ) {
return os << AlgorithmRepr{*a}; } );
344 debug() << std::endl <<
endmsg;
constexpr static const auto FAILURE
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
const std::string & name() const override
The identifying name of the algorithm object.
StatusCode finalize() override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
StatusCode start() override
::details::reverse_wrapper< T > reverse(T &&iterable)
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
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...
StatusCode finalize() override
StatusCode start() override
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
StatusCode sysFinalize() override
System finalization.
decltype(auto) range(Args &&...args)
Zips multiple containers together to form a single range.
StatusCode stop() override
std::vector< Algorithm * > algorithmsRequiredFor(const DataObjIDColl &requested, const std::vector< std::string > &stoppers={}) const 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{})
Base class from which all concrete algorithm classes should be derived.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
const std::string & type() const
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.