| 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 "COptimizedRepresentativeKPSetIntersectionCombinationIterator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Realizer { |
| 29 |
|
| 30 |
|
| 31 |
COptimizedRepresentativeKPSetIntersectionCombinationIterator::COptimizedRepresentativeKPSetIntersectionCombinationIterator(QList<CRealizationIndividualInstanceItemReferenceIterator*> iteratorList) { |
| 32 |
mIteratorList = iteratorList; |
| 33 |
|
| 34 |
for (CRealizationIndividualInstanceItemReferenceIterator* iterator : mIteratorList) { |
| 35 |
CRealizationIteratorData* iteratorData = new CRealizationIteratorData(iterator); |
| 36 |
mIteratorDataList.append(iteratorData); |
| 37 |
} |
| 38 |
|
| 39 |
mVisitedIterationCount = 0; |
| 40 |
mIntersectionIterationCount = 0; |
| 41 |
|
| 42 |
mIteratingIteratorData = nullptr; |
| 43 |
|
| 44 |
|
| 45 |
init(); |
| 46 |
|
| 47 |
moveToValidPosition(); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
COptimizedRepresentativeKPSetIntersectionCombinationIterator::~COptimizedRepresentativeKPSetIntersectionCombinationIterator() { |
| 52 |
qDeleteAll(mIteratorList); |
| 53 |
qDeleteAll(mIteratorDataList); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::init() { |
| 58 |
bool requiresInitialization = false; |
| 59 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 60 |
if (iterationData->mIterator->currentIndividualInstanceItemReference().getIndividualID() == -1) { |
| 61 |
if (iterationData->mIterator->requiresInitialization()) { |
| 62 |
requiresInitialization = true; |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if (!requiresInitialization) { |
| 70 |
cint64 currentMinMaxCount = 0; |
| 71 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 72 |
cint64 itMaxCount = iterationData->mIterator->remainingInstancesEstimation().getMaximalCount(); |
| 73 |
if (!mIteratingIteratorData || itMaxCount < currentMinMaxCount) { |
| 74 |
currentMinMaxCount = itMaxCount; |
| 75 |
mIteratingIteratorData = iterationData; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
mIterateAscending = true; |
| 80 |
if (mIteratingIteratorData) { |
| 81 |
mIterateAscending = mIteratingIteratorData->mIterator->getSorting().getOrder() != CRealizationIndividualSorting::SORT_ORDER_DESCENDING; |
| 82 |
return true; |
| 83 |
} |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::requiresInitialization() { |
| 90 |
if (mIteratingIteratorData) { |
| 91 |
if (!mIteratingIteratorData->mIterator->atEnd()) { |
| 92 |
if (mIteratingIteratorData->mIterator->requiresInitialization()) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
cint64 currentId = mIteratingIteratorData->mIterator->currentIndividualInstanceItemReference().getIndividualID(); |
| 96 |
for (CRealizationIteratorData* iteratorData : mIteratorDataList) { |
| 97 |
if (iteratorData != mIteratingIteratorData) { |
| 98 |
if (iteratorData->mIterator->currentIndividualInstanceItemReference().getIndividualID() == currentId) { |
| 99 |
if (iteratorData->mIterator->requiresInitialization()) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} else { |
| 107 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 108 |
if (iterationData->mIterator->requiresInitialization()) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::begin() { |
| 117 |
|
| 118 |
bool requiredInitializiation = false; |
| 119 |
bool iteratorMoved = false; |
| 120 |
bool moreRequiredInitialization = false; |
| 121 |
|
| 122 |
if (requiresInitialization()) { |
| 123 |
for (CRealizationIteratorData* iteratorData : mIteratorDataList) { |
| 124 |
if (iteratorData->mIterator->requiresInitialization()) { |
| 125 |
cint64 prevIndiId = iteratorData->mIterator->currentIndividualInstanceItemReference().getIndividualID(); |
| 126 |
requiredInitializiation = true; |
| 127 |
iteratorData->mIterator->begin(); |
| 128 |
cint64 newIndiId = iteratorData->mIterator->currentIndividualInstanceItemReference().getIndividualID(); |
| 129 |
if (newIndiId != prevIndiId) { |
| 130 |
iteratorMoved = true; |
| 131 |
if (newIndiId == -1 && !iteratorData->mIterator->atEnd()) { |
| 132 |
moreRequiredInitialization = true; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (!mIteratingIteratorData) { |
| 140 |
init(); |
| 141 |
} |
| 142 |
|
| 143 |
if ((iteratorMoved && !moreRequiredInitialization ) || !requiresInitialization()) { |
| 144 |
if (moveToValidPosition()) { |
| 145 |
if (requiredInitializiation && !requiresInitialization()) { |
| 146 |
++mIntersectionIterationCount; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::moveNext() { |
| 158 |
bool successfullyMoved = true; |
| 159 |
|
| 160 |
if (mIteratingIteratorData && !mIteratingIteratorData->mIterator->atEnd()) { |
| 161 |
bool moved = mIteratingIteratorData->mIterator->moveNext(); |
| 162 |
if (moved) { |
| 163 |
++mVisitedIterationCount; |
| 164 |
CRealizationIndividualInstanceItemReference indiInstItemRef = mIteratingIteratorData->mIterator->currentIndividualInstanceItemReference(); |
| 165 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 166 |
if (iterationData != mIteratingIteratorData) { |
| 167 |
iterationData->mIterator->moveTo(indiInstItemRef, false); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
successfullyMoved = moveToValidPosition(); |
| 172 |
if (successfullyMoved) { |
| 173 |
++mIntersectionIterationCount; |
| 174 |
} |
| 175 |
|
| 176 |
} else { |
| 177 |
successfullyMoved = false; |
| 178 |
} |
| 179 |
} |
| 180 |
return successfullyMoved; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::moveToValidPosition() { |
| 186 |
bool successfullyMoved = false; |
| 187 |
bool validIteratorPosition = false; |
| 188 |
bool positionRequiresInitialization = false; |
| 189 |
while ((!validIteratorPosition || !successfullyMoved) && !atEnd() && !positionRequiresInitialization) { |
| 190 |
successfullyMoved = true; |
| 191 |
positionRequiresInitialization = false; |
| 192 |
validIteratorPosition = true; |
| 193 |
|
| 194 |
|
| 195 |
cint64 minIndiId = CINT64_MAX; |
| 196 |
cint64 maxIndiId = CINT64_MIN; |
| 197 |
CRealizationIndividualInstanceItemReference minIndiItemRef; |
| 198 |
CRealizationIndividualInstanceItemReference maxIndiItemRef; |
| 199 |
|
| 200 |
CRealizationIndividualInstanceItemReference indiInstItemRef = mIteratingIteratorData->mIterator->currentIndividualInstanceItemReference(); |
| 201 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 202 |
if (iterationData != mIteratingIteratorData) { |
| 203 |
CRealizationIndividualInstanceItemReference iterIndiInstItemRef = iterationData->mIterator->currentIndividualInstanceItemReference(); |
| 204 |
if (mIterateAscending && iterIndiInstItemRef.getIndividualID() < indiInstItemRef.getIndividualID() || !mIterateAscending && iterIndiInstItemRef.getIndividualID() > indiInstItemRef.getIndividualID()) { |
| 205 |
if (!iterationData->mIterator->moveTo(indiInstItemRef, false)) { |
| 206 |
successfullyMoved = false; |
| 207 |
} |
| 208 |
cint64 movedIndiId = iterationData->mIterator->currentIndividualInstanceItemReference().getIndividualID(); |
| 209 |
if (movedIndiId < minIndiId) { |
| 210 |
minIndiId = movedIndiId; |
| 211 |
minIndiItemRef = iterationData->mIterator->currentIndividualInstanceItemReference(); |
| 212 |
} |
| 213 |
if (movedIndiId > maxIndiId) { |
| 214 |
maxIndiId = movedIndiId; |
| 215 |
maxIndiItemRef = iterationData->mIterator->currentIndividualInstanceItemReference(); |
| 216 |
} |
| 217 |
if (movedIndiId != indiInstItemRef.getIndividualID()) { |
| 218 |
validIteratorPosition = false; |
| 219 |
} else if (iterationData->mIterator->requiresInitialization()) { |
| 220 |
positionRequiresInitialization = true; |
| 221 |
} |
| 222 |
} else if (mIterateAscending && iterIndiInstItemRef.getIndividualID() > indiInstItemRef.getIndividualID() || !mIterateAscending && iterIndiInstItemRef.getIndividualID() < indiInstItemRef.getIndividualID()) { |
| 223 |
if (iterIndiInstItemRef.getIndividualID() < minIndiId) { |
| 224 |
minIndiId = iterIndiInstItemRef.getIndividualID(); |
| 225 |
minIndiItemRef = iterIndiInstItemRef; |
| 226 |
} |
| 227 |
if (iterIndiInstItemRef.getIndividualID() > maxIndiId) { |
| 228 |
maxIndiId = iterIndiInstItemRef.getIndividualID(); |
| 229 |
maxIndiItemRef = iterIndiInstItemRef; |
| 230 |
} |
| 231 |
validIteratorPosition = false; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
if (!validIteratorPosition) { |
| 236 |
if (minIndiId != CINT64_MAX && maxIndiId != CINT64_MIN && minIndiId != -1 && maxIndiId != -1) { |
| 237 |
cint64 moveToIndiId = maxIndiId; |
| 238 |
CRealizationIndividualInstanceItemReference moveToIndiItemRef = maxIndiItemRef; |
| 239 |
if (!mIterateAscending) { |
| 240 |
moveToIndiId = minIndiId; |
| 241 |
moveToIndiItemRef = minIndiItemRef; |
| 242 |
} |
| 243 |
++mVisitedIterationCount; |
| 244 |
mIteratingIteratorData->mIterator->moveTo(moveToIndiItemRef, false); |
| 245 |
} else { |
| 246 |
if (!mIteratingIteratorData->mIterator->moveNext()) { |
| 247 |
successfullyMoved = false; |
| 248 |
} else { |
| 249 |
++mVisitedIterationCount; |
| 250 |
} |
| 251 |
} |
| 252 |
} else { |
| 253 |
if (requiresInitialization()) { |
| 254 |
positionRequiresInitialization = true; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
return validIteratorPosition && successfullyMoved && !positionRequiresInitialization; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::moveTo(const CRealizationIndividualInstanceItemReference& indiInstItemRef, bool moveOver) { |
| 264 |
bool successfullyMoved = true; |
| 265 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 266 |
if (!iterationData->mIterator->moveTo(indiInstItemRef, moveOver)) { |
| 267 |
successfullyMoved = false; |
| 268 |
} |
| 269 |
} |
| 270 |
if (successfullyMoved) { |
| 271 |
++mVisitedIterationCount; |
| 272 |
if (!moveToValidPosition()) { |
| 273 |
successfullyMoved = false; |
| 274 |
} |
| 275 |
} |
| 276 |
return successfullyMoved; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::atEnd() { |
| 282 |
return !mIteratingIteratorData || mIteratingIteratorData->mIterator->atEnd(); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
CRealizationIndividualInstanceItemReference COptimizedRepresentativeKPSetIntersectionCombinationIterator::currentIndividualInstanceItemReference() { |
| 288 |
if (mIteratingIteratorData) { |
| 289 |
return mIteratingIteratorData->mIterator->currentIndividualInstanceItemReference(); |
| 290 |
} else { |
| 291 |
return CRealizationIndividualInstanceItemReference(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::isCurrentInstancePossible() { |
| 298 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 299 |
if (iterationData->mIterator->isCurrentInstancePossible()) { |
| 300 |
return true; |
| 301 |
} |
| 302 |
} |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
bool COptimizedRepresentativeKPSetIntersectionCombinationIterator::isCurrentInstanceCertain() { |
| 307 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 308 |
if (!iterationData->mIterator->isCurrentInstanceCertain()) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
} |
| 312 |
return true; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
CRealizationIndividualInstanceItemReferenceIterator* COptimizedRepresentativeKPSetIntersectionCombinationIterator::getCopy() { |
| 318 |
QList<CRealizationIndividualInstanceItemReferenceIterator*> copiedIteratorList; |
| 319 |
for (CRealizationIndividualInstanceItemReferenceIterator* iterator : mIteratorList) { |
| 320 |
CRealizationIndividualInstanceItemReferenceIterator* copiedIterator = iterator->getCopy(); |
| 321 |
copiedIteratorList.append(copiedIterator); |
| 322 |
} |
| 323 |
COptimizedRepresentativeKPSetIntersectionCombinationIterator* iteratorCopy = new COptimizedRepresentativeKPSetIntersectionCombinationIterator(copiedIteratorList); |
| 324 |
iteratorCopy->mVisitedIterationCount = mVisitedIterationCount; |
| 325 |
iteratorCopy->mIntersectionIterationCount = mIntersectionIterationCount; |
| 326 |
return iteratorCopy; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
CRealizationRemainingInstancesEstimation COptimizedRepresentativeKPSetIntersectionCombinationIterator::remainingInstancesEstimation() { |
| 331 |
CRealizationRemainingInstancesEstimation estimation; |
| 332 |
|
| 333 |
cint64 minTotalCount = 0; |
| 334 |
cint64 maxTotalCount = -1; |
| 335 |
for (CRealizationIteratorData* iterationData : mIteratorDataList) { |
| 336 |
if (maxTotalCount == -1) { |
| 337 |
maxTotalCount = iterationData->mIterator->remainingInstancesEstimation().getMaximalCount(); |
| 338 |
} else { |
| 339 |
maxTotalCount = qMin(maxTotalCount, iterationData->mIterator->remainingInstancesEstimation().getMaximalCount()); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
estimation.setMaximalCount(maxTotalCount); |
| 345 |
estimation.setMinimalCount(minTotalCount); |
| 346 |
cint64 remainingEstimation = 0; |
| 347 |
if (!atEnd()) { |
| 348 |
remainingEstimation = mIteratingIteratorData->mIterator->remainingInstancesEstimation().getEstimatedCount() * ((double)mIntersectionIterationCount / mVisitedIterationCount); |
| 349 |
remainingEstimation = qMin(remainingEstimation, maxTotalCount); |
| 350 |
remainingEstimation = qMax(remainingEstimation, (cint64)0); |
| 351 |
} |
| 352 |
estimation.setEstimatedCount(remainingEstimation); |
| 353 |
return estimation; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
CRealizationIndividualSorting COptimizedRepresentativeKPSetIntersectionCombinationIterator::getSorting() { |
| 360 |
if (mIteratingIteratorData) { |
| 361 |
return mIteratingIteratorData->mIterator->getSorting(); |
| 362 |
} else { |
| 363 |
return CRealizationIndividualSorting(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
QList<CRealizationIndividualInstanceItemReferenceIterator*>* COptimizedRepresentativeKPSetIntersectionCombinationIterator::getCombinedIteratorList() { |
| 369 |
return &mIteratorList; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
}; // end namespace Realizer |
| 375 |
|
| 376 |
}; // end namespace Reasoner |
| 377 |
|
| 378 |
}; // end namespace Konclude |