| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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