| 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 "CAnsweringManagerThread.h" |
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Reasoner { |
| 26 |
|
| 27 |
namespace Answerer { |
| 28 |
|
| 29 |
|
| 30 |
CAnsweringManagerThread::CAnsweringManagerThread(CReasonerManager* reasonerManager, CConfiguration* config) : CThread("Answerer-Manager-Thread"), mHandlerProvider(new CConfigurationAnsweringHandlerFactory(config)) { |
| 31 |
mConfig = config; |
| 32 |
mConfMaxThreadCount = 2; |
| 33 |
mCurrentThreadCount = 0; |
| 34 |
mCompositionQueryProcessingCount = 0; |
| 35 |
mMaxCompositionQueryProcessingCount = 1; |
| 36 |
mReasonerManager = reasonerManager; |
| 37 |
|
| 38 |
if (mQueuedThreadList.isEmpty() && mCurrentThreadCount < mConfMaxThreadCount) { |
| 39 |
CAnswererThread* answererThread = new CAnswererThread(mReasonerManager); |
| 40 |
answererThread->initializeAnswerer(mConfig); |
| 41 |
mQueuedThreadList.append(answererThread); |
| 42 |
mAnswererThreadContainer.append(answererThread); |
| 43 |
++mCurrentThreadCount; |
| 44 |
} |
| 45 |
startThread(QThread::HighPriority); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
CAnsweringManagerThread::~CAnsweringManagerThread() { |
| 50 |
stopThread(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
CAnsweringManager* CAnsweringManagerThread::answerComplexQuery(CComplexAnsweringQuery* complexAnsweringQuery, CCallbackData* callback) { |
| 55 |
postEvent(new CAnswerComplexQueryEvent(complexAnsweringQuery, callback)); |
| 56 |
return this; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
CAnsweringProgress* CAnsweringManagerThread::getAnsweringProgress() { |
| 61 |
CAnsweringProgress newAnsweringProg; |
| 62 |
double percentAvg = 0; |
| 63 |
cint64 percentCount = 0; |
| 64 |
mReadWriteLock.lockForRead(); |
| 65 |
|
| 66 |
for (CAnswererThread* answererThread : mAnswererThreadContainer) { |
| 67 |
CAnsweringProgress* answeringProgress = answererThread->getAnsweringProgress(); |
| 68 |
if (answeringProgress) { |
| 69 |
newAnsweringProg.setTotalInstances(newAnsweringProg.getTotalInstances() + answeringProgress->getTotalInstances()); |
| 70 |
newAnsweringProg.setTestedInstances(newAnsweringProg.getTestedInstances() + answeringProgress->getTestedInstances()); |
| 71 |
newAnsweringProg.setRemainingMilliSeconds(qMax(newAnsweringProg.getRemainingMilliSeconds(), answeringProgress->getRemainingMilliSeconds())); |
| 72 |
percentAvg += answeringProgress->getProgessPercent(); |
| 73 |
++percentCount; |
| 74 |
} |
| 75 |
} |
| 76 |
mReadWriteLock.unlock(); |
| 77 |
if (percentCount != 0) { |
| 78 |
percentAvg /= (double)percentCount; |
| 79 |
} |
| 80 |
newAnsweringProg.setProgessPercent(percentAvg); |
| 81 |
mAnsweringProgress = newAnsweringProg; |
| 82 |
return &mAnsweringProgress; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
CAnsweringStatistics* CAnsweringManagerThread::getAnsweringStatistics(CConcreteOntology* ontology) { |
| 89 |
CAnsweringStatisticsCollectionStrings* stats = new CAnsweringStatisticsCollectionStrings(); |
| 90 |
mHandlerProvider.addAnsweringStatistics(ontology, stats); |
| 91 |
return stats; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
CAnsweringManager* CAnsweringManagerThread::finishComplexQueryAnswering(CAnsweringHandler* answeringHandler, CAnsweringManagerQueryData* queryData) { |
| 97 |
mHandlerProvider.releaseAnsweringHandler(answeringHandler); |
| 98 |
if (queryData) { |
| 99 |
if (queryData->isCompositionQuery()) { |
| 100 |
--mCompositionQueryProcessingCount; |
| 101 |
} |
| 102 |
mProcessingQuerySet.remove(queryData); |
| 103 |
CAnswererThread* thread1 = queryData->getThread(); |
| 104 |
mProcessingThreadSet.remove(thread1); |
| 105 |
mQueuedThreadList.append(thread1); |
| 106 |
CCallbackData* callback = queryData->getCallback(); |
| 107 |
if (callback) { |
| 108 |
callback->doCallback(); |
| 109 |
} |
| 110 |
delete queryData; |
| 111 |
} |
| 112 |
return this; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
CAnsweringManager* CAnsweringManagerThread::queueComplexQueryAnswering(CComplexAnsweringQuery* complexAnsweringQuery, CCallbackData* callback) { |
| 118 |
bool compositionQuery = dynamic_cast<CComplexAnsweringCompositionQuery*>(complexAnsweringQuery); |
| 119 |
CAnsweringManagerQueryData* queryData = new CAnsweringManagerQueryData(complexAnsweringQuery, callback, compositionQuery); |
| 120 |
if (compositionQuery) { |
| 121 |
mQueuedCompositionQueryList.append(queryData); |
| 122 |
} else { |
| 123 |
mQueuedExpressionQueryList.append(queryData); |
| 124 |
} |
| 125 |
return this; |
| 126 |
} |
| 127 |
|
| 128 |
CAnsweringManager* CAnsweringManagerThread::manageComplexQueryAnswering() { |
| 129 |
if (!mQueuedExpressionQueryList.isEmpty() || !mQueuedCompositionQueryList.isEmpty()) { |
| 130 |
createQueuedAnswererThread(); |
| 131 |
if (!mQueuedThreadList.isEmpty()) { |
| 132 |
CAnsweringManagerQueryData* queryData = nullptr; |
| 133 |
if (!mQueuedExpressionQueryList.isEmpty()) { |
| 134 |
queryData = mQueuedExpressionQueryList.takeFirst(); |
| 135 |
} else if (!mQueuedCompositionQueryList.isEmpty() && mCompositionQueryProcessingCount < mMaxCompositionQueryProcessingCount) { |
| 136 |
queryData = mQueuedCompositionQueryList.takeFirst(); |
| 137 |
++mCompositionQueryProcessingCount; |
| 138 |
} |
| 139 |
if (queryData) { |
| 140 |
CComplexAnsweringQuery* complexAnsweringQuery = queryData->getQuery(); |
| 141 |
CAnsweringHandler* handler = mHandlerProvider.getAnsweringHandler(complexAnsweringQuery); |
| 142 |
CAnswererThread* thread = mQueuedThreadList.takeFirst(); |
| 143 |
queryData->setThread(thread); |
| 144 |
mProcessingThreadSet.insert(thread); |
| 145 |
mProcessingQuerySet.insert(queryData); |
| 146 |
CAnsweringComplexQueryCompletedEvent* completedEventCallback = new CAnsweringComplexQueryCompletedEvent(this, handler, queryData); |
| 147 |
thread->handleAnswering(handler, complexAnsweringQuery, completedEventCallback); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
return this; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
CAnsweringManager* CAnsweringManagerThread::prepareAnswering(CConcreteOntology* ontology, CCallbackData* callback) { |
| 156 |
postEvent(new CAnsweringOntologyPreparationEvent(ontology, callback)); |
| 157 |
return this; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
bool CAnsweringManagerThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 162 |
if (CThread::processCustomsEvents(type, event)) { |
| 163 |
return true; |
| 164 |
} else if (type == CAnsweringOntologyPreparationEvent::EVENTTYPE) { |
| 165 |
CAnsweringOntologyPreparationEvent* ape = (CAnsweringOntologyPreparationEvent *)event; |
| 166 |
createQueuedAnswererThread(); |
| 167 |
|
| 168 |
if (!mQueuedThreadList.isEmpty()) { |
| 169 |
CConcreteOntology* ontology = ape->getOntology(); |
| 170 |
CAnsweringHandler* expressionHandler = mHandlerProvider.getAnsweringHandler(ontology, false); |
| 171 |
CAnswererThread* thread1 = mQueuedThreadList.takeFirst(); |
| 172 |
CAnsweringPreparedEvent* preparedEventCallback = new CAnsweringPreparedEvent(thread1, expressionHandler, this, ape->getCallbackData()); |
| 173 |
thread1->prepareAnswering(expressionHandler, preparedEventCallback); |
| 174 |
mProcessingThreadSet.insert(thread1); |
| 175 |
} else { |
| 176 |
ape->getCallbackData()->doCallback(); |
| 177 |
} |
| 178 |
createQueuedAnswererThread(); |
| 179 |
return true; |
| 180 |
} else if (type == CAnswerComplexQueryEvent::EVENTTYPE) { |
| 181 |
CAnswerComplexQueryEvent* acqe = (CAnswerComplexQueryEvent *)event; |
| 182 |
queueComplexQueryAnswering(acqe->getComplexQuery(), acqe->getCallbackData()); |
| 183 |
manageComplexQueryAnswering(); |
| 184 |
return true; |
| 185 |
} else if (type == CAnsweringComplexQueryCompletedEvent::EVENTTYPE) { |
| 186 |
CAnsweringComplexQueryCompletedEvent* acqce = (CAnsweringComplexQueryCompletedEvent *)event; |
| 187 |
finishComplexQueryAnswering(acqce->getAnsweringHandler(), acqce->getQueryData()); |
| 188 |
manageComplexQueryAnswering(); |
| 189 |
return true; |
| 190 |
} else if (type == CAnsweringPreparedEvent::EVENTTYPE) { |
| 191 |
CAnsweringPreparedEvent* ape = (CAnsweringPreparedEvent *)event; |
| 192 |
CAnswererThread* thread = ape->getAnswererThread(); |
| 193 |
mProcessingThreadSet.remove(thread); |
| 194 |
mQueuedThreadList.append(thread); |
| 195 |
mHandlerProvider.releaseAnsweringHandler(ape->getAnsweringHandler()); |
| 196 |
ape->getPreparedCallback()->doCallback(); |
| 197 |
manageComplexQueryAnswering(); |
| 198 |
return true; |
| 199 |
} |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
void CAnsweringManagerThread::createQueuedAnswererThread() { |
| 204 |
if (mQueuedThreadList.isEmpty() && mCurrentThreadCount < mConfMaxThreadCount) { |
| 205 |
CAnswererThread* answererThread = new CAnswererThread(mReasonerManager); |
| 206 |
answererThread->initializeAnswerer(mConfig); |
| 207 |
mQueuedThreadList.append(answererThread); |
| 208 |
mReadWriteLock.lockForWrite(); |
| 209 |
mAnswererThreadContainer.append(answererThread); |
| 210 |
mReadWriteLock.unlock(); |
| 211 |
++mCurrentThreadCount; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
}; // end namespace Answerer |
| 216 |
|
| 217 |
}; // end namespace Reasoner |
| 218 |
|
| 219 |
}; // end namespace Konclude |