| 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 "CRealizerThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Realizer { |
| 29 |
|
| 30 |
|
| 31 |
CRealizerThread::CRealizerThread(CReasonerManager *reasonerManager) : CThread("Realizer-Thread") { |
| 32 |
mReasoner = reasonerManager; |
| 33 |
mCalculationManager = 0; |
| 34 |
mCurrRunningTestParallelCount = 0; |
| 35 |
mConfMaxTestParallelCount = 1; |
| 36 |
mStatCalculatingJobs = 0; |
| 37 |
|
| 38 |
startThread(QThread::HighPriority); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
CRealizerThread::~CRealizerThread() { |
| 43 |
stopThread(); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
bool CRealizerThread::realize(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList, CCallbackData* callback) { |
| 48 |
postEvent(new CRealizeOntologyEvent(ontology,config,requirementList,callback)); |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
bool CRealizerThread::realize(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList) { |
| 54 |
CBlockingCallbackData callback; |
| 55 |
postEvent(new CRealizeOntologyEvent(ontology,config,requirementList,&callback)); |
| 56 |
callback.waitForCallback(); |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
bool CRealizerThread::callbackRealized(CConcreteOntology* ontology, CCallbackData* callback) { |
| 61 |
bool callbackRealizedOnto = false; |
| 62 |
CRealization* realization = ontology->getRealization(); |
| 63 |
if (realization && realization->isRealized()) { |
| 64 |
callbackRealizedOnto = true; |
| 65 |
if (callback) { |
| 66 |
callback->doCallback(); |
| 67 |
} |
| 68 |
} else { |
| 69 |
CBlockingCallbackData callbackBlock; |
| 70 |
postEvent(new CCallbackRealizedOntologyEvent(ontology,&callbackBlock,callback)); |
| 71 |
callbackBlock.waitForCallback(); |
| 72 |
CCallbackDataContext* callbackContext = callbackBlock.getCallbackDataContext(); |
| 73 |
if (callbackContext) { |
| 74 |
CRealizingCallbackDataContext* possCallbackContext = dynamic_cast<CRealizingCallbackDataContext*>(callbackContext); |
| 75 |
callbackRealizedOnto = possCallbackContext->isRealized(); |
| 76 |
} |
| 77 |
} |
| 78 |
return callbackRealizedOnto; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
bool CRealizerThread::canProcessMoreTests() { |
| 84 |
return mCurrRunningTestParallelCount < mConfMaxTestParallelCount; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
bool CRealizerThread::doNextPendingTests() { |
| 89 |
bool createdNewTests = false; |
| 90 |
bool nextTestCreated = true; |
| 91 |
while (canProcessMoreTests() && nextTestCreated) { |
| 92 |
nextTestCreated = false; |
| 93 |
bool workCreated = createNextTest(); |
| 94 |
if (workCreated) { |
| 95 |
nextTestCreated = true; |
| 96 |
createdNewTests = true; |
| 97 |
} else { |
| 98 |
nextTestCreated = false; |
| 99 |
} |
| 100 |
} |
| 101 |
return createdNewTests; |
| 102 |
} |
| 103 |
|
| 104 |
CRealizerThread* CRealizerThread::processCalculationJob(CSatisfiableCalculationJob* job, COntologyRealizingItem* preCompItem, CRealizingTestingItem* preTestItem) { |
| 105 |
|
| 106 |
CRealizingCalculatedCallbackEvent* callbackEvent = new CRealizingCalculatedCallbackEvent(this,job,preTestItem); |
| 107 |
preCompItem->getRealizingTestingItemSet()->insert(preTestItem); |
| 108 |
|
| 109 |
mCurrRunningTestParallelCount++; |
| 110 |
++mStatCalculatingJobs; |
| 111 |
if (!mCalculationManager) { |
| 112 |
mCalculationManager = mReasoner->getCalculationManager(); |
| 113 |
} |
| 114 |
if (mCalculationManager) { |
| 115 |
mCalculationManager->calculateJob(job,callbackEvent); |
| 116 |
} else { |
| 117 |
mReasoner->reasoningSatisfiableCalcualtionJob(job,callbackEvent); |
| 118 |
} |
| 119 |
return this; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
COntologyRealizingItem* CRealizerThread::initializeOntologyRealizingItem(CConcreteOntology* ontology, CConfigurationBase* config) { |
| 124 |
COntologyRealizingItem* item = new COntologyRealizingItem(); |
| 125 |
item->initRealizingItem(ontology,config); |
| 126 |
return item; |
| 127 |
} |
| 128 |
|
| 129 |
bool CRealizerThread::addOntologyRealizingCallback(COntologyRealizingItem* item, CCallbackData* callback) { |
| 130 |
item->addRealizingFinishedCallback(callback); |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
bool CRealizerThread::addOntologyRealizingRequirements(COntologyRealizingItem* item, COntologyProcessingRequirement* requirement, COntologyRealizingDynamicRequirmentProcessingData* procData) { |
| 136 |
item->addProcessingRequirement(requirement, procData); |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
CRealizationMessageObserver* CRealizerThread::tellRealizationMessage(CConcreteOntology *ontology, CRealizationMessageData* messageData, CMemoryPool* memoryPool) { |
| 145 |
postEvent(new CRealizationMessageEvent(ontology,messageData,memoryPool)); |
| 146 |
return this; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
bool CRealizerThread::processCustomsEvents(QEvent::Type type, CCustomEvent* event) { |
| 152 |
if (CThread::processCustomsEvents(type,event)) { |
| 153 |
return true; |
| 154 |
} else if (type == CRealizationMessageEvent::EVENTTYPE) { |
| 155 |
CRealizationMessageEvent* otcme = (CRealizationMessageEvent *)event; |
| 156 |
CConcreteOntology* ontology = otcme->getOntology(); |
| 157 |
COntologyRealizingItem* ontRealItem = mOntItemHash.value(ontology,0); |
| 158 |
if (ontRealItem) { |
| 159 |
CRealizationMessageData* messageData = otcme->getMessageData(); |
| 160 |
CMemoryPool* memoryPools = otcme->getMemoryPool(); |
| 161 |
processRealizationMessage(ontRealItem,messageData,memoryPools); |
| 162 |
mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools); |
| 163 |
} |
| 164 |
doNextPendingTests(); |
| 165 |
return true; |
| 166 |
} else if (type == CRealizeOntologyEvent::EVENTTYPE) { |
| 167 |
CRealizeOntologyEvent* poe = (CRealizeOntologyEvent*)event; |
| 168 |
|
| 169 |
CCallbackData* callbackData = poe->getCallbackData(); |
| 170 |
CConfigurationBase* configBase = poe->getConfiguration(); |
| 171 |
CConcreteOntology* ontology = poe->getOntology(); |
| 172 |
QList<COntologyProcessingRequirement*>* requirementList = poe->getRealizingRequirementList(); |
| 173 |
|
| 174 |
|
| 175 |
COntologyRealizingItem* item = mOntItemHash.value(ontology); |
| 176 |
if (!item) { |
| 177 |
item = initializeOntologyRealizingItem(ontology,configBase); |
| 178 |
mOntItemHash.insert(ontology,item); |
| 179 |
mActiveOntItemSet.insert(item); |
| 180 |
mProcessingOntItemList.append(item); |
| 181 |
} else { |
| 182 |
if (!mActiveOntItemSet.contains(item)) { |
| 183 |
mInactiveOntItemSet.remove(item); |
| 184 |
mActiveOntItemSet.insert(item); |
| 185 |
mProcessingOntItemList.append(item); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
COntologyRealizingDynamicRequirmentCallbackData* callbackProcData = new COntologyRealizingDynamicRequirmentCallbackData(callbackData); |
| 190 |
callbackProcData->incProcessingRequirmentCount(requirementList->count()); |
| 191 |
for (QList<COntologyProcessingRequirement*>::const_iterator it = requirementList->constBegin(), itEnd = requirementList->constEnd(); it != itEnd; ++it) { |
| 192 |
COntologyProcessingRequirement* requ(*it); |
| 193 |
COntologyRealizingDynamicRequirmentProcessingData* procData = new COntologyRealizingDynamicRequirmentProcessingData(requ, callbackProcData); |
| 194 |
addOntologyRealizingRequirements(item, requ, procData); |
| 195 |
} |
| 196 |
item->logRequirementProcessingStartStatistics(callbackProcData->getStatistics()); |
| 197 |
|
| 198 |
//for (QList<COntologyProcessingRequirement*>::const_iterator it = requirementList->constBegin(), itEnd = requirementList->constEnd(); it != itEnd; ++it) { |
| 199 |
// COntologyProcessingRequirement* requ(*it); |
| 200 |
// addOntologyRealizingRequirements(item,requ, procData); |
| 201 |
//} |
| 202 |
//if (callbackData) { |
| 203 |
// addOntologyRealizingCallback(item, callbackData); |
| 204 |
//} |
| 205 |
doNextPendingTests(); |
| 206 |
return true; |
| 207 |
|
| 208 |
} else if (type == CCallbackRealizedOntologyEvent::EVENTTYPE) { |
| 209 |
CCallbackRealizedOntologyEvent* cpoe = (CCallbackRealizedOntologyEvent*)event; |
| 210 |
|
| 211 |
CConcreteOntology* ontology = cpoe->getOntology(); |
| 212 |
CCallbackData* realizeedCallbackData = cpoe->getRealizedCallbackData(); |
| 213 |
CCallbackData* installedCallbackData = cpoe->getCallbackPossibleCallbackData(); |
| 214 |
COntologyRealizingItem* item = mOntItemHash.value(ontology); |
| 215 |
if (item) { |
| 216 |
addOntologyRealizingCallback(item,realizeedCallbackData); |
| 217 |
if (installedCallbackData) { |
| 218 |
CRealizingCallbackDataContext* callbackDataContext = new CRealizingCallbackDataContext(true,ontology); |
| 219 |
installedCallbackData->setCallbackDataContext(callbackDataContext); |
| 220 |
installedCallbackData->doCallback(); |
| 221 |
} |
| 222 |
} else { |
| 223 |
if (installedCallbackData) { |
| 224 |
CRealizingCallbackDataContext* callbackDataContext = new CRealizingCallbackDataContext(false,ontology); |
| 225 |
installedCallbackData->setCallbackDataContext(callbackDataContext); |
| 226 |
installedCallbackData->doCallback(); |
| 227 |
} |
| 228 |
} |
| 229 |
return true; |
| 230 |
|
| 231 |
} else if (type == CRealizingCalculatedCallbackEvent::EVENTTYPE) { |
| 232 |
CRealizingCalculatedCallbackEvent* pcce = (CRealizingCalculatedCallbackEvent*)event; |
| 233 |
|
| 234 |
--mCurrRunningTestParallelCount; |
| 235 |
|
| 236 |
CRealizingTestingItem* testingItem = pcce->getTestingItem(); |
| 237 |
COntologyRealizingItem* ontPreproItem = testingItem->getOntologyRealizingItem(); |
| 238 |
|
| 239 |
ontPreproItem->getRealizingTestingItemSet()->remove(testingItem); |
| 240 |
realizingTested(ontPreproItem,testingItem,pcce); |
| 241 |
doNextPendingTests(); |
| 242 |
|
| 243 |
return true; |
| 244 |
} |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
}; // end namespace Realizer |
| 251 |
|
| 252 |
}; // end namespace Reasoner |
| 253 |
|
| 254 |
}; // end namespace Konclude |