Debian Codegraph @

Source/Reasoner/Consistiser/CPrecomputationThread.cpp

raw
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 "CPrecomputationThread.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Consistiser {
29
30
31 CPrecomputationThread::CPrecomputationThread(CReasonerManager *reasonerManager) : CThread("Precomputation-Thread") {
32 mReasoner = reasonerManager;
33 mCalculationManager = 0;
34 mCurrRunningTestParallelCount = 0;
35 mConfMaxTestParallelCount = 1;
36 mStatCalculatingJobs = 0;
37 mConfMaxTestBatchCreationCount = -1;
38
39 startThread(QThread::HighPriority);
40 }
41
42
43 CPrecomputationThread::~CPrecomputationThread() {
44 stopThread();
45 }
46
47
48 bool CPrecomputationThread::precompute(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList, CCallbackData* callback) {
49 postEvent(new CPrecomputeOntologyEvent(ontology,config,requirementList,callback));
50 return true;
51 }
52
53
54 bool CPrecomputationThread::precompute(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList) {
55 CBlockingCallbackData callback;
56 postEvent(new CPrecomputeOntologyEvent(ontology,config,requirementList,&callback));
57 callback.waitForCallback();
58 return true;
59 }
60
61 bool CPrecomputationThread::callbackPrecomputed(CConcreteOntology* ontology, CCallbackData* callback) {
62 bool callbackPecomputedOnto = false;
63 CPrecomputation* precomputation = ontology->getPrecomputation();
64 if (precomputation && precomputation->isPrecomputed()) {
65 callbackPecomputedOnto = true;
66 if (callback) {
67 callback->doCallback();
68 }
69 } else {
70 CBlockingCallbackData callbackBlock;
71 postEvent(new CCallbackPrecomputedOntologyEvent(ontology,&callbackBlock,callback));
72 callbackBlock.waitForCallback();
73 CCallbackDataContext* callbackContext = callbackBlock.getCallbackDataContext();
74 if (callbackContext) {
75 CPrecomputationCallbackDataContext* possCallbackContext = dynamic_cast<CPrecomputationCallbackDataContext*>(callbackContext);
76 callbackPecomputedOnto = possCallbackContext->isPrecomputing();
77 }
78 }
79 return callbackPecomputedOnto;
80 }
81
82
83
84 bool CPrecomputationThread::canProcessMoreTests() {
85 return mCurrRunningTestParallelCount < mConfMaxTestParallelCount;
86 }
87
88
89 bool CPrecomputationThread::doNextPendingTests() {
90 bool createdNewTests = false;
91 bool nextTestCreated = true;
92 cint64 jobCreationCount = 0;
93 while (canProcessMoreTests() && nextTestCreated) {
94 nextTestCreated = false;
95 bool workCreated = createNextTest();
96 if (workCreated) {
97 ++jobCreationCount;
98 nextTestCreated = true;
99 if (mConfMaxTestBatchCreationCount == -1 || jobCreationCount <= mConfMaxTestBatchCreationCount) {
100 createdNewTests = true;
101 } else {
102 createdNewTests = false;
103 postEvent(new CRescheduleJobCreationEvent());
104 }
105 } else {
106 nextTestCreated = false;
107 }
108 }
109 return createdNewTests;
110 }
111
112 CPrecomputationThread* CPrecomputationThread::processCalculationJob(CSatisfiableCalculationJob* job, COntologyPrecomputationItem* preCompItem, CPrecomputationTestingItem* preTestItem) {
113
114 CConsistenceCalculationStatisticsCollection* statColl = nullptr;
115 if (preCompItem->isProcessStatisticsCollectionConfigured()) {
116 statColl = preCompItem->getCalculationStatisticsCollection();
117 preTestItem->setStatisticCollection(statColl);
118 }
119 job->setCalclulationStatisticsCollector(statColl);
120
121 CPrecomputationCalculatedCallbackEvent* callbackEvent = new CPrecomputationCalculatedCallbackEvent(this,job,preTestItem);
122 preCompItem->getPrecomputationTestingItemSet()->insert(preTestItem);
123
124 mCurrRunningTestParallelCount++;
125 ++mStatCalculatingJobs;
126 if (!mCalculationManager) {
127 mCalculationManager = mReasoner->getCalculationManager();
128 }
129 if (mCalculationManager) {
130 mCalculationManager->calculateJob(job,callbackEvent);
131 } else {
132 mReasoner->reasoningSatisfiableCalcualtionJob(job,callbackEvent);
133 }
134 return this;
135 }
136
137
138 CPrecomputationThread* CPrecomputationThread::processCalculationJob(CApproximatedSaturationCalculationJob* job, COntologyPrecomputationItem* preCompItem, CPrecomputationTestingItem* preTestItem) {
139
140 CConsistenceCalculationStatisticsCollection* statColl = nullptr;
141 if (preCompItem->isProcessStatisticsCollectionConfigured()) {
142 statColl = preCompItem->getCalculationStatisticsCollection();
143 preTestItem->setStatisticCollection(statColl);
144 }
145 job->setCalclulationStatisticsCollector(statColl);
146
147 CSaturationPrecomputationCalculatedCallbackEvent* callbackEvent = new CSaturationPrecomputationCalculatedCallbackEvent(this,job,preTestItem);
148 preCompItem->getPrecomputationTestingItemSet()->insert(preTestItem);
149
150 mCurrRunningTestParallelCount++;
151 ++mStatCalculatingJobs;
152 if (!mCalculationManager) {
153 mCalculationManager = mReasoner->getCalculationManager();
154 }
155 if (mCalculationManager) {
156 mCalculationManager->calculateJob(job,callbackEvent);
157 }
158 return this;
159 }
160
161
162 COntologyPrecomputationItem* CPrecomputationThread::initializeOntologyPrecomputionItem(CConcreteOntology* ontology, CConfigurationBase* config) {
163 COntologyPrecomputationItem* item = new COntologyPrecomputationItem();
164 item->initPrecomputationItem(ontology,config);
165 return item;
166 }
167
168 bool CPrecomputationThread::addOntologyPrecomputionCallback(COntologyPrecomputationItem* item, CCallbackData* callback) {
169 item->addPrecomputationFinishedCallback(callback);
170 return this;
171 }
172
173 bool CPrecomputationThread::addOntologyPrecomputionRequirements(COntologyPrecomputationItem* item, COntologyProcessingRequirement* requirement) {
174 item->addPrecomputationRequirement(requirement);
175 return true;
176 }
177
178 bool CPrecomputationThread::processCustomsEvents(QEvent::Type type, CCustomEvent* event) {
179 if (CThread::processCustomsEvents(type,event)) {
180 return true;
181 } else if (type == CPrecomputeOntologyEvent::EVENTTYPE) {
182 CPrecomputeOntologyEvent* poe = (CPrecomputeOntologyEvent*)event;
183
184 CCallbackData* callbackData = poe->getCallbackData();
185 CConfigurationBase* configBase = poe->getConfiguration();
186 CConcreteOntology* ontology = poe->getOntology();
187 QList<COntologyProcessingRequirement*>* requirementList = poe->getProcessingRequirementList();
188
189
190 COntologyPrecomputationItem* item = mOntItemHash.value(ontology);
191 if (!item) {
192 item = initializeOntologyPrecomputionItem(ontology,configBase);
193 mOntItemHash.insert(ontology,item);
194 mActiveOntItemSet.insert(item);
195 mProcessingOntItemList.append(item);
196 } else {
197 if (!mActiveOntItemSet.contains(item)) {
198 mInactiveOntItemSet.remove(item);
199 mActiveOntItemSet.insert(item);
200 mProcessingOntItemList.append(item);
201 }
202 }
203
204 for (QList<COntologyProcessingRequirement*>::const_iterator it = requirementList->constBegin(), itEnd = requirementList->constEnd(); it != itEnd; ++it) {
205 COntologyProcessingRequirement* requ(*it);
206 addOntologyPrecomputionRequirements(item,requ);
207 }
208
209 if (callbackData) {
210 addOntologyPrecomputionCallback(item,callbackData);
211 }
212 doNextPendingTests();
213 return true;
214
215 } else if (type == CCallbackPrecomputedOntologyEvent::EVENTTYPE) {
216 CCallbackPrecomputedOntologyEvent* cpoe = (CCallbackPrecomputedOntologyEvent*)event;
217
218 CConcreteOntology* ontology = cpoe->getOntology();
219 CCallbackData* precomputedCallbackData = cpoe->getPrecomputedCallbackData();
220 CCallbackData* installedCallbackData = cpoe->getCallbackPossibleCallbackData();
221 COntologyPrecomputationItem* item = mOntItemHash.value(ontology);
222 if (item) {
223 addOntologyPrecomputionCallback(item,precomputedCallbackData);
224 if (installedCallbackData) {
225 CPrecomputationCallbackDataContext* callbackDataContext = new CPrecomputationCallbackDataContext(true,ontology);
226 installedCallbackData->setCallbackDataContext(callbackDataContext);
227 installedCallbackData->doCallback();
228 }
229 } else {
230 if (installedCallbackData) {
231 CPrecomputationCallbackDataContext* callbackDataContext = new CPrecomputationCallbackDataContext(false,ontology);
232 installedCallbackData->setCallbackDataContext(callbackDataContext);
233 installedCallbackData->doCallback();
234 }
235 }
236 return true;
237
238 } else if (type == CRetrievedPrecomputationIndividualsCallbackEvent::EVENTTYPE) {
239 CRetrievedPrecomputationIndividualsCallbackEvent* cpoe = (CRetrievedPrecomputationIndividualsCallbackEvent*)event;
240
241 precomputationIndividualsRetrieved(cpoe->getOntologyPrecomputationItem(), cpoe);
242 return true;
243
244 } else if (type == CPrecomputationCalculatedCallbackEvent::EVENTTYPE) {
245 CPrecomputationCalculatedCallbackEvent* pcce = (CPrecomputationCalculatedCallbackEvent*)event;
246
247 --mCurrRunningTestParallelCount;
248
249 CPrecomputationTestingItem* testingItem = pcce->getTestingItem();
250 COntologyPrecomputationItem* ontPreCompItem = testingItem->getOntologyPrecomputationItem();
251
252 ontPreCompItem->getPrecomputationTestingItemSet()->remove(testingItem);
253 CConsistenceCalculationStatisticsCollection* statisticCollection = testingItem->getUsedStatisticsCollection();
254 precomputationTested(ontPreCompItem, testingItem, pcce);
255
256 if (statisticCollection) {
257 ontPreCompItem->reuseCalculationStatisticsCollection(statisticCollection);
258 }
259
260 doNextPendingTests();
261
262 return true;
263
264 } else if (type == CSaturationPrecomputationCalculatedCallbackEvent::EVENTTYPE) {
265 CSaturationPrecomputationCalculatedCallbackEvent* spcce = (CSaturationPrecomputationCalculatedCallbackEvent*)event;
266
267 --mCurrRunningTestParallelCount;
268
269 CPrecomputationTestingItem* testingItem = spcce->getPrecomputationTestingItem();
270 COntologyPrecomputationItem* ontPreCompItem = testingItem->getOntologyPrecomputationItem();
271
272 ontPreCompItem->getPrecomputationTestingItemSet()->remove(testingItem);
273
274 CConsistenceCalculationStatisticsCollection* statisticCollection = testingItem->getUsedStatisticsCollection();
275
276 precomputationTested(ontPreCompItem,testingItem,spcce);
277
278 if (statisticCollection) {
279 ontPreCompItem->reuseCalculationStatisticsCollection(statisticCollection);
280 }
281
282
283 doNextPendingTests();
284
285 return true;
286 } else if (type == CRescheduleJobCreationEvent::EVENTTYPE) {
287 doNextPendingTests();
288 return true;
289 }
290 return false;
291 }
292
293
294
295 }; // end namespace Consistiser
296
297 }; // end namespace Reasoner
298
299 }; // end namespace Konclude