| 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 "CFirstQueuedAnsweringHandlerProvider.h" |
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Reasoner { |
| 26 |
|
| 27 |
namespace Answerer { |
| 28 |
|
| 29 |
|
| 30 |
CFirstQueuedAnsweringHandlerProvider::CFirstQueuedAnsweringHandlerProvider(CAnsweringHandlerFactory* answeringHandlerFactory) { |
| 31 |
mAnsweringHandlerFactory = answeringHandlerFactory; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
CFirstQueuedAnsweringHandlerProvider::~CFirstQueuedAnsweringHandlerProvider() { |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
CAnsweringHandler* CFirstQueuedAnsweringHandlerProvider::getAnsweringHandler(CComplexAnsweringQuery* complexAnsweringQuery) { |
| 40 |
CConcreteOntology* ontology = complexAnsweringQuery->getBaseOntology(); |
| 41 |
return getAnsweringHandler(ontology, dynamic_cast<CComplexAnsweringCompositionQuery*>(complexAnsweringQuery) != nullptr); |
| 42 |
} |
| 43 |
|
| 44 |
CAnsweringHandler* CFirstQueuedAnsweringHandlerProvider::getAnsweringHandler(CConcreteOntology* ontology, bool composedQuery) { |
| 45 |
CAnsweringHandler* answeringHandler = nullptr; |
| 46 |
CAnsweringHandlerData*& handlerData = mOntoloyHandlerDataHash[ontology]; |
| 47 |
if (!handlerData) { |
| 48 |
handlerData = new CAnsweringHandlerData(); |
| 49 |
} |
| 50 |
if (!handlerData->mQueuedHandlerList.isEmpty()) { |
| 51 |
for (QList<CAnsweringHandler*>::iterator it = handlerData->mQueuedHandlerList.begin(), itEnd = handlerData->mQueuedHandlerList.end(); it != itEnd && !answeringHandler; ++it) { |
| 52 |
CAnsweringHandler* testAnsweringHandler = *it; |
| 53 |
if (testAnsweringHandler->canAnsweringComplexQuery(composedQuery)) { |
| 54 |
answeringHandler = testAnsweringHandler; |
| 55 |
it = handlerData->mQueuedHandlerList.erase(it); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
if (!answeringHandler) { |
| 60 |
answeringHandler = mAnsweringHandlerFactory->createAnsweringHandler(ontology, composedQuery); |
| 61 |
mHandlerHandlerDataHash.insert(answeringHandler, handlerData); |
| 62 |
} |
| 63 |
if (handlerData->mStatisticsCollection) { |
| 64 |
answeringHandler->storeAnsweringStatistics(); |
| 65 |
} |
| 66 |
handlerData->mProcessingHandlerSet.insert(answeringHandler); |
| 67 |
return answeringHandler; |
| 68 |
} |
| 69 |
|
| 70 |
CAnsweringHandlerProvider* CFirstQueuedAnsweringHandlerProvider::releaseAnsweringHandler(CAnsweringHandler* answeringHandler) { |
| 71 |
CAnsweringHandlerData* handlerData = mHandlerHandlerDataHash[answeringHandler]; |
| 72 |
handlerData->mProcessingHandlerSet.remove(answeringHandler); |
| 73 |
handlerData->mQueuedHandlerList.append(answeringHandler); |
| 74 |
if (handlerData->mStatisticsCollection) { |
| 75 |
answeringHandler->addAnsweringStatistics(handlerData->mCollectedStats, true); |
| 76 |
} |
| 77 |
return this; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
CAnsweringHandlerProvider* CFirstQueuedAnsweringHandlerProvider::addAnsweringStatistics(CConcreteOntology* ontology, CAnsweringStatistics* stats) { |
| 83 |
CAnsweringHandlerData*& handlerData = mOntoloyHandlerDataHash[ontology]; |
| 84 |
if (!handlerData) { |
| 85 |
handlerData = new CAnsweringHandlerData(); |
| 86 |
} |
| 87 |
if (!handlerData->mStatisticsCollection) { |
| 88 |
handlerData->mStatisticsCollection = true; |
| 89 |
handlerData->mCollectedStats = new CAnsweringStatisticsCollectionStrings(); |
| 90 |
for (CAnsweringHandler* handler : handlerData->mQueuedHandlerList) { |
| 91 |
handler->addAnsweringStatistics(handlerData->mCollectedStats, false); |
| 92 |
} |
| 93 |
} |
| 94 |
for (const QString& statName : handlerData->mCollectedStats->getStatisticsNameStringList()) { |
| 95 |
cint64 value = handlerData->mCollectedStats->getStatisticIntegerValue(statName); |
| 96 |
stats->addProcessingStatistics(statName, value); |
| 97 |
} |
| 98 |
return this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
}; // end namespace Answerer |
| 103 |
|
| 104 |
}; // end namespace Reasoner |
| 105 |
|
| 106 |
}; // end namespace Konclude |