Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:28

0001 #ifndef L1GlobalTrigger_L1GtLogicParser_h
0002 #define L1GlobalTrigger_L1GtLogicParser_h
0003 
0004 /**
0005  * \class L1GtLogicParser
0006  *
0007  *
0008  * Description: parses a logical expression, with predefined operators.
0009  *
0010  * Implementation:
0011  *    <TODO: enter implementation details>
0012  *
0013  * \author: Vasile Mihai Ghete - HEPHY Vienna
0014  *
0015  *
0016  */
0017 
0018 // system include files
0019 #include <string>
0020 #include <vector>
0021 #include <list>
0022 #include <map>
0023 
0024 #include <iosfwd>
0025 
0026 // user include files
0027 
0028 // forward declarations
0029 
0030 // class declaration
0031 class L1GtLogicParser {
0032 public:
0033   struct OperandToken {
0034     std::string tokenName;
0035     int tokenNumber;
0036     bool tokenResult;
0037   };
0038 
0039   enum OperationType {
0040     OP_NULL = 1,
0041     OP_INVALID = 2,
0042     OP_AND = 4,
0043     OP_OR = 8,
0044     OP_NOT = 16,
0045     OP_OPERAND = 32,
0046     OP_OPENBRACKET = 64,
0047     OP_CLOSEBRACKET = 128
0048   };
0049 
0050   struct TokenRPN {
0051     OperationType operation;  // type of operation: AND, OR, NOT or OPERAND
0052     std::string operand;      // a possible operand
0053   };
0054 
0055   typedef std::vector<TokenRPN> RpnVector;
0056 
0057 public:
0058   /// constructor(s)
0059 
0060   ///   default constructor
0061   L1GtLogicParser();
0062 
0063   ///   from the RPN vector and the operand token vector
0064   ///   no checks for consistency, empty logical and numerical expressions
0065   ///   requires special care when used
0066   L1GtLogicParser(const RpnVector&, const std::vector<OperandToken>&);
0067 
0068   ///   from a constant logical expression
0069   ///   numerical expression will be empty
0070   L1GtLogicParser(const std::string& logicalExpressionVal);
0071 
0072   //   from a non-constant logical expression - add/remove spaces if needed
0073   //   numerical expression will be empty
0074   L1GtLogicParser(std::string& logicalExpressionVal);
0075 
0076   ///   from a logical and a numerical expression
0077   L1GtLogicParser(const std::string logicalExpressionVal, const std::string numericalExpressionVal);
0078 
0079   ///   from a logical and a numerical expression
0080   ///   no checks for correctness - use it only after the correctness was tested
0081   L1GtLogicParser(const std::string& logicalExpressionVal, const std::string& numericalExpressionVal, const bool dummy);
0082 
0083   /// destructor
0084   virtual ~L1GtLogicParser();
0085 
0086 public:
0087   /// return the logical expression
0088   inline std::string logicalExpression() const { return m_logicalExpression; }
0089 
0090   /// check a logical expression for correctness - add/remove spaces if needed
0091   bool checkLogicalExpression(std::string&);
0092 
0093   /// return the numerical expression
0094   inline std::string numericalExpression() const { return m_numericalExpression; }
0095 
0096 public:
0097   /// build the rpn vector
0098   bool buildRpnVector(const std::string&);
0099 
0100   /// clear possible old rpn vector
0101   void clearRpnVector();
0102 
0103   /// return the RPN vector
0104   inline RpnVector rpnVector() const { return m_rpnVector; }
0105 
0106   /// build from the RPN vector the operand token vector
0107   /// dummy tokenNumber and tokenResult
0108   void buildOperandTokenVector();
0109 
0110   /// return the vector of operand tokens
0111   inline std::vector<OperandToken>& operandTokenVector() { return m_operandTokenVector; }
0112   inline const std::vector<OperandToken>& operandTokenVector() const { return m_operandTokenVector; }
0113 
0114 public:
0115   /// return the position index of the operand in the logical expression
0116   int operandIndex(const std::string& operandNameVal) const;
0117 
0118   /// return the name of the (iOperand)th operand in the logical expression
0119   std::string operandName(const int iOperand) const;
0120 
0121   /// return the result for an operand with name operandNameVal
0122   /// in the logical expression using the operand token vector
0123   bool operandResult(const std::string& operandNameVal) const;
0124 
0125   /// return the result for an operand with tokenNumberVal
0126   /// using the operand token vector
0127   bool operandResult(const int tokenNumberVal) const;
0128 
0129   /// return the result for the logical expression
0130   /// require a proper operand token vector
0131   virtual const bool expressionResult() const;
0132 
0133   /// return the result for an operand with name operandNameVal
0134   /// in the logical expression using a numerical expression
0135   bool operandResultNumExp(const std::string& operandNameVal) const;
0136 
0137   /// return the result for an operand with index iOperand
0138   /// in the logical expression using a numerical expression
0139   bool operandResultNumExp(const int iOperand) const;
0140 
0141   /// build from the RPN vector the operand token vector
0142   /// using a numerical expression
0143   void buildOperandTokenVectorNumExp();
0144 
0145   /// return the result for the logical expression
0146   /// require a proper numerical expression
0147   virtual const bool expressionResultNumExp() const;
0148 
0149   /// convert the logical expression composed with names to
0150   /// a logical expression composed with int numbers using
0151   /// a (string, int)  map
0152   void convertNameToIntLogicalExpression(const std::map<std::string, int>& nameToIntMap);
0153 
0154   /// convert a logical expression composed with integer numbers to
0155   /// a logical expression composed with names using a map (int, string)
0156 
0157   void convertIntToNameLogicalExpression(const std::map<int, std::string>& intToNameMap);
0158 
0159   /// return the list of operand tokens for the logical expression
0160   /// which are to be used as seeds
0161   std::vector<L1GtLogicParser::OperandToken> expressionSeedsOperandList();
0162 
0163 protected:
0164   struct OperationRule {
0165     const char* opString;
0166     int opType;
0167     int forbiddenLastOperation;  // int for bitmask of forbidden operations
0168   };
0169 
0170   virtual OperationType getOperation(const std::string& tokenString,
0171                                      OperationType lastOperation,
0172                                      TokenRPN& rpnToken) const;
0173 
0174   /// get the rule entry to an operation type
0175   const OperationRule* getRuleFromType(OperationType t);
0176 
0177   static const struct OperationRule m_operationRules[];
0178 
0179 protected:
0180   /// add spaces before and after parantheses
0181   void addBracketSpaces(const std::string&, std::string&);
0182 
0183   /// set the logical expression - check for correctness the input string
0184   bool setLogicalExpression(const std::string&);
0185 
0186   /// set the numerical expression (the logical expression with each operand
0187   /// replaced with the value) from a string
0188   /// check also for correctness the input string
0189   bool setNumericalExpression(const std::string&);
0190 
0191 protected:
0192   /// logical expression to be parsed
0193   std::string m_logicalExpression;
0194 
0195   /// numerical expression
0196   /// (logical expression with operands replaced with the actual values)
0197   std::string m_numericalExpression;
0198 
0199   /// RPN vector - equivalent to the logical expression
0200   RpnVector m_rpnVector;
0201 
0202   /// vector of operand tokens
0203   std::vector<OperandToken> m_operandTokenVector;
0204 };
0205 
0206 #endif /*L1GlobalTrigger_L1GtLogicParser_h*/