Debian Codegraph @

Source/Reasoner/Preprocess/CPreprocessingThread.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 "CPreprocessingThread.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Preprocess {
29
30
31 CPreprocessingThread::CPreprocessingThread(CReasonerManager *reasonerManager) : CThread("Preprocessing-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 CPreprocessingThread::~CPreprocessingThread() {
43 stopThread();
44 }
45
46
47 bool CPreprocessingThread::preprocess(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList, CCallbackData* callback) {
48 postEvent(new CPreprocessOntologyEvent(ontology,config,requirementList,callback));
49 return true;
50 }
51
52
53 bool CPreprocessingThread::preprocess(CConcreteOntology* ontology, CConfigurationBase* config, const QList<COntologyProcessingRequirement*>& requirementList) {
54 CBlockingCallbackData callback;
55 postEvent(new CPreprocessOntologyEvent(ontology,config,requirementList,&callback));
56 callback.waitForCallback();
57 return true;
58 }
59
60 bool CPreprocessingThread::callbackPreprocessed(CConcreteOntology* ontology, CCallbackData* callback) {
61 bool callbackPreprocessedOnto = false;
62 CPreprocessing* preprocessing = ontology->getPreprocessing();
63 if (preprocessing && preprocessing->isPreprocessed()) {
64 callbackPreprocessedOnto = true;
65 if (callback) {
66 callback->doCallback();
67 }
68 } else {
69 CBlockingCallbackData callbackBlock;
70 postEvent(new CCallbackPreprocessedOntologyEvent(ontology,&callbackBlock,callback));
71 callbackBlock.waitForCallback();
72 CCallbackDataContext* callbackContext = callbackBlock.getCallbackDataContext();
73 if (callbackContext) {
74 CPreprocessingCallbackDataContext* possCallbackContext = dynamic_cast<CPreprocessingCallbackDataContext*>(callbackContext);
75 callbackPreprocessedOnto = possCallbackContext->isPreprocessed();
76 }
77 }
78 return callbackPreprocessedOnto;
79 }
80
81
82
83 bool CPreprocessingThread::canProcessMoreTests() {
84 return mCurrRunningTestParallelCount < mConfMaxTestParallelCount;
85 }
86
87
88 bool CPreprocessingThread::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 CPreprocessingThread* CPreprocessingThread::processCalculationJob(CSatisfiableCalculationJob* job, COntologyPreprocessingItem* preCompItem, CPreprocessingTestingItem* preTestItem) {
105
106 CPreprocessingCalculatedCallbackEvent* callbackEvent = new CPreprocessingCalculatedCallbackEvent(this,job,preTestItem);
107 preCompItem->getPreprocessingTestingItemSet()->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 COntologyPreprocessingItem* CPreprocessingThread::initializeOntologyPreprocessingItem(CConcreteOntology* ontology, CConfigurationBase* config) {
124 COntologyPreprocessingItem* item = new COntologyPreprocessingItem();
125 item->initPreprocessingItem(ontology,config);
126 return item;
127 }
128
129 bool CPreprocessingThread::addOntologyPreprocessingCallback(COntologyPreprocessingItem* item, CCallbackData* callback) {
130 item->addPreprocessingFinishedCallback(callback);
131 return true;
132 }
133
134
135 bool CPreprocessingThread::addOntologyPreprocessingRequirements(COntologyPreprocessingItem* item, COntologyProcessingRequirement* requirement) {
136 item->addProcessingRequirement(requirement);
137 return true;
138 }
139
140
141 bool CPreprocessingThread::processCustomsEvents(QEvent::Type type, CCustomEvent* event) {
142 if (CThread::processCustomsEvents(type,event)) {
143 return true;
144 } else if (type == CPreprocessOntologyEvent::EVENTTYPE) {
145 CPreprocessOntologyEvent* poe = (CPreprocessOntologyEvent*)event;
146
147 CCallbackData* callbackData = poe->getCallbackData();
148 CConfigurationBase* configBase = poe->getConfiguration();
149 CConcreteOntology* ontology = poe->getOntology();
150 QList<COntologyProcessingRequirement*>* requirementList = poe->getPreprocessingRequirementList();
151
152
153 COntologyPreprocessingItem* item = mOntItemHash.value(ontology);
154 if (!item) {
155 item = initializeOntologyPreprocessingItem(ontology,configBase);
156 mOntItemHash.insert(ontology,item);
157 mActiveOntItemSet.insert(item);
158 mProcessingOntItemList.append(item);
159 } else {
160 if (!mActiveOntItemSet.contains(item)) {
161 mInactiveOntItemSet.remove(item);
162 mActiveOntItemSet.insert(item);
163 mProcessingOntItemList.append(item);
164 }
165 }
166
167 for (QList<COntologyProcessingRequirement*>::const_iterator it = requirementList->constBegin(), itEnd = requirementList->constEnd(); it != itEnd; ++it) {
168 COntologyProcessingRequirement* requ(*it);
169 addOntologyPreprocessingRequirements(item,requ);
170 }
171
172 if (callbackData) {
173 addOntologyPreprocessingCallback(item,callbackData);
174 }
175 doNextPendingTests();
176 return true;
177
178 } else if (type == CCallbackPreprocessedOntologyEvent::EVENTTYPE) {
179 CCallbackPreprocessedOntologyEvent* cpoe = (CCallbackPreprocessedOntologyEvent*)event;
180
181 CConcreteOntology* ontology = cpoe->getOntology();
182 CCallbackData* preprocessedCallbackData = cpoe->getPreprocessedCallbackData();
183 CCallbackData* installedCallbackData = cpoe->getCallbackPossibleCallbackData();
184 COntologyPreprocessingItem* item = mOntItemHash.value(ontology);
185 if (item) {
186 addOntologyPreprocessingCallback(item,preprocessedCallbackData);
187 if (installedCallbackData) {
188 CPreprocessingCallbackDataContext* callbackDataContext = new CPreprocessingCallbackDataContext(true,ontology);
189 installedCallbackData->setCallbackDataContext(callbackDataContext);
190 installedCallbackData->doCallback();
191 }
192 } else {
193 if (installedCallbackData) {
194 CPreprocessingCallbackDataContext* callbackDataContext = new CPreprocessingCallbackDataContext(false,ontology);
195 installedCallbackData->setCallbackDataContext(callbackDataContext);
196 installedCallbackData->doCallback();
197 }
198 }
199 return true;
200
201 } else if (type == CPreprocessingCalculatedCallbackEvent::EVENTTYPE) {
202 CPreprocessingCalculatedCallbackEvent* pcce = (CPreprocessingCalculatedCallbackEvent*)event;
203
204 --mCurrRunningTestParallelCount;
205
206 CPreprocessingTestingItem* testingItem = pcce->getTestingItem();
207 COntologyPreprocessingItem* ontPreproItem = testingItem->getOntologyPreprocessingItem();
208
209 ontPreproItem->getPreprocessingTestingItemSet()->remove(testingItem);
210 preprocessingTested(ontPreproItem,testingItem,pcce);
211 doNextPendingTests();
212
213 return true;
214 }
215 return false;
216 }
217
218
219
220 }; // end namespace Preprocess
221
222 }; // end namespace Reasoner
223
224 }; // end namespace Konclude