Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
// -*- C++ -*-
//
// Package:     Core
// Class  :     FWExpressionEvaluator
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Fri Feb 29 13:39:56 PST 2008
//

// system include files
#include <sstream>

#include "FWCore/Reflection/interface/ObjectWithDict.h"

// user include files
#include "Fireworks/Core/interface/FWExpressionEvaluator.h"
#include "Fireworks/Core/interface/FWExpressionException.h"
#include "CommonTools/Utils/interface/parser/Grammar.h"
#include "CommonTools/Utils/interface/parser/Exception.h"

#include "Fireworks/Core/src/expressionFormatHelpers.h"

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
FWExpressionEvaluator::FWExpressionEvaluator(const std::string& iExpression, const std::string& iClassName)
    : m_className(iClassName), m_type(edm::TypeWithDict::byName(iClassName)) {
  setExpression(iExpression);
}

// FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
// {
//    // do actual copying here;
// }

FWExpressionEvaluator::~FWExpressionEvaluator() {}

//
// assignment operators
//
// const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
// {
//   //An exception safe implementation is
//   FWExpressionEvaluator temp(rhs);
//   swap(rhs);
//
//   return *this;
// }

//
// member functions
//
void FWExpressionEvaluator::setExpression(const std::string& iExpression) {
  if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
    using namespace fireworks::expression;

    //Backwards compatibility with old format
    std::string temp = oldToNewFormat(iExpression);

    //now setup the parser
    using namespace boost::spirit::classic;
    reco::parser::ExpressionPtr tmpPtr;
    reco::parser::Grammar grammar(tmpPtr, m_type);
    try {
      if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
        m_expr = tmpPtr;
        m_expression = iExpression;
      } else {
        throw FWExpressionException("syntax error", -1);
        //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
      }
    } catch (const reco::parser::BaseException& e) {
      //NOTE: need to calculate actual position before doing the regex
      throw FWExpressionException(reco::parser::baseExceptionWhat(e),
                                  indexFromNewFormatToOldFormat(temp, e.where - temp.c_str(), iExpression));
      //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
    }
  } else {
    m_expression = iExpression;
  }
}

void FWExpressionEvaluator::setClassName(const std::string& iClassName) {
  //NOTE: How do we handle the case where the filter was created before
  // the library for the class was loaded and therefore we don't have
  // a dictionary for it?

  m_className = iClassName;
  m_type = edm::TypeWithDict::byName(iClassName);
  setExpression(m_expression);
}

//
// const member functions
//
const std::string& FWExpressionEvaluator::expression() const { return m_expression; }

double FWExpressionEvaluator::evalExpression(const void* iObject) const {
  if (m_expression.empty() || !m_expr.get()) {
    return 0;
  }

  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
  return m_expr->value(o);
}

//
// static member functions
//