| 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 "CVariableBindingsAnswerResultMapOrderer.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
CVariableBindingsAnswerResultMapOrderer::CVariableBindingsAnswerResultMapOrderer(CVariableBindingsListAnswerResult* bindingResult, CVariableBindingsAnswerResultMapOrdering* odering) { |
| 31 |
mBindingResult = bindingResult; |
| 32 |
mOdering = odering; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
bool CVariableBindingsAnswerResultMapOrderer::operator<(const CVariableBindingsAnswerResultMapOrderer& orderer) const { |
| 37 |
cint64 count = mOdering->getVariableCount(); |
| 38 |
bool stillEqual = true; |
| 39 |
for (cint64 varNr = 0; varNr < count && stillEqual; ++varNr) { |
| 40 |
cint64 varIdx = mOdering->getVariableIndex(varNr); |
| 41 |
CVariableBindingResult* binding = mBindingResult->getResult(varIdx); |
| 42 |
CVariableBindingResult* otherBinding = orderer.mBindingResult->getResult(varIdx); |
| 43 |
if (!mOdering->isAscending(varNr)) { |
| 44 |
if (isLess(binding, otherBinding, stillEqual, count == varNr)) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
} else { |
| 48 |
if (isLess(otherBinding, binding, stillEqual, count == varNr)) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
bool CVariableBindingsAnswerResultMapOrderer::isLess(CVariableBindingResult* binding1, CVariableBindingResult* binding2, bool& stillEqual, bool lastComparison) const { |
| 57 |
bool compared = false; |
| 58 |
if (!compared && binding1->getVariableBindingType() == CVariableBindingResult::VBTANONYMOUSINDIVIDUAL && binding2->getVariableBindingType() == CVariableBindingResult::VBTNAMEDINDIVIDUAL) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
if (!compared && binding1->getVariableBindingType() == CVariableBindingResult::VBTLITERAL && binding2->getVariableBindingType() == CVariableBindingResult::VBTLITERAL) { |
| 62 |
CVariableBindingStringDataResult* stringDataBinding1 = dynamic_cast<CVariableBindingStringDataResult*>(binding1); |
| 63 |
CVariableBindingStringDataResult* stringDataBinding2 = dynamic_cast<CVariableBindingStringDataResult*>(binding2); |
| 64 |
if (stringDataBinding1 && stringDataBinding2) { |
| 65 |
CDataLiteral* dataLiteral1 = stringDataBinding1->getDataLiteral(); |
| 66 |
CDataLiteral* dataLiteral2 = stringDataBinding2->getDataLiteral(); |
| 67 |
CDataLiteralValue* value1 = dataLiteral1->getDataLiteralValue(); |
| 68 |
CDataLiteralValue* value2 = dataLiteral2->getDataLiteralValue(); |
| 69 |
if (value1->getDataValueType() == value2->getDataValueType()) { |
| 70 |
CDataLiteralCompareValue* compValue1 = dynamic_cast<CDataLiteralCompareValue*>(value1); |
| 71 |
CDataLiteralCompareValue* compValue2 = dynamic_cast<CDataLiteralCompareValue*>(value2); |
| 72 |
if (compValue1 && compValue2) { |
| 73 |
if (compValue1->isLessThan(compValue2)) { |
| 74 |
return true; |
| 75 |
} else if (stillEqual && !compValue1->isEqualTo(compValue2)) { |
| 76 |
stillEqual = false; |
| 77 |
} |
| 78 |
compared = true; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
int comp = 0; |
| 84 |
if (!compared) { |
| 85 |
comp = binding1->getQueryResultString().compare(binding2->getQueryResultString(), Qt::CaseSensitive); |
| 86 |
} |
| 87 |
if (comp > 0) { |
| 88 |
return true; |
| 89 |
} |
| 90 |
if (stillEqual && comp != 0) { |
| 91 |
stillEqual = false; |
| 92 |
} |
| 93 |
if (stillEqual && lastComparison && mOdering->isDistinct()) { |
| 94 |
return binding1 < binding2; |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
CVariableBindingsListAnswerResult* CVariableBindingsAnswerResultMapOrderer::getBindingResult() const { |
| 101 |
return mBindingResult; |
| 102 |
} |
| 103 |
|
| 104 |
}; // end namespace Query |
| 105 |
|
| 106 |
}; // end namespace Reasoner |
| 107 |
|
| 108 |
}; // end namespace Konclude |