CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlgoCheck.cc
Go to the documentation of this file.
3 #include <cstdio>
4 
5 bool AlgoCheck::check(parS_type & ps, parE_type & pe, std::string & err)
6 {
7  bool nok = false; // not ok - flag, negative logic inside the function!
8 
9  // first expression parameters
10  constraintsE_type::iterator e_it = constraintsE_.begin();
11  for (; e_it != constraintsE_.end(); ++e_it) {
12 
13  parE_type::iterator pit = pe.find(e_it->first);
14  if (e_it->second.use_) { // required parameter
15 
16  if (pit != pe.end()) {
17  nok |= checkBounds(pit,e_it,err);
18  }
19  else { // missing required parameter
20  err += std::string("\tmissing required parameter: ") + e_it->first + std::string("\n");
21  nok |= true;
22  }
23 
24  }
25 
26  else { // optional parameter
27  // an optional parameter MUST be here if a default-value is specified !
28 
29  DCOUT('e', "AlgoCheck::check(): optional param=" << e_it->first);
30 
31 
32  if (pit != pe.end()) { // optional parameter present
33  // (can be a possible default-val or a user-supplied-val)
34  nok |= checkBounds(pit,e_it,err);
35  }
36  else { // optional parameter absent although an default value is specified.
37  // provide the default value at least minOccurs-times
38  if (e_it->second.default_) {
39  for(int j=0; j<e_it->second.minOccurs_; ++j)
40  pe[e_it->first].push_back(e_it->second.defaultVal_);
41  }
42  }
43  }
44 
45  }
46 
47  // then std::string parameters
48  // FIXME: implement AlgoCheck::check std::string-valued parameter checks!
49 
50  constraintsS_type::iterator s_it = constraintsS_.begin();
51  for (; s_it != constraintsS_.end(); ++s_it) {
52 
53  parS_type::iterator sit = ps.find(s_it->first);
54  if (s_it->second.use_) { // required parameter
55 
56  if (sit != ps.end()) {
57  nok |= checkStrings(sit, s_it, err);
58  }
59  else { // missing required parameter
60  err += std::string("\tmissing required parameter: ") + s_it->first + std::string("\n");
61  nok |= true;
62  }
63  }
64  else { // optional parameter absent although a default value is specified.
65  // provide the default value at least minOccurs-times
66 
67  if (s_it->second.default_) {
68  for (int j = 0; j < s_it->second.minOccurs_; ++j)
69  ps[s_it->first].push_back(s_it->second.defaultVal_);
70  }
71  }
72  }
73  return !nok;
74 }
75 
76 
77 // checks occurences and bounds; returns true, if it is NOT OK !
78 bool AlgoCheck::checkBounds(parE_type::iterator pit,
79  constraintsE_type::iterator cit,
80  std::string & err
81  )
82 {
83  bool nok = false;
84 
85  DCOUT('e', "AlgoCheck::checkBounds(), par-name=" << pit->first );
86  DCOUT('e', " minOccurs = " << cit->second.minOccurs_ );
87  DCOUT('e', " maxOccurs = " << cit->second.maxOccurs_ );
88  DCOUT('e', " par-occurs = " << pit->second.size() );
89  // occurences
90  if (cit->second.minOccurs_ > int(pit->second.size()) ) {
91  err += "\tpar. " + cit->first
92  + " occurs=" + d2s(pit->second.size()) + std::string(" < minOccurs; minOccurs=")
93  + d2s(cit->second.minOccurs_) + std::string(" maxOccurs=")
94  + d2s(cit->second.maxOccurs_)
95  + "\n";
96  nok |= true;
97  DCOUT('e', " VIOLATION of minOccurs");
98  }
99  //if (!nok) {
100  if (cit->second.maxOccurs_ < int(pit->second.size()) ) {
101  err += "\tpar. " + cit->first
102  + " occurs=" + d2s(pit->second.size()) + std::string(" > maxOccurs; minOccurs=")
103  + d2s(cit->second.minOccurs_) + std::string(" maxOccurs=")
104  + d2s(cit->second.maxOccurs_)
105  + "\n";
106  nok |= true;
107  DCOUT('e', " VIOLATION of maxOccurs");
108  }
109  //}
110 
111  // min, max
112  // if (!nok) { // only check bounds if occurences were right!
113  int c=0; // error count
114  int i=0;
115  int m=int(pit->second.size());
116  for (;i<m;++i) {
117  if (pit->second[i] < cit->second.min_) {
118  err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("]=")
119  + d2s(pit->second[i]) + std::string(" < min; min=")
120  + d2s(cit->second.min_) + std::string(" max=") + d2s(cit->second.max_)
121  + "\n";
122  ++c; // error count
123  }
124  if (pit->second[i] > cit->second.max_) {
125  err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("]=")
126  + d2s(pit->second[i]) + std::string(" > max; min=")
127  + d2s(cit->second.min_) + std::string(" max=") + d2s(cit->second.max_)
128  + "\n";
129  ++c; // error count
130  }
131  }
132  if (c)
133  nok |= true;
134  //}
135  return nok;
136 }
137 
138 std::string
139 AlgoCheck::d2s( double x )
140 {
141  char buffer [25];
142  int len = snprintf( buffer, 25, "%g", x );
143  if( len >= 25 )
144  edm::LogError( "DoubleToString" ) << "Length truncated (from " << len << ")";
145  return std::string( buffer );
146 }
147 
148 bool
149 AlgoCheck::checkStrings( parS_type::iterator sit,
150  constraintsS_type::iterator cit,
151  std::string & err )
152 {
153  // occurences
154  bool nok = false;
155  if (cit->second.minOccurs_ > int(sit->second.size()) ) {
156  err += "\tpar. " + cit->first
157  + " occurs=" + d2s(sit->second.size()) + std::string(" < minOccurs; minOccurs=")
158  + d2s(cit->second.minOccurs_) + std::string(" maxOccurs=")
159  + d2s(cit->second.maxOccurs_)
160  + "\n";
161  nok |= true;
162  DCOUT('e', " VIOLATION of minOccurs");
163  }
164  //if (!nok) {
165  if (cit->second.maxOccurs_ < int(sit->second.size()) ) {
166  err += "\tpar. " + cit->first
167  + " occurs=" + d2s(sit->second.size()) + std::string(" > maxOccurs; minOccurs=")
168  + d2s(cit->second.minOccurs_) + std::string(" maxOccurs=")
169  + d2s(cit->second.maxOccurs_)
170  + "\n";
171  nok |= true;
172  DCOUT('e', " VIOLATION of maxOccurs");
173  }
174  //}
175 
176  int i = 0;
177  int m = int(sit->second.size());
178  for (; i<m;++i){
179 
180  std::string s = sit->second[i];
181  int ss = int(s.size());
182 
183  int j = 0;
184  for (; j < ss; ++j) {
185  if (s[j] != ' '){
186  break;
187  }
188  if (j == ss && s[j] == ' ') {
189  err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("] is blank");
190  nok |= true;
191  DCOUT('e', " VIOLATION of requiring non-blank std::strings");
192  }
193  }
194  }
195 
196  return nok;
197 
198 }
199 
200 
int i
Definition: DBlmapReader.cc:9
bool checkBounds(parE_type::iterator pit, constraintsE_type::iterator cit, std::string &err)
Definition: AlgoCheck.cc:78
static std::string d2s(double x)
ahh, converts a double into a std::string ... yet another one of this kind!
Definition: AlgoCheck.cc:139
bool check(parS_type &ps, parE_type &pe, std::string &err)
returns true if the check was successfull (parameters conform to XML specification) ...
Definition: AlgoCheck.cc:5
std::map< std::string, std::vector< std::string > > parS_type
Definition: DDAlgoPar.h:9
int j
Definition: DBlmapReader.cc:9
constraintsS_type constraintsS_
format: &quot;ParameterName&quot; -&gt; ConstraintS
Definition: AlgoCheck.h:76
constraintsE_type constraintsE_
format: &quot;ParameterName&quot; -&gt; ConstraintE
Definition: AlgoCheck.h:73
bool checkStrings(parS_type::iterator sit, constraintsS_type::iterator cit, std::string &err)
Definition: AlgoCheck.cc:149
x
Definition: VDTMath.h:216
#define DCOUT(M_v_Y, M_v_S)
Definition: DDdebug.h:53
std::map< std::string, std::vector< double > > parE_type
Definition: DDAlgoPar.h:12