All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
JobOptionsSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Boost:
3 // ============================================================================
4 #include <boost/foreach.hpp>
5 // ============================================================================
6 // Local:
7 // ============================================================================
8 #include "JobOptionsSvc.h"
9 #include "Analyzer.h"
10 #include "Messages.h"
11 #include "Catalog.h"
12 #include "Units.h"
13 #include "PragmaOptions.h"
14 #include "Node.h"
15 // ============================================================================
16 // Gaudi:
17 // ============================================================================
18 #include "GaudiKernel/IProperty.h"
19 #include "GaudiKernel/Property.h"
20 
21 #include "GaudiKernel/MsgStream.h"
22 #include "GaudiKernel/System.h"
23 // ============================================================================
25 // ============================================================================
26 // Namespace aliases:
27 // ============================================================================
28 namespace gp = Gaudi::Parsers;
29 // ============================================================================
30 JobOptionsSvc::JobOptionsSvc(const std::string& name,ISvcLocator* svc):
31 base_class(name,svc),
32 m_pmgr()
33 , m_source_path()
34 , m_source_type()
35 , m_dir_search_path()
36 , m_dump()
37 {
38  std::string tmp ;
39  tmp = System::getEnv ( "JOBOPTSEARCHPATH" ) ;
40  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dir_search_path = tmp ; }
41  tmp = System::getEnv ( "JOBOPTSDUMPFILE" ) ;
42  if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dump = tmp ; }
43 
44  m_pmgr.declareProperty( "TYPE" , m_source_type ) ;
45  m_pmgr.declareProperty( "PATH" , m_source_path ) ;
46  m_pmgr.declareProperty( "SEARCHPATH" , m_dir_search_path ) ;
47  m_pmgr.declareProperty( "DUMPFILE" , m_dump ) ;
48 }
49 // ============================================================================
51 {
52  return m_pmgr.setProperty( p );
53 }
54 // ============================================================================
56 {
57 
58  return m_pmgr.getProperty( p );
59 }
60 // ============================================================================
62 {
63  // Call base class initializer
65  if( !sc.isSuccess() ) return sc;
66  // Read the job options if needed
67  if ( this->m_source_type == "NONE" ) {
69  }
70  else {
72  }
73  return sc;
74 }
75 
77 {
78  // finalize the base class
79  return Service::finalize();
80 }
81 // ============================================================================
83 ( const std::string& client,
84  const Property& property )
85 {
86  Property* p = new StringProperty ( property.name(), "" ) ;
87  if ( !property.load( *p ) ) { delete p ; return StatusCode::FAILURE ; }
88  return m_svc_catalog.addProperty( client , p );
89 }
90 // ============================================================================
93 ( const std::string& client,
94  const std::string& name )
95 {
96  return m_svc_catalog.removeProperty(client,name);
97 }
98 // ============================================================================
100 JobOptionsSvc::getProperties( const std::string& client) const
101 {
102  return m_svc_catalog.getProperties(client);
103 }
104 // ============================================================================
105 StatusCode JobOptionsSvc::setMyProperties( const std::string& client,
106  IProperty* myInt )
107 {
108  const SvcCatalog::PropertiesT* props =
110 
111  if ( NULL == props ){ return StatusCode::SUCCESS; }
112 
113  bool fail = false;
114  for ( std::vector<const Property*>::const_iterator cur = props->begin();
115  cur != props->end(); cur++)
116  {
117  StatusCode sc = myInt->setProperty (**cur ) ;
118  if ( sc.isFailure() )
119  {
120  MsgStream my_log( this->msgSvc(), this->name() );
121  my_log
122  << MSG::ERROR
123  << "Unable to set the property '" << (*cur)->name() << "'"
124  << " of '" << client << "'. "
125  << "Check option and algorithm names, type and bounds."
126  << endmsg;
127  fail = true;
128  }
129  }
130  return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
131 }
132 
134 std::vector<std::string> JobOptionsSvc::getClients() const {
135  return m_svc_catalog.getClients();
136 }
137 
138 
139 void JobOptionsSvc::dump (const std::string& file,
140  const gp::Catalog& catalog) const {
141  MsgStream log ( msgSvc() , name() ) ;
142  std::ofstream out
143  ( file.c_str() , std::ios_base::out | std::ios_base::trunc ) ;
144  // perform the actual dumping
145  if ( !out ) {
146  log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endmsg ;
147  return ; // RETURN
148  }
149  else {
150  log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endmsg ;
151  }
152  // perform the actual dump:
153  out << catalog;
154 }
155 
156 void JobOptionsSvc::fillServiceCatalog(const gp::Catalog& catalog) {
157  BOOST_FOREACH(const gp::Catalog::value_type& client, catalog) {
158  for (gp::Catalog::CatalogSet::mapped_type::const_iterator current
159  = client.second.begin(); current != client.second.end();
160  ++current) {
161  StringProperty tmp (current->NameInClient(), current->ValueAsString()) ;
162  addPropertyToCatalogue ( client.first , tmp ) ;
163  }
164  }
165 }
166 
168  const std::string& path) {
169  std::string search_path = path;
170  if ( search_path.empty() && !m_dir_search_path.empty() )
171  { search_path = m_dir_search_path ; }
172  //
173  MsgStream my_log( this->msgSvc(), this->name() );
174  if (UNLIKELY(outputLevel() <= MSG::DEBUG))
175  my_log << MSG::DEBUG // debug
176  << "Reading options from the file "
177  << "'" << file << "'" << endmsg;
178  gp::Messages messages(my_log);
179  gp::Catalog catalog;
180  gp::Units units;
181  gp::PragmaOptions pragma;
182  gp::Node ast;
183  StatusCode sc = gp::ReadOptions(file, path, &messages, &catalog, &units,
184  &pragma, &ast);
185 
186  // --------------------------------------------------------------------------
187  if ( sc.isSuccess() )
188  {
189  if (pragma.IsPrintOptions()) {
190  my_log << MSG::INFO << "Print options" << std::endl << catalog
191  << endmsg;
192  }
193  if (pragma.IsPrintTree()) {
194  my_log << MSG::INFO << "Print tree:" << std::endl << ast.ToString()
195  << endmsg;
196  }
197  if (pragma.HasDumpFile()) {
198  dump(pragma.dumpFile(), catalog);
199  }
200  my_log << MSG::INFO
201  << "Job options successfully read in from " << file << endmsg;
202  fillServiceCatalog(catalog);
203  }
204  else
205  {
206  my_log << MSG::FATAL << "Job options errors."<< endmsg;
207  }
208  // ----------------------------------------------------------------------------
209  return sc;
210 }
211 
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:608
StatusCode setProperty(const Property &p)
IProperty implementation (needed for initialisation)
#define UNLIKELY(x)
Definition: Kernel.h:127
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode setProperty(const Property &p)
set the property form another property
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual std::vector< std::string > getClients() const
Get the list of clients.
std::string m_source_type
Definition: JobOptionsSvc.h:78
const PropertiesT * getProperties(const std::string &client) const
Definition: SvcCatalog.cpp:71
const std::string & name() const
property name
Definition: Property.h:47
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:81
virtual StatusCode setMyProperties(const std::string &client, IProperty *me)
Override default properties of the calling client.
virtual StatusCode addPropertyToCatalogue(const std::string &client, const Property &property)
Add a property into the JobOptions catalog.
int outputLevel() const
get the Service's output level
Definition: Service.h:250
virtual bool load(Property &dest) const =0
export the property value to the destination
bool ReadOptions(const std::string &filename, const std::string &search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Definition: Analyzer.cpp:438
virtual StatusCode removePropertyFromCatalogue(const std::string &client, const std::string &name)
Remove a property from the JobOptions catalog.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:72
SimpleProperty< std::string > StringProperty
Definition: Property.h:743
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:73
std::string m_dir_search_path
Definition: JobOptionsSvc.h:79
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
StatusCode getProperty(Property *p) const
Get the property by property.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
StatusCode getProperty(Property *p) const
get the property
list file
Definition: ana.py:160
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
virtual StatusCode finalize()
Service finalization method.
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
Templated class to add the standard messaging functionalities.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
PropertyMgr m_pmgr
dump the content of Properties catalog to the predefined file
Definition: JobOptionsSvc.h:76
std::vector< const Property * > PropertiesT
Definition: JobOptionsSvc.h:23
std::vector< const Property * > PropertiesT
Definition: SvcCatalog.h:31
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
virtual StatusCode readOptions(const std::string &file, const std::string &path="")
look for file 'file' into search path 'path' and read it to update existing JobOptionsCatalogue ...
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:22
std::string m_source_path
Definition: JobOptionsSvc.h:77
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual const std::vector< Property * > & getProperties() const
Get list of properties.
Definition: Service.cpp:366