24 #include <boost/algorithm/string.hpp> 47 const std::vector<OperandToken>& opTokenVector)
66 <<
"\nError in parsing the logical expression = " << logicalExpressionVal
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
140 <<
"\nError in building RPN vector for the logical expression = " 141 << logicalExpressionVal
175 LogDebug(
"L1TGlobal") <<
"\nGtLogicParser::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) ) {
296 operatorStack.push(rpnToken);
302 while (!operatorStack.empty() &&
303 (operatorStack.top().operation ==
OP_NOT ||
304 operatorStack.top().operation ==
OP_XOR ||
305 operatorStack.top().operation ==
OP_AND) ) {
311 operatorStack.push(rpnToken);
318 operatorStack.push(rpnToken);
324 if (operatorStack.empty()) {
326 int errorPosition = exprStringStream.tellg();
329 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 330 <<
"\n Syntax error during parsing - misplaced ')':" 331 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
332 <<
"\n " << exprStringStream.str().substr(errorPosition)
333 <<
"\n Returned empty RPN vector and result false." 348 if (operatorStack.empty()) {
350 int errorPosition = exprStringStream.tellg();
353 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 354 <<
"\n Syntax error during parsing - misplaced ')':" 355 <<
"\n " << exprStringStream.str().substr(0,errorPosition)
356 <<
"\n " << exprStringStream.str().substr(errorPosition)
357 <<
"\n Returned empty RPN vector and result false." 376 lastOperation = actualOperation;
381 while (!operatorStack.empty()) {
385 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 386 <<
"\n Syntax error during parsing - missing ')':" 387 <<
"\n Returned empty RPN vector and result false." 404 if (it->operation ==
OP_OR || it->operation ==
OP_AND || it->operation ==
OP_XOR)
409 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 410 <<
"\n Syntax error during parsing - too many operators" 411 <<
"\n Returned empty RPN vector and result false." 423 <<
"\nLogical expression = '" << logicalExpressionVal <<
"'" 424 <<
"\n Syntax error during parsing - too many operands" 425 <<
"\n Returned empty RPN vector and result false." 467 switch (it->operation) {
529 while (!exprStringStream.eof()) {
531 exprStringStream >> tokenString;
537 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
543 <<
"\n Invalid operation/operand " << operandNameVal
544 <<
"\n Returned index is by default out of range (-1)." 558 if (rpnToken.
operand == operandNameVal) {
570 lastOperation = actualOperation;
576 <<
"\n Operand " << operandNameVal <<
" not found in the logical expression" 577 <<
"\n Returned index is by default out of range (-1)." 601 while (!exprStringStream.eof()) {
603 exprStringStream >> tokenString;
609 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
615 <<
"\n Invalid operation/operand at position " << iOperand
616 <<
"\n Returned empty name by default." 630 if (tmpIndex == iOperand) {
642 lastOperation = actualOperation;
648 <<
"\n No operand found at position " << iOperand
649 <<
"\n Returned empty name by default." 669 <<
"\n Operand " << operandNameVal <<
" not found in the operand token vector" 670 <<
"\n Returned false by default." 690 <<
"\n No operand with token number " << tokenNumberVal
691 <<
" found in the operand token vector" 692 <<
"\n Returned false by default." 711 <<
"\n No built RPN vector exists." 712 <<
"\n Returned false by default." 718 std::stack<bool> resultStack;
729 switch (it->operation) {
737 b1 = resultStack.top();
739 resultStack.push(!b1);
744 b1 = resultStack.top();
746 b2 = resultStack.top();
748 resultStack.push(b1 || b2);
753 b1 = resultStack.top();
755 b2 = resultStack.top();
757 resultStack.push(b1 ^ b2);
762 b1 = resultStack.top();
764 b2 = resultStack.top();
766 resultStack.push(b1 && b2);
786 return resultStack.top();
829 while (!exprStringStream.eof()) {
831 exprStringStream >> tokenString;
837 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
843 <<
"\n Invalid operation/operand at position " << iOperand
844 <<
"\n Returned false by default." 858 if (tmpIndex == iOperand) {
870 <<
"\n Invalid result for operand at position " << iOperand
872 <<
"\n It must be 0 or 1" 873 <<
"\n Returned false by default." 891 lastOperation = actualOperation;
897 <<
"\n No operand found at position " << iOperand
898 <<
"\n Returned false by default." 928 switch (it->operation) {
986 <<
"\n No built RPN vector exists." 987 <<
"\n Returned false by default." 993 std::stack<bool> resultStack;
1004 switch (it->operation) {
1012 b1 = resultStack.top();
1014 resultStack.push(!b1);
1019 b1 = resultStack.top();
1021 b2 = resultStack.top();
1023 resultStack.push(b1 || b2);
1028 b1 = resultStack.top();
1030 b2 = resultStack.top();
1032 resultStack.push(b1 ^ b2);
1037 b1 = resultStack.top();
1039 b2 = resultStack.top();
1041 resultStack.push(b1 && b2);
1063 return resultStack.top();
1073 const std::map<std::string, int>& nameToIntMap)
1096 while (!exprStringStream.eof()) {
1098 exprStringStream >> tokenString;
1100 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1106 <<
"\n Invalid operation/operand in logical expression." 1107 <<
"\n Return empty logical expression." 1117 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1121 typedef std::map<std::string, int>::const_iterator CIter;
1123 CIter it = nameToIntMap.find(rpnToken.
operand);
1124 if (it != nameToIntMap.end()) {
1126 intValue = it->second;
1127 std::stringstream intStr;
1129 convertedLogicalExpression.append(intStr.str());
1136 <<
"\n Could not convert " << rpnToken.
operand <<
" to integer!" 1137 <<
"\n Return empty logical expression." 1146 convertedLogicalExpression.append(
" ");
1147 lastOperation = actualOperation;
1155 <<
"\nGtLogicParser::convertNameToIntLogicalExpression - " 1157 <<
"\nLogical expression (int) = '" << convertedLogicalExpression <<
"'\n" 1173 const std::map<int, std::string>& intToNameMap) {
1192 while (!exprStringStream.eof()) {
1194 exprStringStream >> tokenString;
1196 actualOperation =
getOperation(tokenString, lastOperation, rpnToken);
1201 <<
"'" <<
"\n Invalid operation/operand in logical expression." 1202 <<
"\n Return empty logical expression." << std::endl;
1211 convertedLogicalExpression.append(
getRuleFromType(actualOperation)->opString);
1215 typedef std::map<int, std::string>::const_iterator CIter;
1219 std::istringstream iss(rpnToken.
operand);
1222 CIter it = intToNameMap.find(indexInt);
1223 if (it != intToNameMap.end()) {
1225 convertedLogicalExpression.append(it->second);
1232 << rpnToken.
operand <<
" to string!" 1233 <<
"\n Return empty logical expression." << std::endl;
1241 convertedLogicalExpression.append(
" ");
1242 lastOperation = actualOperation;
1266 std::vector<GlobalLogicParser::OperandToken>
1277 std::vector<OperandToken> opVector;
1281 std::stack<OperandToken> tmpStack;
1282 std::vector<OperandToken> tmpVector;
1287 bool newOperandBlock =
true;
1288 bool oneBlockOnly =
true;
1289 bool operandOnly =
true;
1305 switch (it->operation) {
1313 if ( (!newOperandBlock) ) {
1315 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1316 itOp != tmpVector.rend(); itOp++) {
1318 opVector.push_back(*itOp);
1329 newOperandBlock =
true;
1330 oneBlockOnly =
false;
1348 newOperandBlock =
false;
1349 operandOnly =
false;
1351 b1 = tmpStack.top();
1354 tmpStack.push(dummyToken);
1367 newOperandBlock =
false;
1368 operandOnly =
false;
1370 b1 = tmpStack.top();
1372 b2 = tmpStack.top();
1375 tmpStack.push(dummyToken);
1378 tmpVector.push_back(b1);
1387 tmpVector.push_back(b2);
1400 newOperandBlock =
false;
1401 operandOnly =
false;
1403 b1 = tmpStack.top();
1405 b2 = tmpStack.top();
1408 tmpStack.push(dummyToken);
1411 tmpVector.push_back(b1);
1420 tmpVector.push_back(b2);
1433 newOperandBlock =
false;
1434 operandOnly =
false;
1436 b1 = tmpStack.top();
1438 b2 = tmpStack.top();
1441 tmpStack.push(dummyToken);
1445 tmpVector.push_back(b1);
1454 tmpVector.push_back(b2);
1476 if ( oneBlockOnly || operandOnly ) {
1481 b1 = tmpStack.top();
1482 tmpVector.push_back(b1);
1486 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1487 itOp != tmpVector.rend(); itOp++) {
1489 opVector.push_back(*itOp);
1503 for (std::vector<OperandToken>::reverse_iterator itOp = tmpVector.rbegin();
1504 itOp != tmpVector.rend(); itOp++) {
1506 opVector.push_back(*itOp);
1520 std::vector<OperandToken> opVectorU;
1521 opVectorU.reserve(opVector.size());
1523 for (std::vector<OperandToken>::const_iterator constIt = opVector.begin(); constIt
1524 != opVector.end(); constIt++) {
1526 bool tokenIncluded =
false;
1528 for (std::vector<OperandToken>::iterator itOpU = opVectorU.begin(); itOpU
1529 != opVectorU.end(); itOpU++) {
1531 if ( ( *itOpU ).tokenName == ( *constIt ).tokenName) {
1532 tokenIncluded =
true;
1537 if (!tokenIncluded) {
1538 opVectorU.push_back(*constIt);
1587 rpnToken.
operand = tokenString;
1597 return actualOperation;
1636 dstExpression = srcExpression;
1639 while ((position = dstExpression.find_first_of(brackets, position))
1640 != std::string::npos) {
1643 if (((position + 1) != std::string::npos) && (dstExpression[position
1645 dstExpression.insert(position + 1,
" ");
1649 if ((position != 0) && (dstExpression[position - 1] !=
' ')) {
1650 dstExpression.insert(position,
" ");
RpnVector m_rpnVector
RPN vector - equivalent to the logical expression.
virtual const bool expressionResult() const
const OperationRule * getRuleFromType(OperationType t)
get the rule entry to an operation type
std::vector< OperandToken > m_operandTokenVector
vector of operand tokens
void buildOperandTokenVectorNumExp()
static void trim(std::string &s)
virtual const bool expressionResultNumExp() const
bool setLogicalExpression(const std::string &)
set the logical expression - check for correctness the input string
std::string m_numericalExpression
std::vector< GlobalLogicParser::OperandToken > expressionSeedsOperandList()
void clearRpnVector()
clear possible old rpn vector
bool buildRpnVector(const std::string &)
build the rpn vector
std::string m_logicalExpression
logical expression to be parsed
int operandIndex(const std::string &operandNameVal) const
return the position index of the operand in the logical expression
void convertIntToNameLogicalExpression(const std::map< int, std::string > &intToNameMap)
void addBracketSpaces(const std::string &, std::string &)
add spaces before and after parantheses
std::vector< TokenRPN > RpnVector
bool checkLogicalExpression(std::string &)
check a logical expression for correctness - add/remove spaces if needed
void convertNameToIntLogicalExpression(const std::map< std::string, int > &nameToIntMap)
bool operandResultNumExp(const std::string &operandNameVal) const
bool operandResult(const std::string &operandNameVal) const
static int position[264][3]
GlobalLogicParser()
constructor(s)
virtual ~GlobalLogicParser()
destructor
void buildOperandTokenVector()
static const struct OperationRule m_operationRules[]
std::string operandName(const int iOperand) const
return the name of the (iOperand)th 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 OperationType getOperation(const std::string &tokenString, OperationType lastOperation, TokenRPN &rpnToken) const
bool setNumericalExpression(const std::string &)