26 #include <boost/algorithm/string.hpp>
49 const std::vector<OperandToken>& opTokenVector)
68 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
83 std::string logicalExpressionBS;
87 boost::trim(logicalExpressionBS);
92 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
101 logicalExpressionVal = logicalExpressionBS;
112 const std::string numericalExpressionVal) {
119 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
128 <<
"\nError in parsing the numerical expression = " << numericalExpressionVal
137 const std::string& numericalExpressionVal,
const bool dummy) {
142 <<
"\nError in building RPN vector for the logical expression = "
143 << logicalExpressionVal
165 std::string logicalExpressionBS;
169 boost::trim(logicalExpressionBS);
177 LogDebug(
"L1GtLogicParser") <<
"\nL1GtLogicParser::checkLogicalExpression - "
178 <<
"\nInitial logical expression = '" << logicalExpressionVal <<
"'"
179 <<
"\nFinal logical expression = '" << logicalExpressionBS <<
"'\n"
182 logicalExpressionVal = logicalExpressionBS;
210 std::string tokenString;
212 std::stack<TokenRPN> operatorStack;
214 static const std::string whitespaces=
" \r\v\n\t";
220 std::istringstream exprStringStream(logicalExpressionVal);
222 while ( !exprStringStream.eof() ) {
224 exprStringStream >> std::skipws >> std::ws >> tokenString;
227 if (tokenString.find_first_not_of(whitespaces) == std::string::npos ||
228 tokenString.length() == 0) {
237 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
246 switch (actualOperation) {
255 int errorPosition = exprStringStream.tellg();
259 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
260 <<
"\n Syntax error during parsing: "
261 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
262 <<
"\n " << exprStringStream.str().substr(errorPosition)
263 <<
"\n Returned empty RPN vector and result false."
274 operatorStack.push(rpnToken);
281 while (!operatorStack.empty() && operatorStack.top().operation ==
OP_NOT) {
285 operatorStack.push(rpnToken);
291 while (!operatorStack.empty() &&
292 (operatorStack.top().operation ==
OP_NOT ||
293 operatorStack.top().operation ==
OP_AND) ) {
299 operatorStack.push(rpnToken);
306 operatorStack.push(rpnToken);
312 if (operatorStack.empty()) {
314 int errorPosition = exprStringStream.tellg();
317 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
318 <<
"\n Syntax error during parsing - misplaced ')':"
319 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
320 <<
"\n " << exprStringStream.str().substr(errorPosition)
321 <<
"\n Returned empty RPN vector and result false."
336 if (operatorStack.empty()) {
338 int errorPosition = exprStringStream.tellg();
341 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
342 <<
"\n Syntax error during parsing - misplaced ')':"
343 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
344 <<
"\n " << exprStringStream.str().substr(errorPosition)
345 <<
"\n Returned empty RPN vector and result false."
364 lastOperation = actualOperation;
369 while (!operatorStack.empty()) {
373 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
374 <<
"\n Syntax error during parsing - missing ')':"
375 <<
"\n Returned empty RPN vector and result false."
392 if (it->operation ==
OP_OR || it->operation ==
OP_AND)
397 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
398 <<
"\n Syntax error during parsing - too many operators"
399 <<
"\n Returned empty RPN vector and result false."
411 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'"
412 <<
"\n Syntax error during parsing - too many operands"
413 <<
"\n Returned empty RPN vector and result false."
456 switch (it->operation) {
506 std::string tokenString;
515 while (!exprStringStream.eof()) {
517 exprStringStream >> tokenString;
523 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
529 <<
"\n Invalid operation/operand " << operandNameVal
530 <<
"\n Returned index is by default out of range (-1)."
544 if (rpnToken.
operand == operandNameVal) {
556 lastOperation = actualOperation;
562 <<
"\n Operand " << operandNameVal <<
" not found in the logical expression"
563 <<
"\n Returned index is by default out of range (-1)."
578 std::string tokenString;
587 while (!exprStringStream.eof()) {
589 exprStringStream >> tokenString;
595 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
601 <<
"\n Invalid operation/operand at position " << iOperand
602 <<
"\n Returned empty name by default."
616 if (tmpIndex == iOperand) {
628 lastOperation = actualOperation;
634 <<
"\n No operand found at position " << iOperand
635 <<
"\n Returned empty name by default."
655 <<
"\n Operand " << operandNameVal <<
" not found in the operand token vector"
656 <<
"\n Returned false by default."
676 <<
"\n No operand with token number " << tokenNumberVal
677 <<
" found in the operand token vector"
678 <<
"\n Returned false by default."
697 <<
"\n No built RPN vector exists."
698 <<
"\n Returned false by default."
704 std::stack<bool> resultStack;
715 switch (it->operation) {
723 b1 = resultStack.top();
725 resultStack.push(!b1);
730 b1 = resultStack.top();
732 b2 = resultStack.top();
734 resultStack.push(b1 || b2);
739 b1 = resultStack.top();
741 b2 = resultStack.top();
743 resultStack.push(b1 && b2);
763 return resultStack.top();
797 std::string tokenString;
806 while (!exprStringStream.eof()) {
808 exprStringStream >> tokenString;
814 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
820 <<
"\n Invalid operation/operand at position " << iOperand
821 <<
"\n Returned false by default."
835 if (tmpIndex == iOperand) {
847 <<
"\n Invalid result for operand at position " << iOperand
849 <<
"\n It must be 0 or 1"
850 <<
"\n Returned false by default."
868 lastOperation = actualOperation;
874 <<
"\n No operand found at position " << iOperand
875 <<
"\n Returned false by default."
905 switch (it->operation) {
958 <<
"\n No built RPN vector exists."
959 <<
"\n Returned false by default."
965 std::stack<bool> resultStack;
976 switch (it->operation) {
984 b1 = resultStack.top();
986 resultStack.push(!b1);
991 b1 = resultStack.top();
993 b2 = resultStack.top();
995 resultStack.push(b1 || b2);
1000 b1 = resultStack.top();
1002 b2 = resultStack.top();
1004 resultStack.push(b1 && b2);
1026 return resultStack.top();
1036 const std::map<std::string, int>& nameToIntMap)
1050 std::string tokenString;
1057 std::string convertedLogicalExpression;
1059 while (!exprStringStream.eof()) {
1061 exprStringStream >> tokenString;
1063 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1069 <<
"\n Invalid operation/operand in logical expression."
1070 <<
"\n Return empty logical expression."
1080 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1084 typedef std::map<std::string, int>::const_iterator CIter;
1086 CIter it = nameToIntMap.find(rpnToken.
operand);
1087 if (it != nameToIntMap.end()) {
1089 intValue = it->second;
1090 std::stringstream intStr;
1092 convertedLogicalExpression.append(intStr.str());
1099 <<
"\n Could not convert " << rpnToken.
operand <<
" to integer!"
1100 <<
"\n Return empty logical expression."
1109 convertedLogicalExpression.append(
" ");
1110 lastOperation = actualOperation;
1115 boost::trim(convertedLogicalExpression);
1118 <<
"\nL1GtLogicParser::convertNameToIntLogicalExpression - "
1120 <<
"\nLogical expression (int) = '" << convertedLogicalExpression <<
"'\n"
1136 const std::map<int, std::string>& intToNameMap) {
1148 std::string tokenString;
1153 std::string convertedLogicalExpression;
1155 while (!exprStringStream.eof()) {
1157 exprStringStream >> tokenString;
1159 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1164 <<
"'" <<
"\n Invalid operation/operand in logical expression."
1165 <<
"\n Return empty logical expression." << std::endl;
1174 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1178 typedef std::map<int, std::string>::const_iterator CIter;
1182 std::istringstream iss(rpnToken.
operand);
1183 iss >> std::dec >> indexInt;
1185 CIter it = intToNameMap.find(indexInt);
1186 if (it != intToNameMap.end()) {
1188 convertedLogicalExpression.append(it->second);
1193 edm::LogError(
"L1GtLogicParser") <<
"\nLogical expression = '"
1195 << rpnToken.
operand <<
" to string!"
1196 <<
"\n Return empty logical expression." << std::endl;
1204 convertedLogicalExpression.append(
" ");
1205 lastOperation = actualOperation;
1210 boost::trim(convertedLogicalExpression);
1229 std::vector<L1GtLogicParser::OperandToken>
1240 std::vector<OperandToken> opVector;
1244 std::stack<OperandToken> tmpStack;
1245 std::vector<OperandToken> tmpVector;
1250 bool newOperandBlock =
true;
1251 bool oneBlockOnly =
true;
1252 bool operandOnly =
true;
1268 switch (it->operation) {
1276 if ( (!newOperandBlock) ) {
1278 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1279 itOp != tmpVector.rend(); itOp++) {
1281 opVector.push_back(*itOp);
1292 newOperandBlock =
true;
1293 oneBlockOnly =
false;
1311 newOperandBlock =
false;
1312 operandOnly =
false;
1314 b1 = tmpStack.top();
1317 tmpStack.push(dummyToken);
1330 newOperandBlock =
false;
1331 operandOnly =
false;
1333 b1 = tmpStack.top();
1335 b2 = tmpStack.top();
1338 tmpStack.push(dummyToken);
1341 tmpVector.push_back(b1);
1350 tmpVector.push_back(b2);
1363 newOperandBlock =
false;
1364 operandOnly =
false;
1366 b1 = tmpStack.top();
1368 b2 = tmpStack.top();
1371 tmpStack.push(dummyToken);
1375 tmpVector.push_back(b1);
1384 tmpVector.push_back(b2);
1406 if ( oneBlockOnly || operandOnly ) {
1411 b1 = tmpStack.top();
1412 tmpVector.push_back(b1);
1416 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1417 itOp != tmpVector.rend(); itOp++) {
1419 opVector.push_back(*itOp);
1433 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1434 itOp != tmpVector.rend(); itOp++) {
1436 opVector.push_back(*itOp);
1450 std::vector<OperandToken> opVectorU;
1451 opVectorU.reserve(opVector.size());
1453 for (std::vector<OperandToken>::const_iterator constIt = opVector.begin(); constIt
1454 != opVector.end(); constIt++) {
1456 bool tokenIncluded =
false;
1458 for (std::vector<OperandToken>::iterator itOpU = opVectorU.begin(); itOpU
1459 != opVectorU.end(); itOpU++) {
1461 if ( ( *itOpU ).tokenName == ( *constIt ).tokenName) {
1462 tokenIncluded =
true;
1467 if (!tokenIncluded) {
1468 opVectorU.push_back(*constIt);
1493 const std::string& tokenString,
1517 rpnToken.
operand = tokenString;
1527 return actualOperation;
1562 std::string& dstExpression) {
1564 static const std::string
brackets =
"()";
1566 dstExpression = srcExpression;
1569 while ((position = dstExpression.find_first_of(brackets, position))
1570 != std::string::npos) {
1573 if (((position + 1) != std::string::npos) && (dstExpression[position
1575 dstExpression.insert(position + 1,
" ");
1579 if ((position != 0) && (dstExpression[position - 1] !=
' ')) {
1580 dstExpression.insert(position,
" ");
1593 std::string logicalExpressionBS;
1597 boost::trim(logicalExpressionBS);
1624 std::string numericalExpressionBS;
1631 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
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