| 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 "CSubsumptionClassifierThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Classifier { |
| 29 |
|
| 30 |
|
| 31 |
CSubsumptionClassifierThread::CSubsumptionClassifierThread(CReasonerManager *reasonerManager) : CThread("Classifier-Thread") { |
| 32 |
reasoner = reasonerManager; |
| 33 |
calculationManager = 0; |
| 34 |
currRunningTestParallelCount = 0; |
| 35 |
confMaxTestParallelCount = 1; |
| 36 |
confMinTestParallelCount = 15; |
| 37 |
mStatCalculatingJobs = 0; |
| 38 |
mClassifierActive = false; |
| 39 |
statistics = new CClassifierStatistics(); |
| 40 |
startThread(QThread::HighPriority); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
CSubsumptionClassifierThread::~CSubsumptionClassifierThread() { |
| 45 |
stopThread(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
bool CSubsumptionClassifierThread::classify(CConcreteOntology *ontology, CConfigurationBase *config, const QList<COntologyProcessingRequirement*>& requirementList, CCallbackData* callback) { |
| 50 |
postEvent(new CClassifyOntologyEvent(ontology,config,requirementList,callback)); |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
bool CSubsumptionClassifierThread::classify(CConcreteOntology *ontology, CConfigurationBase *config, const QList<COntologyProcessingRequirement*>& requirementList) { |
| 56 |
CBlockingCallbackData callback; |
| 57 |
postEvent(new CClassifyOntologyEvent(ontology,config,requirementList,&callback)); |
| 58 |
callback.waitForCallback(); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
bool CSubsumptionClassifierThread::callbackClassified(CConcreteOntology *ontology, CCallbackData *callback) { |
| 64 |
bool isClassifyOnto = false; |
| 65 |
CTaxonomy *taxonomy = ontology->getConceptTaxonomy(); |
| 66 |
if (taxonomy && taxonomy->isComplete()) { |
| 67 |
isClassifyOnto = true; |
| 68 |
if (callback) { |
| 69 |
callback->doCallback(); |
| 70 |
} |
| 71 |
} else { |
| 72 |
CBlockingCallbackData callbackBlock; |
| 73 |
postEvent(new CCallbackClassifiedOntologyEvent(ontology,&callbackBlock,callback)); |
| 74 |
callbackBlock.waitForCallback(); |
| 75 |
CClassifyingCallbackDataContext *classifyingContext = dynamic_cast<CClassifyingCallbackDataContext *>(callbackBlock.getCallbackDataContext()); |
| 76 |
if (classifyingContext) { |
| 77 |
isClassifyOnto = classifyingContext->isClassifying(); |
| 78 |
} |
| 79 |
} |
| 80 |
return isClassifyOnto; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
CClassificationMessageDataObserver* CSubsumptionClassifierThread::tellClassificationMessage(CConcreteOntology *ontology, CClassificationMessageData* messageData, CMemoryPool* memoryPool) { |
| 85 |
postEvent(new COntologyTellClassificationMessageEvent(ontology,messageData,memoryPool)); |
| 86 |
return this; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
CSubsumptionClassifierThread *CSubsumptionClassifierThread::addOntologyClassifiedCallback(CConcreteOntology *ontology, CCallbackData *callback) { |
| 93 |
COntologyClassificationItem *ontClassItem = ontItemHash.value(ontology,0); |
| 94 |
if (ontClassItem) { |
| 95 |
ontClassItem->addClassifiedCallback(callback); |
| 96 |
} |
| 97 |
return this; |
| 98 |
} |
| 99 |
|
| 100 |
CSubsumptionClassifierThread* CSubsumptionClassifierThread::addOntologyClassifyRequirements(CConcreteOntology *ontology, const QList<COntologyProcessingRequirement*>& requirementList) { |
| 101 |
COntologyClassificationItem *ontClassItem = ontItemHash.value(ontology,nullptr); |
| 102 |
if (ontClassItem) { |
| 103 |
ontClassItem->addOntologyProcessingRequirements(requirementList); |
| 104 |
} |
| 105 |
return this; |
| 106 |
} |
| 107 |
|
| 108 |
CSubsumptionClassifierThread *CSubsumptionClassifierThread::scheduleOntologyClassification(CConcreteOntology *ontology, CClassificationCalculationSupport *classificationSupport, CConfigurationBase *config) { |
| 109 |
COntologyClassificationItem *ontClassItem = new COntologyClassificationItem(config,statistics); |
| 110 |
ontItemList.append(ontClassItem); |
| 111 |
processingOntItemList.append(ontClassItem); |
| 112 |
ontItemHash.insert(ontology,ontClassItem); |
| 113 |
return this; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
CSubsumptionClassifierThread* CSubsumptionClassifierThread::rescheduleOntologyClassification(CConcreteOntology *ontology) { |
| 119 |
COntologyClassificationItem *ontClassItem = ontItemHash.value(ontology); |
| 120 |
processingOntItemList.append(ontClassItem); |
| 121 |
return this; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
bool CSubsumptionClassifierThread::isOntologyClassificationScheduled(CConcreteOntology *ontology) { |
| 126 |
return ontItemHash.contains(ontology); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
bool CSubsumptionClassifierThread::canProcessMoreTests() { |
| 132 |
return currRunningTestParallelCount < confMaxTestParallelCount; |
| 133 |
} |
| 134 |
|
| 135 |
bool CSubsumptionClassifierThread::startProcessMoreTests() { |
| 136 |
return currRunningTestParallelCount <= confMinTestParallelCount; |
| 137 |
} |
| 138 |
|
| 139 |
bool CSubsumptionClassifierThread::doNextPendingTests() { |
| 140 |
bool createdNewTests = false; |
| 141 |
if (startProcessMoreTests()) { |
| 142 |
bool nextTestCreated = true; |
| 143 |
while (canProcessMoreTests() && nextTestCreated) { |
| 144 |
nextTestCreated = false; |
| 145 |
bool workCreated = createNextSubsumtionTest(); |
| 146 |
if (workCreated) { |
| 147 |
nextTestCreated = true; |
| 148 |
createdNewTests = true; |
| 149 |
} else { |
| 150 |
nextTestCreated = false; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
submitCalculationJobs(); |
| 155 |
return createdNewTests; |
| 156 |
} |
| 157 |
|
| 158 |
CSubsumptionClassifierThread* CSubsumptionClassifierThread::submitCalculationJobs() { |
| 159 |
if (!mJobCallbackList.isEmpty()) { |
| 160 |
calculationManager->calculateJobs(mJobCallbackList); |
| 161 |
mJobCallbackList.clear(); |
| 162 |
} |
| 163 |
return this; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
CSubsumptionClassifierThread* CSubsumptionClassifierThread::processCalculationJob(CSatisfiableCalculationJob* job, COntologyClassificationItem *ontClassItem, CCallbackData* callback, bool directlySubmit) { |
| 168 |
currRunningTestParallelCount++; |
| 169 |
++mStatCalculatingJobs; |
| 170 |
if (!calculationManager) { |
| 171 |
calculationManager = reasoner->getCalculationManager(); |
| 172 |
} |
| 173 |
if (!calculationManager || directlySubmit || currRunningTestParallelCount-1 < confMinTestParallelCount/2) { |
| 174 |
if (calculationManager) { |
| 175 |
calculationManager->calculateJob(job,callback); |
| 176 |
} else { |
| 177 |
reasoner->reasoningSatisfiableCalcualtionJob(job,callback); |
| 178 |
} |
| 179 |
} else { |
| 180 |
mJobCallbackList.append(QPair<CCalculationJob*,CCallbackData*>(job,callback)); |
| 181 |
} |
| 182 |
return this; |
| 183 |
} |
| 184 |
|
| 185 |
bool CSubsumptionClassifierThread::processToldClassificationMessage(COntologyClassificationItem *ontClassItem, CClassificationMessageData* messageData, CMemoryPool* memoryPools) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
bool CSubsumptionClassifierThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 191 |
if (CThread::processCustomsEvents(type,event)) { |
| 192 |
return true; |
| 193 |
} else if (type == CClassifyOntologyEvent::EVENTTYPE) { |
| 194 |
CClassifyOntologyEvent *coe = (CClassifyOntologyEvent *)event; |
| 195 |
CConcreteOntology* onto = coe->getOntology(); |
| 196 |
CCallbackData *callback = coe->getCallbackData(); |
| 197 |
CConfigurationBase* config = coe->getConfiguration(); |
| 198 |
QList<COntologyProcessingRequirement*>* reqList = coe->getClassificationRequirementList(); |
| 199 |
if (!isOntologyClassificationScheduled(onto)) { |
| 200 |
scheduleOntologyClassification(onto,nullptr,config); |
| 201 |
addOntologyClassifyRequirements(onto,*reqList); |
| 202 |
} else { |
| 203 |
rescheduleOntologyClassification(onto); |
| 204 |
} |
| 205 |
|
| 206 |
if (reqList) { |
| 207 |
addOntologyClassifyRequirements(onto,*reqList); |
| 208 |
} |
| 209 |
if (callback) { |
| 210 |
addOntologyClassifiedCallback(onto,callback); |
| 211 |
} |
| 212 |
doNextPendingTests(); |
| 213 |
return true; |
| 214 |
} else if (type == COntologyTellClassificationMessageEvent::EVENTTYPE) { |
| 215 |
COntologyTellClassificationMessageEvent *otcme = (COntologyTellClassificationMessageEvent *)event; |
| 216 |
CConcreteOntology *ontology = otcme->getOntology(); |
| 217 |
COntologyClassificationItem *ontClassItem = ontItemHash.value(ontology,0); |
| 218 |
if (ontClassItem) { |
| 219 |
CClassificationMessageData* messageData = otcme->getMessageData(); |
| 220 |
CMemoryPool* memoryPools = otcme->getMemoryPool(); |
| 221 |
processToldClassificationMessage(ontClassItem,messageData,memoryPools); |
| 222 |
mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools); |
| 223 |
} |
| 224 |
doNextPendingTests(); |
| 225 |
return true; |
| 226 |
} else if (type == CTestCalculatedCallbackEvent::EVENTTYPE) { |
| 227 |
CTestCalculatedCallbackEvent *tcue = (CTestCalculatedCallbackEvent *)event; |
| 228 |
interpreteTestResults(tcue); |
| 229 |
currRunningTestParallelCount--; |
| 230 |
doNextPendingTests(); |
| 231 |
return true; |
| 232 |
} else if (type == CCallbackClassifiedOntologyEvent::EVENTTYPE) { |
| 233 |
CCallbackClassifiedOntologyEvent *ccoe = (CCallbackClassifiedOntologyEvent *)event; |
| 234 |
CCallbackData *possCallback = ccoe->getCallbackPossibleCallbackData(); |
| 235 |
COntologyClassificationItem *ontClassItem = ontItemHash.value(ccoe->getOntology(),0); |
| 236 |
if (possCallback) { |
| 237 |
CClassifyingCallbackDataContext *possCallbackContext = new CClassifyingCallbackDataContext(ontClassItem,ccoe->getOntology()); |
| 238 |
possCallback->setCallbackDataContext(possCallbackContext); |
| 239 |
possCallback->doCallback(); |
| 240 |
} |
| 241 |
CCallbackData *classifiedCallback = ccoe->getClassifiedCallbackData(); |
| 242 |
if (classifiedCallback) { |
| 243 |
addOntologyClassifiedCallback(ccoe->getOntology(),classifiedCallback); |
| 244 |
rescheduleOntologyClassification(ccoe->getOntology()); |
| 245 |
} |
| 246 |
return true; |
| 247 |
} |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
CClassifierStatistics *CSubsumptionClassifierThread::getClassificationStatistics() { |
| 253 |
return statistics; |
| 254 |
} |
| 255 |
|
| 256 |
CClassificationProgress* CSubsumptionClassifierThread::getClassificationProgress() { |
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
bool CSubsumptionClassifierThread::isClassifierActive() { |
| 262 |
return isThreadProcessingEvents(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
}; // end namespace Classifier |
| 269 |
|
| 270 |
}; // end namespace Reasoner |
| 271 |
|
| 272 |
}; // end namespace Konclude |