CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/DetectorDescription/ExprAlgo/src/AlgoCheck.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/Base/interface/DDdebug.h"
00002 #include "DetectorDescription/ExprAlgo/interface/AlgoCheck.h"
00003 #include <cstdio>
00004 
00005 bool AlgoCheck::check(parS_type & ps, parE_type & pe, std::string & err)
00006 {
00007   bool nok = false; // not ok - flag, negative logic inside the function!
00008   
00009   // first expression parameters
00010   constraintsE_type::iterator e_it = constraintsE_.begin();
00011   for (; e_it != constraintsE_.end(); ++e_it) {
00012     
00013     parE_type::iterator pit = pe.find(e_it->first);
00014     if (e_it->second.use_) { // required parameter
00015       
00016       if (pit != pe.end()) {
00017         nok |= checkBounds(pit,e_it,err);
00018       }
00019       else { // missing required parameter
00020         err += std::string("\tmissing required parameter: ") + e_it->first + std::string("\n");
00021         nok |= true;
00022       }
00023         
00024     }
00025     
00026     else { // optional parameter
00027            // an optional parameter MUST be here if a default-value is specified !
00028       
00029       DCOUT('e', "AlgoCheck::check(): optional param=" << e_it->first);
00030       
00031       
00032       if (pit != pe.end()) { // optional parameter present 
00033                              // (can be a possible default-val or a user-supplied-val)
00034         nok |= checkBounds(pit,e_it,err);
00035       } 
00036       else { // optional parameter absent although an default value is specified. 
00037              // provide the default value at least minOccurs-times
00038         if (e_it->second.default_) {
00039           for(int j=0; j<e_it->second.minOccurs_; ++j)
00040             pe[e_it->first].push_back(e_it->second.defaultVal_);
00041         }
00042       }
00043     }
00044   
00045   }
00046   
00047   // then std::string parameters
00048   // FIXME: implement AlgoCheck::check std::string-valued parameter checks! 
00049 
00050   constraintsS_type::iterator s_it = constraintsS_.begin();
00051   for (; s_it != constraintsS_.end(); ++s_it) {
00052     
00053     parS_type::iterator sit = ps.find(s_it->first);
00054     if (s_it->second.use_) { // required parameter
00055       
00056       if (sit != ps.end()) {
00057         nok |= checkStrings(sit, s_it, err);
00058       }
00059       else { // missing required parameter
00060         err += std::string("\tmissing required parameter: ") + s_it->first + std::string("\n");
00061         nok |= true;
00062       }
00063     }
00064     else { // optional parameter absent although a default value is specified. 
00065            // provide the default value at least minOccurs-times
00066 
00067       if (s_it->second.default_) {
00068         for (int j = 0; j < s_it->second.minOccurs_; ++j)
00069           ps[s_it->first].push_back(s_it->second.defaultVal_);
00070       }
00071     }
00072   }
00073   return !nok;
00074 }
00075 
00076 
00077 // checks occurences and bounds; returns true, if it is NOT OK !
00078 bool AlgoCheck::checkBounds(parE_type::iterator pit, 
00079                             constraintsE_type::iterator cit, 
00080                             std::string & err 
00081                             )
00082 {
00083    bool nok = false;
00084    
00085    DCOUT('e', "AlgoCheck::checkBounds(), par-name=" << pit->first );
00086    DCOUT('e', "                          minOccurs  = " << cit->second.minOccurs_ );
00087    DCOUT('e', "                          maxOccurs  = " << cit->second.maxOccurs_ );
00088    DCOUT('e', "                          par-occurs = " << pit->second.size() );
00089    // occurences
00090    if (cit->second.minOccurs_ > int(pit->second.size()) ) {
00091      err += "\tpar. " + cit->first 
00092          +  " occurs=" + d2s(pit->second.size()) + std::string(" < minOccurs; minOccurs=")
00093          +  d2s(cit->second.minOccurs_) + std::string(" maxOccurs=") 
00094          +  d2s(cit->second.maxOccurs_)
00095          +  "\n";
00096      nok |= true; 
00097      DCOUT('e', "                          VIOLATION of minOccurs");
00098    }
00099    //if (!nok) {
00100      if (cit->second.maxOccurs_ < int(pit->second.size()) ) {
00101      err += "\tpar. " + cit->first 
00102          +  " occurs=" + d2s(pit->second.size()) + std::string(" > maxOccurs; minOccurs=")
00103          +  d2s(cit->second.minOccurs_) + std::string(" maxOccurs=") 
00104          +  d2s(cit->second.maxOccurs_)
00105          +  "\n";
00106        nok |= true;   
00107        DCOUT('e', "                          VIOLATION of maxOccurs");
00108      }
00109    //} 
00110    
00111    // min, max
00112   // if (!nok) { // only check bounds if occurences were right!
00113      int c=0; // error count
00114      int i=0;
00115      int m=int(pit->second.size());
00116      for (;i<m;++i) {
00117        if (pit->second[i] < cit->second.min_) {
00118          err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("]=")
00119              +  d2s(pit->second[i]) + std::string(" < min; min=")
00120              +  d2s(cit->second.min_) + std::string(" max=") +  d2s(cit->second.max_)
00121              +  "\n";
00122          ++c; // error count
00123        }
00124        if (pit->second[i] > cit->second.max_) {
00125          err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("]=")
00126              +  d2s(pit->second[i]) + std::string(" > max; min=")
00127              +  d2s(cit->second.min_) + std::string(" max=") +  d2s(cit->second.max_)
00128              +  "\n";
00129          ++c; // error count       
00130        }
00131      }
00132      if (c)
00133       nok |= true;
00134    //}
00135    return nok;
00136 }
00137 
00138 std::string
00139 AlgoCheck::d2s( double x )
00140 {
00141   char buffer [25];
00142   int len = snprintf( buffer, 25, "%g", x );
00143   if( len >= 25 )
00144     edm::LogError( "DoubleToString" ) << "Length truncated (from " << len << ")";
00145   return std::string( buffer );
00146 }
00147 
00148 bool
00149 AlgoCheck::checkStrings( parS_type::iterator sit, 
00150                          constraintsS_type::iterator cit, 
00151                          std::string & err )
00152 {
00153    // occurences
00154    bool nok = false;
00155    if (cit->second.minOccurs_ > int(sit->second.size()) ) {
00156      err += "\tpar. " + cit->first 
00157          +  " occurs=" + d2s(sit->second.size()) + std::string(" < minOccurs; minOccurs=")
00158          +  d2s(cit->second.minOccurs_) + std::string(" maxOccurs=") 
00159          +  d2s(cit->second.maxOccurs_)
00160          +  "\n";
00161      nok |= true; 
00162      DCOUT('e', "                          VIOLATION of minOccurs");
00163    }
00164    //if (!nok) {
00165      if (cit->second.maxOccurs_ < int(sit->second.size()) ) {
00166      err += "\tpar. " + cit->first 
00167          +  " occurs=" + d2s(sit->second.size()) + std::string(" > maxOccurs; minOccurs=")
00168          +  d2s(cit->second.minOccurs_) + std::string(" maxOccurs=") 
00169          +  d2s(cit->second.maxOccurs_)
00170          +  "\n";
00171        nok |= true;   
00172        DCOUT('e', "                          VIOLATION of maxOccurs");
00173      }
00174    //} 
00175 
00176   int i = 0;
00177   int m = int(sit->second.size());  
00178   for (; i<m;++i){
00179 
00180     std::string s = sit->second[i];
00181     int ss = int(s.size());
00182 
00183     int j = 0;
00184     for (; j < ss; ++j) {
00185       if (s[j] != ' '){
00186         break;
00187       }
00188       if (j == ss && s[j] == ' ') {
00189         err += "\tpar. " + cit->first + std::string("[") + d2s(i) + std::string("] is blank");
00190         nok |= true;
00191         DCOUT('e', "                         VIOLATION of requiring non-blank std::strings");
00192       }
00193     }
00194   }
00195 
00196   return nok;
00197 
00198 }
00199 
00200