Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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