24 #include <boost/algorithm/string.hpp> 55 throw cms::Exception(
"FailModule") <<
"\nError in parsing the logical expression = " << logicalExpressionVal
74 throw cms::Exception(
"FailModule") <<
"\nError in parsing the logical expression = " << logicalExpressionVal
83 logicalExpressionVal = logicalExpressionBS;
97 throw cms::Exception(
"FailModule") <<
"\nError in parsing the logical expression = " << logicalExpressionVal
103 throw cms::Exception(
"FileModule") <<
"\nError in parsing the numerical expression = " << numericalExpressionVal
115 throw cms::Exception(
"FileModule") <<
"\nError in building RPN vector for the logical expression = " 116 << logicalExpressionVal << std::endl;
145 LogDebug(
"L1GtLogicParser") <<
"\nL1GtLogicParser::checkLogicalExpression - " 146 <<
"\nInitial logical expression = '" << logicalExpressionVal <<
"'" 147 <<
"\nFinal logical expression = '" << logicalExpressionBS <<
"'\n" 150 logicalExpressionVal = logicalExpressionBS;
176 std::stack<TokenRPN> operatorStack;
178 static const std::string whitespaces =
" \r\v\n\t";
184 std::istringstream exprStringStream(logicalExpressionVal);
186 while (!exprStringStream.eof()) {
187 exprStringStream >> std::skipws >>
std::ws >> tokenString;
190 if (tokenString.find_first_not_of(whitespaces) == std::string::npos || tokenString.length() == 0) {
198 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
207 switch (actualOperation) {
215 int errorPosition = exprStringStream.tellg();
217 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 218 <<
"\n Syntax error during parsing: " 219 <<
"\n " << exprStringStream.str().substr(0, errorPosition) <<
"\n " 220 << exprStringStream.str().substr(errorPosition)
221 <<
"\n Returned empty RPN vector and result false." << std::endl;
231 operatorStack.push(rpnToken);
238 while (!operatorStack.empty() && operatorStack.top().operation ==
OP_NOT) {
242 operatorStack.push(rpnToken);
248 while (!operatorStack.empty() &&
249 (operatorStack.top().operation ==
OP_NOT || operatorStack.top().operation ==
OP_AND)) {
254 operatorStack.push(rpnToken);
260 operatorStack.push(rpnToken);
266 if (operatorStack.empty()) {
267 int errorPosition = exprStringStream.tellg();
269 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 270 <<
"\n Syntax error during parsing - misplaced ')':" 271 <<
"\n " << exprStringStream.str().substr(0, errorPosition) <<
"\n " 272 << exprStringStream.str().substr(errorPosition)
273 <<
"\n Returned empty RPN vector and result false." << std::endl;
287 if (operatorStack.empty()) {
289 int errorPosition = exprStringStream.tellg();
291 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 292 <<
"\n Syntax error during parsing - misplaced ')':" 293 <<
"\n " << exprStringStream.str().substr(0, errorPosition)
294 <<
"\n " << exprStringStream.str().substr(errorPosition)
295 <<
"\n Returned empty RPN vector and result false." << std::endl;
312 lastOperation = actualOperation;
316 while (!operatorStack.empty()) {
318 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 319 <<
"\n Syntax error during parsing - missing ')':" 320 <<
"\n Returned empty RPN vector and result false." << std::endl;
336 if (it->operation ==
OP_OR || it->operation ==
OP_AND)
339 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 340 <<
"\n Syntax error during parsing - too many operators" 341 <<
"\n Returned empty RPN vector and result false." << std::endl;
350 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 351 <<
"\n Syntax error during parsing - too many operands" 352 <<
"\n Returned empty RPN vector and result false." << std::endl;
384 switch (it->operation) {
438 while (!exprStringStream.eof()) {
439 exprStringStream >> tokenString;
445 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
449 <<
"\n Invalid operation/operand " << operandNameVal
450 <<
"\n Returned index is by default out of range (-1)." << std::endl;
460 if (rpnToken.
operand == operandNameVal) {
472 lastOperation = actualOperation;
477 <<
"\n Operand " << operandNameVal <<
" not found in the logical expression" 478 <<
"\n Returned index is by default out of range (-1)." << std::endl;
499 while (!exprStringStream.eof()) {
500 exprStringStream >> tokenString;
506 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
510 <<
"\n Invalid operation/operand at position " << iOperand
511 <<
"\n Returned empty name by default." << std::endl;
521 if (tmpIndex == iOperand) {
533 lastOperation = actualOperation;
538 <<
"\n No operand found at position " << iOperand
539 <<
"\n Returned empty name by default." << std::endl;
554 edm::LogError(
"L1GtLogicParser") <<
"\n Operand " << operandNameVal <<
" not found in the operand token vector" 555 <<
"\n Returned false by default." << std::endl;
570 edm::LogError(
"L1GtLogicParser") <<
"\n No operand with token number " << tokenNumberVal
571 <<
" found in the operand token vector" 572 <<
"\n Returned false by default." << std::endl;
586 edm::LogError(
"L1GtLogicParser") <<
"\n No built RPN vector exists." 587 <<
"\n Returned false by default." << std::endl;
592 std::stack<bool> resultStack;
601 switch (it->operation) {
608 b1 = resultStack.top();
610 resultStack.push(!
b1);
615 b1 = resultStack.top();
617 b2 = resultStack.top();
619 resultStack.push(
b1 ||
b2);
624 b1 = resultStack.top();
626 b2 = resultStack.top();
628 resultStack.push(
b1 &&
b2);
647 return resultStack.top();
682 while (!exprStringStream.eof()) {
683 exprStringStream >> tokenString;
689 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
693 <<
"\n Invalid operation/operand at position " << iOperand
694 <<
"\n Returned false by default." << std::endl;
705 if (tmpIndex == iOperand) {
715 <<
"\n Invalid result for operand at position " << iOperand <<
": " 716 << rpnToken.
operand <<
"\n It must be 0 or 1" 717 <<
"\n Returned false by default." << std::endl;
734 lastOperation = actualOperation;
739 <<
"\n No operand found at position " << iOperand
740 <<
"\n Returned false by default." << std::endl;
764 switch (it->operation) {
810 edm::LogError(
"L1GtLogicParser") <<
"\n No built RPN vector exists." 811 <<
"\n Returned false by default." << std::endl;
816 std::stack<bool> resultStack;
825 switch (it->operation) {
832 b1 = resultStack.top();
834 resultStack.push(!
b1);
839 b1 = resultStack.top();
841 b2 = resultStack.top();
843 resultStack.push(
b1 ||
b2);
848 b1 = resultStack.top();
850 b2 = resultStack.top();
852 resultStack.push(
b1 &&
b2);
873 return resultStack.top();
899 while (!exprStringStream.eof()) {
900 exprStringStream >> tokenString;
902 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
906 <<
"\n Invalid operation/operand in logical expression." 907 <<
"\n Return empty logical expression." << std::endl;
914 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
917 typedef std::map<std::string, int>::const_iterator CIter;
919 CIter it = nameToIntMap.find(rpnToken.
operand);
920 if (it != nameToIntMap.end()) {
922 std::stringstream intStr;
924 convertedLogicalExpression.append(intStr.str());
929 <<
"\n Could not convert " << rpnToken.
operand <<
" to integer!" 930 <<
"\n Return empty logical expression." << std::endl;
937 convertedLogicalExpression.append(
" ");
938 lastOperation = actualOperation;
945 LogDebug(
"L1GtLogicParser") <<
"\nL1GtLogicParser::convertNameToIntLogicalExpression - " 947 <<
"\nLogical expression (int) = '" << convertedLogicalExpression <<
"'\n" 978 while (!exprStringStream.eof()) {
979 exprStringStream >> tokenString;
981 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
985 <<
"\n Invalid operation/operand in logical expression." 986 <<
"\n Return empty logical expression." << std::endl;
993 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
996 typedef std::map<int, std::string>::const_iterator CIter;
1000 std::istringstream iss(rpnToken.
operand);
1003 CIter it = intToNameMap.find(indexInt);
1004 if (it != intToNameMap.end()) {
1005 convertedLogicalExpression.append(it->second);
1010 <<
"\n Could not convert " << rpnToken.
operand <<
" to string!" 1011 <<
"\n Return empty logical expression." << std::endl;
1018 convertedLogicalExpression.append(
" ");
1019 lastOperation = actualOperation;
1051 std::vector<OperandToken> opVector;
1055 std::stack<OperandToken> tmpStack;
1056 std::vector<OperandToken> tmpVector;
1061 bool newOperandBlock =
true;
1062 bool oneBlockOnly =
true;
1063 bool operandOnly =
true;
1078 switch (it->operation) {
1084 if ((!newOperandBlock)) {
1085 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin(); itOp != tmpVector.rend();
1087 opVector.push_back(*itOp);
1097 newOperandBlock =
true;
1098 oneBlockOnly =
false;
1113 newOperandBlock =
false;
1114 operandOnly =
false;
1116 b1 = tmpStack.top();
1119 tmpStack.push(dummyToken);
1131 newOperandBlock =
false;
1132 operandOnly =
false;
1134 b1 = tmpStack.top();
1136 b2 = tmpStack.top();
1139 tmpStack.push(dummyToken);
1141 if (
b1.tokenNumber >= 0) {
1142 tmpVector.push_back(
b1);
1150 if (
b2.tokenNumber >= 0) {
1151 tmpVector.push_back(
b2);
1163 newOperandBlock =
false;
1164 operandOnly =
false;
1166 b1 = tmpStack.top();
1168 b2 = tmpStack.top();
1171 tmpStack.push(dummyToken);
1173 if (
b1.tokenNumber >= 0) {
1174 tmpVector.push_back(
b1);
1182 if (
b2.tokenNumber >= 0) {
1183 tmpVector.push_back(
b2);
1203 if (oneBlockOnly || operandOnly) {
1207 b1 = tmpStack.top();
1208 tmpVector.push_back(
b1);
1212 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin(); itOp != tmpVector.rend(); itOp++) {
1213 opVector.push_back(*itOp);
1225 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin(); itOp != tmpVector.rend(); itOp++) {
1226 opVector.push_back(*itOp);
1237 std::vector<OperandToken> opVectorU;
1238 opVectorU.reserve(opVector.size());
1240 for (std::vector<OperandToken>::const_iterator constIt = opVector.begin(); constIt != opVector.end(); constIt++) {
1241 bool tokenIncluded =
false;
1243 for (std::vector<OperandToken>::iterator itOpU = opVectorU.begin(); itOpU != opVectorU.end(); itOpU++) {
1244 if ((*itOpU).tokenName == (*constIt).tokenName) {
1245 tokenIncluded =
true;
1250 if (!tokenIncluded) {
1251 opVectorU.push_back(*constIt);
1293 rpnToken.
operand = tokenString;
1302 return actualOperation;
1333 dstExpression = srcExpression;
1338 if (((
position + 1) != std::string::npos) && (dstExpression[
position + 1] !=
' ')) {
1339 dstExpression.insert(
position + 1,
" ");
1344 dstExpression.insert(
position,
" ");
std::vector< L1GtLogicParser::OperandToken > expressionSeedsOperandList()
void clearRpnVector()
clear possible old rpn vector
void convertNameToIntLogicalExpression(const std::map< std::string, int > &nameToIntMap)
bool operandResult(const std::string &operandNameVal) const
bool operandResultNumExp(const std::string &operandNameVal) const
int operandIndex(const std::string &operandNameVal) const
return the position index of the operand in the logical expression
bool buildRpnVector(const std::string &)
build the rpn vector
static void trim(std::string &s)
void buildOperandTokenVectorNumExp()
Log< level::Error, false > LogError
void addBracketSpaces(const std::string &, std::string &)
add spaces before and after parantheses
L1GtLogicParser()
constructor(s)
void convertIntToNameLogicalExpression(const std::map< int, std::string > &intToNameMap)
std::vector< OperandToken > m_operandTokenVector
vector of operand tokens
const OperationRule * getRuleFromType(OperationType t)
get the rule entry to an operation type
bool setNumericalExpression(const std::string &)
std::string m_logicalExpression
logical expression to be parsed
virtual const bool expressionResultNumExp() const
virtual OperationType getOperation(const std::string &tokenString, OperationType lastOperation, TokenRPN &rpnToken) const
RpnVector m_rpnVector
RPN vector - equivalent to the logical expression.
virtual ~L1GtLogicParser()
destructor
std::string operandName(const int iOperand) const
return the name of the (iOperand)th operand in the logical expression
static int position[264][3]
static const struct OperationRule m_operationRules[]
std::string m_numericalExpression
bool checkLogicalExpression(std::string &)
check a logical expression for correctness - add/remove spaces if needed
std::vector< TokenRPN > RpnVector
void buildOperandTokenVector()
static constexpr float b2
bool setLogicalExpression(const std::string &)
set the logical expression - check for correctness the input string
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle brackets
virtual const bool expressionResult() const
static constexpr float b1