The Gaudi Framework  master (37c0b60a)
ToolVisitorTestLib.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "GaudiEnv.h"
12 #include "ITestTool.h"
13 #include <GaudiKernel/AlgTool.h>
14 #include <GaudiKernel/Algorithm.h>
17 #include <deque>
18 #include <sstream>
19 
20 #ifndef NDEBUG
21 # include <iostream>
22 #endif
23 
24 namespace GaudiTesting {
25  void throwConditionFailed( bool condition, const std::string& file_name, int line_no,
26  const std::string& condition_string ) {
27  if ( !condition ) {
29  msg << "Conditoin Failed: " << file_name << ":" << line_no << " " << condition_string;
30  throw std::runtime_error( msg.str() );
31  }
32  }
33 
34  std::string extractBaseName( const std::string& full_name ) {
35  std::string::size_type pos = full_name.rfind( "." );
36  std::string::size_type pos2 = full_name.rfind( "/" );
37  if ( pos == std::string::npos ) {
38  pos = pos2;
39  } else if ( pos2 > pos ) {
40  pos = pos2;
41  }
42  return pos != std::string::npos ? full_name.substr( pos + 1 ) : full_name;
43  }
44 
45  class TestTool : public extends<AlgTool, ITestTool> {
46  public:
48  : base_class( type, name, parent ) {
49  setDefaults();
50  }
51 
52  void process() const override {}
53  void setDefaults() {
54  std::string base_name( extractBaseName( name() ) );
55  DEBUG_TRACE( std::cout << "DEBUG " << name() << " setDefaults" << base_name << std::endl; );
56  if ( base_name == "tool1" ) {
57  addInput( "Renounce" );
58  addInput( "Input11" );
59  } else if ( base_name == "tool2" ) {
60  addInput( "Input21" );
61  addInput( "Input22" );
62  addTool( "tool1" );
63  } else if ( base_name == "tool3" ) {
64  addInput( "Input31" );
65  addInput( "Input32" );
66  addTool( "tool4" );
67  addTool( "tool2" );
68  } else if ( base_name == "tool4" ) {
69  addInput( "Input41" );
70  addInput( "Input42" );
71  addInput( "Renounce" );
72  } else if ( base_name == "tool5" || base_name == "tool5Bug" ) {
73  addInput( "Input51" );
74  addInput( "Input52" );
75  addOutput( "Renounce" );
76  addTool( "tool3" );
77  } else if ( base_name == "tool6" ) {
78  addInput( "Input61" );
79  addTool( "tool5" );
80  }
81  DEBUG_TRACE( std::cout << "DEBUG " << name() << " added tools:" << m_algTools.size()
82  << ", data handles:" << m_handles.size() << std::endl );
83  }
84 
85  // a do-nothing helper class which implements the logger interface
87  void renounce( [[maybe_unused]] std::string_view tool, [[maybe_unused]] std::string_view input ) override {
88  DEBUG_TRACE( std::cout << "renounce " << tool << " . " << input << std::endl );
89  }
90  };
91 
92  void addInput( const std::string& key ) {
95  this->declare( m_handles.back() );
96  }
97  void addOutput( const std::string& key ) {
100  this->declare( m_handles.back() );
101  }
102  void addTool( const std::string& name ) {
103  m_algTools.emplace_back( this, name, "GaudiTesting::TestTool/" + name, "" );
104  Ensures( this->declareTool( m_algTools.back(), m_algTools.back().typeAndName(), true ).isSuccess() );
105  }
106  StatusCode initialize() override {
107  DEBUG_TRACE( std::cout << "DEBUG " << name() << " initialize." << std::endl );
108  std::string base_name( extractBaseName( name() ) );
109  for ( auto& handle : m_algTools ) {
110  DEBUG_TRACE( std::cout << name() << " tool:" << handle.typeAndName() << std::endl );
111  handle.retrieve().ignore();
112  }
113  if ( base_name == "tool5" ) {
114  auto i = std::find_if( m_handles.begin(), m_handles.end(), []( const auto& data ) {
115  return data.mode() == Gaudi::DataHandle::Writer && data.objKey() == "Renounce";
116  } );
117  if ( i != m_handles.end() ) {
119  DEBUG_TRACE( std::cout << "DEBUG " << name() << " renounce tools "
120  << "Renounce" << std::endl );
121  auto visitor = RenounceToolInputsVisitor{ { i->fullKey() }, logger };
122  ToolVisitor::visit( tools(), visitor );
123  }
124  }
125  DEBUG_TRACE( std::cout << "DEBUG " << name() << " INITIALIZED: tools " << m_algTools.size()
126  << ", data handles:" << m_handles.size() << std::endl );
127  return StatusCode::SUCCESS;
128  }
129 
133  };
134 
135  class TestAlg : public Algorithm {
136  public:
137  TestAlg( const std::string& aname, ISvcLocator* svc_Locator ) : Algorithm( aname, svc_Locator ) {
138  Ensures( setProperty( "FilterCircularDependencies", false ).isSuccess() );
139  DEBUG_TRACE( std::cout << "DEBUG TestAlg ctor " << name() << std::endl );
140  if ( extractBaseName( name() ) == "TestAlgBug" ) {
141  addTool( "GaudiTesting::TestTool/tool5Bug" );
142  Ensures( this->setProperty( "OutputLevel", static_cast<int>( MSG::FATAL ) ).isSuccess() );
143  } else {
144  addTool( "GaudiTesting::TestTool/tool5" );
145  }
146  }
147  ~TestAlg() { DEBUG_TRACE( std::cout << "DEBUG " << name() << "::dtor" << std::endl ); }
148  StatusCode initialize() override {
149  for ( auto& handle : m_algTools ) {
150  DEBUG_TRACE( std::cout << name() << " tool:" << handle.typeAndName() << std::endl );
151  if ( handle.retrieve().isFailure() ) { return StatusCode::FAILURE; }
152  }
153  dumpData( "Renounce" );
154  return StatusCode::SUCCESS;
155  }
156  void dumpData( const std::string& pattern ) const {
157  auto dumper = [&pattern]( const IAlgTool* tool ) {
158  const AlgTool* alg_tool = dynamic_cast<const AlgTool*>( tool );
159  if ( alg_tool ) {
160  for ( Gaudi::DataHandle* handle : alg_tool->inputHandles() ) {
161  if ( handle->objKey().find( pattern ) != std::string::npos ) {
162  DEBUG_TRACE( std::cout << "DEBUG input Handle " << tool->name() << " . " << handle->objKey()
163  << std::endl );
164  }
165  }
166  for ( Gaudi::DataHandle* handle : alg_tool->outputHandles() ) {
167  if ( handle->objKey().find( pattern ) != std::string::npos ) {
168  DEBUG_TRACE( std::cout << "DEBUG output Handle " << tool->name() << " . " << handle->objKey()
169  << std::endl );
170  }
171  }
172  for ( auto elm : alg_tool->inputDataObjs() ) {
173  if ( elm.key().find( pattern ) != std::string::npos ) {
174  DEBUG_TRACE( std::cout << "DEBUGinput Handle " << tool->name() << " . " << elm.key() << std::endl );
175  }
176  }
177  }
178  };
179  ToolVisitor::visit( tools(), dumper );
180  }
181  StatusCode execute() override { return StatusCode::SUCCESS; }
182  StatusCode finalize() override {
183 #ifndef NDEBUG
184  for ( const DataObjID& const_obj_id : this->inputDataObjs() ) {
185  DEBUG_TRACE( std::cout << "DEBUG " << name() << " input:" << const_obj_id.key() << std::endl );
186  }
187  for ( const DataObjID& const_obj_id : this->outputDataObjs() ) {
188  DEBUG_TRACE( std::cout << "DEBUG " << name() << " output:" << const_obj_id.key() << std::endl );
189  }
190 #endif
191  return StatusCode::SUCCESS;
192  }
193 
194  private:
195  void addTool( const std::string& name ) {
196  m_algTools.emplace_back( this, name, "GaudiTesting::TestTool/" + name, "" );
197  Ensures( this->declareTool( m_algTools.back(), m_algTools.back().typeAndName(), true ).isSuccess() );
198  }
200  };
201 
204 } // namespace GaudiTesting
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
GaudiTesting::TestTool::addInput
void addInput(const std::string &key)
Definition: ToolVisitorTestLib.cpp:92
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiTesting::TestTool::setDefaults
void setDefaults()
Definition: ToolVisitorTestLib.cpp:53
AlgTool::parent
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition: AlgTool.cpp:81
GaudiTesting::TestTool::addOutput
void addOutput(const std::string &key)
Definition: ToolVisitorTestLib.cpp:97
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:175
std::vector< DataObjID >
std::find_if
T find_if(T... args)
std::deque::size
T size(T... args)
PropertyHolder< CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > > >::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p)
Set the property from a property.
Definition: IProperty.h:39
GaudiTesting
Definition: __init__.py:1
GaudiTesting::TestAlg::initialize
StatusCode initialize() override
Definition: ToolVisitorTestLib.cpp:148
ISvcLocator
Definition: ISvcLocator.h:46
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
Algorithm.h
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::stringstream
STL class.
Gaudi::DataHandle
Definition: DataHandle.h:38
AlgTool::type
const std::string & type() const override
Retrieve type (concrete class) of the sub-algtool.
Definition: AlgTool.cpp:74
GaudiTesting::extractBaseName
std::string extractBaseName(const std::string &full_name)
Definition: ToolVisitorTestLib.cpp:34
DataHandleHolderBase::outputHandles
std::vector< Gaudi::DataHandle * > outputHandles() const override
Definition: DataHandleHolderBase.h:41
std::vector::back
T back(T... args)
GaudiTesting::TestTool::process
void process() const override
Definition: ToolVisitorTestLib.cpp:52
AlgTool::name
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:67
GaudiTesting::TestTool::m_algTools
std::deque< ToolHandle< ITestTool > > m_algTools
Definition: ToolVisitorTestLib.cpp:132
Gaudi::DataHandle::Writer
@ Writer
Definition: DataHandle.h:40
DataHandleHolderBase< PropertyHolder< CommonMessaging< implements< IAlgTool, IDataHandleHolder, IProperty, IStateful > > > >::declare
void declare(Gaudi::DataHandle &handle) override
Definition: DataHandleHolderBase.h:48
ITestTool.h
Gaudi::Algorithm::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Algorithm.h:369
GaudiTesting::TestTool::Logger::renounce
void renounce([[maybe_unused]] std::string_view tool, [[maybe_unused]] std::string_view input) override
Definition: ToolVisitorTestLib.cpp:87
RenounceToolInputsVisitor::ILogger
Helper class interface to optionally log renounce operations.
Definition: RenounceToolInputsVisitor.h:31
RenounceToolInputsVisitor.h
GaudiEnv.h
bug_34121.tool
tool
Definition: bug_34121.py:18
StatusCode
Definition: StatusCode.h:65
GaudiTesting::TestTool::TestTool
TestTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: ToolVisitorTestLib.cpp:47
std::cout
GaudiTesting::TestAlg::addTool
void addTool(const std::string &name)
Definition: ToolVisitorTestLib.cpp:195
GaudiTesting::TestTool::initialize
StatusCode initialize() override
Definition: ToolVisitorTestLib.cpp:106
CommonMessaging
Definition: CommonMessaging.h:66
GaudiTesting::TestAlg::TestAlg
TestAlg(const std::string &aname, ISvcLocator *svc_Locator)
Definition: ToolVisitorTestLib.cpp:137
std::runtime_error
STL class.
std::deque< Gaudi::DataHandle >
GaudiTesting::TestAlg::m_algTools
std::deque< ToolHandle< ITestTool > > m_algTools
Definition: ToolVisitorTestLib.cpp:199
Gaudi::Algorithm::tools
const std::vector< IAlgTool * > & tools() const
Definition: Algorithm.cpp:615
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
GaudiTesting::TestTool
Definition: ToolVisitorTestLib.cpp:45
MSG::FATAL
@ FATAL
Definition: IMessageSvc.h:25
DataObjID
Definition: DataObjID.h:47
OffloadAtlasMCRecoScenario.pattern
pattern
Definition: OffloadAtlasMCRecoScenario.py:52
DataHandleHolderBase::inputHandles
std::vector< Gaudi::DataHandle * > inputHandles() const override
Definition: DataHandleHolderBase.h:38
DEBUG_TRACE
#define DEBUG_TRACE(a)
Definition: GaudiEnv.h:28
std::string::substr
T substr(T... args)
GaudiTesting::TestAlg::~TestAlg
~TestAlg()
Definition: ToolVisitorTestLib.cpp:147
std::vector::emplace_back
T emplace_back(T... args)
Gaudi::PluginService::v1::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginServiceV1.cpp:318
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
GaudiTesting::TestTool::m_keys
std::vector< DataObjID > m_keys
Definition: ToolVisitorTestLib.cpp:130
AlgTool
Definition: AlgTool.h:62
GaudiTesting::throwConditionFailed
void throwConditionFailed(bool condition, const std::string &file_name, int line_no, const std::string &condition_string)
Definition: ToolVisitorTestLib.cpp:25
DataHandleHolderBase< PropertyHolder< CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > > > >::outputDataObjs
const DataObjIDColl & outputDataObjs() const override
Definition: DataHandleHolderBase.h:84
AlgTool::tools
const std::vector< IAlgTool * > & tools() const
Definition: AlgTool.cpp:399
std::deque::begin
T begin(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
IInterface
Definition: IInterface.h:239
GaudiTesting::TestAlg::finalize
StatusCode finalize() override
Definition: ToolVisitorTestLib.cpp:182
GaudiTesting::TestAlg::dumpData
void dumpData(const std::string &pattern) const
Definition: ToolVisitorTestLib.cpp:156
Gaudi::DataHandle::Reader
@ Reader
Definition: DataHandle.h:40
AlgTool.h
DataHandleHolderBase::inputDataObjs
const DataObjIDColl & inputDataObjs() const override
Definition: DataHandleHolderBase.h:83
RenounceToolInputsVisitor
Helper class to be used in conjunction with the recursive tool visitor to renounce certain inputs.
Definition: RenounceToolInputsVisitor.h:27
std::deque::end
T end(T... args)
GaudiTesting::TestAlg::execute
StatusCode execute() override
Definition: ToolVisitorTestLib.cpp:181
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ToolVisitor.h
Ensures
#define Ensures(a)
Definition: GaudiEnv.h:26
GaudiTesting::TestTool::Logger
Definition: ToolVisitorTestLib.cpp:86
GaudiTesting::TestTool::addTool
void addTool(const std::string &name)
Definition: ToolVisitorTestLib.cpp:102
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiTesting::TestAlg
Definition: ToolVisitorTestLib.cpp:135
GaudiTesting::TestTool::m_handles
std::deque< Gaudi::DataHandle > m_handles
Definition: ToolVisitorTestLib.cpp:131
std::string::rfind
T rfind(T... args)