AlgorithmManager.cpp
Go to the documentation of this file.00001
00002
00003
00004 #include "AlgorithmManager.h"
00005 #include "GaudiKernel/IAlgorithm.h"
00006 #include "GaudiKernel/ISvcLocator.h"
00007 #include "GaudiKernel/AlgFactory.h"
00008 #include "GaudiKernel/System.h"
00009 #include "GaudiKernel/MsgStream.h"
00010 #include "GaudiKernel/TypeNameString.h"
00011 #include <iostream>
00012 #ifndef _WIN32
00013 #include <errno.h>
00014 #endif
00015
00017 static SmartIF<IAlgorithm> no_algorithm;
00018
00019 using ROOT::Reflex::PluginService;
00020
00021
00022 AlgorithmManager::AlgorithmManager(IInterface* application):
00023 base_class(application, IAlgorithm::interfaceID())
00024 {
00025 addRef();
00026 }
00027
00028
00029 AlgorithmManager::~AlgorithmManager() {
00030 }
00031
00032
00033 StatusCode AlgorithmManager::addAlgorithm(IAlgorithm* alg) {
00034 m_listalg.push_back(alg);
00035 return StatusCode::SUCCESS;
00036 }
00037
00038
00039 StatusCode AlgorithmManager::removeAlgorithm(IAlgorithm* alg) {
00040 ListAlg::iterator it = std::find(m_listalg.begin(), m_listalg.end(), alg);
00041 if (it != m_listalg.end()) {
00042 m_listalg.erase(it);
00043 return StatusCode::SUCCESS;
00044 }
00045 return StatusCode::FAILURE;
00046 }
00047
00048
00049 StatusCode AlgorithmManager::createAlgorithm( const std::string& algtype,
00050 const std::string& algname,
00051 IAlgorithm*& algorithm,
00052 bool managed)
00053 {
00054
00055 if( existsAlgorithm( algname ) ) {
00056
00057 return StatusCode::FAILURE;
00058 }
00059 algorithm = PluginService::Create<IAlgorithm*>(algtype, algname, serviceLocator().get());
00060 if ( !algorithm ) {
00061 algorithm = PluginService::CreateWithId<IAlgorithm*>(algtype, algname, serviceLocator().get());
00062 }
00063 if ( algorithm ) {
00064
00065 if( !isValidInterface(algorithm) ) {
00066 fatal() << "Incompatible interface IAlgorithm version for " << algtype << endmsg;
00067 return StatusCode::FAILURE;
00068 }
00069 StatusCode rc;
00070 m_listalg.push_back(AlgorithmItem(algorithm, managed));
00071 if ( managed ) {
00072
00073 if (FSMState() >= Gaudi::StateMachine::INITIALIZED) {
00074 rc = algorithm->sysInitialize();
00075 if (rc.isSuccess() && FSMState() >= Gaudi::StateMachine::RUNNING) {
00076 rc = algorithm->sysStart();
00077 }
00078 }
00079 if ( !rc.isSuccess() ) {
00080 this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg;
00081 }
00082 }
00083 return rc;
00084 }
00085 this->error() << "Algorithm of type " << algtype
00086 << " is unknown (No factory available)." << endmsg;
00087 #ifndef _WIN32
00088 errno = 0xAFFEDEAD;
00089 #endif
00090 std::string err = System::getLastErrorString();
00091 if (! err.empty()) {
00092 this->error() << err << endmsg;
00093 }
00094 this->error() << "More information may be available by setting the global jobOpt \"ReflexPluginDebugLevel\" to 1" << endmsg;
00095
00096 return StatusCode::FAILURE;
00097 }
00098
00099 SmartIF<IAlgorithm>& AlgorithmManager::algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
00100 ListAlg::iterator it = std::find(m_listalg.begin(), m_listalg.end(), typeName.name());
00101 if (it != m_listalg.end()) {
00102 return it->algorithm;
00103 }
00104 if (createIf) {
00105 IAlgorithm* alg;
00106 if (createAlgorithm(typeName.type(), typeName.name(), alg, true).isSuccess()) {
00107 return algorithm(typeName, false);
00108 }
00109 }
00110 return no_algorithm;
00111 }
00112
00113
00114 bool AlgorithmManager::existsAlgorithm(const std::string& name) const {
00115 ListAlg::const_iterator it = std::find(m_listalg.begin(), m_listalg.end(), name);
00116 return it != m_listalg.end();
00117 }
00118
00119
00120 const std::list<IAlgorithm*>& AlgorithmManager::getAlgorithms() const
00121 {
00122 m_listOfPtrs.clear();
00123 for (ListAlg::const_iterator it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00124 m_listOfPtrs.push_back(const_cast<IAlgorithm*>(it->algorithm.get()));
00125 }
00126 return m_listOfPtrs;
00127 }
00128
00129 StatusCode AlgorithmManager::initialize() {
00130 StatusCode rc;
00131 ListAlg::iterator it;
00132 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00133 if (!it->managed) continue;
00134 rc = it->algorithm->sysInitialize();
00135 if ( rc.isFailure() ) return rc;
00136 }
00137 return rc;
00138 }
00139
00140 StatusCode AlgorithmManager::start() {
00141 StatusCode rc;
00142 ListAlg::iterator it;
00143 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00144 if (!it->managed) continue;
00145 rc = it->algorithm->sysStart();
00146 if ( rc.isFailure() ) return rc;
00147 }
00148 return rc;
00149 }
00150
00151 StatusCode AlgorithmManager::stop() {
00152 StatusCode rc;
00153 ListAlg::iterator it;
00154 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00155 if (!it->managed) continue;
00156 rc = it->algorithm->sysStop();
00157 if ( rc.isFailure() ) return rc;
00158 }
00159 return rc;
00160 }
00161
00162 StatusCode AlgorithmManager::finalize() {
00163 StatusCode rc;
00164 ListAlg::iterator it = m_listalg.begin();
00165 while (it != m_listalg.end()){
00166 if (it->managed) {
00167 rc = it->algorithm->sysFinalize();
00168 if( rc.isFailure() ) return rc;
00169 it = m_listalg.erase(it);
00170 } else {
00171 ++it;
00172 }
00173 }
00174 return rc;
00175 }
00176
00177 StatusCode AlgorithmManager::reinitialize() {
00178 StatusCode rc;
00179 ListAlg::iterator it;
00180 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00181 if (!it->managed) continue;
00182 rc = it->algorithm->sysReinitialize();
00183 if( rc.isFailure() ){
00184 this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
00185 return rc;
00186 }
00187 }
00188 return rc;
00189 }
00190
00191 StatusCode AlgorithmManager::restart() {
00192 StatusCode rc;
00193 ListAlg::iterator it;
00194 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00195 if (!it->managed) continue;
00196 rc = it->algorithm->sysRestart();
00197 if( rc.isFailure() ){
00198 this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
00199 return rc;
00200 }
00201 }
00202 return rc;
00203 }