| 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 "CConceptProcessingQueue.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Process { |
| 31 |
|
| 32 |
|
| 33 |
CConceptProcessingQueue::CConceptProcessingQueue(CProcessContext* processContext) : mProcessContext(processContext) { |
| 34 |
mMaxPriorityIndex = -1; |
| 35 |
mDesCount = 0; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
CConceptProcessingQueue::~CConceptProcessingQueue() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
CConceptProcessingQueue* CConceptProcessingQueue::initProcessingQueue(CConceptProcessingQueue* processingQueue) { |
| 44 |
if (processingQueue) { |
| 45 |
mMaxPriorityIndex = processingQueue->mMaxPriorityIndex; |
| 46 |
mDesCount = processingQueue->mDesCount; |
| 47 |
for (cint64 i = 0; i < mMaxIndex; ++i) { |
| 48 |
mPriorityVec[i] = processingQueue->mPriorityVec[i]; |
| 49 |
mPriorityVec[i].mPrevSortedPriorityDescriptorLinker = processingQueue->mPriorityVec[i].mSortedPriorityDescriptorLinker; |
| 50 |
} |
| 51 |
} else { |
| 52 |
mMaxPriorityIndex = -1; |
| 53 |
mDesCount = 0; |
| 54 |
for (cint64 i = 0; i < mMaxIndex; ++i) { |
| 55 |
CConceptProcessingPriorityQueueData& queueData = mPriorityVec[i]; |
| 56 |
queueData.mDefaultPriorityDescriptorLinker = nullptr; |
| 57 |
queueData.mSortedPriorityDescriptorLinker = nullptr; |
| 58 |
queueData.mPrevSortedPriorityDescriptorLinker = nullptr; |
| 59 |
} |
| 60 |
} |
| 61 |
return this; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
CConceptProcessingQueue* CConceptProcessingQueue::resetProcessingQueueModification() { |
| 66 |
for (cint64 i = 0; i < mMaxIndex; ++i) { |
| 67 |
CConceptProcessingPriorityQueueData& queueData = mPriorityVec[i]; |
| 68 |
queueData.mPrevSortedPriorityDescriptorLinker = nullptr; |
| 69 |
} |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
qint64 CConceptProcessingQueue::getDescriptorCount() { |
| 75 |
return mDesCount; |
| 76 |
} |
| 77 |
|
| 78 |
bool CConceptProcessingQueue::hasProcessDescriptor() { |
| 79 |
return mDesCount > 0; |
| 80 |
} |
| 81 |
|
| 82 |
bool CConceptProcessingQueue::isEmpty() { |
| 83 |
return mDesCount <= 0; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
CConceptProcessingQueue *CConceptProcessingQueue::reinsertConceptProcessDescriptor(CConceptProcessDescriptor *conProDes) { |
| 88 |
CConceptProcessPriority conPriority = conProDes->getProcessPriority(); |
| 89 |
double priority = conPriority.getPriority(); |
| 90 |
cint64 priorityIndex = priority; |
| 91 |
++mDesCount; |
| 92 |
mMaxPriorityIndex = qMax(mMaxPriorityIndex,priorityIndex); |
| 93 |
CConceptProcessingPriorityQueueData& proPriQueData = mPriorityVec[priorityIndex]; |
| 94 |
if (priority == (double)priorityIndex) { |
| 95 |
proPriQueData.mDefaultPriorityDescriptorLinker = conProDes; |
| 96 |
} else { |
| 97 |
proPriQueData.mSortedPriorityDescriptorLinker = conProDes; |
| 98 |
if (proPriQueData.mPrevSortedPriorityDescriptorLinker && proPriQueData.mPrevSortedPriorityDescriptorLinker == conProDes->getNext()) { |
| 99 |
proPriQueData.mPrevSortedPriorityDescriptorLinker = conProDes; |
| 100 |
} |
| 101 |
} |
| 102 |
return this; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
CConceptProcessingQueue* CConceptProcessingQueue::insertConceptProcessDescriptor(CConceptProcessDescriptor *conProDes) { |
| 107 |
CConceptProcessPriority conPriority = conProDes->getProcessPriority(); |
| 108 |
double priority = conPriority.getPriority(); |
| 109 |
cint64 priorityIndex = priority; |
| 110 |
++mDesCount; |
| 111 |
mMaxPriorityIndex = qMax(mMaxPriorityIndex,priorityIndex); |
| 112 |
CConceptProcessingPriorityQueueData& proPriQueData = mPriorityVec[priorityIndex]; |
| 113 |
if (priority == (double)priorityIndex) { |
| 114 |
proPriQueData.mDefaultPriorityDescriptorLinker = conProDes->append(proPriQueData.mDefaultPriorityDescriptorLinker); |
| 115 |
} else { |
| 116 |
CConceptProcessDescriptor* sortedDesLinker = proPriQueData.mSortedPriorityDescriptorLinker; |
| 117 |
if (!sortedDesLinker || *conProDes <= *sortedDesLinker) { |
| 118 |
// insert to begin |
| 119 |
proPriQueData.mSortedPriorityDescriptorLinker = conProDes->append(proPriQueData.mSortedPriorityDescriptorLinker); |
| 120 |
} else { |
| 121 |
CConceptProcessDescriptor* lastSortedDesLinker = nullptr; |
| 122 |
while (sortedDesLinker && !(*conProDes <= *sortedDesLinker)) { |
| 123 |
if (sortedDesLinker == proPriQueData.mPrevSortedPriorityDescriptorLinker) { |
| 124 |
// make descriptor local |
| 125 |
CConceptProcessDescriptor* newLocalDescriptor = CObjectAllocator<CConceptProcessDescriptor>::allocateAndConstruct(CContext::getMemoryAllocationManager(mProcessContext)); |
| 126 |
newLocalDescriptor->initCopy(sortedDesLinker); |
| 127 |
|
| 128 |
proPriQueData.mPrevSortedPriorityDescriptorLinker = proPriQueData.mPrevSortedPriorityDescriptorLinker->getNext(); |
| 129 |
sortedDesLinker = sortedDesLinker->getNext(); |
| 130 |
|
| 131 |
if (lastSortedDesLinker) { |
| 132 |
lastSortedDesLinker->setNext(newLocalDescriptor); |
| 133 |
} else { |
| 134 |
proPriQueData.mSortedPriorityDescriptorLinker = newLocalDescriptor; |
| 135 |
} |
| 136 |
lastSortedDesLinker = newLocalDescriptor; |
| 137 |
} else { |
| 138 |
lastSortedDesLinker = sortedDesLinker; |
| 139 |
sortedDesLinker = sortedDesLinker->getNext(); |
| 140 |
} |
| 141 |
} |
| 142 |
if (lastSortedDesLinker) { |
| 143 |
conProDes->append(sortedDesLinker); |
| 144 |
lastSortedDesLinker->setNext(conProDes); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
return this; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
CConceptProcessDescriptor* CConceptProcessingQueue::takeNextConceptDescriptorProcess() { |
| 153 |
CConceptProcessDescriptor* conProDes = nullptr; |
| 154 |
while (mDesCount > 0 && !conProDes && mMaxPriorityIndex >= 0) { |
| 155 |
CConceptProcessingPriorityQueueData& proPriQueData = mPriorityVec[mMaxPriorityIndex]; |
| 156 |
conProDes = proPriQueData.mDefaultPriorityDescriptorLinker; |
| 157 |
if (conProDes) { |
| 158 |
proPriQueData.mDefaultPriorityDescriptorLinker = conProDes->getNext(); |
| 159 |
} else { |
| 160 |
conProDes = proPriQueData.mSortedPriorityDescriptorLinker; |
| 161 |
if (conProDes) { |
| 162 |
if (conProDes == proPriQueData.mPrevSortedPriorityDescriptorLinker) { |
| 163 |
proPriQueData.mPrevSortedPriorityDescriptorLinker = proPriQueData.mPrevSortedPriorityDescriptorLinker->getNext(); |
| 164 |
} |
| 165 |
proPriQueData.mSortedPriorityDescriptorLinker = conProDes->getNext(); |
| 166 |
} else { |
| 167 |
--mMaxPriorityIndex; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
if (conProDes) { |
| 172 |
--mDesCount; |
| 173 |
} |
| 174 |
return conProDes; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
bool CConceptProcessingQueue::getNextConceptProcessPriority(CConceptProcessPriority* priority) { |
| 179 |
CConceptProcessDescriptor* conProDes = nullptr; |
| 180 |
if (mDesCount <= 0) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
while (!conProDes && mMaxPriorityIndex >= 0) { |
| 184 |
CConceptProcessingPriorityQueueData& proPriQueData = mPriorityVec[mMaxPriorityIndex]; |
| 185 |
conProDes = proPriQueData.mDefaultPriorityDescriptorLinker; |
| 186 |
if (!conProDes) { |
| 187 |
conProDes = proPriQueData.mSortedPriorityDescriptorLinker; |
| 188 |
if (!conProDes) { |
| 189 |
--mMaxPriorityIndex; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
*priority = conProDes->getProcessPriority(); |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
CConceptProcessingQueueIterator CConceptProcessingQueue::getConceptProcessingQueueIterator() { |
| 198 |
return CConceptProcessingQueueIterator(mPriorityVec,mDesCount); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
}; // end namespace Process |
| 203 |
|
| 204 |
}; // end namespace Kernel |
| 205 |
|
| 206 |
}; // end namespace Reasoner |
| 207 |
|
| 208 |
}; // end namespace Konclude |