Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:08

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWExpressionEvaluator
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Feb 29 13:39:56 PST 2008
0011 //
0012 
0013 // system include files
0014 #include <sstream>
0015 
0016 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0017 
0018 // user include files
0019 #include "Fireworks/Core/interface/FWExpressionEvaluator.h"
0020 #include "Fireworks/Core/interface/FWExpressionException.h"
0021 #include "CommonTools/Utils/interface/Grammar.h"
0022 #include "CommonTools/Utils/interface/Exception.h"
0023 
0024 #include "Fireworks/Core/src/expressionFormatHelpers.h"
0025 
0026 //
0027 // constants, enums and typedefs
0028 //
0029 
0030 //
0031 // static data member definitions
0032 //
0033 
0034 //
0035 // constructors and destructor
0036 //
0037 FWExpressionEvaluator::FWExpressionEvaluator(const std::string& iExpression, const std::string& iClassName)
0038     : m_className(iClassName), m_type(edm::TypeWithDict::byName(iClassName)) {
0039   setExpression(iExpression);
0040 }
0041 
0042 // FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
0043 // {
0044 //    // do actual copying here;
0045 // }
0046 
0047 FWExpressionEvaluator::~FWExpressionEvaluator() {}
0048 
0049 //
0050 // assignment operators
0051 //
0052 // const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
0053 // {
0054 //   //An exception safe implementation is
0055 //   FWExpressionEvaluator temp(rhs);
0056 //   swap(rhs);
0057 //
0058 //   return *this;
0059 // }
0060 
0061 //
0062 // member functions
0063 //
0064 void FWExpressionEvaluator::setExpression(const std::string& iExpression) {
0065   if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
0066     using namespace fireworks::expression;
0067 
0068     //Backwards compatibility with old format
0069     std::string temp = oldToNewFormat(iExpression);
0070 
0071     //now setup the parser
0072     using namespace boost::spirit::classic;
0073     reco::parser::ExpressionPtr tmpPtr;
0074     reco::parser::Grammar grammar(tmpPtr, m_type);
0075     try {
0076       if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
0077         m_expr = tmpPtr;
0078         m_expression = iExpression;
0079       } else {
0080         throw FWExpressionException("syntax error", -1);
0081         //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
0082       }
0083     } catch (const reco::parser::BaseException& e) {
0084       //NOTE: need to calculate actual position before doing the regex
0085       throw FWExpressionException(reco::parser::baseExceptionWhat(e),
0086                                   indexFromNewFormatToOldFormat(temp, e.where - temp.c_str(), iExpression));
0087       //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
0088     }
0089   } else {
0090     m_expression = iExpression;
0091   }
0092 }
0093 
0094 void FWExpressionEvaluator::setClassName(const std::string& iClassName) {
0095   //NOTE: How do we handle the case where the filter was created before
0096   // the library for the class was loaded and therefore we don't have
0097   // a dictionary for it?
0098 
0099   m_className = iClassName;
0100   m_type = edm::TypeWithDict::byName(iClassName);
0101   setExpression(m_expression);
0102 }
0103 
0104 //
0105 // const member functions
0106 //
0107 const std::string& FWExpressionEvaluator::expression() const { return m_expression; }
0108 
0109 double FWExpressionEvaluator::evalExpression(const void* iObject) const {
0110   if (m_expression.empty() || !m_expr.get()) {
0111     return 0;
0112   }
0113 
0114   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0115   return m_expr->value(o);
0116 }
0117 
0118 //
0119 // static member functions
0120 //