| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CVariableBindingFilteringLiteralComparison.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
CVariableBindingFilteringLiteralComparison::CVariableBindingFilteringLiteralComparison(COMPARING_TYPE comparingType, CExpressionVariable* variableExp, CDataLiteralExpression* dataLiteralExp) : CVariableBindingFiltering(CVariableBindingFiltering::LITERAL_COMPARISON) { |
| 33 |
mComparingType = comparingType; |
| 34 |
mVariableExp = variableExp; |
| 35 |
mDataLiteralExp = dataLiteralExp; |
| 36 |
mDataLiteral = nullptr; |
| 37 |
} |
| 38 |
|
| 39 |
CVariableBindingFilteringLiteralComparison::COMPARING_TYPE CVariableBindingFilteringLiteralComparison::getFilteringType() { |
| 40 |
return mComparingType; |
| 41 |
} |
| 42 |
|
| 43 |
CExpressionVariable* CVariableBindingFilteringLiteralComparison::getVariableExpression() { |
| 44 |
return mVariableExp; |
| 45 |
} |
| 46 |
|
| 47 |
CDataLiteralExpression* CVariableBindingFilteringLiteralComparison::getDataLiteralExpression() { |
| 48 |
return mDataLiteralExp; |
| 49 |
} |
| 50 |
|
| 51 |
CDataLiteral* CVariableBindingFilteringLiteralComparison::getDataLiteral() { |
| 52 |
return mDataLiteral; |
| 53 |
} |
| 54 |
|
| 55 |
CVariableBindingFilteringLiteralComparison* CVariableBindingFilteringLiteralComparison::setDataLiteral(CDataLiteral* dataLiteral) { |
| 56 |
mDataLiteral = dataLiteral; |
| 57 |
return this; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
CVariableBindingFilteringAnswerMapping* CVariableBindingFilteringLiteralComparison::createFitleringAnswerMapping(QHash<CExpressionVariable*, cint64> varPosHash) { |
| 62 |
CVariableBindingFilteringIndexArrayAnswerMapping* indexArrayAnswerMapping = new CVariableBindingFilteringIndexArrayAnswerMapping(1); |
| 63 |
indexArrayAnswerMapping->getVariableIndexArray()[0] = varPosHash.value(mVariableExp, 0); |
| 64 |
return indexArrayAnswerMapping; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
bool CVariableBindingFilteringLiteralComparison::isFiltered(CVariableBindingFilteringAnswerMapping* answeringMapping, CVariableBindingsAnswerResult* bindingsAnswer) { |
| 69 |
CVariableBindingFilteringIndexArrayAnswerMapping* indexArrayAnswerMapping = (CVariableBindingFilteringIndexArrayAnswerMapping*)answeringMapping; |
| 70 |
cint64 varPos = indexArrayAnswerMapping->getVariableIndexArray()[0]; |
| 71 |
CVariableBindingsListAnswerResult* listBindingsAnswer = (CVariableBindingsListAnswerResult*)bindingsAnswer; |
| 72 |
CVariableBindingResult* bindingResult = listBindingsAnswer->getResult(varPos); |
| 73 |
if (bindingResult) { |
| 74 |
CVariableBindingStringDataResult* stringDataResult = (CVariableBindingStringDataResult*)bindingResult; |
| 75 |
CDataLiteral* answerDataLiteral = stringDataResult->getDataLiteral(); |
| 76 |
CDataLiteralValue* answerDataLiteralValue = answerDataLiteral->getDataLiteralValue(); |
| 77 |
CDataLiteralValue* dataLiteralValue = mDataLiteral->getDataLiteralValue(); |
| 78 |
if (answerDataLiteralValue && dataLiteralValue) { |
| 79 |
CDataLiteralCompareValue* compDataValue = dynamic_cast<CDataLiteralCompareValue*>(dataLiteralValue); |
| 80 |
CDataLiteralCompareValue* compAnswerDataValue = dynamic_cast<CDataLiteralCompareValue*>(answerDataLiteralValue); |
| 81 |
if (compDataValue && compAnswerDataValue && compDataValue->getDataValueType() == compAnswerDataValue->getDataValueType()) { |
| 82 |
|
| 83 |
if (mComparingType == LESS) { |
| 84 |
return compAnswerDataValue->isLessEqualThan(compDataValue); |
| 85 |
} else if (mComparingType == LESS_EQUAL) { |
| 86 |
return compAnswerDataValue->isLessEqualThan(compDataValue); |
| 87 |
} else if (mComparingType == EQUAL) { |
| 88 |
return compAnswerDataValue->isEqualTo(compDataValue); |
| 89 |
} else if (mComparingType == UNEQUAL) { |
| 90 |
return compAnswerDataValue->isEqualTo(compDataValue); |
| 91 |
} else if (mComparingType == GREATER_EQUAL) { |
| 92 |
return compAnswerDataValue->isGreaterEqualThan(compDataValue); |
| 93 |
} else if (mComparingType == GREATER) { |
| 94 |
return compAnswerDataValue->isGreaterThan(compDataValue); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
QString filterString; |
| 100 |
if (mDataLiteral) { |
| 101 |
if (mDataLiteral->getDataLiteralValue()) { |
| 102 |
filterString = mDataLiteral->getDataLiteralValue()->getValueString(); |
| 103 |
} else { |
| 104 |
filterString = mDataLiteral->getLexicalDataLiteralValueString(); |
| 105 |
} |
| 106 |
} |
| 107 |
QString variableBindingString = bindingResult->getQueryResultString(); |
| 108 |
int compValue = variableBindingString.compare(filterString); |
| 109 |
if (mComparingType == LESS) { |
| 110 |
return compValue < 0; |
| 111 |
} else if (mComparingType == LESS_EQUAL) { |
| 112 |
return compValue <= 0; |
| 113 |
} else if (mComparingType == EQUAL) { |
| 114 |
return compValue == 0; |
| 115 |
} else if (mComparingType == UNEQUAL) { |
| 116 |
return compValue != 0; |
| 117 |
} else if (mComparingType == GREATER_EQUAL) { |
| 118 |
return compValue >= 0; |
| 119 |
} else if (mComparingType == GREATER) { |
| 120 |
return compValue > 0; |
| 121 |
} |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
}; // end namespace Query |
| 128 |
|
| 129 |
}; // end namespace Reasoner |
| 130 |
|
| 131 |
}; // end namespace Konclude |