| 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 "COptimizedComplexCompositionAnsweringHandler.h" |
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Reasoner { |
| 26 |
|
| 27 |
namespace Answerer { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
COptimizedComplexCompositionAnsweringHandler::COptimizedComplexCompositionAnsweringHandler(COptimizedComplexCompositionOntologyAnsweringItem* ontoAnsweringItem) : CAnsweringHandler(), CLogDomain("::Konclude::Reasoner::Kernel::ComplexComposedQueryAnswerer") { |
| 34 |
mOntoAnsweringItem = ontoAnsweringItem; |
| 35 |
|
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
COptimizedComplexCompositionAnsweringHandler::~COptimizedComplexCompositionAnsweringHandler() { |
| 41 |
} |
| 42 |
|
| 43 |
bool COptimizedComplexCompositionAnsweringHandler::canAnsweringComplexQuery(bool composedQuery) { |
| 44 |
return composedQuery; |
| 45 |
} |
| 46 |
|
| 47 |
bool COptimizedComplexCompositionAnsweringHandler::canAnsweringComplexQuery(CComplexAnsweringQuery* complexAnsweringQuery) { |
| 48 |
if (dynamic_cast<CComplexAnsweringCompositionQuery*>(complexAnsweringQuery)) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
bool COptimizedComplexCompositionAnsweringHandler::addAnsweringComplexQuery(CComplexAnsweringQuery* complexAnsweringQuery, CCallbackData* callback) { |
| 55 |
mPendingQueryProcessingList.append(new CComplexQueryCompositionProcessingData((CComplexAnsweringCompositionQuery*)complexAnsweringQuery, callback)); |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
bool COptimizedComplexCompositionAnsweringHandler::initializeProcessing(CAnswererContext* answererContext) { |
| 62 |
while (!mPendingQueryProcessingList.isEmpty()) { |
| 63 |
CComplexQueryCompositionProcessingData* queryData = mPendingQueryProcessingList.takeFirst(); |
| 64 |
|
| 65 |
CAnsweringHandlingStatistics* stats = mOntoAnsweringItem->getAnsweringHandlingStatistics(); |
| 66 |
if (stats) { |
| 67 |
CAnsweringHandlingStatistics* initStats = new CAnsweringHandlingStatistics(); |
| 68 |
*initStats = *stats; |
| 69 |
queryData->setQueryProcessingInitializedAnsweringStatistics(initStats); |
| 70 |
} |
| 71 |
|
| 72 |
initializeQueryProcessing(queryData,answererContext); |
| 73 |
} |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
bool COptimizedComplexCompositionAnsweringHandler::prepareProcessing(CAnswererContext* answererContext) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
COptimizedComplexCompositionAnsweringHandler* COptimizedComplexCompositionAnsweringHandler::initializeQueryProcessing(CComplexQueryCompositionProcessingData* queryData, CAnswererContext* answererContext) { |
| 86 |
queryData->getQuery()->addErrorString("Processing of complex composition query not supported."); |
| 87 |
CCallbackData* callback = queryData->getCallback(); |
| 88 |
if (callback) { |
| 89 |
callback->doCallback(); |
| 90 |
} |
| 91 |
delete queryData; |
| 92 |
return this; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
bool COptimizedComplexCompositionAnsweringHandler::continueCalculationCreation(CAnswererContext* answererContext) { |
| 99 |
bool processing = false; |
| 100 |
|
| 101 |
return processing; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
bool COptimizedComplexCompositionAnsweringHandler::processCalculationMessage(CAnsweringMessageData* message, CAnswererContext* answererContext) { |
| 108 |
while (message) { |
| 109 |
CAnsweringMessageData::ANSWERINGMESSAGEDATA messageType = message->getAnsweringMessageDataType(); |
| 110 |
if (messageType == CAnsweringMessageData::CALCULATIONCOMPLETED) { |
| 111 |
CAnsweringMessageDataCalculationCompleted* calcCompMessage = (CAnsweringMessageDataCalculationCompleted*)message; |
| 112 |
|
| 113 |
bool errorOcurred = false; |
| 114 |
if (calcCompMessage->hasCalculationError()) { |
| 115 |
LOG(ERROR, getDomain(), logTr("Error in computation, answer computation for ontology '%1' failed.").arg(mOntoAnsweringItem->getOntology()->getTerminologyName()), this); |
| 116 |
errorOcurred = true; |
| 117 |
} |
| 118 |
|
| 119 |
CAnsweringMessageDataCalculationCompleted::CALCULATIONTYPE calcType = calcCompMessage->getCalculationType(); |
| 120 |
|
| 121 |
CAnsweringCalculationStatisticsCollection* statColl = calcCompMessage->getStatisticsCollection(); |
| 122 |
if (statColl) { |
| 123 |
mOntoAnsweringItem->reuseCalculationStatisticsCollection(statColl); |
| 124 |
} |
| 125 |
|
| 126 |
} else if (messageType == CAnsweringMessageData::CALCULATIONADAPTER) { |
| 127 |
CAnsweringMessageDataCalculationAdapter* calcAdapMessage = (CAnsweringMessageDataCalculationAdapter*)message; |
| 128 |
CAnsweringMessageDataCalculationAdapter::CALCULATIONADAPTERTYPE calcType = calcAdapMessage->getCalculationAdapterType(); |
| 129 |
|
| 130 |
} |
| 131 |
message = message->getNext(); |
| 132 |
} |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
bool COptimizedComplexCompositionAnsweringHandler::processRequirementMessage(CAnsweringMessageData* message, CAnswererContext* answererContext) { |
| 139 |
while (message) { |
| 140 |
CAnsweringMessageData::ANSWERINGMESSAGEDATA messageType = message->getAnsweringMessageDataType(); |
| 141 |
if (messageType == CAnsweringMessageData::REQUIREMENTCOMPLETED) { |
| 142 |
} |
| 143 |
message = message->getNext(); |
| 144 |
} |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool COptimizedComplexCompositionAnsweringHandler::isAnsweringFinished(CAnswererContext* answererContext) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
CAnsweringProgress* COptimizedComplexCompositionAnsweringHandler::getAnsweringProgress() { |
| 161 |
|
| 162 |
|
| 163 |
if (mTotalPossibleInstancesCount != mTestedPossibleInstancesCount) { |
| 164 |
cint64 tested = mTestedPossibleInstancesCount; |
| 165 |
cint64 total = mTotalPossibleInstancesCount; |
| 166 |
cint64 elapsedTime = mAnsweringStartTime.elapsed(); |
| 167 |
double remainingMilliSeconds = 0.; |
| 168 |
double progessPercent = 0.; |
| 169 |
if (elapsedTime <= 0 && total > 0) { |
| 170 |
remainingMilliSeconds = 0.; |
| 171 |
} else { |
| 172 |
remainingMilliSeconds = ((double)elapsedTime / (double)tested) * (total - tested); |
| 173 |
} |
| 174 |
if (total <= 0) { |
| 175 |
progessPercent = 0.; |
| 176 |
} else { |
| 177 |
progessPercent = ((double)tested / (double)total) * 100.; |
| 178 |
} |
| 179 |
mAnsweringProgress.setProgessPercent(progessPercent); |
| 180 |
mAnsweringProgress.setRemainingMilliSeconds(remainingMilliSeconds); |
| 181 |
mAnsweringProgress.setTestedInstances(tested); |
| 182 |
mAnsweringProgress.setTotalInstances(total); |
| 183 |
} else { |
| 184 |
mAnsweringProgress.setProgessPercent(0.); |
| 185 |
mAnsweringProgress.setRemainingMilliSeconds(0.); |
| 186 |
mAnsweringProgress.setTestedInstances(mTestedPossibleInstancesCount); |
| 187 |
mAnsweringProgress.setTotalInstances(mTotalPossibleInstancesCount); |
| 188 |
} |
| 189 |
return &mAnsweringProgress; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
CAnsweringHandler* COptimizedComplexCompositionAnsweringHandler::storeAnsweringStatistics() { |
| 196 |
CAnsweringStatisticsCollectionStrings* stats = new CAnsweringStatisticsCollectionStrings(); |
| 197 |
addAnsweringStatistics(stats, false); |
| 198 |
mOntoAnsweringItem->setDifferenceStoredAnsweringStatisticsCollectionStrings(stats); |
| 199 |
return this; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
CAnsweringHandler* COptimizedComplexCompositionAnsweringHandler::addAnsweringStatistics(CAnsweringStatistics* stats, bool lastStoredDiff) { |
| 205 |
CAnsweringCalculationStatisticsCollectionStrings* calcStatsColl = mOntoAnsweringItem->getAnsweringStatisticsCollectionStrings(); |
| 206 |
CAnsweringStatisticsCollectionStrings* diffStats = mOntoAnsweringItem->getDifferenceStoredAnsweringStatisticsCollectionStrings(); |
| 207 |
if (calcStatsColl) { |
| 208 |
for (const QString& statName : calcStatsColl->getStatisticsNameStringList()) { |
| 209 |
cint64 value = calcStatsColl->getStatisticIntegerValue(statName); |
| 210 |
if (lastStoredDiff && diffStats) { |
| 211 |
value = value - diffStats->getStatisticIntegerValue(statName); |
| 212 |
} |
| 213 |
stats->addProcessingStatistics(statName, value); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
CAnsweringStatistics* tmpStats = stats; |
| 218 |
if (lastStoredDiff && diffStats) { |
| 219 |
tmpStats = new CAnsweringStatisticsCollectionStrings(); |
| 220 |
} |
| 221 |
CAnsweringHandlingStatistics* handlingStats = mOntoAnsweringItem->getAnsweringHandlingStatistics(); |
| 222 |
if (handlingStats) { |
| 223 |
stats->addProcessingStatistics("answering-processed-query-count", handlingStats->getTotalAnsweredQueriesCount()); |
| 224 |
} |
| 225 |
if (lastStoredDiff && diffStats) { |
| 226 |
for (const QString& statName : tmpStats->getStatisticsNameStringList()) { |
| 227 |
cint64 value = tmpStats->getStatisticIntegerValue(statName); |
| 228 |
value = value - diffStats->getStatisticIntegerValue(statName); |
| 229 |
stats->addProcessingStatistics(statName, value); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return this; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
}; // end namespace Answerer |
| 246 |
|
| 247 |
}; // end namespace Reasoner |
| 248 |
|
| 249 |
}; // end namespace Konclude |