24 #include <boost/algorithm/string.hpp>
47 const std::vector<OperandToken>& opTokenVector)
66 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
85 boost::trim(logicalExpressionBS);
90 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
99 logicalExpressionVal = logicalExpressionBS;
117 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
126 <<
"\nError in parsing the numerical expression = " << numericalExpressionVal
135 const std::string& numericalExpressionVal,
const bool dummy) {
140 <<
"\nError in building RPN vector for the logical expression = "
141 << logicalExpressionVal
167 boost::trim(logicalExpressionBS);
175 LogDebug(
"L1GtLogicParser") <<
"\nL1GtLogicParser::checkLogicalExpression - "
176 <<
"\nInitial logical expression = '" << logicalExpressionVal <<
"'"
177 <<
"\nFinal logical expression = '" << logicalExpressionBS <<
"'\n"
180 logicalExpressionVal = logicalExpressionBS;
210 std::stack<TokenRPN> operatorStack;
218 std::istringstream exprStringStream(logicalExpressionVal);
220 while ( !exprStringStream.eof() ) {
222 exprStringStream >> std::skipws >> std::ws >> tokenString;
225 if (tokenString.find_first_not_of(whitespaces) == std::string::npos ||
226 tokenString.length() == 0) {
235 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
244 switch (actualOperation) {
253 int errorPosition = exprStringStream.tellg();
257 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
258 <<
"\n Syntax error during parsing: "
259 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
260 <<
"\n " << exprStringStream.str().substr(errorPosition)
261 <<
"\n Returned empty RPN vector and result false."
272 operatorStack.push(rpnToken);
279 while (!operatorStack.empty() && operatorStack.top().operation ==
OP_NOT) {
283 operatorStack.push(rpnToken);
289 while (!operatorStack.empty() &&
290 (operatorStack.top().operation ==
OP_NOT ||
291 operatorStack.top().operation ==
OP_AND) ) {
297 operatorStack.push(rpnToken);
304 operatorStack.push(rpnToken);
310 if (operatorStack.empty()) {
312 int errorPosition = exprStringStream.tellg();
315 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
316 <<
"\n Syntax error during parsing - misplaced ')':"
317 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
318 <<
"\n " << exprStringStream.str().substr(errorPosition)
319 <<
"\n Returned empty RPN vector and result false."
334 if (operatorStack.empty()) {
336 int errorPosition = exprStringStream.tellg();
339 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
340 <<
"\n Syntax error during parsing - misplaced ')':"
341 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
342 <<
"\n " << exprStringStream.str().substr(errorPosition)
343 <<
"\n Returned empty RPN vector and result false."
362 lastOperation = actualOperation;
367 while (!operatorStack.empty()) {
371 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
372 <<
"\n Syntax error during parsing - missing ')':"
373 <<
"\n Returned empty RPN vector and result false."
390 if (it->operation ==
OP_OR || it->operation ==
OP_AND)
395 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
396 <<
"\n Syntax error during parsing - too many operators"
397 <<
"\n Returned empty RPN vector and result false."
409 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
410 <<
"\n Syntax error during parsing - too many operands"
411 <<
"\n Returned empty RPN vector and result false."
454 switch (it->operation) {
513 while (!exprStringStream.eof()) {
515 exprStringStream >> tokenString;
521 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
527 <<
"\n Invalid operation/operand " << operandNameVal
528 <<
"\n Returned index is by default out of range (-1)."
542 if (rpnToken.
operand == operandNameVal) {
554 lastOperation = actualOperation;
560 <<
"\n Operand " << operandNameVal <<
" not found in the logical expression"
561 <<
"\n Returned index is by default out of range (-1)."
585 while (!exprStringStream.eof()) {
587 exprStringStream >> tokenString;
593 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
599 <<
"\n Invalid operation/operand at position " << iOperand
600 <<
"\n Returned empty name by default."
614 if (tmpIndex == iOperand) {
626 lastOperation = actualOperation;
632 <<
"\n No operand found at position " << iOperand
633 <<
"\n Returned empty name by default."
653 <<
"\n Operand " << operandNameVal <<
" not found in the operand token vector"
654 <<
"\n Returned false by default."
674 <<
"\n No operand with token number " << tokenNumberVal
675 <<
" found in the operand token vector"
676 <<
"\n Returned false by default."
695 <<
"\n No built RPN vector exists."
696 <<
"\n Returned false by default."
702 std::stack<bool> resultStack;
713 switch (it->operation) {
721 b1 = resultStack.top();
723 resultStack.push(!b1);
728 b1 = resultStack.top();
730 b2 = resultStack.top();
732 resultStack.push(b1 || b2);
737 b1 = resultStack.top();
739 b2 = resultStack.top();
741 resultStack.push(b1 && b2);
761 return resultStack.top();
804 while (!exprStringStream.eof()) {
806 exprStringStream >> tokenString;
812 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
818 <<
"\n Invalid operation/operand at position " << iOperand
819 <<
"\n Returned false by default."
833 if (tmpIndex == iOperand) {
845 <<
"\n Invalid result for operand at position " << iOperand
847 <<
"\n It must be 0 or 1"
848 <<
"\n Returned false by default."
866 lastOperation = actualOperation;
872 <<
"\n No operand found at position " << iOperand
873 <<
"\n Returned false by default."
903 switch (it->operation) {
956 <<
"\n No built RPN vector exists."
957 <<
"\n Returned false by default."
963 std::stack<bool> resultStack;
974 switch (it->operation) {
982 b1 = resultStack.top();
984 resultStack.push(!b1);
989 b1 = resultStack.top();
991 b2 = resultStack.top();
993 resultStack.push(b1 || b2);
998 b1 = resultStack.top();
1000 b2 = resultStack.top();
1002 resultStack.push(b1 && b2);
1024 return resultStack.top();
1034 const std::map<std::string, int>& nameToIntMap)
1057 while (!exprStringStream.eof()) {
1059 exprStringStream >> tokenString;
1061 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1067 <<
"\n Invalid operation/operand in logical expression."
1068 <<
"\n Return empty logical expression."
1078 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1082 typedef std::map<std::string, int>::const_iterator CIter;
1084 CIter it = nameToIntMap.find(rpnToken.
operand);
1085 if (it != nameToIntMap.end()) {
1087 intValue = it->second;
1088 std::stringstream intStr;
1090 convertedLogicalExpression.append(intStr.str());
1097 <<
"\n Could not convert " << rpnToken.
operand <<
" to integer!"
1098 <<
"\n Return empty logical expression."
1107 convertedLogicalExpression.append(
" ");
1108 lastOperation = actualOperation;
1113 boost::trim(convertedLogicalExpression);
1116 <<
"\nL1GtLogicParser::convertNameToIntLogicalExpression - "
1118 <<
"\nLogical expression (int) = '" << convertedLogicalExpression <<
"'\n"
1134 const std::map<int, std::string>& intToNameMap) {
1153 while (!exprStringStream.eof()) {
1155 exprStringStream >> tokenString;
1157 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1162 <<
"'" <<
"\n Invalid operation/operand in logical expression."
1163 <<
"\n Return empty logical expression." << std::endl;
1172 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1176 typedef std::map<int, std::string>::const_iterator CIter;
1180 std::istringstream iss(rpnToken.
operand);
1181 iss >> std::dec >> indexInt;
1183 CIter it = intToNameMap.find(indexInt);
1184 if (it != intToNameMap.end()) {
1186 convertedLogicalExpression.append(it->second);
1191 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '"
1193 << rpnToken.
operand <<
" to string!"
1194 <<
"\n Return empty logical expression." << std::endl;
1202 convertedLogicalExpression.append(
" ");
1203 lastOperation = actualOperation;
1208 boost::trim(convertedLogicalExpression);
1227 std::vector<L1GtLogicParser::OperandToken>
1238 std::vector<OperandToken> opVector;
1242 std::stack<OperandToken> tmpStack;
1243 std::vector<OperandToken> tmpVector;
1248 bool newOperandBlock =
true;
1249 bool oneBlockOnly =
true;
1250 bool operandOnly =
true;
1266 switch (it->operation) {
1274 if ( (!newOperandBlock) ) {
1276 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1277 itOp != tmpVector.rend(); itOp++) {
1279 opVector.push_back(*itOp);
1290 newOperandBlock =
true;
1291 oneBlockOnly =
false;
1309 newOperandBlock =
false;
1310 operandOnly =
false;
1312 b1 = tmpStack.top();
1315 tmpStack.push(dummyToken);
1328 newOperandBlock =
false;
1329 operandOnly =
false;
1331 b1 = tmpStack.top();
1333 b2 = tmpStack.top();
1336 tmpStack.push(dummyToken);
1339 tmpVector.push_back(b1);
1348 tmpVector.push_back(b2);
1361 newOperandBlock =
false;
1362 operandOnly =
false;
1364 b1 = tmpStack.top();
1366 b2 = tmpStack.top();
1369 tmpStack.push(dummyToken);
1373 tmpVector.push_back(b1);
1382 tmpVector.push_back(b2);
1404 if ( oneBlockOnly || operandOnly ) {
1409 b1 = tmpStack.top();
1410 tmpVector.push_back(b1);
1414 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1415 itOp != tmpVector.rend(); itOp++) {
1417 opVector.push_back(*itOp);
1431 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1432 itOp != tmpVector.rend(); itOp++) {
1434 opVector.push_back(*itOp);
1448 std::vector<OperandToken> opVectorU;
1449 opVectorU.reserve(opVector.size());
1451 for (std::vector<OperandToken>::const_iterator constIt = opVector.begin(); constIt
1452 != opVector.end(); constIt++) {
1454 bool tokenIncluded =
false;
1456 for (std::vector<OperandToken>::iterator itOpU = opVectorU.begin(); itOpU
1457 != opVectorU.end(); itOpU++) {
1459 if ( ( *itOpU ).tokenName == ( *constIt ).tokenName) {
1460 tokenIncluded =
true;
1465 if (!tokenIncluded) {
1466 opVectorU.push_back(*constIt);
1515 rpnToken.
operand = tokenString;
1525 return actualOperation;
1564 dstExpression = srcExpression;
1567 while ((position = dstExpression.find_first_of(brackets, position))
1568 != std::string::npos) {
1571 if (((position + 1) != std::string::npos) && (dstExpression[position
1573 dstExpression.insert(position + 1,
" ");
1577 if ((position != 0) && (dstExpression[position - 1] !=
' ')) {
1578 dstExpression.insert(position,
" ");
1595 boost::trim(logicalExpressionBS);
1629 boost::trim(numericalExpressionBS);
std::vector< L1GtLogicParser::OperandToken > expressionSeedsOperandList()
static struct OperationRule m_operationRules[]
void clearRpnVector()
clear possible old rpn vector
void convertNameToIntLogicalExpression(const std::map< std::string, int > &nameToIntMap)
bool buildRpnVector(const std::string &)
build the rpn vector
void buildOperandTokenVectorNumExp()
void addBracketSpaces(const std::string &, std::string &)
add spaces before and after parantheses
static int position[TOTALCHAMBERS][3]
L1GtLogicParser()
constructor(s)
bool operandResultNumExp(const std::string &operandNameVal) const
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 &)
bool operandResult(const std::string &operandNameVal) const
std::string m_logicalExpression
logical expression to be parsed
RpnVector m_rpnVector
RPN vector - equivalent to the logical expression.
virtual ~L1GtLogicParser()
destructor
static std::atomic< unsigned int > counter
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()
std::string operandName(const int iOperand) const
return the name of the (iOperand)th operand in the logical expression
bool setLogicalExpression(const std::string &)
set the logical expression - check for correctness the input string
virtual OperationType getOperation(const std::string &tokenString, OperationType lastOperation, TokenRPN &rpnToken) const
int operandIndex(const std::string &operandNameVal) const
return the position index of the operand in the logical expression
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 expressionResultNumExp() const
virtual const bool expressionResult() const