| 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 "CIndividualConceptBatchProcessingQueue.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Process { |
| 31 |
|
| 32 |
|
| 33 |
CIndividualConceptBatchProcessingQueue::CIndividualConceptBatchProcessingQueue(CProcessContext* processContext) |
| 34 |
: mProcessContext(processContext),mConceptIndiQueueMap(processContext),mBindCountIndiQueueMap(processContext) { |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
CIndividualConceptBatchProcessingQueue* CIndividualConceptBatchProcessingQueue::initProcessingQueue(CIndividualConceptBatchProcessingQueue* processingQueue) { |
| 39 |
if (processingQueue) { |
| 40 |
mConceptIndiQueueMap = processingQueue->mConceptIndiQueueMap; |
| 41 |
mBindCountIndiQueueMap = processingQueue->mBindCountIndiQueueMap; |
| 42 |
mUseCurrentProcessingQueue = processingQueue->mUseCurrentProcessingQueue; |
| 43 |
mCurrentProcessingTag = processingQueue->mCurrentProcessingTag; |
| 44 |
mLocCurrentProcessingQueue = nullptr; |
| 45 |
mBindindBasedQueuedCount = processingQueue->mBindindBasedQueuedCount; |
| 46 |
} else { |
| 47 |
mConceptIndiQueueMap.clear(); |
| 48 |
mBindCountIndiQueueMap.clear(); |
| 49 |
mLocCurrentProcessingQueue = nullptr; |
| 50 |
mUseCurrentProcessingQueue = nullptr; |
| 51 |
mCurrentProcessingTag = -1; |
| 52 |
mBindindBasedQueuedCount = 0; |
| 53 |
} |
| 54 |
return this; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
bool CIndividualConceptBatchProcessingQueue::isEmpty() { |
| 60 |
if (mBindindBasedQueuedCount > 0) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
if (mUseCurrentProcessingQueue) { |
| 64 |
if (!mUseCurrentProcessingQueue->getIndividualQueue()->isEmpty()) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
} |
| 68 |
for (CPROCESSMAP<cint64,CIndividualConceptBatchProcessingQueueData>::const_iterator it = mConceptIndiQueueMap.constBegin(), itEnd = mConceptIndiQueueMap.constEnd(); it != itEnd; ++it) { |
| 69 |
const CIndividualConceptBatchProcessingQueueData& data = it.value(); |
| 70 |
if (data.mUseProcData) { |
| 71 |
if (!data.mUseProcData->getIndividualQueue()->isEmpty()) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
bool CIndividualConceptBatchProcessingQueue::takeNextConceptProcessIndividual(CConcept*& concept, CIndividualProcessNode*& indiNode, CConceptProcessDescriptor*& conProDes) { |
| 82 |
if (!mUseCurrentProcessingQueue) { |
| 83 |
|
| 84 |
if (mBindindBasedQueuedCount > 0) { |
| 85 |
for (CPROCESSMAP<cint64,CIndividualConceptBatchProcessingQueueData>::iterator it = mBindCountIndiQueueMap.begin(), itEnd = mBindCountIndiQueueMap.end(); it != itEnd; ++it) { |
| 86 |
CIndividualConceptBatchProcessingQueueData& data = it.value(); |
| 87 |
if (!data.mUseProcData->getIndividualQueue()->isEmpty()) { |
| 88 |
CIndividualConceptBatchProcessingData* procData = nullptr; |
| 89 |
if (!data.mLocProcData) { |
| 90 |
procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 91 |
procData->initConceptBatchProcessingData(data.mUseProcData); |
| 92 |
data.mLocProcData = procData; |
| 93 |
data.mUseProcData = procData; |
| 94 |
} else { |
| 95 |
procData = data.mUseProcData; |
| 96 |
} |
| 97 |
|
| 98 |
procData->getIndividualQueue()->takeNextProcessIndiviudalConceptProcessDescriptor(indiNode,conProDes); |
| 99 |
cint64 indiID = indiNode->getIndividualNodeID(); |
| 100 |
cint64 conTag = conProDes->getConceptDescriptor()->getConceptTag(); |
| 101 |
CIndividualConceptQueuedData& indConQueData = mBindCountIndiQueuedHash[QPair<cint64,cint64>(conTag,indiID)]; |
| 102 |
indConQueData.mQueued = false; |
| 103 |
--mBindindBasedQueuedCount; |
| 104 |
return true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// TODO: check bug?? |
| 109 |
mBindindBasedQueuedCount = 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
//if (!mBindCountIndiQueueMap.isEmpty()) { |
| 114 |
// CPROCESSMAP<cint64,CIndividualConceptBatchProcessingQueueData>::iterator it = mBindCountIndiQueueMap.begin(); |
| 115 |
// while (it != mBindCountIndiQueueMap.end() && (!mUseCurrentProcessingQueue || mUseCurrentProcessingQueue->getIndividualQueue()->isEmpty())) { |
| 116 |
// CIndividualConceptBatchProcessingQueueData& data = it.value(); |
| 117 |
// mCurrentProcessingTag = it.key(); |
| 118 |
// mUseCurrentProcessingQueue = data.mUseProcData; |
| 119 |
// mLocCurrentProcessingQueue = data.mLocProcData; |
| 120 |
// it = mBindCountIndiQueueMap.erase(it); |
| 121 |
// } |
| 122 |
//} |
| 123 |
if (!mConceptIndiQueueMap.isEmpty()) { |
| 124 |
CPROCESSMAP<cint64,CIndividualConceptBatchProcessingQueueData>::iterator it = mConceptIndiQueueMap.begin(); |
| 125 |
while (it != mConceptIndiQueueMap.end() && (!mUseCurrentProcessingQueue || mUseCurrentProcessingQueue->getIndividualQueue()->isEmpty())) { |
| 126 |
CIndividualConceptBatchProcessingQueueData& data = it.value(); |
| 127 |
mCurrentProcessingTag = it.key(); |
| 128 |
mUseCurrentProcessingQueue = data.mUseProcData; |
| 129 |
mLocCurrentProcessingQueue = data.mLocProcData; |
| 130 |
it = mConceptIndiQueueMap.erase(it); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if (mUseCurrentProcessingQueue) { |
| 135 |
cint64 queuedIndiCount = mUseCurrentProcessingQueue->getIndividualQueue()->getQueuedIndividualCount(); |
| 136 |
if (queuedIndiCount > 1) { |
| 137 |
if (!mLocCurrentProcessingQueue) { |
| 138 |
CIndividualConceptBatchProcessingData* procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 139 |
procData->initConceptBatchProcessingData(mUseCurrentProcessingQueue); |
| 140 |
mLocCurrentProcessingQueue = procData; |
| 141 |
mUseCurrentProcessingQueue = procData; |
| 142 |
} |
| 143 |
mUseCurrentProcessingQueue->getIndividualQueue()->takeNextProcessIndiviudalConceptProcessDescriptor(indiNode,conProDes); |
| 144 |
//if (!mUseCurrentProcessingQueue->hasConcept()) { |
| 145 |
// cint64 indiID = indiNode->getIndividualID(); |
| 146 |
// cint64 conTag = conProDes->getConceptDescriptor()->getConceptTag(); |
| 147 |
// CIndividualConceptQueuedData& indConQueData = mBindCountIndiQueuedHash[QPair<cint64,cint64>(conTag,indiID)]; |
| 148 |
// indConQueData.mQueued = false; |
| 149 |
//} |
| 150 |
} else if (queuedIndiCount == 1) { |
| 151 |
mUseCurrentProcessingQueue->getIndividualQueue()->getNextProcessIndiviudalConceptProcessDescriptor(indiNode,conProDes); |
| 152 |
//if (!mUseCurrentProcessingQueue->hasConcept()) { |
| 153 |
// cint64 indiID = indiNode->getIndividualID(); |
| 154 |
// cint64 conTag = conProDes->getConceptDescriptor()->getConceptTag(); |
| 155 |
// CIndividualConceptQueuedData& indConQueData = mBindCountIndiQueuedHash[QPair<cint64,cint64>(conTag,indiID)]; |
| 156 |
// indConQueData.mQueued = false; |
| 157 |
//} |
| 158 |
mUseCurrentProcessingQueue = nullptr; |
| 159 |
mLocCurrentProcessingQueue = nullptr; |
| 160 |
mCurrentProcessingTag = -1; |
| 161 |
} else { |
| 162 |
mUseCurrentProcessingQueue = nullptr; |
| 163 |
mLocCurrentProcessingQueue = nullptr; |
| 164 |
mCurrentProcessingTag = -1; |
| 165 |
} |
| 166 |
} |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
CIndividualConceptBatchProcessingQueue* CIndividualConceptBatchProcessingQueue::insertIndiviudalForConcept(CConcept* concept, CIndividualProcessNode* individual, CConceptProcessDescriptor* conProDes) { |
| 172 |
CIndividualConceptBatchProcessingData* procData = getBatchProcessingData(concept,true); |
| 173 |
procData->getIndividualQueue()->insertProcessIndiviudalConceptProcessDescriptor(individual,conProDes); |
| 174 |
return this; |
| 175 |
} |
| 176 |
|
| 177 |
CIndividualConceptBatchProcessingQueue* CIndividualConceptBatchProcessingQueue::insertIndiviudalForBindingCount(CConcept* concept, cint64 bindCount, CIndividualProcessNode* individual, CConceptProcessDescriptor* conProDes) { |
| 178 |
cint64 conTag = conProDes->getConceptDescriptor()->getConceptTag(); |
| 179 |
cint64 indiID = individual->getIndividualNodeID(); |
| 180 |
CIndividualConceptQueuedData& queuedData = mBindCountIndiQueuedHash[QPair<cint64,cint64>(conTag,indiID)]; |
| 181 |
if (!queuedData.mQueued || bindCount > queuedData.mPrevTag) { |
| 182 |
CIndividualConceptBatchProcessingData* procData = getBatchProcessingData(concept, bindCount, true); |
| 183 |
if (queuedData.mQueued) { |
| 184 |
// remove previous |
| 185 |
CIndividualConceptBatchProcessingData* prevProcData = getBatchProcessingData(concept,queuedData.mPrevTag,true); |
| 186 |
CConceptProcessDescriptor* prevConProDes = prevProcData->getIndividualQueue()->removeQueuedProcessIndiviudal(individual); |
| 187 |
while (prevConProDes) { |
| 188 |
if (prevConProDes->getProcessingRestrictionSpecification()) { |
| 189 |
procData->getIndividualQueue()->insertProcessIndiviudalConceptProcessDescriptor(individual, prevConProDes); |
| 190 |
} |
| 191 |
prevConProDes = prevProcData->getIndividualQueue()->removeQueuedProcessIndiviudal(individual); |
| 192 |
} |
| 193 |
} else { |
| 194 |
++mBindindBasedQueuedCount; |
| 195 |
} |
| 196 |
procData->getIndividualQueue()->insertProcessIndiviudalConceptProcessDescriptor(individual,conProDes); |
| 197 |
queuedData.mPrevTag = bindCount; |
| 198 |
queuedData.mQueued = true; |
| 199 |
} |
| 200 |
return this; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
CIndividualConceptBatchProcessingData* CIndividualConceptBatchProcessingQueue::getBatchProcessingData(CConcept* concept, bool createAndLocalize) { |
| 205 |
CIndividualConceptBatchProcessingData* procData = nullptr; |
| 206 |
cint64 conTag = concept->getConceptTag(); |
| 207 |
if (createAndLocalize) { |
| 208 |
//if (mCurrentProcessingTag == conTag) { |
| 209 |
// if (!mLocCurrentProcessingQueue) { |
| 210 |
// procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 211 |
// procData->initConceptBatchProcessingData(mUseCurrentProcessingQueue); |
| 212 |
// mLocCurrentProcessingQueue = procData; |
| 213 |
// mUseCurrentProcessingQueue = procData; |
| 214 |
// } else { |
| 215 |
// procData = mUseCurrentProcessingQueue; |
| 216 |
// } |
| 217 |
//} else { |
| 218 |
CIndividualConceptBatchProcessingQueueData& data = mConceptIndiQueueMap[conTag]; |
| 219 |
if (!data.mLocProcData) { |
| 220 |
procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 221 |
procData->initConceptBatchProcessingData(data.mUseProcData); |
| 222 |
procData->setConcept(concept); |
| 223 |
data.mUseProcData = procData; |
| 224 |
data.mLocProcData = procData; |
| 225 |
} |
| 226 |
procData = data.mUseProcData; |
| 227 |
//} |
| 228 |
} else { |
| 229 |
//if (mCurrentProcessingTag == conTag) { |
| 230 |
// procData = mUseCurrentProcessingQueue; |
| 231 |
//} else { |
| 232 |
const CIndividualConceptBatchProcessingQueueData& data = mConceptIndiQueueMap.value(conTag); |
| 233 |
procData = data.mUseProcData; |
| 234 |
//} |
| 235 |
} |
| 236 |
return procData; |
| 237 |
} |
| 238 |
|
| 239 |
CIndividualConceptBatchProcessingData* CIndividualConceptBatchProcessingQueue::getBatchProcessingData(CConcept* concept, cint64 bindCount, bool createAndLocalize) { |
| 240 |
CIndividualConceptBatchProcessingData* procData = nullptr; |
| 241 |
cint64 processingTag = -bindCount; |
| 242 |
if (createAndLocalize) { |
| 243 |
if (mCurrentProcessingTag == processingTag) { |
| 244 |
if (!mLocCurrentProcessingQueue) { |
| 245 |
procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 246 |
procData->initConceptBatchProcessingData(mUseCurrentProcessingQueue); |
| 247 |
mLocCurrentProcessingQueue = procData; |
| 248 |
mUseCurrentProcessingQueue = procData; |
| 249 |
} else { |
| 250 |
procData = mUseCurrentProcessingQueue; |
| 251 |
} |
| 252 |
} else { |
| 253 |
CIndividualConceptBatchProcessingQueueData& data = mBindCountIndiQueueMap[processingTag]; |
| 254 |
if (!data.mLocProcData) { |
| 255 |
procData = CObjectParameterizingAllocator< CIndividualConceptBatchProcessingData,CProcessContext* >::allocateAndConstructAndParameterize(mProcessContext->getUsedMemoryAllocationManager(),mProcessContext); |
| 256 |
procData->initConceptBatchProcessingData(data.mUseProcData); |
| 257 |
data.mUseProcData = procData; |
| 258 |
data.mLocProcData = procData; |
| 259 |
} |
| 260 |
procData = data.mUseProcData; |
| 261 |
} |
| 262 |
} else { |
| 263 |
if (mCurrentProcessingTag == processingTag) { |
| 264 |
procData = mUseCurrentProcessingQueue; |
| 265 |
} else { |
| 266 |
const CIndividualConceptBatchProcessingQueueData& data = mBindCountIndiQueueMap.value(processingTag); |
| 267 |
procData = data.mUseProcData; |
| 268 |
} |
| 269 |
} |
| 270 |
return procData; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
}; // end namespace Process |
| 275 |
|
| 276 |
}; // end namespace Kernel |
| 277 |
|
| 278 |
}; // end namespace Reasoner |
| 279 |
|
| 280 |
}; // end namespace Konclude |