| 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 "COptimizedRepresentativeKPSetIndividualCandiatesIterator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Realizer { |
| 29 |
|
| 30 |
|
| 31 |
COptimizedRepresentativeKPSetIndividualCandiatesIterator::COptimizedRepresentativeKPSetIndividualCandiatesIterator(const QMap<cint64, IndividualIdCandidateData>& candidateIndiIdsMap, CBackendRepresentativeMemoryCacheReader* backendAssocCacheReader, COptimizedKPSetIndividualInstantiatedItemMultiHash* individualInstantiatedItemHash, CIndividualVector* individualVector, const CRealizationIndividualSorting& sorting, const CRealizationIndividualInstanceItemReference& indiInstItemRefCursor, bool moveOverCursor) { |
| 32 |
mIndividualInstantiatedItemHash = individualInstantiatedItemHash; |
| 33 |
mIndividualVector = individualVector; |
| 34 |
mBackendAssocCacheReader = backendAssocCacheReader; |
| 35 |
|
| 36 |
|
| 37 |
mSorting = sorting; |
| 38 |
mIterateAscending = mSorting.getOrder() != CRealizationIndividualSorting::SORT_ORDER_DESCENDING; |
| 39 |
|
| 40 |
mIndiInstItemRefCursor = indiInstItemRefCursor; |
| 41 |
mMoveOverCursor = moveOverCursor; |
| 42 |
mInitializationRequired = false; |
| 43 |
|
| 44 |
|
| 45 |
mComplexRoleDataPointer = nullptr; |
| 46 |
|
| 47 |
mMinIndiId = CINT_MAX; |
| 48 |
mMaxIndiId = CINT_MIN; |
| 49 |
mLastIndiId = -1; |
| 50 |
mAverageIterationIncrementSize = 1; |
| 51 |
mIterationCount = 0; |
| 52 |
mIterationMoved = false; |
| 53 |
mMapInitialized = false; |
| 54 |
|
| 55 |
mCandidateIndiIdsMap = candidateIndiIdsMap; |
| 56 |
|
| 57 |
begin(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::requiresInitialization() { |
| 62 |
return mInitializationRequired; |
| 63 |
} |
| 64 |
|
| 65 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::begin() { |
| 66 |
|
| 67 |
if (!mInitializationRequired && !mMapInitialized) { |
| 68 |
mMapInitialized = true; |
| 69 |
|
| 70 |
mCandidateIndiIdsMapItBegin = mCandidateIndiIdsMap.constBegin(); |
| 71 |
mCandidateIndiIdsMapItEnd = mCandidateIndiIdsMap.constEnd(); |
| 72 |
if (mIndiInstItemRefCursor.isNonEmpty()) { |
| 73 |
cint64 cursorId = mIndiInstItemRefCursor.getIndividualID(); |
| 74 |
|
| 75 |
const QMap<cint64, IndividualIdCandidateData>& candidateIndiIdsMap = mCandidateIndiIdsMap; |
| 76 |
if (mIterateAscending) { |
| 77 |
if (mMoveOverCursor) { |
| 78 |
mCandidateIndiIdsMapItBegin = candidateIndiIdsMap.upperBound(cursorId); |
| 79 |
} else { |
| 80 |
mCandidateIndiIdsMapItBegin = candidateIndiIdsMap.lowerBound(cursorId); |
| 81 |
} |
| 82 |
} else { |
| 83 |
if (mMoveOverCursor) { |
| 84 |
mCandidateIndiIdsMapItEnd = candidateIndiIdsMap.lowerBound(cursorId); |
| 85 |
} else { |
| 86 |
mCandidateIndiIdsMapItEnd = candidateIndiIdsMap.upperBound(cursorId); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (mIterateAscending) { |
| 92 |
mCandidateIndiIdsMapIt = mCandidateIndiIdsMapItBegin; |
| 93 |
} else { |
| 94 |
mCandidateIndiIdsMapIt = mCandidateIndiIdsMapItEnd; |
| 95 |
if (mCandidateIndiIdsMapItBegin != mCandidateIndiIdsMapItEnd) { |
| 96 |
--mCandidateIndiIdsMapIt; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if (mCandidateIndiIdsMapItBegin != mCandidateIndiIdsMapItEnd) { |
| 102 |
mMinIndiId = mCandidateIndiIdsMapItBegin.key(); |
| 103 |
QMap<cint64, IndividualIdCandidateData>::const_iterator lastIt = mCandidateIndiIdsMapItEnd; |
| 104 |
--lastIt; |
| 105 |
mMaxIndiId = lastIt.key(); |
| 106 |
} |
| 107 |
|
| 108 |
mAverageIterationIncrementSize = (mMaxIndiId - mMinIndiId) / (double)mCandidateIndiIdsMap.size(); |
| 109 |
mIterationCount = mCandidateIndiIdsMap.size(); |
| 110 |
|
| 111 |
mHasCurrentIndiId = false; |
| 112 |
mCurrentIndiId = -1; |
| 113 |
mCurrentIndividualItem = nullptr; |
| 114 |
mCurrentIndiInstItemRef = CRealizationIndividualInstanceItemReference(); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
if (mMapInitialized) { |
| 119 |
loadIteratorIndividualItemData(mCandidateIndiIdsMapIt); |
| 120 |
} |
| 121 |
|
| 122 |
return !mInitializationRequired; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::atEnd() { |
| 128 |
return !mHasCurrentIndiId && mMapInitialized; |
| 129 |
} |
| 130 |
|
| 131 |
CRealizationRemainingInstancesEstimation COptimizedRepresentativeKPSetIndividualCandiatesIterator::remainingInstancesEstimation() { |
| 132 |
CRealizationRemainingInstancesEstimation estimation; |
| 133 |
estimation.setMinimalCount(mCandidateIndiIdsMap.size()); |
| 134 |
estimation.setMaximalCount(mCandidateIndiIdsMap.size()); |
| 135 |
|
| 136 |
cint64 remainingEstimation = 0; |
| 137 |
if (mHasCurrentIndiId) { |
| 138 |
cint64 diff = mMaxIndiId - mCurrentIndiId; |
| 139 |
if (!mIterateAscending) { |
| 140 |
diff = mCurrentIndiId - mMinIndiId; |
| 141 |
} |
| 142 |
remainingEstimation = (double)diff / mAverageIterationIncrementSize; |
| 143 |
|
| 144 |
remainingEstimation = qMin(remainingEstimation, (cint64)mCandidateIndiIdsMap.size()); |
| 145 |
remainingEstimation = qMax(remainingEstimation, (cint64)0); |
| 146 |
} |
| 147 |
|
| 148 |
estimation.setEstimatedCount(remainingEstimation); |
| 149 |
return estimation; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::moveNext() { |
| 154 |
if (mCandidateIndiIdsMapIt != mCandidateIndiIdsMapItEnd) { |
| 155 |
if (mIterateAscending) { |
| 156 |
++mCandidateIndiIdsMapIt; |
| 157 |
} else { |
| 158 |
if (mCandidateIndiIdsMapIt == mCandidateIndiIdsMapItBegin) { |
| 159 |
mCandidateIndiIdsMapIt = mCandidateIndiIdsMapItEnd; |
| 160 |
} else { |
| 161 |
--mCandidateIndiIdsMapIt; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
loadIteratorIndividualItemData(mCandidateIndiIdsMapIt); |
| 167 |
|
| 168 |
if (mHasCurrentIndiId) { |
| 169 |
mLastIndiId = mCurrentIndiId; |
| 170 |
} else { |
| 171 |
mLastIndiId = -1; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
return mHasCurrentIndiId && !mInitializationRequired; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
COptimizedRepresentativeKPSetIndividualCandiatesIterator* COptimizedRepresentativeKPSetIndividualCandiatesIterator::loadIteratorIndividualItemData(QMap<cint64, IndividualIdCandidateData>::const_iterator currentIndiIdsMapIt) { |
| 181 |
mHasCurrentIndiId = false; |
| 182 |
mCurrentIndiId = -1; |
| 183 |
mCurrentIndividualItem = nullptr; |
| 184 |
mCurrentIndiInstItemRef = CRealizationIndividualInstanceItemReference(); |
| 185 |
|
| 186 |
if (currentIndiIdsMapIt != mCandidateIndiIdsMapItEnd) { |
| 187 |
mCurrentIndiId = currentIndiIdsMapIt.key(); |
| 188 |
mHasCurrentIndiId = true; |
| 189 |
|
| 190 |
mCurrentIndividualItem = mIndividualInstantiatedItemHash->getIndividualInstantiatedItem(mCurrentIndiId); |
| 191 |
if (mCurrentIndividualItem) { |
| 192 |
mCurentIndividual = mCurrentIndividualItem->getIndividual(); |
| 193 |
mCurrentIndiInstItemRef = CRealizationIndividualInstanceItemReference(mCurentIndividual, mCurrentIndiId, mCurrentIndividualItem); |
| 194 |
} else { |
| 195 |
mCurentIndividual = mIndividualVector->getData(mCurrentIndiId); |
| 196 |
mCurrentIndiInstItemRef = CRealizationIndividualInstanceItemReference(mCurentIndividual, mCurrentIndiId, mCurrentIndividualItem); |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
return this; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::moveTo(const CRealizationIndividualInstanceItemReference& indiInstItemRef, bool moveOverCursor) { |
| 208 |
cint64 cursorId = indiInstItemRef.getIndividualID(); |
| 209 |
|
| 210 |
if (!mHasCurrentIndiId || mIterateAscending && cursorId < mCurrentIndiId || !mIterateAscending && cursorId > mCurrentIndiId) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
cint64 diff = qAbs(cursorId - mCurrentIndiId); |
| 215 |
cint64 iterationSteps = diff / mAverageIterationIncrementSize; |
| 216 |
|
| 217 |
while (mHasCurrentIndiId && iterationSteps < MAX_ITERTION_BEFORE_MOVE_LIMIT && (mIterateAscending && mCurrentIndiId < cursorId || !mIterateAscending && mCurrentIndiId > cursorId)) { |
| 218 |
moveNext(); |
| 219 |
|
| 220 |
diff = qAbs(cursorId - mCurrentIndiId); |
| 221 |
iterationSteps = diff / mAverageIterationIncrementSize; |
| 222 |
} |
| 223 |
|
| 224 |
if (mHasCurrentIndiId && iterationSteps > MAX_ITERTION_BEFORE_MOVE_LIMIT && (mIterateAscending && mCurrentIndiId < cursorId || !mIterateAscending && mCurrentIndiId > cursorId)) { |
| 225 |
mLastIndiId = -1; |
| 226 |
mIterationMoved = true; |
| 227 |
const QMap<cint64, IndividualIdCandidateData>& candidateIndiIdsMap = mCandidateIndiIdsMap; |
| 228 |
if (mIterateAscending) { |
| 229 |
mCandidateIndiIdsMapIt = candidateIndiIdsMap.lowerBound(cursorId); |
| 230 |
} else { |
| 231 |
mCandidateIndiIdsMapIt = candidateIndiIdsMap.upperBound(cursorId); |
| 232 |
if (mCandidateIndiIdsMapIt != mCandidateIndiIdsMapItEnd) { |
| 233 |
--mCandidateIndiIdsMapIt; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
if (moveOverCursor) { |
| 238 |
moveNext(); |
| 239 |
} else { |
| 240 |
loadIteratorIndividualItemData(mCandidateIndiIdsMapIt); |
| 241 |
} |
| 242 |
return mHasCurrentIndiId; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
CRealizationIndividualInstanceItemReference COptimizedRepresentativeKPSetIndividualCandiatesIterator::currentIndividualInstanceItemReference() { |
| 247 |
return mCurrentIndiInstItemRef; |
| 248 |
} |
| 249 |
|
| 250 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::isCurrentInstancePossible() { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
bool COptimizedRepresentativeKPSetIndividualCandiatesIterator::isCurrentInstanceCertain() { |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
CRealizationIndividualInstanceItemReferenceIterator* COptimizedRepresentativeKPSetIndividualCandiatesIterator::getCopy() { |
| 260 |
COptimizedRepresentativeKPSetIndividualCandiatesIterator* copiedIterator = new COptimizedRepresentativeKPSetIndividualCandiatesIterator(mCandidateIndiIdsMap, mBackendAssocCacheReader, mIndividualInstantiatedItemHash, mIndividualVector, mSorting, mIndiInstItemRefCursor, mMoveOverCursor); |
| 261 |
|
| 262 |
copiedIterator->mCandidateIndiIdsMap = mCandidateIndiIdsMap; |
| 263 |
copiedIterator->mCandidateIndiIdsMapItBegin = mCandidateIndiIdsMapItBegin; |
| 264 |
copiedIterator->mCandidateIndiIdsMapItEnd = mCandidateIndiIdsMapItEnd; |
| 265 |
copiedIterator->mCandidateIndiIdsMapIt = mCandidateIndiIdsMapIt; |
| 266 |
copiedIterator->mCurrentIndiId = mCurrentIndiId; |
| 267 |
copiedIterator->mHasCurrentIndiId = mHasCurrentIndiId; |
| 268 |
copiedIterator->mCurrentIndividualItem = mCurrentIndividualItem; |
| 269 |
copiedIterator->mCurentIndividual = mCurentIndividual; |
| 270 |
copiedIterator->mCurrentIndiInstItemRef = mCurrentIndiInstItemRef; |
| 271 |
|
| 272 |
copiedIterator->mMapInitialized = mMapInitialized; |
| 273 |
copiedIterator->mInitializationRequired = mInitializationRequired; |
| 274 |
copiedIterator->mMinIndiId = mMinIndiId; |
| 275 |
copiedIterator->mMaxIndiId = mMaxIndiId; |
| 276 |
copiedIterator->mLastIndiId = mLastIndiId; |
| 277 |
copiedIterator->mAverageIterationIncrementSize = mAverageIterationIncrementSize; |
| 278 |
copiedIterator->mIterationCount = mIterationCount; |
| 279 |
copiedIterator->mIterationMoved = mIterationMoved; |
| 280 |
|
| 281 |
return copiedIterator; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
CRealizationIndividualSorting COptimizedRepresentativeKPSetIndividualCandiatesIterator::getSorting() { |
| 286 |
return mSorting; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
}; // end namespace Realizer |
| 294 |
|
| 295 |
}; // end namespace Reasoner |
| 296 |
|
| 297 |
}; // end namespace Konclude |