AlgFactory.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2020, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
7  University of Liverpool & STFC Rutherford Appleton Laboratory
8 */
9 //____________________________________________________________________________
10 
11 #include <iostream>
12 #include <cstdlib>
13 
14 #include <TROOT.h>
15 #include <TClass.h>
16 
21 
22 using std::endl;
23 
24 using namespace genie;
25 
26 //____________________________________________________________________________
27 namespace genie {
28  ostream & operator<<(ostream & stream, const AlgFactory & algf)
29  {
30  algf.Print(stream);
31  return stream;
32  }
33 }
34 //____________________________________________________________________________
36 //____________________________________________________________________________
38 {
39  fInstance = 0;
40 }
41 //____________________________________________________________________________
43 {
44 // Clean up and report on the concrete algorithms used in this instance.
45 // Don't clutter output if exiting in err.
46 
48  for(alg_iter = fAlgPool.begin(); alg_iter != fAlgPool.end(); ++alg_iter) {
49  Algorithm * alg = alg_iter->second;
50  if(alg) {
51 /*
52  if(!gAbortingInErr) {
53  cout << "- Deleting algorithm: " << alg->Id() << endl;
54  }
55 */
56  delete alg;
57  alg = 0;
58  }
59  }
60  fAlgPool.clear();
61  fInstance = 0;
62 }
63 //____________________________________________________________________________
65 {
66  if(fInstance == 0) {
67  static AlgFactory::Cleaner cleaner;
69 
70  fInstance = new AlgFactory;
71  }
72  return fInstance;
73 }
74 //____________________________________________________________________________
76 {
77 //! Manages the instantiation and "storage/retrieval" of algorithms.
78 //! These algorithms are owned by the factory and it hands over (to the client)
79 //! a "const Algorithm *" that can be dynamically casted to the requested
80 //! Algorithm Interface (eg. XSecAlgorithmI, Decayer, PdfModelI, etc...)
81 
82  return this->GetAlgorithm(algid.Name(), algid.Config());
83 }
84 //____________________________________________________________________________
86 {
87  string key = name + "/" + config;
88 
89  SLOG("AlgFactory", pDEBUG)
90  << "Algorithm: " << key << " requested from AlgFactory";
91 
93  bool found = (alg_iter != fAlgPool.end());
94 
95  if(found) {
96  LOG("AlgFactory", pDEBUG) << key << " algorithm found in memory";
97  return alg_iter->second;
98  } else {
99  //-- instantiate the factory
100  Algorithm * alg_base = this->InstantiateAlgorithm(name,config);
101 
102  //-- cache the algorithm for future use
103  if(alg_base) {
104  pair<string, Algorithm *> key_alg_pair(key, alg_base);
105  fAlgPool.insert(key_alg_pair);
106  } else {
107  LOG("AlgFactory", pFATAL)
108  << "Algorithm: " << key << " could not be instantiated";
109  exit(1);
110  }
111  return alg_base;
112  }
113  return 0;
114 }
115 //____________________________________________________________________________
117 {
118 //! Hands over an algorithm instance that is owned by the client.
119 //! The client can alter this object (eg. reconfigure) but the AlgFactory does
120 //! not keep track of it and the client is responsible for deleting it.
121 
122  return this->AdoptAlgorithm(algid.Name(), algid.Config());
123 }
124 //____________________________________________________________________________
126 {
127  Algorithm * alg_base = InstantiateAlgorithm(name, config);
128  return alg_base;
129 }
130 //____________________________________________________________________________
131 void AlgFactory::ForceReconfiguration(bool ignore_alg_opt_out)
132 {
133  LOG("AlgFactory", pNOTICE)
134  << " ** Forcing algorithm re-configuration";
135 
136  map<string, Algorithm *>::iterator alg_iter = fAlgPool.begin();
137  for( ; alg_iter != fAlgPool.end(); ++alg_iter) {
138  Algorithm * alg = alg_iter->second;
139  bool reconfig = (ignore_alg_opt_out) ? true : alg->AllowReconfig();
140  if(reconfig) {
141  string config = alg->Id().Config();
142  bool skip_conf = (config=="NoConfig" || config=="");
143  if(!skip_conf) {
144 // LOG("AlgFactory", pINFO) << "Reconfiguring: " << alg->Id().Key();
145  alg->Configure(config);
146  }
147  }//allow?
148  }
149 }
150 //____________________________________________________________________________
152 {
153 //! Instantiate the requested object based on the registration of its TClass
154 //! through the generated ROOT dictionaries
155 //! The class of any object instantiated here must have a LinkDef entry.
156 
157  // Get object through ROOT's TROOT::GetClass() mechanism
158  LOG("AlgFactory", pDEBUG) << "Instantiating algorithm = " << name;
159 
160  TClass * tclass = gROOT->GetClass(name.c_str());
161  if(!tclass) {
162  LOG("AlgFactory", pERROR)
163  << "Failed instantiating algorithm = " << name;
164  return 0;
165  }
166  void * vd_base = tclass->New();
167  Algorithm * alg_base = (Algorithm *) (vd_base);
168 
169  // Configure the instantiated algorithm
170 
171  LOG("AlgFactory", pDEBUG) << "Setting Configuration Set = " << config;
172 
173  bool skip_conf = (config=="NoConfig" || config=="");
174  if ( skip_conf ) {
175  LOG("AlgFactory", pDEBUG) << "Skipping algorithm configuration step!";
176  } else {
177  alg_base->Configure(config);
178  }
179 
180  return alg_base;
181 }
182 //____________________________________________________________________________
183 void AlgFactory::Print(ostream & stream) const
184 {
185  string frame(100,'.');
186 
187  stream << endl;
189  for(alg_iter = fAlgPool.begin(); alg_iter != fAlgPool.end(); ++alg_iter) {
190  const Algorithm * alg = alg_iter->second;
191  stream << frame << endl;
192  stream << "Used algorithm: " << alg->Id() << endl;
193  stream << "Printing config:";
194  stream << alg->GetConfig();
195  }
196 
198  const Registry & gc = *(confp->GlobalParameterList());
199 
200  stream << frame << endl;
201  stream << "Printing global parameters list:";
202  stream << gc;
203 }
204 //____________________________________________________________________________
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
void Print(ostream &stream) const
print algorithm factory
Definition: AlgFactory.cxx:183
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
#define pERROR
Definition: Messenger.h:59
A singleton class holding all configuration registries built while parsing all loaded XML configurati...
Definition: AlgConfigPool.h:40
#define pFATAL
Definition: Messenger.h:56
Algorithm abstract base class.
Definition: Algorithm.h:53
virtual ~AlgFactory()
Definition: AlgFactory.cxx:42
intermediate_table::const_iterator const_iterator
singleton cleaner
Definition: AlgFactory.h:80
virtual const Registry & GetConfig(void) const
Definition: Algorithm.cxx:246
static AlgFactory * fInstance
sinleton&#39;s self
Definition: AlgFactory.h:74
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
string Name(void) const
Definition: AlgId.h:44
void ForceReconfiguration(bool ignore_alg_opt_out=false)
Definition: AlgFactory.cxx:131
def key(type, name=None)
Definition: graph.py:13
static Config * config
Definition: config.cpp:1054
virtual void Configure(const Registry &config)
Definition: Algorithm.cxx:62
const Algorithm * GetAlgorithm(const AlgId &algid)
Definition: AlgFactory.cxx:75
Algorithm * AdoptAlgorithm(const AlgId &algid) const
Definition: AlgFactory.cxx:116
Registry * GlobalParameterList(void) const
Algorithm * InstantiateAlgorithm(string name, string config) const
Definition: AlgFactory.cxx:151
Algorithm ID (algorithm name + configuration set name)
Definition: AlgId.h:34
virtual const AlgId & Id(void) const
Get algorithm ID.
Definition: Algorithm.h:97
static AlgFactory * Instance()
Definition: AlgFactory.cxx:64
virtual bool AllowReconfig(void) const
Definition: Algorithm.h:105
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
A registry. Provides the container for algorithm configuration parameters.
Definition: Registry.h:65
#define pNOTICE
Definition: Messenger.h:61
The GENIE Algorithm Factory.
Definition: AlgFactory.h:39
map< string, Algorithm * > fAlgPool
&#39;algorithm key&#39; (namespace::name/config) -> &#39;algorithmic object&#39; map
Definition: AlgFactory.h:77
#define SLOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a short string (using the FUNCTION and...
Definition: Messenger.h:84
QTextStream & endl(QTextStream &s)
static AlgConfigPool * Instance()
#define pDEBUG
Definition: Messenger.h:63
string Config(void) const
Definition: AlgId.h:45