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::list<IAlgorithm*>& AlgorithmManager::getAlgorithms() const
127 {
128  m_listOfPtrs.clear();
129  for (ListAlg::const_iterator it = m_listalg.begin(); it != m_listalg.end(); ++it) {
130  m_listOfPtrs.push_back(const_cast<IAlgorithm*>(it->algorithm.get()));
131  }
132  return m_listOfPtrs;
133 }
134 
136  StatusCode rc;
137  ListAlg::iterator it;
138  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
139  if (!it->managed) continue;
140  rc = it->algorithm->sysInitialize();
141  if ( rc.isFailure() ) return rc;
142  }
143  return rc;
144 }
145 
147  StatusCode rc;
148  ListAlg::iterator it;
149  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
150  if (!it->managed) continue;
151  rc = it->algorithm->sysStart();
152  if ( rc.isFailure() ) return rc;
153  }
154  return rc;
155 }
156 
158  StatusCode rc;
159  ListAlg::iterator it;
160  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
161  if (!it->managed) continue;
162  rc = it->algorithm->sysStop();
163  if ( rc.isFailure() ) return rc;
164  }
165  return rc;
166 }
167 
169  StatusCode rc;
170  ListAlg::iterator it = m_listalg.begin();
171  while (it != m_listalg.end()){ // finalize and remove from the list the managed algorithms
172  if (it->managed) {
173  rc = it->algorithm->sysFinalize();
174  if( rc.isFailure() ) return rc;
175  it = m_listalg.erase(it);
176  } else {
177  ++it;
178  }
179  }
180  return rc;
181 }
182 
184  StatusCode rc;
185  ListAlg::iterator it;
186  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
187  if (!it->managed) continue;
188  rc = it->algorithm->sysReinitialize();
189  if( rc.isFailure() ){
190  this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
191  return rc;
192  }
193  }
194  return rc;
195 }
196 
198  StatusCode rc;
199  ListAlg::iterator it;
200  for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
201  if (!it->managed) continue;
202  rc = it->algorithm->sysRestart();
203  if( rc.isFailure() ){
204  this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
205  return rc;
206  }
207  }
208  return rc;
209 }
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:62
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:72
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).
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
virtual const std::list< IAlgorithm * > & getAlgorithms() const
implementation of IAlgManager::getAlgorithms
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
std::list< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
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
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:243