All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlgorithmManager.cpp
Go to the documentation of this file.
1 // Include files
2 #include "AlgorithmManager.h"
6 #include "GaudiKernel/System.h"
9 #include <iostream>
10 #ifndef _WIN32
11 #include <errno.h>
12 #endif
13 
15 static SmartIF<IAlgorithm> no_algorithm;
16 
17 // constructor
19  base_class(application, IAlgorithm::interfaceID())
20 {
21  addRef(); // Initial count set to 1
22 }
23 
24 // destructor
26 }
27 
28 // addAlgorithm
30  m_listalg.push_back(alg);
31  return StatusCode::SUCCESS;
32 }
33 
34 // removeAlgorithm
36  ListAlg::iterator it = std::find(m_listalg.begin(), m_listalg.end(), alg);
37  if (it != m_listalg.end()) {
38  m_listalg.erase(it);
39  return StatusCode::SUCCESS;
40  }
41  return StatusCode::FAILURE;
42 }
43 
44 // createService
45 StatusCode AlgorithmManager::createAlgorithm( const std::string& algtype,
46  const std::string& algname,
47  IAlgorithm*& algorithm,
48  bool managed)
49 {
50  // Check is the algorithm is already existing
51  if( existsAlgorithm( algname ) ) {
52  // return an error because an algorithm with that name already exists
53  return StatusCode::FAILURE;
54  }
55  std::string actualalgtype(algtype);
56  // a '\' in front of the type name prevents alias replacement
57  if ((actualalgtype.size() > 8) && (actualalgtype.substr(0, 8) == "unalias:")) {
58  actualalgtype = actualalgtype.substr(8);
59  } else {
60  AlgTypeAliasesMap::iterator typeAlias = m_algTypeAliases.find(algtype);
61  if (typeAlias != m_algTypeAliases.end()) {
62  actualalgtype = typeAlias->second;
63  }
64  }
65  algorithm = Algorithm::Factory::create(actualalgtype, algname, serviceLocator().get());
66  if ( algorithm ) {
67  // Check the compatibility of the version of the interface obtained
68  if( !isValidInterface(algorithm) ) {
69  fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
70  return StatusCode::FAILURE;
71  }
72  StatusCode rc;
73  m_listalg.push_back(AlgorithmItem(algorithm, managed));
74  // this is needed to keep the reference count correct, since isValidInterface(algorithm)
75  // implies an increment of the counter by 1
76  algorithm->release();
77  if ( managed ) {
78  // Bring the created algorithm to the same state of the ApplicationMgr
80  rc = algorithm->sysInitialize();
82  rc = algorithm->sysStart();
83  }
84  }
85  if ( !rc.isSuccess() ) {
86  this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg;
87  }
88  }
89  return rc;
90  }
91  this->error() << "Algorithm of type " << actualalgtype
92  << " is unknown (No factory available)." << endmsg;
93 #ifndef _WIN32
94  errno = 0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
95 #endif
96  std::string err = System::getLastErrorString();
97  if (! err.empty()) {
98  this->error() << err << endmsg;
99  }
100  this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1" << endmsg;
101 
102  return StatusCode::FAILURE;
103 }
104 
106  ListAlg::iterator it = std::find(m_listalg.begin(), m_listalg.end(), typeName.name());
107  if (it != m_listalg.end()) { // found
108  return it->algorithm;
109  }
110  if (createIf) {
111  IAlgorithm* alg;
112  if (createAlgorithm(typeName.type(), typeName.name(), alg, true).isSuccess()) {
113  return algorithm(typeName, false);
114  }
115  }
116  return no_algorithm;
117 }
118 
119 // existsAlgorithm
120 bool AlgorithmManager::existsAlgorithm(const std::string& name) const {
121  ListAlg::const_iterator it = std::find(m_listalg.begin(), m_listalg.end(), name);
122  return it != m_listalg.end();
123 }
124 
125  // Return the list of Algorithms
126 const std::vector<IAlgorithm*>& AlgorithmManager::getAlgorithms() const
127 {
128  m_listOfPtrs.clear();
129  m_listOfPtrs.reserve(m_listalg.size());
130  std::transform( std::begin(m_listalg), std::end(m_listalg),
131  std::back_inserter(m_listOfPtrs),
132  [](ListAlg::const_reference alg) {
133  return const_cast<IAlgorithm*>(alg.algorithm.get());
134  } );
135  return m_listOfPtrs;
136 }
137 
139  StatusCode rc;
140  ListAlg::iterator it;
141  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
142  if (!it->managed) continue;
143  rc = it->algorithm->sysInitialize();
144  if ( rc.isFailure() ) return rc;
145  }
146  return rc;
147 }
148 
150  StatusCode rc;
151  ListAlg::iterator it;
152  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
153  if (!it->managed) continue;
154  rc = it->algorithm->sysStart();
155  if ( rc.isFailure() ) return rc;
156  }
157  return rc;
158 }
159 
161  StatusCode rc;
162  ListAlg::iterator it;
163  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
164  if (!it->managed) continue;
165  rc = it->algorithm->sysStop();
166  if ( rc.isFailure() ) return rc;
167  }
168  return rc;
169 }
170 
172  StatusCode rc;
173  ListAlg::iterator it = m_listalg.begin();
174  while (it != m_listalg.end()){ // finalize and remove from the list the managed algorithms
175  if (it->managed) {
176  rc = it->algorithm->sysFinalize();
177  if( rc.isFailure() ) return rc;
178  it = m_listalg.erase(it);
179  } else {
180  ++it;
181  }
182  }
183  return rc;
184 }
185 
187  StatusCode rc;
188  ListAlg::iterator it;
189  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
190  if (!it->managed) continue;
191  rc = it->algorithm->sysReinitialize();
192  if( rc.isFailure() ){
193  this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
194  return rc;
195  }
196  }
197  return rc;
198 }
199 
201  StatusCode rc;
202  ListAlg::iterator it;
203  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
204  if (!it->managed) continue;
205  rc = it->algorithm->sysRestart();
206  if( rc.isFailure() ){
207  this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
208  return rc;
209  }
210  }
211  return rc;
212 }
const std::string & name() const
Return the name of the manager (implementation of INamedInterface)
virtual SmartIF< ISvcLocator > & serviceLocator() const
Needed to locate the message service.
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
AlgTypeAliasesMap m_algTypeAliases
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
std::vector< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:257
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to a service.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
virtual bool existsAlgorithm(const std::string &name) const
implementation of IAlgManager::existsAlgorithm
virtual StatusCode stop()
Stop (from RUNNING to INITIALIZED).
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
virtual const std::vector< IAlgorithm * > & getAlgorithms() const
implementation of IAlgManager::getAlgorithms
ListAlg m_listalg
List of algorithms maintained by AlgorithmManager.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
tuple rc
Definition: IOTest.py:92
virtual StatusCode addAlgorithm(IAlgorithm *alg)
implementation of IAlgManager::addAlgorithm
virtual StatusCode restart()
Initialization (from RUNNING to RUNNING, via INITIALIZED).
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:20
virtual Gaudi::StateMachine::State FSMState() const
Get the current state.
virtual StatusCode removeAlgorithm(IAlgorithm *alg)
implementation of IAlgManager::removeAlgorithm
tuple end
Definition: IOTest.py:101
virtual unsigned long release()=0
Release Interface instance.
virtual StatusCode start()
Start (from INITIALIZED to RUNNING).
const std::string & type() const
virtual StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false)
implementation of IAlgManager::createAlgorithm
Templated class to add the standard messaging functionalities.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
AlgorithmManager(IInterface *iface)
default creator
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:253
const std::string & name() const
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:22
virtual ~AlgorithmManager()
virtual destructor
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244