| 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 "CSatisfiableTaskSaturationOccurrenceStatisticsCollector.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Algorithm { |
| 31 |
|
| 32 |
CSatisfiableTaskSaturationOccurrenceStatisticsCollector::CSatisfiableTaskSaturationOccurrenceStatisticsCollector(COccurrenceStatisticsCacheWriter* occStatsCacheWriter) { |
| 33 |
mOccStatsCacheWriter = occStatsCacheWriter; |
| 34 |
mCollectedOccStatsNodeCount = 0; |
| 35 |
mCollectedOccStatsSuccNodeCount = 0; |
| 36 |
mCollectedOccStatsConceptCount = 0; |
| 37 |
mCollectedOccStatsRoleCount = 0; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CSatisfiableTaskSaturationOccurrenceStatisticsCollector::~CSatisfiableTaskSaturationOccurrenceStatisticsCollector() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
bool CSatisfiableTaskSaturationOccurrenceStatisticsCollector::analyseSatisfiableTask(CSatisfiableCalculationTask* statCalcTask, CCalculationAlgorithmContext* calcAlgContext) { |
| 46 |
mOntology = statCalcTask->getProcessingDataBox()->getOntology(); |
| 47 |
mOntologyTag = mOntology->getOntologyID(); |
| 48 |
return collectOccurrenceStatistics(statCalcTask->getProcessingDataBox(), calcAlgContext); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
CIndividualSaturationProcessNode* CSatisfiableTaskSaturationOccurrenceStatisticsCollector::getSuccessorNode(CConceptSaturationDescriptor* conDes, CIndividualSaturationProcessNode* satNode, CCalculationAlgorithmContext* calcAlgContext) { |
| 56 |
CConcept* concept = conDes->getConcept(); |
| 57 |
bool conNegation = conDes->isNegated(); |
| 58 |
CRole* role = concept->getRole(); |
| 59 |
CConceptData* conceptData = concept->getConceptData(); |
| 60 |
if (conceptData) { |
| 61 |
CConceptProcessData* conProcData = (CConceptProcessData*)conceptData; |
| 62 |
CConceptReferenceLinking* conRefLinking = conProcData->getConceptReferenceLinking(); |
| 63 |
if (conRefLinking) { |
| 64 |
CConceptSaturationReferenceLinkingData* confSatRefLinkingData = (CConceptSaturationReferenceLinkingData*)conRefLinking; |
| 65 |
CSaturationConceptReferenceLinking* extSatCalcRefLinkData = confSatRefLinkingData->getExistentialSuccessorConceptSaturationReferenceLinkingData(); |
| 66 |
if (extSatCalcRefLinkData) { |
| 67 |
CIndividualSaturationProcessNode* existIndiNode = (CIndividualSaturationProcessNode*)extSatCalcRefLinkData->getIndividualProcessNodeForConcept(); |
| 68 |
if (existIndiNode) { |
| 69 |
return existIndiNode; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
CSortedNegLinker<CConcept*>* conceptOpLinkerIt = concept->getOperandList(); |
| 75 |
while (conceptOpLinkerIt) { |
| 76 |
CConcept* opConcept = conceptOpLinkerIt->getData(); |
| 77 |
bool opConNegation = conceptOpLinkerIt->isNegated() ^ conNegation; |
| 78 |
CConceptData* opConceptData = opConcept->getConceptData(); |
| 79 |
if (opConceptData) { |
| 80 |
CConceptProcessData* conProcData = (CConceptProcessData*)opConceptData; |
| 81 |
CConceptReferenceLinking* conRefLinking = conProcData->getConceptReferenceLinking(); |
| 82 |
if (conRefLinking) { |
| 83 |
CConceptSaturationReferenceLinkingData* confSatRefLinkingData = (CConceptSaturationReferenceLinkingData*)conRefLinking; |
| 84 |
CSaturationConceptReferenceLinking* satCalcRefLinkData = confSatRefLinkingData->getConceptSaturationReferenceLinkingData(opConNegation); |
| 85 |
if (satCalcRefLinkData) { |
| 86 |
CIndividualSaturationProcessNode* existIndiNode = (CIndividualSaturationProcessNode*)satCalcRefLinkData->getIndividualProcessNodeForConcept(); |
| 87 |
if (existIndiNode) { |
| 88 |
return existIndiNode; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
conceptOpLinkerIt = conceptOpLinkerIt->getNext(); |
| 94 |
} |
| 95 |
CConcept* baseTopConcept = nullptr; |
| 96 |
if (!role->isDataRole()) { |
| 97 |
baseTopConcept = calcAlgContext->getUsedProcessingDataBox()->getOntologyTopConcept(); |
| 98 |
} else { |
| 99 |
baseTopConcept = calcAlgContext->getUsedProcessingDataBox()->getOntologyTopDataRangeConcept(); |
| 100 |
} |
| 101 |
CConcept* opConcept = baseTopConcept; |
| 102 |
bool opConNegation = false; |
| 103 |
CConceptData* opConceptData = opConcept->getConceptData(); |
| 104 |
if (opConceptData) { |
| 105 |
CConceptProcessData* conProcData = (CConceptProcessData*)opConceptData; |
| 106 |
CConceptReferenceLinking* conRefLinking = conProcData->getConceptReferenceLinking(); |
| 107 |
if (conRefLinking) { |
| 108 |
CConceptSaturationReferenceLinkingData* confSatRefLinkingData = (CConceptSaturationReferenceLinkingData*)conRefLinking; |
| 109 |
CSaturationConceptReferenceLinking* satCalcRefLinkData = confSatRefLinkingData->getConceptSaturationReferenceLinkingData(opConNegation); |
| 110 |
if (satCalcRefLinkData) { |
| 111 |
CIndividualSaturationProcessNode* existIndiNode = (CIndividualSaturationProcessNode*)satCalcRefLinkData->getIndividualProcessNodeForConcept(); |
| 112 |
if (existIndiNode) { |
| 113 |
return existIndiNode; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
return nullptr; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
bool CSatisfiableTaskSaturationOccurrenceStatisticsCollector::visitNodeSuccessors(CIndividualSaturationProcessNode* satNode, |
| 125 |
bool visitedExistentialSuccessessors, bool visitNominalSuccessors, bool visitDataSuccessors, bool analyseConcepts, bool analyseSuccessorLinks, |
| 126 |
function<bool(CIndividualSaturationProcessNode* succNode, cint64 succId)> visitSuccNodeFunc, function<bool(CRole* role, bool inversed)> visitSuccRoleFunc, CCalculationAlgorithmContext* calcAlgContext) { |
| 127 |
|
| 128 |
CReapplyConceptSaturationLabelSet* conSet = satNode->getReapplyConceptSaturationLabelSet(false); |
| 129 |
CLinkedRoleSaturationSuccessorHash* linkedSuccHash = satNode->getLinkedRoleSuccessorHash(false); |
| 130 |
|
| 131 |
CIndividualSaturationProcessNode* copiedNode = satNode->getCopyIndividualNode(); |
| 132 |
if (copiedNode && copiedNode->getReapplyConceptSaturationLabelSet(false) && copiedNode->getReapplyConceptSaturationLabelSet(false)->getConceptSaturationDescriptionLinker() == conSet->getConceptSaturationDescriptionLinker() && (!linkedSuccHash || copiedNode->getLinkedRoleSuccessorHash(false))) { |
| 133 |
return visitNodeSuccessors(copiedNode, visitedExistentialSuccessessors, visitNominalSuccessors, visitDataSuccessors, analyseConcepts, analyseSuccessorLinks, visitSuccNodeFunc, visitSuccRoleFunc, calcAlgContext); |
| 134 |
} |
| 135 |
|
| 136 |
bool visited = false; |
| 137 |
|
| 138 |
if (analyseSuccessorLinks && linkedSuccHash) { |
| 139 |
CPROCESSHASH<CRole*, CLinkedRoleSaturationSuccessorData*>* linkSuccDatahash = linkedSuccHash->getLinkedRoleSuccessorHash(); |
| 140 |
for (CPROCESSHASH<CRole*, CLinkedRoleSaturationSuccessorData*>::const_iterator it = linkSuccDatahash->constBegin(), itEnd = linkSuccDatahash->constEnd(); it != itEnd; ++it) { |
| 141 |
CRole* role = it.key(); |
| 142 |
CLinkedRoleSaturationSuccessorData* linkSuccData = it.value(); |
| 143 |
if (linkSuccData->mSuccCount > 0) { |
| 144 |
CPROCESSMAP<cint64, CSaturationSuccessorData*>* succNodeMap = linkSuccData->getSuccessorNodeDataMap(false); |
| 145 |
for (CPROCESSMAP<cint64, CSaturationSuccessorData*>::const_iterator succIt = succNodeMap->constBegin(), succItEnd = succNodeMap->constEnd(); succIt != succItEnd; ++succIt) { |
| 146 |
bool roleSuccessorVisited = false; |
| 147 |
CSaturationSuccessorData* succData = succIt.value(); |
| 148 |
if (succData->mActiveCount > 0) { |
| 149 |
CIndividualSaturationProcessNode* succSatNode = succData->mSuccIndiNode; |
| 150 |
bool visistSuccessorNode = true; |
| 151 |
if (!succSatNode || succSatNode->isABoxIndividualRepresentationNode()) { |
| 152 |
visistSuccessorNode = false; |
| 153 |
} |
| 154 |
if (succSatNode && succSatNode->getIntegratedNominalIndividual() || succData->mVALUENominalConnection) { |
| 155 |
if (!visitNominalSuccessors) { |
| 156 |
visistSuccessorNode = false; |
| 157 |
} |
| 158 |
} else { |
| 159 |
if (role->isDataRole()) { |
| 160 |
if (!visitDataSuccessors) { |
| 161 |
visistSuccessorNode = false; |
| 162 |
} |
| 163 |
} else { |
| 164 |
if (!visitedExistentialSuccessessors) { |
| 165 |
visistSuccessorNode = false; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
if (visistSuccessorNode) { |
| 170 |
roleSuccessorVisited = true; |
| 171 |
visited |= visitSuccNodeFunc(succSatNode, succSatNode->getIndividualID()); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (roleSuccessorVisited) { |
| 176 |
bool baseRole = false; |
| 177 |
for (CXNegLinker<CRole*>* creationRoleLinkerIt = succData->mCreationRoleLinker; creationRoleLinkerIt && !baseRole; creationRoleLinkerIt = creationRoleLinkerIt->getNext()) { |
| 178 |
if (!creationRoleLinkerIt->isNegated() && creationRoleLinkerIt->getData() == role) { |
| 179 |
baseRole = true; |
| 180 |
} |
| 181 |
} |
| 182 |
if (baseRole) { |
| 183 |
for (CSortedNegLinker<CRole*>* superRoleLinkerIt = role->getIndirectSuperRoleList(); superRoleLinkerIt; superRoleLinkerIt = superRoleLinkerIt->getNext()) { |
| 184 |
CRole* superRole = superRoleLinkerIt->getData(); |
| 185 |
bool superRoleNegation = superRoleLinkerIt->isNegated(); |
| 186 |
visited |= visitSuccRoleFunc(superRole, superRoleNegation); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} else if (analyseConcepts && conSet) { |
| 195 |
for (CConceptSaturationDescriptor* conSatDesIt = conSet->getConceptSaturationDescriptionLinker(); conSatDesIt; conSatDesIt = conSatDesIt->getNext()) { |
| 196 |
CConcept* concept = conSatDesIt->getConcept(); |
| 197 |
bool conNegation = conSatDesIt->isNegated(); |
| 198 |
cint64 conCode = concept->getOperatorCode(); |
| 199 |
CRole* role = concept->getRole(); |
| 200 |
bool existentialSuccessorCand = false; |
| 201 |
if (!conNegation && conCode == CCVALUE && visitNominalSuccessors) { |
| 202 |
visited |= visitSuccNodeFunc(nullptr, concept->getNominalIndividual()->getIndividualID()); |
| 203 |
} |
| 204 |
if (!conNegation && conCode == CCATLEAST || conNegation && conCode == CCATMOST) { |
| 205 |
CIndividualSaturationProcessNode* succSatNode = getSuccessorNode(conSatDesIt, satNode, calcAlgContext); |
| 206 |
CSaturationSuccessorData succData; |
| 207 |
succData.mSuccCount = concept->getParameter(); |
| 208 |
if (conNegation) { |
| 209 |
succData.mSuccCount--; |
| 210 |
} |
| 211 |
visited |= visitSuccNodeFunc(succSatNode, succSatNode->getIndividualID()); |
| 212 |
} |
| 213 |
|
| 214 |
if (!conNegation && (conCode == CCSOME || conCode == CCAQSOME) || conNegation && (conCode == CCALL)) { |
| 215 |
existentialSuccessorCand = true; |
| 216 |
} |
| 217 |
if (role && existentialSuccessorCand) { |
| 218 |
CIndividualSaturationProcessNode* succSatNode = getSuccessorNode(conSatDesIt, satNode, calcAlgContext); |
| 219 |
if (succSatNode) { |
| 220 |
bool visistSuccessorNode = true; |
| 221 |
if (succSatNode->getIntegratedNominalIndividual()) { |
| 222 |
if (!visitNominalSuccessors) { |
| 223 |
visistSuccessorNode = false; |
| 224 |
} |
| 225 |
} else { |
| 226 |
if (role->isDataRole()) { |
| 227 |
if (!visitDataSuccessors) { |
| 228 |
visistSuccessorNode = false; |
| 229 |
} |
| 230 |
} else { |
| 231 |
if (!visitedExistentialSuccessessors) { |
| 232 |
visistSuccessorNode = false; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (visistSuccessorNode) { |
| 238 |
visited |= visitSuccNodeFunc(succSatNode, succSatNode->getIndividualID()); |
| 239 |
|
| 240 |
for (CSortedNegLinker<CRole*>* superRoleLinkerIt = role->getIndirectSuperRoleList(); superRoleLinkerIt; superRoleLinkerIt = superRoleLinkerIt->getNext()) { |
| 241 |
CRole* superRole = superRoleLinkerIt->getData(); |
| 242 |
bool superRoleNegation = superRoleLinkerIt->isNegated(); |
| 243 |
visited |= visitSuccRoleFunc(superRole, superRoleNegation); |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return visited; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
bool CSatisfiableTaskSaturationOccurrenceStatisticsCollector::collectOccurrenceStatistics(CProcessingDataBox* processingDataBox, CCalculationAlgorithmContext* calcAlgContext) { |
| 262 |
if (processingDataBox->hasIndividualSaturationCompletedNodeLinker()) { |
| 263 |
|
| 264 |
CIndividualSaturationProcessNodeLinker* succIdentNodeLinker = nullptr; |
| 265 |
for (CIndividualSaturationProcessNodeLinker* indiNodeLinkerIt = processingDataBox->getIndividualSaturationCompletedNodeLinker(); indiNodeLinkerIt; indiNodeLinkerIt = indiNodeLinkerIt->getNext()) { |
| 266 |
CIndividualSaturationProcessNode* indiProcessNode = indiNodeLinkerIt->getProcessingIndividual(); |
| 267 |
if (indiProcessNode->isOccurrenceStatisticsCollectingRequired() && !indiProcessNode->isOccurrenceStatisticsCollected()) { |
| 268 |
|
| 269 |
CIndividualSaturationProcessNodeLinker* indiProcessNodeLinker = CObjectAllocator< CIndividualSaturationProcessNodeLinker >::allocateAndConstruct(calcAlgContext->getTemporaryMemoryAllocationManager()); |
| 270 |
indiProcessNodeLinker->initProcessNodeLinker(indiProcessNode, true); |
| 271 |
succIdentNodeLinker = indiProcessNodeLinker->append(succIdentNodeLinker); |
| 272 |
|
| 273 |
indiProcessNode->setOccurrenceStatisticsCollected(true); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
for (CIndividualSaturationProcessNodeLinker* indiNodeLinkerIt = processingDataBox->getIndividualSaturationAnalysationNodeLinker(); indiNodeLinkerIt; indiNodeLinkerIt = indiNodeLinkerIt->getNext()) { |
| 278 |
CIndividualSaturationProcessNode* indiProcessNode = indiNodeLinkerIt->getProcessingIndividual(); |
| 279 |
if (indiProcessNode->isOccurrenceStatisticsCollectingRequired() && !indiProcessNode->isOccurrenceStatisticsCollected()) { |
| 280 |
|
| 281 |
CIndividualSaturationProcessNodeLinker* indiProcessNodeLinker = CObjectAllocator< CIndividualSaturationProcessNodeLinker >::allocateAndConstruct(calcAlgContext->getTemporaryMemoryAllocationManager()); |
| 282 |
indiProcessNodeLinker->initProcessNodeLinker(indiProcessNode, true); |
| 283 |
succIdentNodeLinker = indiProcessNodeLinker->append(succIdentNodeLinker); |
| 284 |
|
| 285 |
indiProcessNode->setOccurrenceStatisticsCollected(true); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
while (succIdentNodeLinker) { |
| 290 |
CIndividualSaturationProcessNodeLinker* indiProcessNodeLinker = succIdentNodeLinker; |
| 291 |
succIdentNodeLinker = succIdentNodeLinker->getNext(); |
| 292 |
indiProcessNodeLinker->clearNext(); |
| 293 |
++mCollectedOccStatsNodeCount; |
| 294 |
|
| 295 |
CIndividualSaturationProcessNode* indiProcessNode = indiProcessNodeLinker->getProcessingIndividual(); |
| 296 |
|
| 297 |
visitNodeSuccessors(indiProcessNode, true, false, true, true, true, [&](CIndividualSaturationProcessNode* succNode, cint64 succIndiId)->bool { |
| 298 |
if (succNode && !succNode->isOccurrenceStatisticsCollected() && succNode->isInitialized()) { |
| 299 |
succNode->setOccurrenceStatisticsCollectingRequired(true); |
| 300 |
succNode->setOccurrenceStatisticsCollected(true); |
| 301 |
|
| 302 |
CIndividualSaturationProcessNodeLinker* indiProcessNodeLinker = CObjectAllocator< CIndividualSaturationProcessNodeLinker >::allocateAndConstruct(calcAlgContext->getTemporaryMemoryAllocationManager()); |
| 303 |
indiProcessNodeLinker->initProcessNodeLinker(succNode, true); |
| 304 |
succIdentNodeLinker = indiProcessNodeLinker->append(succIdentNodeLinker); |
| 305 |
++mCollectedOccStatsSuccNodeCount; |
| 306 |
} |
| 307 |
return true; |
| 308 |
}, [&](CRole* role, bool inversed)->bool { |
| 309 |
mOccStatsCacheWriter->incRoleInstanceOccurrencceStatistics(mOntology, role->getRoleTag(), 1, 0, 0, 1, 0, 0); |
| 310 |
++mCollectedOccStatsRoleCount; |
| 311 |
return true; |
| 312 |
}, calcAlgContext); |
| 313 |
|
| 314 |
collectConceptOccurrenceStatistics(indiProcessNode, calcAlgContext); |
| 315 |
} |
| 316 |
|
| 317 |
} |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
bool CSatisfiableTaskSaturationOccurrenceStatisticsCollector::collectConceptOccurrenceStatistics(CIndividualSaturationProcessNode* indiNode, CCalculationAlgorithmContext* calcAlgContext) { |
| 327 |
CReapplyConceptSaturationLabelSet* conSet = indiNode->getReapplyConceptSaturationLabelSet(false); |
| 328 |
if (conSet) { |
| 329 |
for (CConceptSaturationDescriptor* conSatDesLinkerIt = conSet->getConceptSaturationDescriptionLinker(); conSatDesLinkerIt; conSatDesLinkerIt = conSatDesLinkerIt->getNext()) { |
| 330 |
CConcept* concept = conSatDesLinkerIt->getConcept(); |
| 331 |
mOccStatsCacheWriter->incConceptInstanceOccurrencceStatistics(mOntology, concept->getConceptTag(), 1, 0, 0, 1); |
| 332 |
++mCollectedOccStatsConceptCount; |
| 333 |
} |
| 334 |
return true; |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
}; // end namespace Algorithm |
| 342 |
|
| 343 |
}; // end namespace Kernel |
| 344 |
|
| 345 |
}; // end namespace Reasoner |
| 346 |
|
| 347 |
}; // end namespace Konclude |