Debian Codegraph @

Source/Control/Loader/COREBatchProcessingLoader.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 "COREBatchProcessingLoader.h"
22
23
24 namespace Konclude {
25
26 namespace Control {
27
28 namespace Loader {
29
30
31 COREBatchProcessingLoader::COREBatchProcessingLoader() : mORELogIdentifier("::Konclude::OREBatchProcessor",this) {
32 mOWLLinkInterpreter = nullptr;
33 mLoaderConfig = nullptr;
34 mConfLogProcessingTimes = false;
35 mProcessingCommandData = nullptr;
36
37 mCloseAfterOutput = true;
38 mBlockUntilProcessed = true;
39
40 mSatOperationString = QString("sat").toLower();
41 mSatisfiabilityOperationString = QString("satisfiability").toLower();
42 mQueryOperationString = QString("query").toLower();
43 mClassificationOperationString = QString("classification").toLower();
44 mConsistencyOperationString = QString("consistency").toLower();
45 mRealisationOperationString = QString("realisation").toLower();
46 mRealizationOperationString = QString("realization").toLower();
47
48
49 mOperationTimeOutputString = QString("Operation time: ");
50
51 mInfoConsoleObserver = nullptr;
52 mErrorFileObserver = nullptr;
53
54 mConsistencyKBCommand = nullptr;
55 mTriviallyConsistencyKBCommand = nullptr;
56 mAdditionalCheckingTime = 0;
57
58 mReportAllTime = false;
59 }
60
61
62
63 COREBatchProcessingLoader::~COREBatchProcessingLoader() {
64 }
65
66
67
68 CCommandRecorder* COREBatchProcessingLoader::recordData(CCommandRecordData *recData) {
69 LOGC(recData->getRecordErrorLevel(),recData->getRecordDomain(),recData->getRecordMessage(),recData->getCommand());
70 return this;
71 }
72
73
74
75 CLoader *COREBatchProcessingLoader::init(CLoaderFactory *loaderFactory, CConfiguration *config) {
76 mLoaderConfig = config;
77 reasonerCommander = CConfigManagerReader::readCommanderManagerConfig(config);
78
79 mOperationTaskString = CConfigDataReader::readConfigString(config,"Konclude.ORE.OperationTask");
80 mOntologyFileString = CConfigDataReader::readConfigString(config,"Konclude.ORE.OntologyFile");
81 mResponseFileString = CConfigDataReader::readConfigString(config,"Konclude.ORE.ResponseFile");
82 mIRINameString = CConfigDataReader::readConfigString(config,"Konclude.ORE.IRIName");
83 mCloseAfterOutput = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.CloseAfterProcessed");
84 mOutputMeasuredTimeInSeconds = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.OutputTimeMeasurementInSeconds");
85
86 mReportParsingTimeForConsistency = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.ReportParsingTimeForConsistency");
87 mReportParsingTimeForSatisfiability = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.ReportParsingTimeForSatisfiability");
88 mReportParsingTimeForRealisation = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.ReportParsingTimeForRealization");
89 mReportParsingTimeForClassification = CConfigDataReader::readConfigBoolean(config,"Konclude.ORE.ReportParsingTimeForClassification");
90
91 mInfoConsoleObserver = new COREConsolePrintLogObserver(QStringList()<<mORELogIdentifier.getLogDomain());
92 mErrorFileObserver = new COREFilePrintLogObserver(mResponseFileString+"_err");
93 CLogger::getInstance()->addLogObserver(mInfoConsoleObserver);
94 CLogger::getInstance()->addLogObserver(mErrorFileObserver);
95
96 return this;
97 }
98
99
100
101 CLoader *COREBatchProcessingLoader::load() {
102 startProcessing();
103 if (mBlockUntilProcessed) {
104 mBlockingSemaphore.acquire();
105 }
106 return this;
107 }
108
109
110
111 CConfiguration *COREBatchProcessingLoader::getConfiguration() {
112 if (!reasonerCommander) {
113 return mLoaderConfig;
114 } else {
115 CGetDescriptionCommand* getDesComm = new CGetDescriptionCommand();
116 reasonerCommander->delegateCommand(getDesComm);
117 CCommandExecutedBlocker commExeBlocker;
118 commExeBlocker.waitExecutedCommand(getDesComm);
119 CConfiguration* config = getDesComm->getConfiguration();
120 return config;
121 }
122 }
123
124
125 void COREBatchProcessingLoader::createClassificationTestingCommands() {
126 logOutputMessage(QString("Started %1 on %2").arg(mProcessingOperationString).arg(mOntologyFileString));
127 QString oreTestKB = QString("http://konclude.com/ore/test/kb");
128 CCreateKnowledgeBaseCommand* createKBCommand = new CCreateKnowledgeBaseCommand(oreTestKB);
129 QStringList ontoIRIList;
130 ontoIRIList.append(mOntologyFileString);
131 CLoadKnowledgeBaseOWLAutoOntologyCommand* loadKBCommand = new CLoadKnowledgeBaseOWLAutoOntologyCommand(oreTestKB,ontoIRIList);
132 CKnowledgeBaseClassifyCommand* classifyKBCommand = new CKnowledgeBaseClassifyCommand(oreTestKB);
133 CWriteFunctionalSubClassHierarchyQueryCommand* writeHierarchyCommand = new CWriteFunctionalSubClassHierarchyQueryCommand(oreTestKB,mResponseFileString);
134 if (mReportParsingTimeForClassification) {
135 mReportAllTime = true;
136 }
137 addProcessingCommand(createKBCommand);
138 addProcessingCommand(loadKBCommand);
139 addProcessingCommand(classifyKBCommand,true,mOperationTimeOutputString);
140 addProcessingCommand(writeHierarchyCommand);
141 processNextCommand();
142 }
143
144 void COREBatchProcessingLoader::createRealisationTestingCommands() {
145 logOutputMessage(QString("Started %1 on %2").arg(mProcessingOperationString).arg(mOntologyFileString));
146 QString oreTestKB = QString("http://konclude.com/ore/test/kb");
147 CCreateKnowledgeBaseCommand* createKBCommand = new CCreateKnowledgeBaseCommand(oreTestKB);
148 QStringList ontoIRIList;
149 ontoIRIList.append(mOntologyFileString);
150 CLoadKnowledgeBaseOWLAutoOntologyCommand* loadKBCommand = new CLoadKnowledgeBaseOWLAutoOntologyCommand(oreTestKB,ontoIRIList);
151 CKnowledgeBaseRealizeCommand* realizeKBCommand = new CKnowledgeBaseRealizeCommand(oreTestKB);
152 CWriteCustomQueryCommand* writeIndividualTypesCommand = new CWriteCustomQueryCommand(oreTestKB,CWriteQuery::WRITEINDIVIDUALFLATTENEDTYPES,new CWriteQueryFileOWL2FunctionalSerializer(mResponseFileString));
153 //CWriteFunctionalIndividualTypesQueryCommand* writeIndividualTypesCommand = new CWriteFunctionalIndividualTypesQueryCommand(oreTestKB,mResponseFileString);
154 if (mReportParsingTimeForRealisation) {
155 mReportAllTime = true;
156 }
157 addProcessingCommand(createKBCommand);
158 addProcessingCommand(loadKBCommand);
159 addProcessingCommand(realizeKBCommand,true,mOperationTimeOutputString);
160 addProcessingCommand(writeIndividualTypesCommand);
161 processNextCommand();
162 }
163
164
165 void COREBatchProcessingLoader::createConsistencyTestingCommands() {
166 logOutputMessage(QString("Started %1 on %2").arg(mProcessingOperationString).arg(mOntologyFileString));
167 QString oreTestKB = QString("http://konclude.com/ore/test/kb");
168 CCreateKnowledgeBaseCommand* createKBCommand = new CCreateKnowledgeBaseCommand(oreTestKB);
169 QStringList ontoIRIList;
170 ontoIRIList.append(mOntologyFileString);
171 CLoadKnowledgeBaseOWLAutoOntologyCommand* loadKBCommand = new CLoadKnowledgeBaseOWLAutoOntologyCommand(oreTestKB,ontoIRIList);
172 mConsistencyKBCommand = new CIsConsistentQueryCommand(oreTestKB);
173 mTriviallyConsistencyKBCommand = new CIsTriviallyConsistentQueryCommand(oreTestKB);
174 if (mReportParsingTimeForConsistency) {
175 mReportAllTime = true;
176 }
177 addProcessingCommand(createKBCommand);
178 addProcessingCommand(loadKBCommand);
179 addProcessingCommand(mTriviallyConsistencyKBCommand,true,mOperationTimeOutputString,true,mResponseFileString);
180 processNextCommand();
181 }
182
183
184 void COREBatchProcessingLoader::createSatisfiabilityTesingCommands() {
185 logOutputMessage(QString("Started %1 on %2").arg(mProcessingOperationString).arg(mOntologyFileString));
186 QString oreTestKB = QString("http://konclude.com/ore/test/kb");
187 CCreateKnowledgeBaseCommand* createKBCommand = new CCreateKnowledgeBaseCommand(oreTestKB);
188 QStringList ontoIRIList;
189 ontoIRIList.append(mOntologyFileString);
190 mWriteSatisfiabilityPrefixString = mIRINameString+QString(",");
191 CLoadKnowledgeBaseOWLAutoOntologyCommand* loadKBCommand = new CLoadKnowledgeBaseOWLAutoOntologyCommand(oreTestKB,ontoIRIList);
192 CProcessClassNameSatisfiableQueryCommand* consistencyKBCommand = new CProcessClassNameSatisfiableQueryCommand(oreTestKB,mIRINameString);
193 if (mReportParsingTimeForSatisfiability) {
194 mReportAllTime = true;
195 }
196 addProcessingCommand(createKBCommand);
197 addProcessingCommand(loadKBCommand);
198 addProcessingCommand(consistencyKBCommand,true,mOperationTimeOutputString,true,mResponseFileString);
199 processNextCommand();
200 }
201
202 void COREBatchProcessingLoader::addProcessingCommand(CCommand* command) {
203 mProcessCommandList.append(new CCommandProcessData(command));
204 }
205
206 void COREBatchProcessingLoader::addProcessingCommand(CCommand* command, bool measureTime, const QString& measuredOutputString) {
207 mProcessCommandList.append(new CCommandProcessData(command,measureTime,measuredOutputString));
208 }
209
210
211 void COREBatchProcessingLoader::addProcessingCommand(CCommand* command, bool measureTime, const QString& measuredOutputString, bool writeSatisfiabilityOutput, const QString& outputFileName) {
212 mProcessCommandList.append(new CCommandProcessData(command,measureTime,measuredOutputString,writeSatisfiabilityOutput,outputFileName));
213 }
214
215
216 COWLlinkProcessor *COREBatchProcessingLoader::initializeOWLlinkContent() {
217
218
219 QString operationTaskString = mOperationTaskString;
220 operationTaskString = operationTaskString.toLower();
221
222 if (operationTaskString == mSatOperationString) {
223 mProcessingOperationString = mSatOperationString;
224 createSatisfiabilityTesingCommands();
225
226 } else if (operationTaskString == mSatisfiabilityOperationString) {
227 mProcessingOperationString = mSatisfiabilityOperationString;
228 createSatisfiabilityTesingCommands();
229
230 } else if (operationTaskString == mClassificationOperationString) {
231 mProcessingOperationString = mClassificationOperationString;
232 createClassificationTestingCommands();
233
234 } else if (operationTaskString == mConsistencyOperationString) {
235 mProcessingOperationString = mConsistencyOperationString;
236 createConsistencyTestingCommands();
237
238 } else if (operationTaskString == mRealisationOperationString) {
239 mProcessingOperationString = mRealisationOperationString;
240 createRealisationTestingCommands();
241
242 } else if (operationTaskString == mRealizationOperationString) {
243 mProcessingOperationString = mRealizationOperationString;
244 createRealisationTestingCommands();
245
246 } else {
247 logOutputError(QString("Operation %1 not supported").arg(mOperationTaskString));
248 terminateProcessing();
249 }
250
251 return this;
252 }
253
254 COWLlinkProcessor *COREBatchProcessingLoader::concludeOWLlinkContent() {
255 finishCommandProcessing();
256 if (hasMoreProcessingCommands()) {
257 processNextCommand();
258 } else {
259 logOutputMessage(QString("Completed %1 on %2").arg(mProcessingOperationString).arg(mOntologyFileString));
260 terminateProcessing();
261 }
262 return this;
263 }
264
265
266 void COREBatchProcessingLoader::logOutputError(const QString& errorString) {
267 LOG(ERROR,mORELogIdentifier.getLogDomain(),logTr("Error: %1").arg(errorString),this);
268 }
269 void COREBatchProcessingLoader::logOutputNotice(const QString& outputString) {
270 LOG(NOTICE,mORELogIdentifier.getLogDomain(),logTr("%1").arg(outputString),this);
271 }
272
273 void COREBatchProcessingLoader::logOutputMessage(const QString& outputString) {
274 LOG(INFO,mORELogIdentifier.getLogDomain(),logTr("%1").arg(outputString),this);
275 }
276
277 void COREBatchProcessingLoader::forcedPathCreated(const QString& filePath) {
278 QString path = filePath;
279 if (path.contains("/") || path.contains("\\")) {
280 int lastSlash = path.lastIndexOf("/");
281 int lastBackSlash = path.lastIndexOf("\\");
282 int lastSeparator = qMax(lastBackSlash,lastSlash);
283 path = path.mid(0,lastSeparator);
284 if (!path.isEmpty()) {
285 QDir dir;
286 dir.mkpath(path);
287 }
288 }
289 }
290
291 void COREBatchProcessingLoader::writeSatisfiabilityResult(const QString& outputFileName, CCommand* processedCommand) {
292 bool outputWritten = false;
293 bool calculationCompleted = false;
294 CKnowledgeBaseQueryCommand* kbQueryCommand = dynamic_cast<CKnowledgeBaseQueryCommand*>(processedCommand);
295 if (kbQueryCommand) {
296 CQuery* query = kbQueryCommand->getCalculateQueryCommand()->getQuery();
297 if (query) {
298 CQueryResult* queryResult = query->getQueryResult();
299 if (queryResult) {
300 CBooleanQueryResult* boolQueryResult = dynamic_cast<CBooleanQueryResult*>(queryResult);
301 if (boolQueryResult) {
302 calculationCompleted = true;
303 forcedPathCreated(outputFileName);
304 QFile outputFile(outputFileName);
305 if (outputFile.open(QIODevice::Append)) {
306 QString outputData;
307 if (boolQueryResult->getResult() == true) {
308 outputData = mWriteSatisfiabilityPrefixString+QString("true\n");
309 } else {
310 outputData = mWriteSatisfiabilityPrefixString+QString("false\n");
311 }
312 outputFile.write(outputData.toUtf8());
313 outputFile.close();
314 outputWritten = true;
315 } else {
316 logOutputError(QString("Failed writing output to file '%1'").arg(outputFileName));
317 }
318 }
319 }
320 }
321 }
322 if (!calculationCompleted) {
323 logOutputError("Calculation failed");
324 }
325 }
326
327 void COREBatchProcessingLoader::finishCommandProcessing() {
328 if (mProcessingCommandData) {
329 bool writeResults = true;
330 if (mProcessingCommandData->mCommand == mTriviallyConsistencyKBCommand) {
331 CKnowledgeBaseQueryCommand* kbQueryCommand = dynamic_cast<CKnowledgeBaseQueryCommand*>(mProcessingCommandData->mCommand);
332 if (kbQueryCommand) {
333 CQuery* query = kbQueryCommand->getCalculateQueryCommand()->getQuery();
334 if (query) {
335 CQueryResult* queryResult = query->getQueryResult();
336 if (queryResult) {
337 CBooleanQueryResult* boolQueryResult = dynamic_cast<CBooleanQueryResult*>(queryResult);
338 if (boolQueryResult) {
339 if (!boolQueryResult->getResult()) {
340 writeResults = false;
341 mAdditionalCheckingTime += mMeasurementTime.elapsed();
342 addProcessingCommand(mConsistencyKBCommand,true,mOperationTimeOutputString,true,mResponseFileString);
343 }
344 }
345 }
346 }
347 }
348 }
349
350 if (writeResults) {
351 if (mProcessingCommandData->mMeasureTime) {
352 QString outputString = mProcessingCommandData->mMeasuredOutputString;
353 cint64 timeElapsed = mMeasurementTime.elapsed();
354 timeElapsed += mAdditionalCheckingTime;
355 if (mOutputMeasuredTimeInSeconds) {
356 logOutputMessage(outputString+QString("%1").arg(timeElapsed/1000.));
357 } else {
358 logOutputMessage(outputString+QString("%1").arg(timeElapsed));
359 }
360 } else {
361 if (mReportAllTime) {
362 mAdditionalCheckingTime += mMeasurementTime.elapsed();
363 }
364 }
365 if (mProcessingCommandData->mWriteSatisfiableOutput) {
366 QString outputFile = mProcessingCommandData->mWriteOutputFile;
367 writeSatisfiabilityResult(outputFile,mProcessingCommandData->mCommand);
368 }
369 }
370 mProcessingCommandData = nullptr;
371 }
372 }
373
374 void COREBatchProcessingLoader::processNextCommand() {
375 mProcessingCommandData = mProcessCommandList.takeFirst();
376 CCommand* command = mProcessingCommandData->mCommand;
377 logOutputNotice(QString("Starting '%1' command").arg(command->getBriefCommandDescription()));
378 command->setRecorder(this);
379 command->setReportErrorFromSubCommands(false);
380 CCommandProcessedCallbackEvent *proComm = new CCommandProcessedCallbackEvent(this,command);
381 command->addProcessedCallback(proComm);
382 mMeasurementTime.start();
383 preSynchronizer->delegateCommand(command);
384 }
385
386 bool COREBatchProcessingLoader::hasMoreProcessingCommands() {
387 return !mProcessCommandList.isEmpty();
388 }
389
390 void COREBatchProcessingLoader::terminateProcessing() {
391 mInfoConsoleObserver->flushLogging();
392 mErrorFileObserver->flushLogging();
393
394 if (mBlockUntilProcessed) {
395 mBlockingSemaphore.release();
396 }
397
398 if (mCloseAfterOutput) {
399 CLogger::getInstance()->waitSynchronization();
400 QCoreApplication::exit();
401 }
402 }
403
404
405
406 CLoader *COREBatchProcessingLoader::exit() {
407 stopThread();
408 return this;
409 }
410
411
412
413 }; // end namespace Loader
414
415 }; // end namespace Control
416
417 }; // end namespace Konclude