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
00072
00073 algorithm->release();
00074 if ( managed ) {
00075
00076 if (FSMState() >= Gaudi::StateMachine::INITIALIZED) {
00077 rc = algorithm->sysInitialize();
00078 if (rc.isSuccess() && FSMState() >= Gaudi::StateMachine::RUNNING) {
00079 rc = algorithm->sysStart();
00080 }
00081 }
00082 if ( !rc.isSuccess() ) {
00083 this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg;
00084 }
00085 }
00086 return rc;
00087 }
00088 this->error() << "Algorithm of type " << algtype
00089 << " is unknown (No factory available)." << endmsg;
00090 #ifndef _WIN32
00091 errno = 0xAFFEDEAD;
00092 #endif
00093 std::string err = System::getLastErrorString();
00094 if (! err.empty()) {
00095 this->error() << err << endmsg;
00096 }
00097 this->error() << "More information may be available by setting the global jobOpt \"ReflexPluginDebugLevel\" to 1" << endmsg;
00098
00099 return StatusCode::FAILURE;
00100 }
00101
00102 SmartIF<IAlgorithm>& AlgorithmManager::algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
00103 ListAlg::iterator it = std::find(m_listalg.begin(), m_listalg.end(), typeName.name());
00104 if (it != m_listalg.end()) {
00105 return it->algorithm;
00106 }
00107 if (createIf) {
00108 IAlgorithm* alg;
00109 if (createAlgorithm(typeName.type(), typeName.name(), alg, true).isSuccess()) {
00110 return algorithm(typeName, false);
00111 }
00112 }
00113 return no_algorithm;
00114 }
00115
00116
00117 bool AlgorithmManager::existsAlgorithm(const std::string& name) const {
00118 ListAlg::const_iterator it = std::find(m_listalg.begin(), m_listalg.end(), name);
00119 return it != m_listalg.end();
00120 }
00121
00122
00123 const std::list<IAlgorithm*>& AlgorithmManager::getAlgorithms() const
00124 {
00125 m_listOfPtrs.clear();
00126 for (ListAlg::const_iterator it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00127 m_listOfPtrs.push_back(const_cast<IAlgorithm*>(it->algorithm.get()));
00128 }
00129 return m_listOfPtrs;
00130 }
00131
00132 StatusCode AlgorithmManager::initialize() {
00133 StatusCode rc;
00134 ListAlg::iterator it;
00135 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00136 if (!it->managed) continue;
00137 rc = it->algorithm->sysInitialize();
00138 if ( rc.isFailure() ) return rc;
00139 }
00140 return rc;
00141 }
00142
00143 StatusCode AlgorithmManager::start() {
00144 StatusCode rc;
00145 ListAlg::iterator it;
00146 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00147 if (!it->managed) continue;
00148 rc = it->algorithm->sysStart();
00149 if ( rc.isFailure() ) return rc;
00150 }
00151 return rc;
00152 }
00153
00154 StatusCode AlgorithmManager::stop() {
00155 StatusCode rc;
00156 ListAlg::iterator it;
00157 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00158 if (!it->managed) continue;
00159 rc = it->algorithm->sysStop();
00160 if ( rc.isFailure() ) return rc;
00161 }
00162 return rc;
00163 }
00164
00165 StatusCode AlgorithmManager::finalize() {
00166 StatusCode rc;
00167 ListAlg::iterator it = m_listalg.begin();
00168 while (it != m_listalg.end()){
00169 if (it->managed) {
00170 rc = it->algorithm->sysFinalize();
00171 if( rc.isFailure() ) return rc;
00172 it = m_listalg.erase(it);
00173 } else {
00174 ++it;
00175 }
00176 }
00177 return rc;
00178 }
00179
00180 StatusCode AlgorithmManager::reinitialize() {
00181 StatusCode rc;
00182 ListAlg::iterator it;
00183 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00184 if (!it->managed) continue;
00185 rc = it->algorithm->sysReinitialize();
00186 if( rc.isFailure() ){
00187 this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
00188 return rc;
00189 }
00190 }
00191 return rc;
00192 }
00193
00194 StatusCode AlgorithmManager::restart() {
00195 StatusCode rc;
00196 ListAlg::iterator it;
00197 for (it = m_listalg.begin(); it != m_listalg.end(); ++it) {
00198 if (!it->managed) continue;
00199 rc = it->algorithm->sysRestart();
00200 if( rc.isFailure() ){
00201 this->error() << "Unable to re-initialize algorithm: " << it->algorithm->name() << endmsg;
00202 return rc;
00203 }
00204 }
00205 return rc;
00206 }