| 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 "CAnswererThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Answerer { |
| 29 |
|
| 30 |
|
| 31 |
CAnswererThread::CAnswererThread(CReasonerManager *reasonerManager) : CThread("Answerer-Thread") { |
| 32 |
mReasonerManager = reasonerManager; |
| 33 |
mCalculationManager = 0; |
| 34 |
mCurrRunningTestParallelCount = 0; |
| 35 |
mCurrRunningRequirementParallelCount = 0; |
| 36 |
mConfMaxTestParallelCount = 1; |
| 37 |
mConfMinTestParallelCount = 15; |
| 38 |
mStatCalculationJobsSubmitted = 0; |
| 39 |
mConfDirectSubmission = false; |
| 40 |
mOccStatsCacheReader = nullptr; |
| 41 |
mAnswererContext = new CAnswererContext(this); |
| 42 |
startThread(QThread::HighPriority); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
CAnswererThread::~CAnswererThread() { |
| 49 |
stopThread(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
CAnswererThread* CAnswererThread::initializeAnswerer(CConfiguration* config) { |
| 56 |
|
| 57 |
|
| 58 |
bool configErrorFlag = false; |
| 59 |
cint64 processorCount = 1; |
| 60 |
QString processorCountString = CConfigDataReader::readConfigString(config, "Konclude.Calculation.ProcessorCount", QString(), &configErrorFlag); |
| 61 |
if (!configErrorFlag) { |
| 62 |
if (processorCountString == "AUTO") { |
| 63 |
processorCount = CThread::idealThreadCount(); |
| 64 |
} |
| 65 |
else { |
| 66 |
qint64 convertedWorkerCount = processorCountString.toInt(&configErrorFlag); |
| 67 |
if (configErrorFlag) { |
| 68 |
processorCount = convertedWorkerCount; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
bool mulConfigErrorFlag = false; |
| 74 |
cint64 multiplicator = CConfigDataReader::readConfigInteger(config, "Konclude.Calculation.Answering.MultipliedUnitsParallelTestingCalculationCount", 1, &mulConfigErrorFlag); |
| 75 |
mConfMaxTestParallelCount = processorCount*multiplicator; |
| 76 |
bool maxConfigErrorFlag = false; |
| 77 |
cint64 maxParallel = CConfigDataReader::readConfigInteger(config, "Konclude.Calculation.Answering.MaximumParallelTestingCalculationCount", 1, &maxConfigErrorFlag); |
| 78 |
if (!maxConfigErrorFlag) { |
| 79 |
if (!mulConfigErrorFlag) { |
| 80 |
mConfMaxTestParallelCount = qMin(mConfMaxTestParallelCount, maxParallel); |
| 81 |
} |
| 82 |
else { |
| 83 |
mConfMaxTestParallelCount = maxParallel; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
bool CAnswererThread::handleAnswering(CAnsweringHandler* answeringHandler, CComplexAnsweringQuery* query, CCallbackData* callback) { |
| 96 |
postEvent(new CAnsweringInitializingEvent(answeringHandler, query, callback)); |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
bool CAnswererThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 107 |
if (CThread::processCustomsEvents(type,event)) { |
| 108 |
return true; |
| 109 |
} else if (type == CAnsweringInitializingEvent::EVENTTYPE) { |
| 110 |
CAnsweringInitializingEvent* aie = (CAnsweringInitializingEvent *)event; |
| 111 |
CAnsweringHandler* answeringHandler = aie->getAnsweringHandler(); |
| 112 |
CCallbackData* callback = aie->getCallbackData(); |
| 113 |
CComplexAnsweringQuery* query = aie->getQuery(); |
| 114 |
CAnsweringHandlerData* answeringHandlingData = new CAnsweringHandlerData(answeringHandler, callback); |
| 115 |
mHandlerHandlerDataHash.insert(answeringHandler, answeringHandlingData); |
| 116 |
mQueuedAnsweringHandlerList.append(answeringHandlingData); |
| 117 |
mReadWriteLock.lockForWrite(); |
| 118 |
mManagedAnsweringHandlerSet.insert(answeringHandlingData); |
| 119 |
mReadWriteLock.unlock(); |
| 120 |
answeringHandlingData->mQueued = true; |
| 121 |
answeringHandler->addAnsweringComplexQuery(query); |
| 122 |
answeringHandler->initializeProcessing(mAnswererContext); |
| 123 |
continueCalculationCreation(); |
| 124 |
return true; |
| 125 |
} else if (type == CAnsweringMessageEvent::EVENTTYPE) { |
| 126 |
CAnsweringMessageEvent* ame = (CAnsweringMessageEvent *)event; |
| 127 |
CAnsweringHandler* answeringHandler = ame->getAnsweringHandler(); |
| 128 |
CAnsweringMessageData* messageData = ame->getMessageData(); |
| 129 |
CMemoryPool* memoryPool = ame->getMemoryPool(); |
| 130 |
answeringHandler->processCalculationMessage(messageData, mAnswererContext); |
| 131 |
if (!memoryPool) { |
| 132 |
//delete messageData; |
| 133 |
} else { |
| 134 |
mAnswererContext->getMemoryPoolProvider()->releaseMemoryPool(memoryPool); |
| 135 |
} |
| 136 |
continueCalculationCreation(); |
| 137 |
return true; |
| 138 |
} else if (type == CAnsweringCalculationCompletedEvent::EVENTTYPE) { |
| 139 |
--mCurrRunningTestParallelCount; |
| 140 |
CAnsweringCalculationCompletedEvent* ame = (CAnsweringCalculationCompletedEvent *)event; |
| 141 |
CAnsweringHandler* answeringHandler = ame->getAnsweringHandler(); |
| 142 |
CAnsweringMessageData* messageData = ame->getMessageData(); |
| 143 |
answeringHandler->processCalculationMessage(messageData, mAnswererContext); |
| 144 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash.value(answeringHandler); |
| 145 |
handlerData->mJobProcessingCount--; |
| 146 |
if (!handlerData->mQueued) { |
| 147 |
handlerData->mQueued = true; |
| 148 |
mQueuedAnsweringHandlerList.prepend(handlerData); |
| 149 |
} |
| 150 |
//delete messageData; |
| 151 |
continueCalculationCreation(); |
| 152 |
return true; |
| 153 |
} else if (type == CAnsweringRequirementCompletedEvent::EVENTTYPE) { |
| 154 |
//sleep(6); |
| 155 |
--mCurrRunningRequirementParallelCount; |
| 156 |
CAnsweringRequirementCompletedEvent* ame = (CAnsweringRequirementCompletedEvent *)event; |
| 157 |
CAnsweringHandler* answeringHandler = ame->getAnsweringHandler(); |
| 158 |
CAnsweringMessageData* messageData = ame->getMessageData(); |
| 159 |
answeringHandler->processRequirementMessage(messageData, mAnswererContext); |
| 160 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash.value(answeringHandler); |
| 161 |
handlerData->mRequirmentProcessingCount--; |
| 162 |
if (!handlerData->mQueued) { |
| 163 |
handlerData->mQueued = true; |
| 164 |
mQueuedAnsweringHandlerList.prepend(handlerData); |
| 165 |
} |
| 166 |
//delete messageData; |
| 167 |
continueCalculationCreation(); |
| 168 |
return true; |
| 169 |
} else if (type == CAnsweringHandlerPreparationEvent::EVENTTYPE) { |
| 170 |
CAnsweringHandlerPreparationEvent* ape = (CAnsweringHandlerPreparationEvent *)event; |
| 171 |
CAnsweringHandler* answeringHandler = ape->getAnsweringHandler(); |
| 172 |
if (answeringHandler) { |
| 173 |
answeringHandler->prepareProcessing(mAnswererContext); |
| 174 |
} |
| 175 |
ape->getCallbackData()->doCallback(); |
| 176 |
return true; |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
CAnsweringMessageDataObserver* CAnswererThread::sendMessage(CAnsweringHandler* handler, CAnsweringMessageData* message, CMemoryPool* memoryPool) { |
| 183 |
postEvent(new CAnsweringMessageEvent(handler, message, memoryPool)); |
| 184 |
return this; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
CAnsweringCalculationHandler* CAnswererThread::postMessage(CAnsweringHandler* handler, CAnsweringMessageData* message) { |
| 190 |
postEvent(new CAnsweringMessageEvent(handler, message, nullptr)); |
| 191 |
return this; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
bool CAnswererThread::continueCalculationCreation() { |
| 196 |
bool createdCalculationJobs = false; |
| 197 |
if (!mQueuedAnsweringHandlerList.isEmpty() && mCurrRunningTestParallelCount < mConfMaxTestParallelCount) { |
| 198 |
CAnsweringHandlerData* handlerData = mQueuedAnsweringHandlerList.first(); |
| 199 |
bool calculationCreated = false; |
| 200 |
bool calculationCreationContinued = true; |
| 201 |
while (calculationCreationContinued && mCurrRunningTestParallelCount < mConfMaxTestParallelCount) { |
| 202 |
calculationCreationContinued = handlerData->mAnsweringHandler->continueCalculationCreation(mAnswererContext); |
| 203 |
calculationCreated |= calculationCreationContinued; |
| 204 |
} |
| 205 |
if (calculationCreated) { |
| 206 |
handlerData->mProcessing = true; |
| 207 |
mProcessingAnsweringHandlerSet.insert(handlerData); |
| 208 |
} else { |
| 209 |
mProcessingAnsweringHandlerSet.remove(handlerData); |
| 210 |
} |
| 211 |
if (!calculationCreationContinued) { |
| 212 |
handlerData->mQueued = false; |
| 213 |
mQueuedAnsweringHandlerList.removeFirst(); |
| 214 |
if (handlerData->mAnsweringHandler->isAnsweringFinished(mAnswererContext)) { |
| 215 |
mHandlerHandlerDataHash.remove(handlerData->mAnsweringHandler); |
| 216 |
if (handlerData->mCallbackData) { |
| 217 |
handlerData->mCallbackData->doCallback(); |
| 218 |
} |
| 219 |
mProcessingAnsweringHandlerSet.remove(handlerData); |
| 220 |
mReadWriteLock.lockForWrite(); |
| 221 |
mManagedAnsweringHandlerSet.remove(handlerData); |
| 222 |
mReadWriteLock.unlock(); |
| 223 |
delete handlerData; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
submitCalculationJobs(); |
| 228 |
return createdCalculationJobs; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
CAnswererThread* CAnswererThread::submitCalculationJobs() { |
| 234 |
if (!mJobCallbackList.isEmpty()) { |
| 235 |
mCalculationManager->calculateJobs(mJobCallbackList); |
| 236 |
mJobCallbackList.clear(); |
| 237 |
} |
| 238 |
return this; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
CAnsweringCalculationHandler* CAnswererThread::ensureRequirements(CAnsweringHandler* handler, CConcreteOntology* ontology, const QList<COntologyProcessingRequirement*>& requirementList, CAnsweringMessageData* finishedMessage) { |
| 244 |
++mStatRequirmentSubmitted; |
| 245 |
++mCurrRunningRequirementParallelCount; |
| 246 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash.value(handler); |
| 247 |
handlerData->mRequirmentProcessingCount++; |
| 248 |
CAnsweringRequirementCompletedEvent* callbackEvent = new CAnsweringRequirementCompletedEvent(this, handler, finishedMessage); |
| 249 |
mReasonerManager->prepareOntology(ontology,requirementList,callbackEvent); |
| 250 |
return this; |
| 251 |
} |
| 252 |
|
| 253 |
CAnsweringCalculationHandler* CAnswererThread::answerSubQuery(CAnsweringHandler* handler, CQuery* query, CAnsweringMessageData* finishedMessage) { |
| 254 |
++mStatRequirmentSubmitted; |
| 255 |
++mCurrRunningRequirementParallelCount; |
| 256 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash.value(handler); |
| 257 |
handlerData->mRequirmentProcessingCount++; |
| 258 |
CAnsweringRequirementCompletedEvent* callbackEvent = new CAnsweringRequirementCompletedEvent(this, handler, finishedMessage); |
| 259 |
mReasonerManager->reasoningQuery(query, callbackEvent); |
| 260 |
return this; |
| 261 |
} |
| 262 |
|
| 263 |
CAnsweringCalculationHandler* CAnswererThread::postCalculation(CAnsweringHandler* handler, CSatisfiableCalculationJob* satCalcJob, CAnsweringMessageData* finishedMessage, CCallbackDataContext* callbackContext) { |
| 264 |
++mStatCalculationJobsSubmitted; |
| 265 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash.value(handler); |
| 266 |
handlerData->mJobProcessingCount++; |
| 267 |
CAnsweringCalculationCompletedEvent* callbackEvent = new CAnsweringCalculationCompletedEvent(this,handler,finishedMessage, callbackContext); |
| 268 |
if (!mCalculationManager) { |
| 269 |
mCalculationManager = mReasonerManager->getCalculationManager(); |
| 270 |
} |
| 271 |
++mCurrRunningTestParallelCount; |
| 272 |
if (!mCalculationManager || mConfDirectSubmission || mCurrRunningTestParallelCount - 1 < mConfMinTestParallelCount / 2) { |
| 273 |
if (mCalculationManager) { |
| 274 |
mCalculationManager->calculateJob(satCalcJob, callbackEvent); |
| 275 |
} else { |
| 276 |
mReasonerManager->reasoningSatisfiableCalcualtionJob(satCalcJob, callbackEvent); |
| 277 |
} |
| 278 |
} else { |
| 279 |
mJobCallbackList.append(QPair<CCalculationJob*, CCallbackData*>(satCalcJob, callbackEvent)); |
| 280 |
} |
| 281 |
return this; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
COccurrenceStatisticsCacheReader* CAnswererThread::getOccurrenceStatisticsCacheReader() { |
| 286 |
if (!mOccStatsCacheReader) { |
| 287 |
COccurrenceStatisticsCache* occStatsCache = mReasonerManager->getOccurrenceStatisticsCache(); |
| 288 |
if (occStatsCache) { |
| 289 |
mOccStatsCacheReader = occStatsCache->createCacheReader(); |
| 290 |
} |
| 291 |
} |
| 292 |
return mOccStatsCacheReader; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
CAnswererThread* CAnswererThread::prepareAnswering(CAnsweringHandler* answeringHandler, CCallbackData* callback) { |
| 297 |
postEvent(new CAnsweringHandlerPreparationEvent(answeringHandler, callback)); |
| 298 |
return this; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
CAnsweringProgress* CAnswererThread::getAnsweringProgress() { |
| 304 |
CAnsweringProgress newAnsweringProg; |
| 305 |
double percentAvg = 0; |
| 306 |
cint64 percentCount = 0; |
| 307 |
mReadWriteLock.lockForRead(); |
| 308 |
|
| 309 |
for (CAnsweringHandlerData* answeringHandlerData : mManagedAnsweringHandlerSet) { |
| 310 |
CAnsweringProgress* answeringProgress = answeringHandlerData->mAnsweringHandler->getAnsweringProgress(); |
| 311 |
if (answeringProgress) { |
| 312 |
newAnsweringProg.setTotalInstances(newAnsweringProg.getTotalInstances() + answeringProgress->getTotalInstances()); |
| 313 |
newAnsweringProg.setTestedInstances(newAnsweringProg.getTestedInstances() + answeringProgress->getTestedInstances()); |
| 314 |
newAnsweringProg.setRemainingMilliSeconds(qMax(newAnsweringProg.getRemainingMilliSeconds(), answeringProgress->getRemainingMilliSeconds())); |
| 315 |
percentAvg += answeringProgress->getProgessPercent(); |
| 316 |
++percentCount; |
| 317 |
} |
| 318 |
} |
| 319 |
mReadWriteLock.unlock(); |
| 320 |
if (percentCount != 0) { |
| 321 |
percentAvg /= (double)percentCount; |
| 322 |
} |
| 323 |
newAnsweringProg.setProgessPercent(percentAvg); |
| 324 |
mAnsweringProgress = newAnsweringProg; |
| 325 |
return &mAnsweringProgress; |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
CAnswererThread* CAnswererThread::addAnsweringStatistics(CAnsweringStatistics* stats) { |
| 331 |
mReadWriteLock.lockForRead(); |
| 332 |
for (CAnsweringHandlerData* answeringHandlerData : mManagedAnsweringHandlerSet) { |
| 333 |
answeringHandlerData->mAnsweringHandler->addAnsweringStatistics(stats, true); |
| 334 |
} |
| 335 |
mReadWriteLock.unlock(); |
| 336 |
return this; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
}; // end namespace Answerer |
| 343 |
|
| 344 |
}; // end namespace Reasoner |
| 345 |
|
| 346 |
}; // end namespace Konclude |