| 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 |
#ifdef KONCLUDE_REDLAND_INTEGRATION |
| 21 |
|
| 22 |
#include "COptimizedComplexCompositionRedlandRasqalAnsweringHandler.h" |
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Answerer { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
COptimizedComplexCompositionRedlandRasqalAnsweringHandler::COptimizedComplexCompositionRedlandRasqalAnsweringHandler(COptimizedComplexCompositionOntologyAnsweringItem* ontoAnsweringItem) : COptimizedComplexCompositionAnsweringHandler(ontoAnsweringItem) { |
| 35 |
mCurrentProcessingRedlandRasqalQueryProcData = nullptr; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
COptimizedComplexCompositionRedlandRasqalAnsweringHandler::~COptimizedComplexCompositionRedlandRasqalAnsweringHandler() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::canAnsweringComplexQuery(CComplexAnsweringQuery* complexAnsweringQuery) { |
| 45 |
if (dynamic_cast<CRedlandRasqalBGPsCompositionQuery*>(complexAnsweringQuery)) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::addAnsweringComplexQuery(CComplexAnsweringQuery* complexAnsweringQuery, CCallbackData* callback) { |
| 52 |
mPendingQueryProcessingList.append(new CComplexQueryCompositionRedlandRasqalProcessingData((CRedlandRasqalBGPsCompositionQuery*)complexAnsweringQuery, callback)); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
int redland_log_handler_processing_error_notifier(void *user_data, librdf_log_message *message) { |
| 60 |
QStringList* messageList = (QStringList*)user_data; |
| 61 |
if (message->level <= 50 && messageList) { |
| 62 |
messageList->append(QString(QString("%1").arg(message->message))); |
| 63 |
} |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
void rasqal_log_handler_processing_error_notifier(void *user_data, raptor_log_message *message) { |
| 68 |
QStringList* messageList = (QStringList*)user_data; |
| 69 |
if (message->level <= 50 && messageList) { |
| 70 |
messageList->append(QString(QString("%1").arg(message->text))); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
COptimizedComplexCompositionRedlandRasqalAnsweringHandler* COptimizedComplexCompositionRedlandRasqalAnsweringHandler::initializeQueryProcessing(CComplexQueryCompositionProcessingData* queryData, CAnswererContext* answererContext) { |
| 79 |
CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData = (CComplexQueryCompositionRedlandRasqalProcessingData*)queryData; |
| 80 |
|
| 81 |
CRedlandRasqalBGPsCompositionQuery* redlandRasqalQuery = redlandRasqalQueryProcData->getRedlandRasqalQuery(); |
| 82 |
CConcreteOntology* baseOnto = mOntoAnsweringItem->getOntology(); |
| 83 |
CConcreteOntology* exppOnto = redlandRasqalQuery->getExrpessionOntology(); |
| 84 |
CConfigurationBase* config = redlandRasqalQuery->getConfiguration(); |
| 85 |
redlandRasqalQueryProcData->setProcessingStarted(); |
| 86 |
|
| 87 |
rasqal_world_set_log_handler(librdf_world_get_rasqal(redlandRasqalQuery->getRedlandWorld()), redlandRasqalQueryProcData->getRedlandRasqalErrorWarningMessageList(), &rasqal_log_handler_processing_error_notifier); |
| 88 |
librdf_world_set_logger(redlandRasqalQuery->getRedlandWorld(), redlandRasqalQueryProcData->getRedlandRasqalErrorWarningMessageList(), &redland_log_handler_processing_error_notifier); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
mOntoAnsweringItem->addScheduledQueriesProcessingData(redlandRasqalQueryProcData); |
| 97 |
|
| 98 |
mOntoAnsweringItem->incCurrentlyAnsweringQueryCount(); |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::continueCalculationCreation(CAnswererContext* answererContext) { |
| 106 |
bool processing = false; |
| 107 |
|
| 108 |
|
| 109 |
QList<CComplexQueryCompositionProcessingData*>* scheduledQueriesProcDataList = mOntoAnsweringItem->getScheduledQueriesProcessingDataList(); |
| 110 |
if (!scheduledQueriesProcDataList->isEmpty()) { |
| 111 |
CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData = (CComplexQueryCompositionRedlandRasqalProcessingData*)scheduledQueriesProcDataList->takeFirst(); |
| 112 |
CRedlandRasqalBGPsCompositionQuery* query = redlandRasqalQueryProcData->getRedlandRasqalQuery(); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (!processing && !redlandRasqalQueryProcData->hasSubQueriesGenerated()) { |
| 118 |
|
| 119 |
bool subQueryProcessing = generateSubQueries(redlandRasqalQueryProcData, answererContext); |
| 120 |
if (subQueryProcessing) { |
| 121 |
processing = true; |
| 122 |
} |
| 123 |
redlandRasqalQueryProcData->setSubQueriesGenerated(true); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
if (!processing && redlandRasqalQueryProcData->hasRemainingSubQueriesProcessing() && !redlandRasqalQueryProcData->isSubQueryWaiting()) { |
| 128 |
|
| 129 |
bool subQueryProcessing = scheduleNextSubQueryProcessing(redlandRasqalQueryProcData, answererContext); |
| 130 |
if (subQueryProcessing) { |
| 131 |
processing = true; |
| 132 |
} |
| 133 |
redlandRasqalQueryProcData->setSubQueriesGenerated(true); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (!processing && !redlandRasqalQueryProcData->isQueryProcessed() && redlandRasqalQueryProcData->isSubQueryProcessingFinished()) { |
| 138 |
if (redlandRasqalQueryProcData->areResultDirectlyFromSubQueryStreamed()) { |
| 139 |
LOG(INFO, getDomain(), logTr("Redland Rasqal query completely answered with computation of sub-query '%1' in %2 ms.").arg(query->getQueryName()).arg(redlandRasqalQueryProcData->getSubQueryProcessingTime()), this); |
| 140 |
} else { |
| 141 |
processRasqal(query, redlandRasqalQueryProcData); |
| 142 |
} |
| 143 |
redlandRasqalQueryProcData->setQueryProcessed(true); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
if (!processing && redlandRasqalQueryProcData->isQueryProcessed()) { |
| 148 |
|
| 149 |
CCallbackData* callback = redlandRasqalQueryProcData->getCallback(); |
| 150 |
if (callback) { |
| 151 |
callback->doCallback(); |
| 152 |
} |
| 153 |
delete redlandRasqalQueryProcData; |
| 154 |
mOntoAnsweringItem->decCurrentlyAnsweringQueryCount(); |
| 155 |
mOntoAnsweringItem->getAnsweringHandlingStatistics()->incTotalAnsweredQueriesCount(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
return processing; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::scheduleNextSubQueryProcessing(CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData, CAnswererContext* answererContext) { |
| 167 |
QSet<CRedlandRasqalSubQueryProcessingData*>* remainingSubQueryProcSet = redlandRasqalQueryProcData->getRemainingSubQueriesProcessingSet(); |
| 168 |
QSet<cint64>* processedSubQueryIdSet = redlandRasqalQueryProcData->getProcessedSubQueryIdSet(); |
| 169 |
QVector<CRedlandRasqalSubQueryProcessingData*>* subQueryProcDataVec = redlandRasqalQueryProcData->getSubQueryProcessingDataVector(); |
| 170 |
|
| 171 |
|
| 172 |
QSet<CRedlandRasqalSubQueryProcessingData*> withoutUnprocessedDependencySubQuerySet; |
| 173 |
QSet<CRedlandRasqalSubQueryProcessingData*> atleastOneProcessedDependencySubQuerySet; |
| 174 |
QSet<CRedlandRasqalSubQueryProcessingData*> remainingSubQuerySet; |
| 175 |
for (CRedlandRasqalSubQueryProcessingData* remainingSubQueryProc : *remainingSubQueryProcSet) { |
| 176 |
bool withoutUnprocessedDependency = true; |
| 177 |
bool atleastOneProcessedDependency = false; |
| 178 |
QSet<cint64>* depSet = remainingSubQueryProc->getDependentSubQueryIdSet(); |
| 179 |
if (!depSet->isEmpty()) { |
| 180 |
for (cint64 depSubQuery : *depSet) { |
| 181 |
if (!processedSubQueryIdSet->contains(depSubQuery)) { |
| 182 |
withoutUnprocessedDependency = false; |
| 183 |
} else { |
| 184 |
atleastOneProcessedDependency = false; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
if (withoutUnprocessedDependency) { |
| 189 |
withoutUnprocessedDependencySubQuerySet.insert(remainingSubQueryProc); |
| 190 |
} else if (atleastOneProcessedDependency) { |
| 191 |
atleastOneProcessedDependencySubQuerySet.insert(remainingSubQueryProc); |
| 192 |
} else { |
| 193 |
remainingSubQuerySet.insert(remainingSubQueryProc); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
QList<CRedlandRasqalSubQueryProcessingData*> subqueries; |
| 198 |
if (!withoutUnprocessedDependencySubQuerySet.isEmpty()) { |
| 199 |
for (CRedlandRasqalSubQueryProcessingData* subQueryData : withoutUnprocessedDependencySubQuerySet) { |
| 200 |
subqueries.append(subQueryData); |
| 201 |
} |
| 202 |
} |
| 203 |
if (subqueries.isEmpty() && !atleastOneProcessedDependencySubQuerySet.isEmpty()) { |
| 204 |
for (CRedlandRasqalSubQueryProcessingData* subQueryData : atleastOneProcessedDependencySubQuerySet) { |
| 205 |
subqueries.append(subQueryData); |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (subqueries.isEmpty()) { |
| 211 |
for (CRedlandRasqalSubQueryProcessingData* subQueryData : remainingSubQuerySet) { |
| 212 |
subqueries.append(subQueryData); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
QSet<cint64> tmpProcessedSubQueryIdSet(*processedSubQueryIdSet); |
| 218 |
QHash<CQuery*, CRedlandRasqalSubQueryProcessingData*>* queryDataHash = redlandRasqalQueryProcData->getSubQueryDataHash(); |
| 219 |
bool subQueryProcessing = false; |
| 220 |
for (CRedlandRasqalSubQueryProcessingData* subqueryData : subqueries) { |
| 221 |
CQuery* subquery = subqueryData->getSubQuery(); |
| 222 |
CComplexAssertionsIndividualVariablesAnsweringExtendibleQuery* extSubQuery = dynamic_cast<CComplexAssertionsIndividualVariablesAnsweringExtendibleQuery*>(subquery); |
| 223 |
if (extSubQuery) { |
| 224 |
CComplexQueryExtendibleData* extData = extSubQuery->getExtendibleData(); |
| 225 |
QSet<cint64>* depSet = subqueryData->getDependentSubQueryIdSet(); |
| 226 |
|
| 227 |
for (cint64 depQueryId : *depSet) { |
| 228 |
if (tmpProcessedSubQueryIdSet.contains(depQueryId)) { |
| 229 |
extData->addExtendingQuery((*subQueryProcDataVec)[depQueryId]->getSubQuery()); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
redlandRasqalQueryProcData->incSubQueryWaitingCount(); |
| 234 |
processedSubQueryIdSet->insert(subqueryData->getSubQueryId()); |
| 235 |
CRedlandRasqalSubQueryProcessingData* subQueryProcData = queryDataHash->value(subquery); |
| 236 |
remainingSubQueryProcSet->remove(subQueryProcData); |
| 237 |
CAnsweringMessageDataRequirementCompletedSubQueryAnswering* subQueryAnsweringCompMess = new CAnsweringMessageDataRequirementCompletedSubQueryAnswering(redlandRasqalQueryProcData, subquery, subQueryProcData->getSubQueryId()); |
| 238 |
subQueryProcessing |= processSubQuery(answererContext, subquery, redlandRasqalQueryProcData, subQueryAnsweringCompMess); |
| 239 |
} |
| 240 |
return subQueryProcessing; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::generateSubQueries(CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData, CAnswererContext* answererContext) { |
| 245 |
|
| 246 |
CRedlandRasqalBGPsCompositionQuery* redlandRasqalQuery = redlandRasqalQueryProcData->getRedlandRasqalQuery(); |
| 247 |
CConcreteOntology* queryBaseOnto = mOntoAnsweringItem->getOntology(); |
| 248 |
CConcreteOntology* queryExppOnto = redlandRasqalQuery->getExrpessionOntology(); |
| 249 |
CConfigurationBase* config = redlandRasqalQuery->getConfiguration(); |
| 250 |
QList<CQuery*> subqueries; |
| 251 |
cint64 nextSubQueryId = 0; |
| 252 |
QHash<QString, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData*> subqueryNameBGPHash; |
| 253 |
|
| 254 |
QHash<QString, TDependentOntologyPair>* queryKbNameDepOntPair = redlandRasqalQuery->getDependentKnowledgeBaseRevisionHash(); |
| 255 |
QHash<QString, TDependentOntologyPair> kbNameDepOntPair(*queryKbNameDepOntPair); |
| 256 |
kbNameDepOntPair.insert(queryBaseOnto->getOntologyName(), TDependentOntologyPair(queryBaseOnto, queryExppOnto)); |
| 257 |
|
| 258 |
QStringList missingKBNameList; |
| 259 |
for (QHash<QString, TDependentOntologyPair>::const_iterator it = kbNameDepOntPair.constBegin(), itEnd = kbNameDepOntPair.constEnd(); it != itEnd; ++it) { |
| 260 |
QString kbName = it.key(); |
| 261 |
TDependentOntologyPair depOntPair = it.value(); |
| 262 |
CConcreteOntology* baseOnto = depOntPair.first; |
| 263 |
CConcreteOntology* exppOnto = depOntPair.second; |
| 264 |
|
| 265 |
if (!baseOnto || !exppOnto) { |
| 266 |
missingKBNameList.append(kbName); |
| 267 |
} else { |
| 268 |
CConcreteOntologyUpdateSeparateHashingCollectorBuilder *builder = new CConcreteOntologyUpdateSeparateHashingCollectorBuilder(exppOnto); |
| 269 |
CConcreteOntologyQueryExtendedBuilder* queryBuilderGen = new CConcreteOntologyExtendibleQueryExtendedBuilder(baseOnto, exppOnto, config, builder); |
| 270 |
CSPARQLRedlandRasqalReasoningBGPQueryParser* bgpSubQueryParser = new CSPARQLRedlandRasqalReasoningBGPQueryParser(queryBuilderGen, builder, exppOnto, config); |
| 271 |
builder->initializeBuilding(); |
| 272 |
QHash<QString, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData*> tmpSubqueryNameBGPHash = bgpSubQueryParser->parseBGPReasoningSubQueries(redlandRasqalQuery->getRedlandWorld(), redlandRasqalQuery->getRedlandQuery(), redlandRasqalQuery->getRasqalQuery(), redlandRasqalQuery->getQueryText(), kbName, kbName == queryBaseOnto->getOntologyName(), nextSubQueryId); |
| 273 |
subqueryNameBGPHash.unite(tmpSubqueryNameBGPHash); |
| 274 |
builder->completeBuilding(); |
| 275 |
subqueries.append(queryBuilderGen->generateQuerys()); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (!missingKBNameList.isEmpty()) { |
| 280 |
if (missingKBNameList.size() > 1) { |
| 281 |
LOG(WARN, getDomain(), logTr("Ontologies '%1' referred in query are not available.").arg(missingKBNameList.join("', '")), this); |
| 282 |
} else { |
| 283 |
LOG(WARN, getDomain(), logTr("Ontology '%1' referred in query is not available.").arg(missingKBNameList.first()), this); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
QVector<CRedlandRasqalSubQueryProcessingData*>* subQueryProcDataVec = redlandRasqalQueryProcData->getSubQueryProcessingDataVector(); |
| 289 |
subQueryProcDataVec->resize(subqueries.size()); |
| 290 |
QHash<CQuery*, CRedlandRasqalSubQueryProcessingData*>* queryDataHash = redlandRasqalQueryProcData->getSubQueryDataHash(); |
| 291 |
QHash<cint64, CRedlandRasqalSubQueryProcessingData*>* idSubQueryDataHash = redlandRasqalQueryProcData->getIdSubQueryDataHash(); |
| 292 |
QSet<CRedlandRasqalSubQueryProcessingData*>* subQueryProcSet = redlandRasqalQueryProcData->getRemainingSubQueriesProcessingSet(); |
| 293 |
|
| 294 |
for (CQuery* subquery : subqueries) { |
| 295 |
const QString& queryName = subquery->getQueryName(); |
| 296 |
CComplexAnsweringExpressionQuery* compAnsSubQuery = (CComplexAnsweringExpressionQuery*)subquery; |
| 297 |
int subQueryId = queryName.mid(queryName.lastIndexOf("-") + 1).toInt(); |
| 298 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData = subqueryNameBGPHash.value(queryName); |
| 299 |
CRedlandRasqalSubQueryProcessingData* subQueryProcData = new CRedlandRasqalSubQueryProcessingData(subQueryId, compAnsSubQuery, bgpData->bgp, bgpData->tripleStartPos, bgpData->tripleEndPos, redlandRasqalQuery->getRedlandWorld()); |
| 300 |
if (bgpData->completelyHandledResultsTransfer) { |
| 301 |
CComplexAssertionsIndividualVariablesAnsweringQuery* compAssIndiVarAnsSubQuery = dynamic_cast<CComplexAssertionsIndividualVariablesAnsweringQuery*>(subquery); |
| 302 |
if (compAssIndiVarAnsSubQuery) { |
| 303 |
CRedlandRasqalBGPsCompositionQuery* query = redlandRasqalQueryProcData->getRedlandRasqalQuery(); |
| 304 |
CVariableBindingsAnswersStreamingResult* existBindsAnswersStreamingResult = dynamic_cast<CVariableBindingsAnswersStreamingResult*>(query->getQueryResult()); |
| 305 |
if (existBindsAnswersStreamingResult) { |
| 306 |
compAssIndiVarAnsSubQuery->setQueryResult(existBindsAnswersStreamingResult); |
| 307 |
redlandRasqalQueryProcData->setResultDirectlyFromSubQueryStreamed(true); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
*subQueryProcData->getDependentSubQueryIdSet() = bgpData->dependentSubQueryIds.toSet(); |
| 312 |
delete bgpData; |
| 313 |
(*subQueryProcDataVec)[subQueryId] = subQueryProcData; |
| 314 |
queryDataHash->insert(subquery, subQueryProcData); |
| 315 |
subQueryProcSet->insert(subQueryProcData); |
| 316 |
idSubQueryDataHash->insert(subQueryId, subQueryProcData); |
| 317 |
} |
| 318 |
|
| 319 |
if (!subqueries.isEmpty()) { |
| 320 |
LOG(INFO, getDomain(), logTr("Extracted '%1' sub-queries from BGPs of query '%2'.").arg(subqueries.size()).arg(redlandRasqalQuery->getQueryName()), this); |
| 321 |
} |
| 322 |
|
| 323 |
CRedlandRasqalModelSubQueryResolver* resolver = new CRedlandRasqalModelSubQueryResolver(subQueryProcDataVec, redlandRasqalQuery->getRedlandWorld(), redlandRasqalQuery->getRedlandQuery(), redlandRasqalQuery->getRasqalQuery(), queryKbNameDepOntPair); |
| 324 |
redlandRasqalQueryProcData->setResolver(resolver); |
| 325 |
|
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::processSubQuery(CAnswererContext* answererContext, CQuery* subQuery, CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData, CAnsweringMessageDataRequirementCompleted* completedMessage) { |
| 330 |
QString subQueryDepString; |
| 331 |
CComplexAssertionsIndividualVariablesAnsweringExtendibleQuery* extSubQuery = dynamic_cast<CComplexAssertionsIndividualVariablesAnsweringExtendibleQuery*>(subQuery); |
| 332 |
if (extSubQuery) { |
| 333 |
QList<CQuery*>* depQueries = extSubQuery->getExtendibleData()->getExtendingQueryList(); |
| 334 |
if (depQueries && !depQueries->isEmpty()) { |
| 335 |
QHash<CQuery*, CRedlandRasqalSubQueryProcessingData*>* queryDataHash = redlandRasqalQueryProcData->getSubQueryDataHash(); |
| 336 |
QStringList depQueryStringList; |
| 337 |
for (CQuery* depSubQuery : *depQueries) { |
| 338 |
CRedlandRasqalSubQueryProcessingData* depQueryData = queryDataHash->value(depSubQuery); |
| 339 |
if (depQueryData) { |
| 340 |
depQueryStringList += QString::number(depQueryData->getSubQueryId()); |
| 341 |
} |
| 342 |
} |
| 343 |
if (!depQueryStringList.isEmpty()) { |
| 344 |
if (depQueryStringList.size() > 1) { |
| 345 |
subQueryDepString = QString(", based upon computed sub-queries %1").arg(depQueryStringList.join(", ")); |
| 346 |
} else { |
| 347 |
subQueryDepString = QString(", based upon computed sub-query %1").arg(depQueryStringList.first()); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
} |
| 353 |
LOG(INFO, getDomain(), logTr("Scheduling processing of sub-query '%1'%2.").arg(subQuery->getQueryName()).arg(subQueryDepString), this); |
| 354 |
mOntoAnsweringItem->getAnsweringHandlingStatistics()->incRequestedRealizationRequirementCount(1); |
| 355 |
answererContext->getAnsweringCalculationHandler()->answerSubQuery(this, subQuery, completedMessage); |
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
void COptimizedComplexCompositionRedlandRasqalAnsweringHandler::processRasqal(CRedlandRasqalBGPsCompositionQuery* query, CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData) { |
| 361 |
LOG(INFO, getDomain(), logTr("Sub-queries of '%1' computed in %2 ms, starting answer resolving with Redland Rasqal.").arg(query->getQueryName()).arg(redlandRasqalQueryProcData->getSubQueryProcessingTime()), this); |
| 362 |
|
| 363 |
mCurrentProcessingRedlandRasqalQueryProcData = redlandRasqalQueryProcData; |
| 364 |
CRedlandRasqalModelSubQueryResolver* resolver = redlandRasqalQueryProcData->getResolver(); |
| 365 |
librdf_model* model = resolver->getRedlandResolvingModel(); |
| 366 |
|
| 367 |
|
| 368 |
QHash<QString, TDependentOntologyPair>* queryKbNameDepOntPair = query->getDependentKnowledgeBaseRevisionHash(); |
| 369 |
librdf_world* redlandWorld = query->getRedlandWorld(); |
| 370 |
raptor_world* raptorWorld = librdf_world_get_raptor(redlandWorld); |
| 371 |
rasqal_world* rasqalWorld = librdf_world_get_rasqal(redlandWorld); |
| 372 |
|
| 373 |
for (QHash<QString, TDependentOntologyPair>::const_iterator it = queryKbNameDepOntPair->constBegin(), itEnd = queryKbNameDepOntPair->constEnd(); it != itEnd; ++it) { |
| 374 |
QString kbName = it.key(); |
| 375 |
QByteArray kbNameData = kbName.toUtf8(); |
| 376 |
raptor_uri* source_uri = raptor_new_uri(raptorWorld, (const unsigned char*)kbNameData.constData()); |
| 377 |
rasqal_data_graph* dg = rasqal_new_data_graph_from_uri(rasqalWorld, source_uri, source_uri, RASQAL_DATA_GRAPH_NAMED, NULL, NULL, NULL); |
| 378 |
rasqal_query_add_data_graph(query->getRasqalQuery(), dg); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
librdf_query_results* results = librdf_model_query_execute(model, query->getRedlandQuery()); |
| 384 |
|
| 385 |
|
| 386 |
cint64 answerCount = 0; |
| 387 |
if (results && librdf_query_results_is_bindings(results)) { |
| 388 |
|
| 389 |
int bindCount = librdf_query_results_get_bindings_count(results); |
| 390 |
|
| 391 |
|
| 392 |
QStringList varList; |
| 393 |
bool answersWriteable = true; |
| 394 |
CVariableBindingsAnswersResult* bindsAnswersResult = nullptr; |
| 395 |
CVariableBindingsAnswersStreamingResult* existBindsAnswersStreamingResult = nullptr; |
| 396 |
|
| 397 |
CVariableBindingsQueryResultWriter resultWriter; |
| 398 |
|
| 399 |
const char **names = NULL; |
| 400 |
librdf_node** values = new librdf_node*[bindCount]; |
| 401 |
|
| 402 |
CVariableBindingsListAnswerResult* reuseBindAnsRes = nullptr; |
| 403 |
cint64 flushSize = 100; |
| 404 |
double flushSizeIncreasingFactor = 2.0; |
| 405 |
|
| 406 |
|
| 407 |
QByteArray plainLiteralBytes = QString(PREFIX_XML_PLAINLITERAL_DATATYPE).toUtf8(); |
| 408 |
librdf_uri *plainLiteralUri = librdf_new_uri(query->getRedlandWorld(), (const unsigned char*)plainLiteralBytes.constData()); |
| 409 |
|
| 410 |
|
| 411 |
QList<CVariableBindingStringResult*> reserveBindingsList; |
| 412 |
while (!librdf_query_results_finished(results) && answersWriteable) { |
| 413 |
|
| 414 |
if (librdf_query_results_get_bindings(results, &names, values)) |
| 415 |
break; |
| 416 |
|
| 417 |
if (names) { |
| 418 |
int i; |
| 419 |
|
| 420 |
if (!bindsAnswersResult) { |
| 421 |
for (i = 0; names[i]; i++) { |
| 422 |
varList.append(QString::fromUtf8(names[i])); |
| 423 |
} |
| 424 |
|
| 425 |
existBindsAnswersStreamingResult = dynamic_cast<CVariableBindingsAnswersStreamingResult*>(query->getQueryResult()); |
| 426 |
if (existBindsAnswersStreamingResult) { |
| 427 |
answersWriteable &= existBindsAnswersStreamingResult->initResult(varList); |
| 428 |
bindsAnswersResult = existBindsAnswersStreamingResult; |
| 429 |
|
| 430 |
reuseBindAnsRes = new CVariableBindingsListAnswerResult(); |
| 431 |
|
| 432 |
for (i = 0; names[i]; i++) { |
| 433 |
reserveBindingsList.append(new CVariableBindingStringResult()); |
| 434 |
} |
| 435 |
} else { |
| 436 |
bindsAnswersResult = new CVariableBindingsAnswersListResult(varList); |
| 437 |
query->setQueryResult(bindsAnswersResult); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
++answerCount; |
| 442 |
CVariableBindingsListAnswerResult* bindAnsRes = nullptr; |
| 443 |
if (!existBindsAnswersStreamingResult) { |
| 444 |
bindAnsRes = new CVariableBindingsListAnswerResult(); |
| 445 |
} else { |
| 446 |
bindAnsRes = reuseBindAnsRes; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
for (i = 0; names[i]; i++) { |
| 451 |
librdf_node* valueNode = values[i]; |
| 452 |
if (valueNode) { |
| 453 |
librdf_node_type type = librdf_node_get_type(valueNode); |
| 454 |
if (type == LIBRDF_NODE_TYPE_RESOURCE) { |
| 455 |
librdf_uri* uri = librdf_node_get_uri(valueNode); |
| 456 |
unsigned char* uristring = librdf_uri_as_string(uri); |
| 457 |
QString uriString = QString::fromUtf8((const char*)uristring); |
| 458 |
CVariableBindingStringResult* bindingResult = nullptr; |
| 459 |
if (!existBindsAnswersStreamingResult) { |
| 460 |
bindingResult = new CVariableBindingStringResult(uriString, CVariableBindingResult::VBTNAMEDINDIVIDUAL); |
| 461 |
bindAnsRes->addResultVariableBinding(bindingResult); |
| 462 |
} else { |
| 463 |
if (bindAnsRes->getResultCount() <= i) { |
| 464 |
bindingResult = reserveBindingsList.takeFirst(); |
| 465 |
bindingResult->initVariableBinding(uriString, CVariableBindingResult::VBTNAMEDINDIVIDUAL); |
| 466 |
bindAnsRes->addResultVariableBinding(bindingResult); |
| 467 |
} else { |
| 468 |
bindingResult = (CVariableBindingStringResult*)bindAnsRes->getResult(i); |
| 469 |
if (!bindingResult) { |
| 470 |
bindingResult = reserveBindingsList.takeFirst(); |
| 471 |
bindAnsRes->setResult(i, bindingResult); |
| 472 |
} |
| 473 |
bindingResult->initVariableBinding(uriString, CVariableBindingResult::VBTNAMEDINDIVIDUAL); |
| 474 |
} |
| 475 |
} |
| 476 |
} else if (type == LIBRDF_NODE_TYPE_LITERAL) { |
| 477 |
const char* literalValue = (const char*)librdf_node_get_literal_value(valueNode); |
| 478 |
librdf_uri* datatypeUri = librdf_node_get_literal_value_datatype_uri(valueNode); |
| 479 |
QString literalString = QString::fromUtf8((const char*)literalValue); |
| 480 |
|
| 481 |
QString literalDatatypeString = "\"" + literalString; |
| 482 |
if (!librdf_uri_equals(plainLiteralUri, datatypeUri)) { |
| 483 |
unsigned char* datatypeString = librdf_uri_as_string(datatypeUri); |
| 484 |
QString datatypeQString = QString::fromUtf8((const char*)datatypeString); |
| 485 |
literalDatatypeString += "\"^^" + datatypeQString; |
| 486 |
} else { |
| 487 |
literalDatatypeString += "\""; |
| 488 |
} |
| 489 |
|
| 490 |
CVariableBindingStringResult* bindingResult = nullptr; |
| 491 |
if (!existBindsAnswersStreamingResult) { |
| 492 |
bindingResult = new CVariableBindingStringResult(literalDatatypeString, CVariableBindingResult::VBTLITERAL); |
| 493 |
bindAnsRes->addResultVariableBinding(bindingResult); |
| 494 |
} else { |
| 495 |
if (bindAnsRes->getResultCount() <= i) { |
| 496 |
bindingResult = reserveBindingsList.takeFirst(); |
| 497 |
bindingResult->initVariableBinding(literalDatatypeString, CVariableBindingResult::VBTLITERAL); |
| 498 |
bindAnsRes->addResultVariableBinding(bindingResult); |
| 499 |
} else { |
| 500 |
bindingResult = (CVariableBindingStringResult*)bindAnsRes->getResult(i); |
| 501 |
if (!bindingResult) { |
| 502 |
bindingResult = reserveBindingsList.takeFirst(); |
| 503 |
bindAnsRes->setResult(i, bindingResult); |
| 504 |
} |
| 505 |
bindingResult->initVariableBinding(literalDatatypeString, CVariableBindingResult::VBTLITERAL); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
librdf_free_node(valueNode); |
| 510 |
} else { |
| 511 |
if (!existBindsAnswersStreamingResult) { |
| 512 |
bindAnsRes->addResultVariableBinding(nullptr); |
| 513 |
} else { |
| 514 |
if (bindAnsRes->getResultCount() <= i) { |
| 515 |
bindAnsRes->addResultVariableBinding(nullptr); |
| 516 |
} else { |
| 517 |
CVariableBindingStringResult* bindingResult = (CVariableBindingStringResult*)bindAnsRes->getResult(i); |
| 518 |
if (bindingResult) { |
| 519 |
reserveBindingsList.append(bindingResult); |
| 520 |
} |
| 521 |
bindAnsRes->setResult(i, nullptr); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if (!existBindsAnswersStreamingResult) { |
| 528 |
resultWriter.addVariableBindingAnswerToResult(bindsAnswersResult, bindAnsRes); |
| 529 |
} else { |
| 530 |
resultWriter.addReusedVariableBindingAnswerToResult(bindsAnswersResult, bindAnsRes); |
| 531 |
} |
| 532 |
|
| 533 |
} |
| 534 |
|
| 535 |
if (answerCount % flushSize == 0 && existBindsAnswersStreamingResult) { |
| 536 |
answersWriteable &= existBindsAnswersStreamingResult->flushResults(); |
| 537 |
flushSize = flushSize * flushSizeIncreasingFactor; |
| 538 |
} |
| 539 |
|
| 540 |
librdf_query_results_next(results); |
| 541 |
} |
| 542 |
delete[] values; |
| 543 |
if (existBindsAnswersStreamingResult) { |
| 544 |
delete reuseBindAnsRes; |
| 545 |
} |
| 546 |
|
| 547 |
} else if (results && librdf_query_results_is_boolean(results)) { |
| 548 |
++answerCount; |
| 549 |
CBooleanQueryResult* boolRes = nullptr; |
| 550 |
int result = librdf_query_results_get_boolean(results); |
| 551 |
if (result > 0) { |
| 552 |
boolRes = new CBooleanQueryResult(); |
| 553 |
boolRes->setResult(true); |
| 554 |
} else if (result == 0) { |
| 555 |
boolRes = new CBooleanQueryResult(); |
| 556 |
boolRes->setResult(false); |
| 557 |
} |
| 558 |
if (boolRes) { |
| 559 |
query->setQueryResult(boolRes); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
if (results) { |
| 564 |
librdf_free_query_results(results); |
| 565 |
} |
| 566 |
librdf_free_query(query->getRedlandQuery()); |
| 567 |
|
| 568 |
redlandRasqalQueryProcData->setProcessingFinished(); |
| 569 |
cint64 processingTime = redlandRasqalQueryProcData->getTotalProcessingTime(); |
| 570 |
|
| 571 |
QString realReqString; |
| 572 |
CAnsweringHandlingStatistics* stats = mOntoAnsweringItem->getAnsweringHandlingStatistics(); |
| 573 |
if (stats) { |
| 574 |
CAnsweringHandlingStatistics* initialStats = redlandRasqalQueryProcData->getQueryProcessingInitializedAnsweringStatistics(); |
| 575 |
if (initialStats) { |
| 576 |
cint64 reqRealReqCount = stats->getRequestedRealizationRequirementCount() - initialStats->getRequestedRealizationRequirementCount(); |
| 577 |
if (reqRealReqCount > 0) { |
| 578 |
realReqString += QString(", using %1 reasoning-based sub-queries").arg(reqRealReqCount); |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
LOG(INFO, getDomain(), logTr("Complex query processing with Redland Rasqal determined %3 answers in %1 ms%2.").arg(processingTime).arg(realReqString).arg(answerCount), this); |
| 585 |
if (mOntoAnsweringItem->isLogProcessingFinishedStatisticsConfigured()) { |
| 586 |
QString statusString = redlandRasqalQueryProcData->getResolver()->getIterationStatusString(); |
| 587 |
LOG(INFO, getDomain(), statusString, this); |
| 588 |
} |
| 589 |
|
| 590 |
} |
| 591 |
|
| 592 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::processRequirementMessage(CAnsweringMessageData* message, CAnswererContext* answererContext) { |
| 593 |
while (message) { |
| 594 |
CAnsweringMessageData::ANSWERINGMESSAGEDATA messageType = message->getAnsweringMessageDataType(); |
| 595 |
if (messageType == CAnsweringMessageData::REQUIREMENTCOMPLETED) { |
| 596 |
CAnsweringMessageDataRequirementCompleted* reqCompMessage = (CAnsweringMessageDataRequirementCompleted*)message; |
| 597 |
CAnsweringMessageDataRequirementCompleted::REQUIREMENTTYPE reqType = reqCompMessage->getRequirementType(); |
| 598 |
if (reqType == CAnsweringMessageDataRequirementCompletedSubQueryAnswering::SUBQUERYANSWERINGREQUIREMENT) { |
| 599 |
CAnsweringMessageDataRequirementCompletedSubQueryAnswering* compSubQueryAnsReqMess = (CAnsweringMessageDataRequirementCompletedSubQueryAnswering*)reqCompMessage; |
| 600 |
CComplexQueryCompositionRedlandRasqalProcessingData* redlandRasqalQueryProcData = (CComplexQueryCompositionRedlandRasqalProcessingData*)compSubQueryAnsReqMess->getProcessingData(); |
| 601 |
redlandRasqalQueryProcData->decSubQueryWaitingCount(); |
| 602 |
if (!redlandRasqalQueryProcData->isSubQueryWaiting()) { |
| 603 |
mOntoAnsweringItem->addScheduledQueriesProcessingData(redlandRasqalQueryProcData); |
| 604 |
if (!redlandRasqalQueryProcData->hasRemainingSubQueriesProcessing()) { |
| 605 |
redlandRasqalQueryProcData->setSubQueryProcessingFinished(true); |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
message = message->getNext(); |
| 611 |
} |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
bool COptimizedComplexCompositionRedlandRasqalAnsweringHandler::isAnsweringFinished(CAnswererContext* answererContext) { |
| 620 |
return !mOntoAnsweringItem->isCurrentlyAnsweringQueryCount(); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
CAnsweringProgress* COptimizedComplexCompositionRedlandRasqalAnsweringHandler::getAnsweringProgress() { |
| 629 |
if (mCurrentProcessingRedlandRasqalQueryProcData && mOntoAnsweringItem->isLogProcessingProgressStatisticsConfigured()) { |
| 630 |
QString statusString = mCurrentProcessingRedlandRasqalQueryProcData->getResolver()->getIterationStatusString(); |
| 631 |
LOG(INFO, getDomain(), statusString, this); |
| 632 |
} |
| 633 |
return COptimizedComplexCompositionAnsweringHandler::getAnsweringProgress(); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
}; // end namespace Answerer |
| 644 |
|
| 645 |
}; // end namespace Reasoner |
| 646 |
|
| 647 |
}; // end namespace Konclude |
| 648 |
|
| 649 |
#endif // KONCLUDE_REDLAND_INTEGRATION |