| 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 "CCompletionGraphRandomWalkQueryGenerator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Kernel { |
| 30 |
|
| 31 |
namespace Algorithm { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
CCompletionGraphRandomWalkQueryGenerator::CCompletionGraphRandomWalkQueryGenerator() : CLogIdentifier("::Konclude::Test::CompletionGraphRandomWalkQueryGenerator", this) { |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
CCompletionGraphRandomWalkQueryGenerator::~CCompletionGraphRandomWalkQueryGenerator() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
void CCompletionGraphRandomWalkQueryGenerator::generateQueries(CSatisfiableCalculationTask* satCalcTask) { |
| 47 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 48 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 49 |
bool treefoldedCycleQueries = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.TreeFoldedCylceQueries"); |
| 50 |
bool realCycleQueries = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.RealCycleQueries"); |
| 51 |
bool treeShapedQueries = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.TreeShapedQueries"); |
| 52 |
cint64 queryGenerationCount = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.MaximumQueryGenerationCount"); |
| 53 |
cint64 maxOntologyQueryCount = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.MaximumOntologyQueryCount"); |
| 54 |
|
| 55 |
while (queryGenerationCount > 0 && getQueryForOntologyCount(satCalcTask) < maxOntologyQueryCount) { |
| 56 |
if (treefoldedCycleQueries && queryGenerationCount > 0 && getQueryForOntologyCount(satCalcTask) < maxOntologyQueryCount) { |
| 57 |
queryGenerationCount--; |
| 58 |
generateTreeFoldedCycleQuery(satCalcTask); |
| 59 |
} |
| 60 |
if (realCycleQueries && queryGenerationCount > 0 && getQueryForOntologyCount(satCalcTask) < maxOntologyQueryCount) { |
| 61 |
queryGenerationCount--; |
| 62 |
generateRealCycleQuery(satCalcTask); |
| 63 |
} |
| 64 |
} |
| 65 |
LOG(INFO, getLogDomain(), logTr("Generated %1 real-cycle queries, %2 failed extractions").arg(mRealCycleQueryGenerationCount).arg(mRealCycleQueryFailedExtractionCount), this); |
| 66 |
LOG(INFO, getLogDomain(), logTr("Generated %1 tree-folded-cycle queries, %2 failed extractions").arg(mTreeFoldedCycleQueryGenerationCount).arg(mTreeFoldedCycleQueryFailedExtractionCount), this); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
CIndividualProcessNode* CCompletionGraphRandomWalkQueryGenerator::getUpToDateIndividual(cint64 indiId, CSatisfiableCalculationTask* satCalcTask) { |
| 73 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 74 |
CIndividualProcessNodeVector* indiNodeVec = processingDataBox->getIndividualProcessNodeVector(); |
| 75 |
return indiNodeVec->getData(indiId); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
QList<cint64> CCompletionGraphRandomWalkQueryGenerator::getShortestPathBetweenNodes(cint64 nodeId1, cint64 nodeId2, CSatisfiableCalculationTask* satCalcTask) { |
| 82 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 83 |
CIndividualProcessNodeVector* indiNodeVec = processingDataBox->getIndividualProcessNodeVector(); |
| 84 |
|
| 85 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 86 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 87 |
|
| 88 |
class ProcessingData { |
| 89 |
public: |
| 90 |
cint64 indiId = 0; |
| 91 |
QList<cint64> visitedIdList; |
| 92 |
|
| 93 |
ProcessingData(cint64 newIndiId, QList<cint64> prevVisitedIdList) { |
| 94 |
indiId = newIndiId; |
| 95 |
visitedIdList = prevVisitedIdList; |
| 96 |
visitedIdList.append(newIndiId); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
QSet<cint64> visitedNodeSet; |
| 101 |
QList<ProcessingData> visitingNodeList; |
| 102 |
QList<ProcessingData> pathList; |
| 103 |
|
| 104 |
visitedNodeSet.insert(nodeId1); |
| 105 |
if (nodeId1 != nodeId2) { |
| 106 |
visitingNodeList.append(ProcessingData(nodeId1, QList<cint64>())); |
| 107 |
} else { |
| 108 |
pathList.append(ProcessingData(nodeId1, QList<cint64>())); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
while (!visitingNodeList.isEmpty()) { |
| 113 |
ProcessingData procData = visitingNodeList.takeFirst(); |
| 114 |
CIndividualProcessNode* testingNode = getUpToDateIndividual(procData.indiId, satCalcTask); |
| 115 |
|
| 116 |
CSuccessorIterator succIt = testingNode->getSuccessorIterator(); |
| 117 |
while (succIt.hasNext()) { |
| 118 |
cint64 succIndiID = succIt.nextIndividualID(true); |
| 119 |
CIndividualProcessNode* succIndiNode = getUpToDateIndividual(succIndiID, satCalcTask); |
| 120 |
|
| 121 |
if (!succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || succIndiNode->getNominalIndividual())) { |
| 122 |
|
| 123 |
ProcessingData succProcData(succIndiID, procData.visitedIdList); |
| 124 |
|
| 125 |
if (succIndiID == nodeId2) { |
| 126 |
pathList.append(succProcData); |
| 127 |
break; |
| 128 |
} else if (!visitedNodeSet.contains(succIndiID)) { |
| 129 |
visitedNodeSet.insert(succIndiID); |
| 130 |
visitingNodeList.append(succProcData); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (!pathList.isEmpty()) { |
| 137 |
return pathList.first().visitedIdList; |
| 138 |
} |
| 139 |
|
| 140 |
return QList<cint64>(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
QList<cint64> CCompletionGraphRandomWalkQueryGenerator::getRandomizedPathBetweenNodes(QList<cint64> pathList, CSatisfiableCalculationTask* satCalcTask) { |
| 146 |
//srand(time(NULL)); |
| 147 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 148 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 149 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 150 |
|
| 151 |
QList<cint64> randomizedPathList(pathList); |
| 152 |
cint64 maxPathSize = 3; |
| 153 |
cint64 expansionCount = 0; |
| 154 |
if (maxPathSize - pathList.size() > 0) { |
| 155 |
expansionCount = (rand() % (maxPathSize - pathList.size())) + pathList.size(); |
| 156 |
} |
| 157 |
|
| 158 |
for (cint64 i = 0; i < expansionCount; ++i) { |
| 159 |
cint64 index = rand() % randomizedPathList.size(); |
| 160 |
cint64 indiId = randomizedPathList.at(index); |
| 161 |
CIndividualProcessNode* testingNode = getUpToDateIndividual(indiId, satCalcTask); |
| 162 |
CSuccessorIterator succIt = testingNode->getSuccessorIterator(); |
| 163 |
QList<cint64> succIndiIds; |
| 164 |
while (succIt.hasNext()) { |
| 165 |
cint64 succIndiID = succIt.nextIndividualID(true); |
| 166 |
if (succIndiID != indiId) { |
| 167 |
CIndividualProcessNode* succNode = getUpToDateIndividual(succIndiID, satCalcTask); |
| 168 |
if (!succNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !succNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || succNode->getNominalIndividual())) { |
| 169 |
succIndiIds.append(succIndiID); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
if (!succIndiIds.isEmpty()) { |
| 174 |
cint64 succExpIndex = rand() % succIndiIds.size(); |
| 175 |
cint64 expandingSuccIndiId = succIndiIds.at(succExpIndex); |
| 176 |
randomizedPathList.insert(index + 1, indiId); |
| 177 |
randomizedPathList.insert(index + 1, expandingSuccIndiId); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return randomizedPathList; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
bool CCompletionGraphRandomWalkQueryGenerator::generateTreeFoldedCycleQuery(CSatisfiableCalculationTask* satCalcTask) { |
| 187 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 188 |
CIndividualProcessNodeVector* indiNodeVec = processingDataBox->getIndividualProcessNodeVector(); |
| 189 |
cint64 indiStart = indiNodeVec->getItemMinIndex(); |
| 190 |
cint64 indiEnd = indiNodeVec->getItemMaxIndex(); |
| 191 |
|
| 192 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 193 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 194 |
|
| 195 |
cint64 treeFoldedQueryGenerationTryCount = mTreeFoldedCycleQueryGenerationTryCount; |
| 196 |
|
| 197 |
while (treeFoldedQueryGenerationTryCount-- > 0 && (indiEnd - indiStart) > 0) { |
| 198 |
cint64 indiIdx = (rand() % (indiEnd - indiStart)) + indiStart; |
| 199 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(indiIdx, satCalcTask); |
| 200 |
|
| 201 |
if (indiNode && !indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || indiNode->getNominalIndividual())) { |
| 202 |
|
| 203 |
|
| 204 |
QHash<QPair<cint64, cint64>, QString> indiVarIdNameHash; |
| 205 |
QHash<QString, cint64> statisticsHash = getInitialStatisticsHash(satCalcTask); |
| 206 |
statisticsHash["RequestedConjunctiveQueryTreeFoldedCycle"]++; |
| 207 |
|
| 208 |
|
| 209 |
cint64 nextVariableId = 0; |
| 210 |
cint64 nextTreeExtensionNr = 0; |
| 211 |
|
| 212 |
QHash<cint64, cint64> variableNodeIdHash; |
| 213 |
QHash<cint64, QList<cint64>> variableNeighboursHash; |
| 214 |
variableNodeIdHash[nextVariableId++] = indiIdx; |
| 215 |
|
| 216 |
cint64 cylceCount = (rand() % 4) + 1; |
| 217 |
bool queryExtractionFailed = false; |
| 218 |
|
| 219 |
for (cint64 i = 0; i < cylceCount && !queryExtractionFailed; ++i) { |
| 220 |
|
| 221 |
cint64 varId1 = rand() % variableNodeIdHash.size(); |
| 222 |
cint64 varId2 = rand() % variableNodeIdHash.size(); |
| 223 |
|
| 224 |
cint64 indiId1 = variableNodeIdHash[varId1]; |
| 225 |
cint64 indiId2 = variableNodeIdHash[varId2]; |
| 226 |
|
| 227 |
QList<cint64> path = getShortestPathBetweenNodes(indiId1, indiId2, satCalcTask); |
| 228 |
if (path.size() >= 1) { |
| 229 |
statisticsHash["RequestedConjunctiveQueryCycleCount"]++; |
| 230 |
path = getRandomizedPathBetweenNodes(path, satCalcTask); |
| 231 |
|
| 232 |
path.removeFirst(); |
| 233 |
if (!path.isEmpty()) { |
| 234 |
path.removeLast(); |
| 235 |
} |
| 236 |
|
| 237 |
cint64 lastNodeId = indiId1; |
| 238 |
cint64 lastVarId = varId1; |
| 239 |
for (cint64 nodeId : path) { |
| 240 |
cint64 nextVarId = nextVariableId++; |
| 241 |
variableNodeIdHash[nextVarId] = nodeId; |
| 242 |
variableNeighboursHash[lastVarId].append(nextVarId); |
| 243 |
lastVarId = nextVarId; |
| 244 |
lastNodeId = nodeId; |
| 245 |
} |
| 246 |
variableNeighboursHash[lastVarId].append(varId2); |
| 247 |
} else { |
| 248 |
queryExtractionFailed = true; |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (variableNodeIdHash.size() <= 1) { |
| 256 |
queryExtractionFailed = true; |
| 257 |
} |
| 258 |
|
| 259 |
QHash<QString, cint64> varQueryUseCount; |
| 260 |
|
| 261 |
QStringList queryTerms; |
| 262 |
QSet<QString> queryTermSet; |
| 263 |
QSet<QString> selectableVarSet; |
| 264 |
QSet<QString> queryParameters; |
| 265 |
for (QHash<cint64, QList<cint64>>::const_iterator it = variableNeighboursHash.constBegin(), itEnd = variableNeighboursHash.constEnd(); it != itEnd && !queryExtractionFailed; ++it) { |
| 266 |
|
| 267 |
cint64 varId = it.key(); |
| 268 |
QList<cint64> linkedVarIdList = it.value(); |
| 269 |
|
| 270 |
cint64 indiId = variableNodeIdHash[varId]; |
| 271 |
QString indiVar = getIndividualNodeQueryVariable(indiId, varId, indiVarIdNameHash, statisticsHash, satCalcTask); |
| 272 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(indiId, satCalcTask); |
| 273 |
|
| 274 |
if (indiNode && indiNode->getNominalIndividual()) { |
| 275 |
selectableVarSet.insert(indiVar); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
generateConceptTerms(indiNode, indiVar, queryParameters, queryTermSet, queryTerms, statisticsHash, satCalcTask); |
| 281 |
|
| 282 |
|
| 283 |
generateTreeExpansion(indiNode, indiVar, queryParameters, queryTermSet, queryTerms, statisticsHash, nextTreeExtensionNr, satCalcTask); |
| 284 |
|
| 285 |
|
| 286 |
for (cint64 linkedVarId : linkedVarIdList) { |
| 287 |
|
| 288 |
cint64 linkedIndiId = variableNodeIdHash[linkedVarId]; |
| 289 |
QString linkedIndiVar = getIndividualNodeQueryVariable(linkedIndiId, linkedVarId, indiVarIdNameHash, statisticsHash, satCalcTask); |
| 290 |
|
| 291 |
|
| 292 |
if (!generateRoleTerm(indiNode, indiVar, getUpToDateIndividual(linkedIndiId, satCalcTask), linkedIndiVar, queryParameters, queryTermSet, queryTerms, varQueryUseCount, statisticsHash, satCalcTask)) { |
| 293 |
queryExtractionFailed = true; |
| 294 |
break; |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
bool hasAllIndisAtleastTwice = true; |
| 302 |
for (cint64 usedCount : varQueryUseCount) { |
| 303 |
if (usedCount <= 1) { |
| 304 |
hasAllIndisAtleastTwice = false; |
| 305 |
queryExtractionFailed = true; |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if (!queryExtractionFailed) { |
| 312 |
|
| 313 |
if (!writeQuery(queryTerms, "tree-folded-cycle", selectableVarSet.toList(), queryParameters.toList(), statisticsHash, satCalcTask)) { |
| 314 |
queryExtractionFailed = true; |
| 315 |
} else { |
| 316 |
mTreeFoldedCycleQueryGenerationCount++; |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
if (queryExtractionFailed) { |
| 322 |
mTreeFoldedCycleQueryFailedExtractionCount++; |
| 323 |
} |
| 324 |
} else { |
| 325 |
mTreeFoldedCycleQueryFailedExtractionCount++; |
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
cint64 CCompletionGraphRandomWalkQueryGenerator::getQueryForOntologyCount(CSatisfiableCalculationTask* satCalcTask) { |
| 338 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 339 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 340 |
|
| 341 |
QString queryFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.QueryFileBasePath"); |
| 342 |
QString ontologyFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.OntologyFileBasePath"); |
| 343 |
|
| 344 |
QStringList loadedOntologyIris = processingDataBox->getOntology()->getLoadData()->getLoadedOntologyIRIs(); |
| 345 |
QString loadedOntologyIri; |
| 346 |
if (!loadedOntologyIris.isEmpty()) { |
| 347 |
loadedOntologyIri = loadedOntologyIris.first(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
loadedOntologyIri = loadedOntologyIri.replace(ontologyFileBasePath, ""); |
| 352 |
QStringList loadedOntologyFilePathElements = loadedOntologyIri.split("/"); |
| 353 |
QString queryDestPath = queryFileBasePath + loadedOntologyFilePathElements.join("/"); |
| 354 |
|
| 355 |
QDir queryDestDir(queryDestPath); |
| 356 |
cint64 queryfileCount = queryDestDir.entryList(QDir::Files).size(); |
| 357 |
return queryfileCount; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
bool CCompletionGraphRandomWalkQueryGenerator::writeQueryStatistics(const QString& ontologyFileName, const QString& queryFileName, QHash<QString, cint64>& statisticsHash, CSatisfiableCalculationTask* satCalcTask) { |
| 364 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 365 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 366 |
QString ontologyStatisticsFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.OntologyStatisticsFileBasePath"); |
| 367 |
QString queryStatisticsFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.QueryStatisticsFileBasePath"); |
| 368 |
|
| 369 |
QStringList loadedOntologyFilePathElements = ontologyFileName.split("/"); |
| 370 |
QString queryStatisticsDestPath = queryStatisticsFileBasePath + loadedOntologyFilePathElements.join("/"); |
| 371 |
QDir currDir; |
| 372 |
currDir.mkpath(queryStatisticsDestPath); |
| 373 |
|
| 374 |
QString ontStatsString; |
| 375 |
QFile ontStatFile(ontologyStatisticsFileBasePath + ontologyFileName + ".txt"); |
| 376 |
if (ontStatFile.open(QIODevice::ReadOnly)) { |
| 377 |
ontStatsString = ontStatFile.readAll(); |
| 378 |
ontStatFile.close(); |
| 379 |
} |
| 380 |
|
| 381 |
QFile queryStatFile(queryStatisticsDestPath + "/" + queryFileName + ".txt"); |
| 382 |
if (queryStatFile.open(QIODevice::WriteOnly)) { |
| 383 |
queryStatFile.write(ontStatsString.toUtf8()); |
| 384 |
|
| 385 |
for (QHash<QString, cint64>::const_iterator it = statisticsHash.constBegin(), itEnd = statisticsHash.constEnd(); it != itEnd; ++it) { |
| 386 |
QString statName = it.key(); |
| 387 |
cint64 statValue = it.value(); |
| 388 |
QString writeString = QString("%1:\t%2\r\n").arg(statName).arg(statValue); |
| 389 |
queryStatFile.write(writeString.toUtf8()); |
| 390 |
} |
| 391 |
|
| 392 |
queryStatFile.close(); |
| 393 |
return true; |
| 394 |
} |
| 395 |
return false; |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
bool CCompletionGraphRandomWalkQueryGenerator::writeQuery(const QStringList& queryTerms, const QString& queryStructureString, QList<QString> selectableVariables, const QList<QString>& queryParameters, QHash<QString, cint64>& statisticsHash, CSatisfiableCalculationTask* satCalcTask) { |
| 402 |
|
| 403 |
|
| 404 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 405 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 406 |
|
| 407 |
|
| 408 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 409 |
QString queryFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.QueryFileBasePath"); |
| 410 |
QString ontologyFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.OntologyFileBasePath"); |
| 411 |
QString queryTemplateFileName = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.QueryTemplateFile"); |
| 412 |
QString loadFileBasePath = CConfigDataReader::readConfigString(config, "Konclude.Test.ConjunctiveQueryGeneration.LoadFileBasePath"); |
| 413 |
|
| 414 |
|
| 415 |
QStringList loadedOntologyIris = processingDataBox->getOntology()->getLoadData()->getLoadedOntologyIRIs(); |
| 416 |
QString loadedOntologyIri; |
| 417 |
if (!loadedOntologyIris.isEmpty()) { |
| 418 |
loadedOntologyIri = loadedOntologyIris.first(); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
loadedOntologyIri = loadedOntologyIri.replace(ontologyFileBasePath, ""); |
| 423 |
|
| 424 |
|
| 425 |
QStringList loadedOntologyFilePathElements = loadedOntologyIri.split("/"); |
| 426 |
QString ontologyName = loadedOntologyFilePathElements.last(); |
| 427 |
ontologyName = ontologyName.replace(".owl.fss", ""); |
| 428 |
ontologyName = ontologyName.replace(".owl.xml", ""); |
| 429 |
ontologyName = ontologyName.replace(".owl", ""); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
QString queryTemplateString; |
| 436 |
QFile templateFile(queryTemplateFileName); |
| 437 |
if (templateFile.open(QIODevice::ReadOnly)) { |
| 438 |
queryTemplateString = templateFile.readAll(); |
| 439 |
templateFile.close(); |
| 440 |
} |
| 441 |
queryTemplateString = queryTemplateString.replace("$QUERYTERMS", queryTerms.join("\r\n")); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
queryTemplateString = queryTemplateString.replace("$QUERYONTOLOGYLOAD", loadFileBasePath + loadedOntologyIri); |
| 446 |
|
| 447 |
|
| 448 |
QString queryDestPath = queryFileBasePath + loadedOntologyFilePathElements.join("/"); |
| 449 |
|
| 450 |
QDir currDir; |
| 451 |
currDir.mkpath(queryDestPath); |
| 452 |
|
| 453 |
|
| 454 |
QString queryTypeString; |
| 455 |
cint64 ontologyQueryNr = getQueryForOntologyCount(satCalcTask); |
| 456 |
|
| 457 |
QString querySelectionString = "ASK"; |
| 458 |
queryTypeString = "ASK"; |
| 459 |
cint64 minVarSelCount = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.MinimalVariableSelectCount"); |
| 460 |
cint64 maxVarSelCount = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.MaximumVariableSelectCount"); |
| 461 |
if (maxVarSelCount > 0) { |
| 462 |
queryTypeString = "SELECT"; |
| 463 |
querySelectionString = "SELECT "; |
| 464 |
QStringList selectVarStringList; |
| 465 |
maxVarSelCount = qMin(maxVarSelCount, (cint64)selectableVariables.size()); |
| 466 |
cint64 selectTermsCount = ((rand() % (maxVarSelCount - minVarSelCount)) + minVarSelCount); |
| 467 |
for (cint64 i = 0; i < selectTermsCount; ++i) { |
| 468 |
QString varName; |
| 469 |
cint64 nextVarPos = rand() % selectableVariables.size(); |
| 470 |
|
| 471 |
while (nextVarPos-- > 0) { |
| 472 |
varName = selectableVariables.takeFirst(); |
| 473 |
selectableVariables.append(varName); |
| 474 |
} |
| 475 |
varName = selectableVariables.takeFirst(); |
| 476 |
selectVarStringList.append(varName); |
| 477 |
statisticsHash["RequestedConjunctiveQuerySelectedVariableCount"]++; |
| 478 |
|
| 479 |
|
| 480 |
} |
| 481 |
querySelectionString += selectVarStringList.join(" ") + "\r\nWHERE"; |
| 482 |
} |
| 483 |
|
| 484 |
queryTemplateString = queryTemplateString.replace("$QUERYSELECTION", querySelectionString); |
| 485 |
|
| 486 |
QList<QString> queryPars = queryParameters; |
| 487 |
qSort(queryPars.begin(), queryPars.end()); |
| 488 |
QString parameters = queryPars.join("-and-"); |
| 489 |
|
| 490 |
QString queryFileName = QString("%1--query-nr-%5-%2-%3-%4.sparql").arg(ontologyName).arg(queryStructureString).arg(parameters).arg(queryTypeString).arg(ontologyQueryNr + 1); |
| 491 |
QString queryPathFileName = QString("%1/%2").arg(queryDestPath).arg(queryFileName); |
| 492 |
QFile queryFile(queryPathFileName); |
| 493 |
if (queryFile.open(QIODevice::WriteOnly)) { |
| 494 |
QByteArray data = queryTemplateString.toUtf8(); |
| 495 |
queryFile.write(data); |
| 496 |
queryFile.close(); |
| 497 |
|
| 498 |
writeQueryStatistics(loadedOntologyIri, queryFileName, statisticsHash, satCalcTask); |
| 499 |
return true; |
| 500 |
} else { |
| 501 |
return false; |
| 502 |
} |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
bool CCompletionGraphRandomWalkQueryGenerator::generateRoleTerm(CIndividualProcessNode* indiNode1, QString indiVar1, CIndividualProcessNode* indiNode2, QString indiVar2, QSet<QString>& queryParameters, QSet<QString>& queryTermSet, QStringList& queryTerms, QHash<QString, cint64>& varQueryUseCount, QHash<QString, cint64>& statisticsHash, CSatisfiableCalculationTask* satCalcTask) { |
| 510 |
|
| 511 |
CSuccessorRoleIterator succRoleIt = indiNode1->getSuccessorRoleIterator(indiNode2->getIndividualNodeID()); |
| 512 |
bool foundRole = false; |
| 513 |
QList<QPair<QString, bool>> roleInverseList; |
| 514 |
while (succRoleIt.hasNext()) { |
| 515 |
CIndividualLinkEdge* link = succRoleIt.next(true); |
| 516 |
CRole* role = link->getLinkRole(); |
| 517 |
if (role->getRoleTag() != 1) { |
| 518 |
if (role->getPropertyNameLinker()) { |
| 519 |
QString roleName = CIRIName::getRecentIRIName(role->getPropertyNameLinker()); |
| 520 |
roleInverseList.append(QPair<QString, bool>(roleName, false)); |
| 521 |
foundRole = true; |
| 522 |
} else if (role->getInverseRole() && role->getInverseRole()->getPropertyNameLinker()) { |
| 523 |
QString roleName = CIRIName::getRecentIRIName(role->getInverseRole()->getPropertyNameLinker()); |
| 524 |
roleInverseList.append(QPair<QString, bool>(roleName, true)); |
| 525 |
foundRole = true; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
if (foundRole) { |
| 532 |
|
| 533 |
cint64 roleId = rand() % roleInverseList.size(); |
| 534 |
QString roleName = roleInverseList.at(roleId).first; |
| 535 |
bool inversed = roleInverseList.at(roleId).second; |
| 536 |
|
| 537 |
QString queryTermString; |
| 538 |
if (!inversed) { |
| 539 |
queryTermString = QString(" %1 <%2> %3 . ").arg(indiVar1).arg(roleName).arg(indiVar2); |
| 540 |
} else { |
| 541 |
queryTermString = QString(" %1 <%2> %3 . ").arg(indiVar2).arg(roleName).arg(indiVar1); |
| 542 |
} |
| 543 |
if (!queryTermSet.contains(queryTermString)) { |
| 544 |
statisticsHash["RequestedConjunctiveQueryRoleTermCount"]++; |
| 545 |
statisticsHash["RequestedConjunctiveQueryTermCount"]++; |
| 546 |
queryTermSet.insert(queryTermString); |
| 547 |
|
| 548 |
varQueryUseCount[indiVar1]++; |
| 549 |
varQueryUseCount[indiVar2]++; |
| 550 |
|
| 551 |
queryTerms.append(queryTermString); |
| 552 |
} |
| 553 |
return true; |
| 554 |
} else { |
| 555 |
return false; |
| 556 |
} |
| 557 |
|
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
void CCompletionGraphRandomWalkQueryGenerator::generateConceptTerms(CIndividualProcessNode* indiNode, QString indiVar, QSet<QString>& queryParameters, QSet<QString>& queryTermSet, QStringList& queryTerms, QHash<QString, cint64>& statisticsHash, CSatisfiableCalculationTask* satCalcTask) { |
| 564 |
|
| 565 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 566 |
bool generateConceptTerms = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.GenerateConceptTerms"); |
| 567 |
|
| 568 |
if (generateConceptTerms) { |
| 569 |
CReapplyConceptLabelSet* conSet = indiNode->getReapplyConceptLabelSet(false); |
| 570 |
QList<QString> conceptList; |
| 571 |
auto conSetIt = conSet->getConceptLabelSetIterator(false, false, false); |
| 572 |
while (conSetIt.hasNext()) { |
| 573 |
CConceptDescriptor* conDes = conSetIt.getConceptDescriptor(); |
| 574 |
CConcept* concept = conDes->getConcept(); |
| 575 |
if (concept->getOperatorCode() == CCATOM || concept->getOperatorCode() == CCNONE || concept->getOperatorCode() == CCSUB || concept->getOperatorCode() == CCEQ) { |
| 576 |
if (!conDes->getNegation()) { |
| 577 |
QString conceptName = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 578 |
conceptList.append(conceptName); |
| 579 |
} |
| 580 |
} |
| 581 |
conSetIt.moveNext(); |
| 582 |
} |
| 583 |
queryParameters.insert("with-concept-terms"); |
| 584 |
cint64 conTerms = ((rand() % 200) + 50) / 100; |
| 585 |
for (cint64 i = 0; i < conTerms && conceptList.size() > 0; ++i) { |
| 586 |
cint64 conId = rand() % conceptList.size(); |
| 587 |
QString conName = conceptList.at(conId); |
| 588 |
|
| 589 |
QString queryTermString = QString(" %1 rdf:type <%2> . ").arg(indiVar).arg(conName); |
| 590 |
if (!queryTermSet.contains(queryTermString)) { |
| 591 |
queryTermSet.insert(queryTermString); |
| 592 |
queryTerms.append(queryTermString); |
| 593 |
statisticsHash["RequestedConjunctiveQueryConceptTermCount"]++; |
| 594 |
statisticsHash["RequestedConjunctiveQueryAtomicConceptTermCount"]++; |
| 595 |
statisticsHash["RequestedConjunctiveQueryTermCount"]++; |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
void CCompletionGraphRandomWalkQueryGenerator::generateTreeExpansion(CIndividualProcessNode* indiNode, QString indiVar, QSet<QString>& queryParameters, QSet<QString>& queryTermSet, QStringList& queryTerms, QHash<QString, cint64>& statisticsHash, cint64& nextTreeExpansionNr, CSatisfiableCalculationTask* satCalcTask) { |
| 608 |
|
| 609 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 610 |
bool generateTreeExtensions = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.GenerateTreeExtensions"); |
| 611 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.TreeExtensionsOnlyOverIndividualNodes"); |
| 612 |
|
| 613 |
|
| 614 |
cint64 existentialVariableConversionRation = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.TreeExtensionExistentialVariableConversionRatio"); |
| 615 |
|
| 616 |
cint64 extensionCreationRatio = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.TreeExtensionCreationRatio"); |
| 617 |
|
| 618 |
|
| 619 |
cint64 treeExpansionVarId = 0; |
| 620 |
QHash<QPair<cint64, cint64>, QString> treeExtIndiVarIdNameHash; |
| 621 |
|
| 622 |
class CTreeExpansionNodeData { |
| 623 |
public: |
| 624 |
CTreeExpansionNodeData(CIndividualProcessNode* indiNode, QString varName, cint64 depth) { |
| 625 |
mIndiNode = indiNode; |
| 626 |
mVarName = varName; |
| 627 |
mExistential = mVarName.startsWith("_:"); |
| 628 |
mDepth = depth; |
| 629 |
} |
| 630 |
|
| 631 |
CIndividualProcessNode* mIndiNode; |
| 632 |
QString mVarName; |
| 633 |
bool mExistential; |
| 634 |
cint64 mDepth; |
| 635 |
}; |
| 636 |
cint64 creationRatio = rand() % 100; |
| 637 |
if (generateTreeExtensions && creationRatio < extensionCreationRatio) { |
| 638 |
cint64 maxTreeExtensionSize = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.MaximumTreeExtensionSize"); |
| 639 |
cint64 teeExtensionSize = rand() % maxTreeExtensionSize; |
| 640 |
|
| 641 |
QList<CTreeExpansionNodeData> nodeVarNamePairList; |
| 642 |
nodeVarNamePairList.append(CTreeExpansionNodeData(indiNode, indiVar, 0)); |
| 643 |
if (teeExtensionSize > 0) { |
| 644 |
|
| 645 |
statisticsHash["RequestedConjunctiveQueryTreeExpansionCount"]++; |
| 646 |
queryParameters.insert("with-tree-extensions"); |
| 647 |
cint64 treeExpansionNr = nextTreeExpansionNr++; |
| 648 |
|
| 649 |
while (teeExtensionSize-- > 0) { |
| 650 |
cint64 extIdx = rand() % nodeVarNamePairList.size(); |
| 651 |
CTreeExpansionNodeData extIndiVarNameData = nodeVarNamePairList.at(extIdx); |
| 652 |
CIndividualProcessNode* extIndiNode = extIndiVarNameData.mIndiNode; |
| 653 |
QString extVarName = extIndiVarNameData.mVarName; |
| 654 |
bool existential = extIndiVarNameData.mExistential; |
| 655 |
cint64 depth = extIndiVarNameData.mDepth; |
| 656 |
|
| 657 |
QList<CIndividualProcessNode*> succNodeList; |
| 658 |
CSuccessorIterator succIt = extIndiNode->getSuccessorIterator(); |
| 659 |
while (succIt.hasNext()) { |
| 660 |
cint64 succIndiID = succIt.nextIndividualID(true); |
| 661 |
|
| 662 |
CIndividualProcessNode* succIndiNode = getUpToDateIndividual(succIndiID, satCalcTask); |
| 663 |
if (!succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || extIndiNode->getNominalIndividual())) { |
| 664 |
|
| 665 |
succNodeList.append(succIndiNode); |
| 666 |
|
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
if (!succNodeList.isEmpty()) { |
| 672 |
cint64 extSuccIdx = rand() % succNodeList.size(); |
| 673 |
CIndividualProcessNode* succIndiNode = succNodeList.at(extSuccIdx); |
| 674 |
|
| 675 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedVariableCount"]++; |
| 676 |
|
| 677 |
|
| 678 |
bool createDistinguishedVariable = true; |
| 679 |
if (existentialVariableConversionRation > 0) { |
| 680 |
createDistinguishedVariable = rand() % 100 > existentialVariableConversionRation; |
| 681 |
} |
| 682 |
if (createDistinguishedVariable && existential) { |
| 683 |
createDistinguishedVariable = false; |
| 684 |
} |
| 685 |
|
| 686 |
QString succIndiVarName = getIndividualNodeQueryVariable(succIndiNode->getIndividualNodeID(), ++treeExpansionVarId, treeExtIndiVarIdNameHash, statisticsHash, createDistinguishedVariable, QString("tree%1_d%2").arg(treeExpansionNr).arg(depth), satCalcTask); |
| 687 |
|
| 688 |
|
| 689 |
QHash<QString, cint64> varQueryUseCount; |
| 690 |
if (generateRoleTerm(extIndiNode, extVarName, succIndiNode, succIndiVarName, queryParameters, queryTermSet, queryTerms, varQueryUseCount, statisticsHash, satCalcTask)) { |
| 691 |
CTreeExpansionNodeData treeExpNodeData = CTreeExpansionNodeData(succIndiNode, succIndiVarName, depth + 1); |
| 692 |
nodeVarNamePairList.append(treeExpNodeData); |
| 693 |
|
| 694 |
generateConceptTerms(succIndiNode, succIndiVarName, queryParameters, queryTermSet, queryTerms, statisticsHash, satCalcTask); |
| 695 |
|
| 696 |
if (treeExpNodeData.mExistential) { |
| 697 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedNondistinguishedVariableCount"]++; |
| 698 |
} else { |
| 699 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedDistinguishedVariableCount"]++; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
} |
| 708 |
|
| 709 |
} |
| 710 |
|
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
QHash<QString, cint64> CCompletionGraphRandomWalkQueryGenerator::getInitialStatisticsHash(CSatisfiableCalculationTask* satTask) { |
| 719 |
QHash<QString, cint64> statisticsHash; |
| 720 |
statisticsHash["RequestedConjunctiveQueryCount"] = 1; |
| 721 |
statisticsHash["RequestedConjunctiveQueryTermCount"] = 1; |
| 722 |
statisticsHash["RequestedConjunctiveQueryConceptTermCount"] = 0; |
| 723 |
statisticsHash["RequestedConjunctiveQueryAtomicConceptTermCount"] = 0; |
| 724 |
statisticsHash["RequestedConjunctiveQueryComplexConceptTermCount"] = 0; |
| 725 |
statisticsHash["RequestedConjunctiveQueryRoleTermCount"] = 0; |
| 726 |
statisticsHash["RequestedConjunctiveQueryCycleCount"] = 0; |
| 727 |
statisticsHash["RequestedConjunctiveQueryNondistinguishedVariablesCount"] = 0; |
| 728 |
statisticsHash["RequestedConjunctiveQueryNominalNodeBasedVariablesCount"] = 0; |
| 729 |
statisticsHash["RequestedConjunctiveQueryBlockableNodeBasedVariablesCount"] = 0; |
| 730 |
statisticsHash["RequestedConjunctiveQueryDistinguishedVariablesCount"] = 0; |
| 731 |
statisticsHash["RequestedConjunctiveQuerySelectedVariableCount"] = 0; |
| 732 |
statisticsHash["RequestedConjunctiveQueryRealCycle"] = 0; |
| 733 |
statisticsHash["RequestedConjunctiveQueryTreeFoldedCycle"] = 0; |
| 734 |
statisticsHash["RequestedConjunctiveQueryTreeExpansionCount"] = 0; |
| 735 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedVariableCount"] = 0; |
| 736 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedDistinguishedVariableCount"] = 0; |
| 737 |
statisticsHash["RequestedConjunctiveQueryTreeExpandedNondistinguishedVariableCount"] = 0; |
| 738 |
statisticsHash["RequestedConjunctiveQueryVariableCount"] = 0; |
| 739 |
statisticsHash["RequestedConjunctiveQueryConstantCount"] = 0; |
| 740 |
return statisticsHash; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
bool CCompletionGraphRandomWalkQueryGenerator::generateRealCycleQuery(CSatisfiableCalculationTask* satCalcTask) { |
| 746 |
CProcessingDataBox* processingDataBox = satCalcTask->getProcessingDataBox(); |
| 747 |
CIndividualProcessNodeVector* indiNodeVec = processingDataBox->getIndividualProcessNodeVector(); |
| 748 |
cint64 indiStart = indiNodeVec->getItemMinIndex(); |
| 749 |
cint64 indiEnd = indiNodeVec->getItemMaxIndex(); |
| 750 |
|
| 751 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 752 |
|
| 753 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 754 |
|
| 755 |
|
| 756 |
class ReachabilityDistanceData { |
| 757 |
public: |
| 758 |
cint64 indiId = 0; |
| 759 |
cint64 reachCount = 0; |
| 760 |
QList<cint64> depList; |
| 761 |
cint64 minDistance = 999999; |
| 762 |
cint64 maxDistance = -9999999; |
| 763 |
}; |
| 764 |
|
| 765 |
class ProcessingDistanceData { |
| 766 |
public: |
| 767 |
cint64 prevIndiId = 0; |
| 768 |
cint64 indiId = 0; |
| 769 |
cint64 distance = 0; |
| 770 |
ProcessingDistanceData(cint64 id, cint64 distance, cint64 prevIndiId) { |
| 771 |
indiId = id; |
| 772 |
this->distance = distance; |
| 773 |
this->prevIndiId = prevIndiId; |
| 774 |
} |
| 775 |
}; |
| 776 |
|
| 777 |
cint64 queryGenerationTryCount = mRealCycleQueryGenerationTryCount; |
| 778 |
|
| 779 |
while (queryGenerationTryCount-- > 0 && (indiEnd - indiStart) > 0) { |
| 780 |
cint64 indiIdx = (rand() % (indiEnd - indiStart)) + indiStart; |
| 781 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(indiIdx, satCalcTask); |
| 782 |
if (indiNode && !indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || indiNode->getNominalIndividual())) { |
| 783 |
QHash<cint64, ReachabilityDistanceData> reachabilityCountHash; |
| 784 |
QSet<cint64> processedSet; |
| 785 |
QList<ProcessingDistanceData> processingList; |
| 786 |
if (indiNode) { |
| 787 |
processedSet.insert(indiNode->getIndividualNodeID()); |
| 788 |
processingList.append(ProcessingDistanceData(indiNode->getIndividualNodeID(), 0, indiNode->getIndividualNodeID())); |
| 789 |
|
| 790 |
|
| 791 |
QHash<QPair<cint64, cint64>, QString> indiVarIdNameHash; |
| 792 |
QHash<QString, cint64> statisticsHash = getInitialStatisticsHash(satCalcTask); |
| 793 |
statisticsHash["RequestedConjunctiveQueryRealCycle"]++; |
| 794 |
|
| 795 |
cint64 connection = 0; |
| 796 |
|
| 797 |
cint64 nextTreeExtensionNr = 0; |
| 798 |
|
| 799 |
while (!processingList.isEmpty()) { |
| 800 |
ProcessingDistanceData processingData = processingList.takeFirst(); |
| 801 |
|
| 802 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(processingData.indiId, satCalcTask); |
| 803 |
CSuccessorIterator succIt = indiNode->getSuccessorIterator(); |
| 804 |
while (succIt.hasNext()) { |
| 805 |
cint64 succIndiID = succIt.nextIndividualID(true); |
| 806 |
|
| 807 |
CIndividualProcessNode* succIndiNode = getUpToDateIndividual(succIndiID, satCalcTask); |
| 808 |
if (!succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCONCRETEDATAINDINODE) && !succIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && (!onlyIndividualNodesWalking || succIndiNode->getNominalIndividual())) { |
| 809 |
|
| 810 |
if (succIndiID != processingData.indiId && processingData.prevIndiId != succIndiID) { |
| 811 |
ReachabilityDistanceData& reachData = reachabilityCountHash[succIndiID]; |
| 812 |
reachData.indiId = succIndiID; |
| 813 |
if (connection < 6) { |
| 814 |
++reachData.reachCount; |
| 815 |
if (reachData.reachCount > 1) { |
| 816 |
connection++; |
| 817 |
} |
| 818 |
reachData.depList.append(processingData.indiId); |
| 819 |
reachData.minDistance = qMin(reachData.minDistance, processingData.distance + 1); |
| 820 |
reachData.maxDistance = qMax(reachData.maxDistance, processingData.distance + 1); |
| 821 |
|
| 822 |
|
| 823 |
if (processingData.distance + 1 < 10 && connection < 3) { |
| 824 |
if (!processedSet.contains(succIndiID)) { |
| 825 |
processedSet.insert(succIndiID); |
| 826 |
processingList.append(ProcessingDistanceData(succIndiID, processingData.distance + 1, processingData.indiId)); |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
class TracingBackData { |
| 838 |
public: |
| 839 |
cint64 indiId = 0; |
| 840 |
cint64 tracedBackIndiId = 0; |
| 841 |
TracingBackData(cint64 indiId, cint64 tracedBackIndiId) { |
| 842 |
this->indiId = indiId; |
| 843 |
this->tracedBackIndiId = tracedBackIndiId; |
| 844 |
} |
| 845 |
}; |
| 846 |
|
| 847 |
cint64 indiQueryCount = 0; |
| 848 |
for (ReachabilityDistanceData& reachData : reachabilityCountHash) { |
| 849 |
QStringList queryTerms; |
| 850 |
if (reachData.reachCount > 1) { |
| 851 |
// generate query by tracking back |
| 852 |
|
| 853 |
QList<TracingBackData> tracingBackDataList; |
| 854 |
for (cint64 depIndi : reachData.depList) { |
| 855 |
tracingBackDataList.append(TracingBackData(reachData.indiId, depIndi)); |
| 856 |
} |
| 857 |
|
| 858 |
bool queryExtractionFailed = false; |
| 859 |
QSet<QPair<cint64, cint64>> tracedBackPairsSet; |
| 860 |
QSet<QString> queryParameters; |
| 861 |
QSet<QString> queryTermSet; |
| 862 |
QSet<QString> selectableVarSet; |
| 863 |
QHash<QString, cint64> varQueryUseCount; |
| 864 |
QSet<cint64> trackedbackIndiSet; |
| 865 |
while (!tracingBackDataList.isEmpty() && !queryExtractionFailed) { |
| 866 |
TracingBackData tracingBackData = tracingBackDataList.takeFirst(); |
| 867 |
|
| 868 |
if (trackedbackIndiSet.contains(tracingBackData.indiId)) { |
| 869 |
statisticsHash["RequestedConjunctiveQueryCycleCount"]++; |
| 870 |
} else { |
| 871 |
trackedbackIndiSet.insert(tracingBackData.indiId); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
QString indiVar = getIndividualNodeQueryVariable(tracingBackData.indiId, 0, indiVarIdNameHash, statisticsHash, satCalcTask); |
| 876 |
QString traceBackIndiVar = getIndividualNodeQueryVariable(tracingBackData.tracedBackIndiId, 0, indiVarIdNameHash, statisticsHash, satCalcTask); |
| 877 |
|
| 878 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(tracingBackData.indiId, satCalcTask); |
| 879 |
if (indiNode && indiNode->getNominalIndividual()) { |
| 880 |
selectableVarSet.insert(indiVar); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
generateConceptTerms(indiNode, indiVar, queryParameters, queryTermSet, queryTerms, statisticsHash, satCalcTask); |
| 885 |
|
| 886 |
|
| 887 |
generateTreeExpansion(indiNode, indiVar, queryParameters, queryTermSet, queryTerms, statisticsHash, nextTreeExtensionNr, satCalcTask); |
| 888 |
|
| 889 |
if (!generateRoleTerm(indiNode, indiVar, getUpToDateIndividual(tracingBackData.tracedBackIndiId, satCalcTask), traceBackIndiVar, queryParameters, queryTermSet, queryTerms, varQueryUseCount, statisticsHash, satCalcTask)) { |
| 890 |
queryExtractionFailed = true; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
ReachabilityDistanceData& depReachData = reachabilityCountHash[tracingBackData.tracedBackIndiId]; |
| 897 |
for (cint64 depIndi : depReachData.depList) { |
| 898 |
if (!tracedBackPairsSet.contains(QPair<cint64, cint64>(depReachData.indiId, depIndi))) { |
| 899 |
tracedBackPairsSet.insert(QPair<cint64, cint64>(depReachData.indiId, depIndi)); |
| 900 |
tracingBackDataList.append(TracingBackData(depReachData.indiId, depIndi)); |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
bool hasAllIndisAtleastTwice = true; |
| 908 |
for (cint64 usedCount : varQueryUseCount) { |
| 909 |
if (usedCount <= 1) { |
| 910 |
hasAllIndisAtleastTwice = false; |
| 911 |
queryExtractionFailed = true; |
| 912 |
break; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
if (!queryExtractionFailed) { |
| 918 |
++indiQueryCount; |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
if (!writeQuery(queryTerms, "real-cycle", selectableVarSet.toList(), queryParameters.toList(), statisticsHash, satCalcTask)) { |
| 923 |
queryExtractionFailed = true; |
| 924 |
} else { |
| 925 |
mRealCycleQueryGenerationCount++; |
| 926 |
return true; |
| 927 |
} |
| 928 |
|
| 929 |
} |
| 930 |
if (queryExtractionFailed) { |
| 931 |
mRealCycleQueryFailedExtractionCount++; |
| 932 |
} |
| 933 |
|
| 934 |
if (indiQueryCount > 10) { |
| 935 |
break; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
} else { |
| 946 |
mRealCycleQueryFailedExtractionCount++; |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
return false; |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
QString CCompletionGraphRandomWalkQueryGenerator::getIndividualNodeQueryVariable(cint64 indiId, cint64 varId, QHash<QPair<cint64, cint64>, QString>& indiVarIdNameHash, QHash<QString, cint64>& statisticsHash, CSatisfiableCalculationTask* satCalcTask) { |
| 957 |
CCalculationConfigurationExtension* config = satCalcTask->getCalculationConfiguration(); |
| 958 |
bool onlyIndividualNodesWalking = CConfigDataReader::readConfigBoolean(config, "Konclude.Test.ConjunctiveQueryGeneration.OnlyWalkOverIndividualNodes"); |
| 959 |
cint64 existentialVariableInterpRation = CConfigDataReader::readConfigInteger(config, "Konclude.Test.ConjunctiveQueryGeneration.IndividualNodeAsExistentialVariableInterpretationRatio"); |
| 960 |
|
| 961 |
bool createDistinguishedVariable = true; |
| 962 |
if (existentialVariableInterpRation > 0) { |
| 963 |
createDistinguishedVariable = rand() % 100 < existentialVariableInterpRation; |
| 964 |
} |
| 965 |
return getIndividualNodeQueryVariable(indiId, varId, indiVarIdNameHash, statisticsHash, createDistinguishedVariable, "cyc", satCalcTask); |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
QString CCompletionGraphRandomWalkQueryGenerator::getIndividualNodeQueryVariable(cint64 indiId, cint64 varId, QHash<QPair<cint64,cint64>, QString>& indiVarIdNameHash, QHash<QString, cint64>& statisticsHash, bool createDistinguishedVariableIfPossible, const QString& varPrefixString, CSatisfiableCalculationTask* satCalcTask) { |
| 970 |
|
| 971 |
QPair<cint64, cint64> indiVarIdPair(indiId, varId); |
| 972 |
if (indiVarIdNameHash.contains(indiVarIdPair)) { |
| 973 |
return indiVarIdNameHash.value(indiVarIdPair); |
| 974 |
} |
| 975 |
|
| 976 |
bool isDistinguishedVariable = false; |
| 977 |
CIndividualProcessNode* indiNode = getUpToDateIndividual(indiId, satCalcTask); |
| 978 |
QString nodeSuffix; |
| 979 |
if (indiNode->getNominalIndividual() && createDistinguishedVariableIfPossible) { |
| 980 |
QString indiName = CIRIName::getRecentIRIName(indiNode->getNominalIndividual()->getIndividualNameLinker()); |
| 981 |
indiName = indiName.replace("-", "_"); |
| 982 |
indiName = indiName.replace(",", "_"); |
| 983 |
indiName = indiName.replace(".", "_"); |
| 984 |
indiName = indiName.replace("+", "_"); |
| 985 |
indiName = indiName.replace("*", "_"); |
| 986 |
indiName = indiName.replace("'", "_"); |
| 987 |
int lastPos = indiName.lastIndexOf("#"); |
| 988 |
lastPos = qMax(lastPos, indiName.lastIndexOf("/")); |
| 989 |
lastPos = qMax(lastPos, indiName.lastIndexOf(":")); |
| 990 |
lastPos = qMax(lastPos, indiName.lastIndexOf("\\")); |
| 991 |
QString lastIndiNamePart = indiName.mid(lastPos + 1); |
| 992 |
for (cint64 i = 0; i < lastIndiNamePart.length(); ++i) { |
| 993 |
QChar nameChar = lastIndiNamePart.at(i); |
| 994 |
if (nameChar.isLetterOrNumber()) { |
| 995 |
nodeSuffix += nameChar; |
| 996 |
} |
| 997 |
} |
| 998 |
while (indiName.contains("__")) { |
| 999 |
indiName = indiName.replace("__", "_"); |
| 1000 |
} |
| 1001 |
if (nodeSuffix.length() > 0) { |
| 1002 |
nodeSuffix += "__"; |
| 1003 |
} |
| 1004 |
nodeSuffix += QString("n%1").arg(qAbs(indiId)); |
| 1005 |
isDistinguishedVariable = true; |
| 1006 |
statisticsHash["RequestedConjunctiveQueryDistinguishedVariablesCount"]++; |
| 1007 |
} else { |
| 1008 |
nodeSuffix += QString("b%1").arg(qAbs(indiId)); |
| 1009 |
statisticsHash["RequestedConjunctiveQueryNondistinguishedVariablesCount"]++; |
| 1010 |
} |
| 1011 |
QString indiVar; |
| 1012 |
if (isDistinguishedVariable) { |
| 1013 |
indiVar = QString("?%1_v%2__%3").arg(varPrefixString).arg(varId).arg(nodeSuffix); |
| 1014 |
} else { |
| 1015 |
indiVar = QString("_:%1_v%2__%3").arg(varPrefixString).arg(varId).arg(nodeSuffix); |
| 1016 |
} |
| 1017 |
indiVarIdNameHash.insert(indiVarIdPair, indiVar); |
| 1018 |
|
| 1019 |
statisticsHash["RequestedConjunctiveQueryVariableCount"]++; |
| 1020 |
if (indiNode->getNominalIndividual()) { |
| 1021 |
statisticsHash["RequestedConjunctiveQueryNominalNodeBasedVariablesCount"]++; |
| 1022 |
} else { |
| 1023 |
statisticsHash["RequestedConjunctiveQueryBlockableNodeBasedVariablesCount"]++; |
| 1024 |
} |
| 1025 |
|
| 1026 |
return indiVar; |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
}; // end namespace Algorithm |
| 1033 |
|
| 1034 |
}; // end namespace Kernel |
| 1035 |
|
| 1036 |
|
| 1037 |
}; // end namespace Reasoner |
| 1038 |
|
| 1039 |
}; // end namespace Konclude |