| # Gaudi Framework, version v23r0 |

Home |  | Generated: Mon Jan 30 2012

# JobOptionsSvc.cpp

Go to the documentation of this file.

    00001 // ============================================================================
    00002 // Boost:
    00003 // ============================================================================
    00004 #include <boost/foreach.hpp>
    00005 // ============================================================================
    00006 // Local:
    00007 // ============================================================================
    00008 #include "JobOptionsSvc.h"
    00009 #include "Analyzer.h"
    00010 #include "Messages.h"
    00011 #include "Catalog.h"
    00012 #include "Units.h"
    00013 #include "PragmaOptions.h"
    00014 #include "Node.h"
    00015 // ============================================================================
    00016 // Gaudi:
    00017 // ============================================================================
    00018 #include "GaudiKernel/IProperty.h"
    00019 #include "GaudiKernel/Property.h"
    00020
    00021 #include "GaudiKernel/SvcFactory.h"
    00022 #include "GaudiKernel/MsgStream.h"
    00023 #include "GaudiKernel/System.h"
    00024 // ============================================================================
    00025 DECLARE_SERVICE_FACTORY(JobOptionsSvc)
    00026 // ============================================================================
    00027 // Namespace aliases:
    00028 // ============================================================================
    00029 namespace gp = Gaudi::Parsers;
    00030 // ============================================================================
    00031 JobOptionsSvc::JobOptionsSvc(const std::string& name,ISvcLocator* svc):
    00032 base_class(name,svc),
    00033 m_pmgr()
    00034 , m_source_path()
    00035 , m_source_type()
    00036 , m_dir_search_path()
    00037 , m_dump()
    00038 , m_dumped( false )
    00039 {
    00040   std::string tmp ;
    00041   tmp = System::getEnv ( "JOBOPTSEARCHPATH" ) ;
    00042   if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dir_search_path = tmp ; }
    00043   tmp = System::getEnv ( "JOBOPTSDUMPFILE"  ) ;
    00044   if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dump = tmp ; }
    00045
    00046   m_pmgr.declareProperty( "TYPE"       , m_source_type   ) ;
    00047   m_pmgr.declareProperty( "PATH"       , m_source_path   ) ;
    00048   m_pmgr.declareProperty( "SEARCHPATH" , m_dir_search_path ) ;
    00049   m_pmgr.declareProperty( "DUMPFILE"   , m_dump          ) ;
    00050 }
    00051 // ============================================================================
    00052 StatusCode JobOptionsSvc::setProperty( const Property &p )
    00053 {
    00054   return m_pmgr.setProperty( p );
    00055 }
    00056 // ============================================================================
    00057 StatusCode JobOptionsSvc::getProperty( Property *p ) const
    00058 {
    00059
    00060   return m_pmgr.getProperty( p );
    00061 }
    00062 // ============================================================================
    00063 StatusCode JobOptionsSvc::initialize()
    00064 {
    00065   // Call base class initializer
    00066   StatusCode sc = Service::initialize();
    00067   if( !sc.isSuccess() ) return sc;
    00068   // Read the job options if needed
    00069   if ( this->m_source_type == "NONE" ) {
    00070     sc =  StatusCode::SUCCESS;
    00071   }
    00072   else {
    00073     sc = this->readOptions( m_source_path , m_dir_search_path);
    00074   }
    00075   return sc;
    00076 }
    00077
    00078 StatusCode JobOptionsSvc::finalize()
    00079 {
    00080   // finalize the base class
    00081   return Service::finalize();
    00082 }
    00083 // ============================================================================
    00084 StatusCode JobOptionsSvc::addPropertyToCatalogue
    00085 ( const std::string& client,
    00086   const Property& property )
    00087 {
    00088   Property* p = new StringProperty ( property.name(), "" ) ;
    00089   if ( !property.load( *p ) ) { delete p ; return StatusCode::FAILURE ; }
    00090   return m_svc_catalog.addProperty( client , p );
    00091 }
    00092 // ============================================================================
    00093 StatusCode
    00094 JobOptionsSvc::removePropertyFromCatalogue
    00095 ( const std::string& client,
    00096   const std::string& name )
    00097 {
    00098   return m_svc_catalog.removeProperty(client,name);
    00099 }
    00100 // ============================================================================
    00101 const JobOptionsSvc::PropertiesT*
    00102 JobOptionsSvc::getProperties( const std::string& client) const
    00103 {
    00104   return m_svc_catalog.getProperties(client);
    00105 }
    00106 // ============================================================================
    00107 StatusCode JobOptionsSvc::setMyProperties( const std::string& client,
    00108                                            IProperty* myInt )
    00109 {
    00110   const SvcCatalog::PropertiesT* props =
    00111     m_svc_catalog.getProperties(client);
    00112
    00113   if ( NULL == props ){ return StatusCode::SUCCESS; }
    00114
    00115   bool fail = false;
    00116   for ( std::vector<const Property*>::const_iterator cur = props->begin();
    00117     cur != props->end(); cur++)
    00118   {
    00119     StatusCode sc = myInt->setProperty (**cur ) ;
    00120     if ( sc.isFailure() )
    00121     {
    00122       MsgStream my_log( this->msgSvc(), this->name() );
    00123       my_log
    00124         << MSG::ERROR
    00125         << "Unable to set the property '" << (*cur)->name() << "'"
    00126         <<                        " of '" << client         << "'. "
    00127         << "Check option and algorithm names, type and bounds."
    00128         << endmsg;
    00129       fail = true;
    00130     }
    00131   }
    00132   return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
    00133 }
    00134
    00136 std::vector<std::string> JobOptionsSvc::getClients() const {
    00137   return m_svc_catalog.getClients();
    00138 }
    00139
    00140
    00141 void JobOptionsSvc::dump (const std::string& file,
    00142     const gp::Catalog& catalog) const {
    00143   MsgStream log ( msgSvc() , name() ) ;
    00144   std::ofstream out
    00145     ( file.c_str() , std::ios_base::out | std::ios_base::trunc ) ;
    00146   // perform the actual dumping
    00147   if ( !out ) {
    00148     log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endmsg ;
    00149     return ;                                                   // RETURN
    00150   }
    00151   else {
    00152     log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endmsg ;
    00153   }
    00154   // perform the actual dump:
    00155   out << catalog;
    00156 }
    00157
    00158 void JobOptionsSvc::fillServiceCatalog(const gp::Catalog& catalog) {
    00159   BOOST_FOREACH(const gp::Catalog::value_type& client, catalog) {
    00160     for (gp::Catalog::CatalogSet::mapped_type::const_iterator current
    00161           = client.second.begin(); current != client.second.end();
    00162                                                                   ++current) {
    00163       StringProperty tmp (current->NameInClient(), current->ValueAsString()) ;
    00164       addPropertyToCatalogue ( client.first , tmp ) ;
    00165     }
    00166   }
    00167 }
    00168
    00169 StatusCode JobOptionsSvc::readOptions ( const std::string& file,
    00170        const std::string& path) {
    00171     std::string search_path = path;
    00172     if ( search_path.empty() && !m_dir_search_path.empty() )
    00173     { search_path =  m_dir_search_path ; }
    00174     //
    00175     MsgStream my_log( this->msgSvc(), this->name() );
    00176     if (UNLIKELY(outputLevel() <= MSG::DEBUG))
    00177       my_log << MSG::DEBUG                             // debug
    00178              << "Reading options from the file "
    00179              << "'" << file << "'" << endmsg;
    00180     gp::Messages messages(my_log);
    00181     gp::Catalog catalog;
    00182     gp::Units units;
    00183     gp::PragmaOptions pragma;
    00184     gp::Node ast;
    00185     StatusCode sc = gp::ReadOptions(file, path, &messages, &catalog, &units,
    00186         &pragma, &ast);
    00187
    00188     // --------------------------------------------------------------------------
    00189     if ( sc.isSuccess() )
    00190     {
    00191       if (pragma.IsPrintOptions()) {
    00192         my_log << MSG::INFO << "Print options" << std::endl << catalog
    00193             << endmsg;
    00194       }
    00195       if (pragma.IsPrintTree()) {
    00196         my_log << MSG::INFO << "Print tree:" << std::endl << ast.ToString()
    00197             << endmsg;
    00198       }
    00199       if (pragma.HasDumpFile()) {
    00200         dump(pragma.dumpFile(), catalog);
    00201       }
    00202       my_log << MSG::INFO
    00203              << "Job options successfully read in from " << file << endmsg;
    00204       fillServiceCatalog(catalog);
    00205     }
    00206     else
    00207     {
    00208       my_log << MSG::FATAL << "Job options errors."<< endmsg;
    00209     }
    00210     // ----------------------------------------------------------------------------
    00211     return sc;
    00212 }
    00213

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines