| 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 "CVariableBindingsAnswersListResult.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CVariableBindingsAnswersListResult::CVariableBindingsAnswersListResult(const QStringList& varNames) { |
| 32 |
mVariableNames = varNames; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
CVariableBindingsAnswersListResult::~CVariableBindingsAnswersListResult() { |
| 37 |
for (auto bindings : mBindingsList) { |
| 38 |
delete bindings; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
CVariableBindingsAnswersListResult* CVariableBindingsAnswersListResult::addResultVariableBindings(CVariableBindingsAnswerResult* varBindings, cint64 cardinality) { |
| 44 |
mBindingsContainer.append(varBindings); |
| 45 |
for (cint64 i = 0; i < cardinality; ++i) { |
| 46 |
mBindingsList.append(varBindings); |
| 47 |
} |
| 48 |
return this; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
CVariableBindingsAnswersListResult* CVariableBindingsAnswersListResult::setResultVariableBindingsList(const QList<CVariableBindingsAnswerResult*>& indiBindingList) { |
| 53 |
mBindingsList = indiBindingList; |
| 54 |
mBindingsContainer = mBindingsList.toSet().toList(); |
| 55 |
return this; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
QList<CVariableBindingsAnswerResult*>* CVariableBindingsAnswersListResult::getResult() { |
| 61 |
return &mBindingsList; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
QString CVariableBindingsAnswersListResult::getQueryResultString() { |
| 67 |
return QString(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
bool CVariableBindingsAnswersListResult::isResultEquivalentTo(CQueryResult *otherQueryResult) { |
| 72 |
if (otherQueryResult) { |
| 73 |
CVariableBindingsAnswersListResult *otherBoolQueryResult = dynamic_cast<CVariableBindingsAnswersListResult *>(otherQueryResult); |
| 74 |
if (otherBoolQueryResult) { |
| 75 |
return isResultEquivalentTo(otherBoolQueryResult); |
| 76 |
} |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
bool CVariableBindingsAnswersListResult::isResultEquivalentTo(CVariableBindingsAnswersListResult *otherQueryResult) { |
| 83 |
if (otherQueryResult) { |
| 84 |
if (mBindingsList.count() != otherQueryResult->mBindingsList.count()) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
for (QList<CVariableBindingsAnswerResult*>::const_iterator it1 = mBindingsList.constBegin(), it2 = otherQueryResult->mBindingsList.constBegin(), it1End = mBindingsList.constEnd(), it2End = otherQueryResult->mBindingsList.constEnd(); it1 != it1End && it2 != it2End; ++it1, ++it2) { |
| 88 |
CVariableBindingsAnswerResult* bind1(*it1); |
| 89 |
CVariableBindingsAnswerResult* bind2(*it2); |
| 90 |
if (bind1 == nullptr && bind2 != nullptr) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
if (bind1 && !bind1->isResultEquivalentTo(bind2)) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
return true; |
| 98 |
} else { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
const QStringList& CVariableBindingsAnswersListResult::getVariableNames() { |
| 105 |
return mVariableNames; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
CVariableBindingsAnswersResultIterator* CVariableBindingsAnswersListResult::getVariableBindingsAnswersIterator(CVariableBindingsAnswersResultIterator* reuseIt) { |
| 111 |
class CVariableBindingsListAnswerResultIterator : public CVariableBindingsAnswersResultIterator { |
| 112 |
public: |
| 113 |
CVariableBindingsListAnswerResultIterator(const QList<CVariableBindingsAnswerResult*>& list) { |
| 114 |
init(list); |
| 115 |
} |
| 116 |
|
| 117 |
CVariableBindingsListAnswerResultIterator* init(const QList<CVariableBindingsAnswerResult*>& list) { |
| 118 |
mIt = list.constBegin(); |
| 119 |
mItEnd = list.constEnd(); |
| 120 |
return this; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
virtual CVariableBindingsAnswerResult* getNext(bool moveNext = true) { |
| 125 |
QList<CVariableBindingsAnswerResult*>::const_iterator it = mIt; |
| 126 |
if (it != mItEnd) { |
| 127 |
if (moveNext) { |
| 128 |
++mIt; |
| 129 |
} |
| 130 |
return *it; |
| 131 |
} |
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
|
| 135 |
virtual bool hasNext() { |
| 136 |
return mIt != mItEnd; |
| 137 |
} |
| 138 |
|
| 139 |
QList<CVariableBindingsAnswerResult*>::const_iterator mIt, mItEnd; |
| 140 |
|
| 141 |
}; |
| 142 |
if (reuseIt) { |
| 143 |
CVariableBindingsListAnswerResultIterator* listReuseIt = (CVariableBindingsListAnswerResultIterator*)reuseIt; |
| 144 |
return listReuseIt->init(mBindingsList); |
| 145 |
} |
| 146 |
return new CVariableBindingsListAnswerResultIterator(mBindingsList); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
cint64 CVariableBindingsAnswersListResult::getResultCount() { |
| 152 |
return mBindingsList.count(); |
| 153 |
} |
| 154 |
|
| 155 |
CVariableBindingsAnswersListResult* CVariableBindingsAnswersListResult::clear() { |
| 156 |
mBindingsList.clear(); |
| 157 |
mBindingsContainer.clear(); |
| 158 |
return this; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
}; // end namespace Query |
| 164 |
|
| 165 |
}; // end namespace Reasoner |
| 166 |
|
| 167 |
}; // end namespace Konclude |