Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CMProcessor.cc
Go to the documentation of this file.
1 #include "marlin/CMProcessor.h"
2 
3 #include <iostream>
4 
5 using namespace std;
6 
7 namespace marlin{
8 
9  CMProcessor* CMProcessor::_me = NULL;
10 
11  CMProcessor* CMProcessor::instance() {
12  if( _me == NULL ){
13  _me = new CMProcessor;
14  }
15  return _me ;
16  }
17 
18  CMProcessor::CMProcessor(){
19 
20  //get a list of all available processor types from the marlin Processor Manager
21  _mpTypes = ProcessorMgr::instance()->getAvailableProcessorTypes();
22 
23  for( sSet::const_iterator p = _mpTypes.begin() ; p != _mpTypes.end() ; p++ ){
24 
25  Processor* pp = ProcessorMgr::instance()->getProcessor( (*p) );
26  //checks if marlin has this processor type installed
27  if( pp ){
28 
29  //create a new marlin processor of this type
30  _mProcs[ (*p) ] = pp->newProcessor();
31 
32  //set it's status
33  _mpStatus[ (*p) ] = true;
34 
35  //add it's description
36  _mpDescriptions[ (*p) ] = pp->description();
37 
38  //create new string parameters for each processor
39  _mpSParameters[ (*p) ] = std::make_shared<StringParameters>();
40 
41  //set the parameters keys
42  _mProcs[ (*p) ]->setProcessorParameters( _mpSParameters[ (*p) ] );
43  _mProcs[ (*p) ]->updateParameters();
44 
45  //get the parameters keys
46  StringVec keys;
47  _mpSParameters[ (*p) ]->getStringKeys( keys );
48 
49  //for each key
50  for( unsigned i=0; i<keys.size(); i++ ){
51  StringVec values;
52  values.push_back( keys[i] );
53 
54  //set the parameter with the default value
55  //since the default value is always a string we might have to tokenize it
56  if( isParamVec( *p, keys[i] )){
57  tokenize( getParam( (*p), keys[i] )->defaultValue(), values );
58  }
59  else{
60  values.push_back( getParam( (*p), keys[i] )->defaultValue() );
61  }
62  //add the default value to the parameters
63  _mpSParameters[ (*p) ]->add(values);
64  }
65  }
66  //this processor type is not installed
67  else{
68  _mProcs[ (*p) ] = NULL;
69 
70  //set it's status
71  _mpStatus[ (*p) ] = false;
72 
73  //add it's description
74  _mpDescriptions[ (*p) ] = "This processor is NOT installed!! Please install it before using it...";
75 
76  //set string parameters
77  _mpSParameters[ (*p) ] = NULL;
78  }
79  }
80  } //end constructor
81 
82  //destructor
83  CMProcessor::~CMProcessor(){}
84 
85  bool CMProcessor::isInstalled( const string& type ){
86  if( _mpStatus.find( type ) != _mpStatus.end() ){
87  return _mpStatus[ type ];
88  }
89  return false;
90  }
91 
92  Processor* CMProcessor::getProc( const string& type ){
93  if( isInstalled( type )){
94  return _mProcs[ type ];
95  }
96  return NULL;
97  }
98 
99  std::shared_ptr<StringParameters> CMProcessor::getSParams( const string& type ){
100  if( isInstalled( type )){
101  return _mpSParameters[ type ];
102  }
103  return NULL;
104  }
105 
106  std::shared_ptr<StringParameters> CMProcessor::mergeParams( const string& type, std::shared_ptr<StringParameters> sp ){
107  if( isInstalled( type )){
108  if( sp ){
109  StringVec keys;
110  //get the keys for the default string parameters of this processor
111  _mpSParameters[ type ]->getStringKeys( keys );
112 
113  //for each key
114  for( unsigned i=0; i<keys.size(); i++ ){
115 
116  //check if the parameter value is already set
117  if( !sp->isParameterSet( keys[i] )){
118 
119  //get the default values for this key
120  StringVec values;
121  _mpSParameters[ type ]->getStringVals( keys[i], values );
122 
123  //add the default parameter to the given (xml) parameters
124  sp->add( keys[i], values);
125  }
126  //parameter is already set in the xml file
127  else{
128  //if the parameter is an optional parameter that isn't
129  //commented out in the given xml file we don't want to
130  //write it as a comment when saving to an xml file again,
131  //so we pass this information to the CCProcessor
132  if( isParamOpt( type, keys[i] )){
133  StringVec optParams;
134  optParams.push_back( keys[i] );
135  sp->add( "Opt_Params_Set", optParams );
136  }
137  }
138  }
139  return sp;
140  }
141  //if processor is installed and no parameters were passed (from the xml file)
142  //create a copy from the default parameters for this processor and return it
143  std::shared_ptr<StringParameters> defaultSP= std::make_shared<StringParameters>( *(_mpSParameters[ type ].get()) );
144 
145  return defaultSP;
146  }
147  //if processor is not installed no parameters can be merged
148  //just return the given parameters back
149  return sp;
150  }
151 
152  ProcessorParameter* CMProcessor::getParam( const string& type, const string& key ){
153  if( isInstalled( type )){
154  ProcParamMap::const_iterator p = _mProcs[ type ]->procMap().find( key );
155  if( p != _mProcs[ type ]->procMap().end() ){
156  ProcessorParameter* par = p->second;
157  if( par ){
158  return par;
159  }
160  }
161  }
162  return NULL;
163  }
164 
165  const string CMProcessor::getParamD( const string& type, const string& key ){
166  ProcessorParameter* par = getParam( type, key );
167  if( par ){
168  return par->description();
169  }
170  return "Sorry, no description for this parameter";
171  }
172 
173  const string CMProcessor::getParamT( const string& type, const string& key ){
174  ProcessorParameter* par = getParam( type, key );
175  if( par ){
176  return par->type();
177  }
178  return "Undefined!!";
179  }
180 
181  int CMProcessor::getParamSetSize( const string& type, const string& key ){
182  ProcessorParameter* par = getParam( type, key );
183  if( par ){
184  return par->setSize();
185  }
186  return 0;
187  }
188 
189  bool CMProcessor::isParamOpt( const string& type, const string& key ){
190  ProcessorParameter* par = getParam( type, key );
191  if( par ){
192  return par->isOptional();
193  }
194  //for consistency reasons return false if parameter isn't recognized
195  return false;
196  }
197 
198  bool CMProcessor::isParamVec( const std::string& type, const std::string& key ){
199  ProcessorParameter* par = getParam( type, key );
200  if( par ){
201  if( par->type() == "StringVec" || par->type() == "FloatVec" || par->type() == "IntVec" ){
202  return true;
203  }
204  else{
205  return false;
206  }
207  }
208  //for consistency reasons return true if parameter isn't recognized
209  return true;
210  }
211 
212  void CMProcessor::tokenize( const string str, StringVec& tokens, const string& delimiters ){
213  // Skip delimiters at beginning.
214  string::size_type lastPos = str.find_first_not_of(delimiters, 0);
215  // Find first "non-delimiter".
216  string::size_type pos = str.find_first_of(delimiters, lastPos);
217 
218  while( string::npos != pos || string::npos != lastPos ){
219  // Found a token, add it to the vector.
220  tokens.push_back( str.substr( lastPos, pos - lastPos ));
221  // Skip delimiters. Note the "not_of"
222  lastPos = str.find_first_not_of( delimiters, pos );
223  // Find next "non-delimiter"
224  pos = str.find_first_of( delimiters, lastPos );
225  }
226  }
227 
228 } //end namespace marlin
229 
virtual const std::string type()=0
This singleton class contains an instance of every available marlin processor type.
Definition: CMProcessor.h:21
T find_first_not_of(T...args)
virtual Processor * newProcessor()=0
Return a new instance of the processor.
T end(T...args)
STL class.
T push_back(T...args)
T find_first_of(T...args)
const std::string & description()
Description of processor.
Definition: Processor.h:193
virtual const std::string & description()
Class that holds a steering variable for a marlin processor - automatically created by Processor::reg...
T size(T...args)
T substr(T...args)
Base class for Marlin processors.
Definition: Processor.h:64