| 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 "CSatisfiableTaskIncrementalConsistencyPreyingAnalyser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Algorithm { |
| 31 |
|
| 32 |
CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::CSatisfiableTaskIncrementalConsistencyPreyingAnalyser() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::~CSatisfiableTaskIncrementalConsistencyPreyingAnalyser() { |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
bool CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::analyseSatisfiableTask(CSatisfiableCalculationTask* statCalcTask, CCalculationAlgorithmContext* calcAlgContext) { |
| 41 |
CSatisfiableTaskIncrementalConsistencyTestingAdapter* incConsAdapter = statCalcTask->getSatisfiableTaskIncrementalConsistencyTestingAdapter(); |
| 42 |
if (incConsAdapter) { |
| 43 |
CConsistenceObserver* consObs = nullptr; |
| 44 |
consObs = incConsAdapter->getConsistenceObserver(); |
| 45 |
if (consObs) { |
| 46 |
CConcreteOntology* prevConsOntology = incConsAdapter->getPreviousConsistentOntology(); |
| 47 |
CConsistenceTaskData* prevConsTaskData = nullptr; |
| 48 |
if (prevConsOntology) { |
| 49 |
CConsistence* prevConsistence = prevConsOntology->getConsistence(); |
| 50 |
if (prevConsistence) { |
| 51 |
CConsistenceData* prevConsData = prevConsistence->getConsistenceModelData(); |
| 52 |
if (prevConsData) { |
| 53 |
prevConsTaskData = dynamic_cast<CConsistenceTaskData*>(prevConsData); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
CSatisfiableCalculationTask* rootStatCalcTask = (CSatisfiableCalculationTask*)statCalcTask->getRootTask(); |
| 59 |
CIncrementalConsistenceTaskData* consTaskData = new CIncrementalConsistenceTaskData(rootStatCalcTask,statCalcTask,prevConsOntology,prevConsTaskData); |
| 60 |
analyseIncrementalChanges(statCalcTask,consTaskData,calcAlgContext); |
| 61 |
consObs->tellConsistenceData(consTaskData); |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
CIndividualProcessNode* CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::getMergedIntoIndividualNode(CIndividualProcessNode* indiNode, CIndividualProcessNodeVector* indiNodeVec, CCalculationAlgorithmContext* calcAlgContext) { |
| 72 |
CIndividualProcessNode* mergedIntoIndiNode = indiNode; |
| 73 |
while (mergedIntoIndiNode && mergedIntoIndiNode->getMergedIntoIndividualNodeID() != mergedIntoIndiNode->getIndividualNodeID()) { |
| 74 |
mergedIntoIndiNode = indiNodeVec->getData(mergedIntoIndiNode->getMergedIntoIndividualNodeID()); |
| 75 |
} |
| 76 |
return mergedIntoIndiNode; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
bool CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::analyseIncrementalChanges(CSatisfiableCalculationTask* statCalcTask, CIncrementalConsistenceTaskData* incConsData, CCalculationAlgorithmContext* calcAlgContext) { |
| 83 |
|
| 84 |
CConcreteOntology* prevConsOntology = incConsData->getPreviousOntology(); |
| 85 |
CConsistenceTaskData* prevConsTaskData = incConsData->getPreviousConsistenceData(); |
| 86 |
QSet<cint64>* indirectlyChangedNodeSet = incConsData->getIndirectlyChangedNodeSet(); |
| 87 |
QSet<cint64>* deterministicallyChangedNodeSet = incConsData->getDeterministicallyChangedNodeSet(); |
| 88 |
QSet<cint64>* changedCompatibleNodeSet = incConsData->getChangedCompatibleNodeSet(); |
| 89 |
if (prevConsTaskData) { |
| 90 |
CSatisfiableCalculationTask* prevCompGraphCalcTask = prevConsTaskData->getCompletionGraphCachedSatisfiableTask(); |
| 91 |
CIndividualProcessNodeVector* indiNodeVec = statCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector(); |
| 92 |
CIndividualProcessNodeVector* prevIndiNodeVec = prevCompGraphCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector(); |
| 93 |
cint64 incExpID = statCalcTask->getProcessingDataBox()->getIncrementalExpansionID(); |
| 94 |
cint64 indiNodeCount = indiNodeVec->getItemCount(); |
| 95 |
cint64 indiNodeStartIdx = indiNodeVec->getItemMinIndex(); |
| 96 |
cint64 addNodeCount = 0; |
| 97 |
cint64 totalNodeCount = 0; |
| 98 |
cint64 prevNodeCount = 0; |
| 99 |
statCalcTask->getProcessingDataBox()->setMaxIncrementalPreviousCompletionGraphNodeID(prevIndiNodeVec->getItemCount()); |
| 100 |
for (cint64 i = indiNodeStartIdx; i < indiNodeCount; ++i) { |
| 101 |
CIndividualProcessNode* indiNode = indiNodeVec->getData(i); |
| 102 |
CIndividualProcessNode* prevIndiNode = prevIndiNodeVec->getData(i); |
| 103 |
if (prevIndiNode) { |
| 104 |
++prevNodeCount; |
| 105 |
} |
| 106 |
if (indiNode) { |
| 107 |
++totalNodeCount; |
| 108 |
if (!prevIndiNode) { |
| 109 |
++addNodeCount; |
| 110 |
} |
| 111 |
if (!indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) || prevIndiNode && !prevIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFPURGEDBLOCKED) && prevIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFSUCCESSORNOMINALCONNECTION)) { |
| 112 |
bool indirectlyChanged = false; |
| 113 |
bool deterministicallyChanged = false; |
| 114 |
if (prevIndiNode || indiNode->isNominalIndividualNode()) { |
| 115 |
if (indiNode != prevIndiNode) { |
| 116 |
indirectlyChangedNodeSet->insert(-i); |
| 117 |
indirectlyChanged = true; |
| 118 |
} |
| 119 |
if (indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFINCREMENTALEXPANDING)) { |
| 120 |
if (indiNode->getIncrementalExpansionID() == incExpID) { |
| 121 |
deterministicallyChangedNodeSet->insert(-i); |
| 122 |
deterministicallyChanged = true; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (indirectlyChanged || deterministicallyChanged) { |
| 128 |
bool compatible = false; |
| 129 |
if (indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHED) && !indiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALID | CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALIDATED)) { |
| 130 |
compatible = true; |
| 131 |
} else { |
| 132 |
CIndividualProcessNode* mergedIntoIndiNode = getMergedIntoIndividualNode(indiNode,indiNodeVec,calcAlgContext); |
| 133 |
if (prevIndiNode) { |
| 134 |
CIndividualProcessNode* prevMergedIntoIndiNode = getMergedIntoIndividualNode(prevIndiNode,prevIndiNodeVec,calcAlgContext); |
| 135 |
if (mergedIntoIndiNode->getIndividualNodeID() == prevMergedIntoIndiNode->getIndividualNodeID()) { |
| 136 |
CReapplyConceptLabelSet* conSet = mergedIntoIndiNode->getReapplyConceptLabelSet(false); |
| 137 |
if (conSet && prevMergedIntoIndiNode) { |
| 138 |
CReapplyConceptLabelSet* prevConSet = prevMergedIntoIndiNode->getReapplyConceptLabelSet(false); |
| 139 |
if (prevConSet && prevConSet->getConceptSignatureValue() == conSet->getConceptSignatureValue() && conSet->getConceptCount() == prevConSet->getConceptCount()) { |
| 140 |
CReapplyConceptLabelSetIterator prevConIt = prevConSet->getConceptLabelSetIterator(true); |
| 141 |
CReapplyConceptLabelSetIterator conIt = conSet->getConceptLabelSetIterator(true); |
| 142 |
bool identicalLabels = true; |
| 143 |
while (prevConIt.hasValue() && conIt.hasValue() && identicalLabels) { |
| 144 |
CConceptDescriptor* conDes = conIt.getConceptDescriptor(); |
| 145 |
CConceptDescriptor* prevConDes = prevConIt.getConceptDescriptor(); |
| 146 |
if (conDes && prevConDes) { |
| 147 |
if (conDes->getConcept() != prevConDes->getConcept()) { |
| 148 |
identicalLabels = false; |
| 149 |
} |
| 150 |
if (conDes->isNegated() != prevConDes->isNegated()) { |
| 151 |
identicalLabels = false; |
| 152 |
} |
| 153 |
} else if (conDes || prevConDes) { |
| 154 |
identicalLabels = false; |
| 155 |
} |
| 156 |
conIt.moveNext(); |
| 157 |
prevConIt.moveNext(); |
| 158 |
} |
| 159 |
if (conIt.hasNext() || prevConIt.hasNext()) { |
| 160 |
identicalLabels = false; |
| 161 |
} |
| 162 |
if (identicalLabels) { |
| 163 |
compatible = true; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
if (compatible) { |
| 171 |
|
| 172 |
// test for problematic at-most cardinality restrictions |
| 173 |
bool problematicConcepts = false; |
| 174 |
CReapplyConceptLabelSet* conSet = indiNode->getReapplyConceptLabelSet(false); |
| 175 |
for (CConceptDescriptor* conDesIt = conSet->getAddingSortedConceptDescriptionLinker(); conDesIt && !problematicConcepts; conDesIt = conDesIt->getNext()) { |
| 176 |
CConcept* concept = conDesIt->getConcept(); |
| 177 |
bool negation = conDesIt->isNegated(); |
| 178 |
cint64 opCode = concept->getOperatorCode(); |
| 179 |
if (!negation && opCode == CCATMOST || negation && opCode == CCATLEAST) { |
| 180 |
problematicConcepts = true; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
bool problematicNeighbour = false; |
| 186 |
if (problematicConcepts) { |
| 187 |
CSuccessorIterator succIt(indiNode->getSuccessorIterator()); |
| 188 |
while (succIt.hasNext() && !problematicNeighbour) { |
| 189 |
CIndividualLinkEdge* succLink = succIt.nextLink(); |
| 190 |
CIndividualProcessNode* succIndi = getSuccessorIndividual(indiNode,succLink,calcAlgContext); |
| 191 |
if (succIndi->isNominalIndividualNode()) { |
| 192 |
if (!prevIndiNode->hasRoleSuccessorToIndividual(succLink->getLinkRole(),succIndi->getIndividualNodeID(),false)) { |
| 193 |
problematicNeighbour = true; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
CConnectionSuccessorSetIterator connIt(indiNode->getConnectionSuccessorIterator()); |
| 198 |
while (connIt.hasNext() && !problematicNeighbour) { |
| 199 |
cint64 connID = connIt.next(); |
| 200 |
CIndividualProcessNode* connIndiNode = indiNodeVec->getData(connID); |
| 201 |
if (connIndiNode->isNominalIndividualNode()) { |
| 202 |
CIndividualProcessNode* prevConnIndiNode = prevIndiNodeVec->getData(connID); |
| 203 |
if (prevConnIndiNode) { |
| 204 |
CSuccessorRoleIterator connRoleIt(connIndiNode->getSuccessorRoleIterator(indiNode)); |
| 205 |
while (connRoleIt.hasNext() && !problematicNeighbour) { |
| 206 |
CIndividualLinkEdge* connLink = connRoleIt.next(); |
| 207 |
if (!prevConnIndiNode->hasRoleSuccessorToIndividual(connLink->getLinkRole(),prevIndiNode,false)) { |
| 208 |
problematicNeighbour = true; |
| 209 |
} |
| 210 |
} |
| 211 |
} else { |
| 212 |
problematicNeighbour = true; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
if (!problematicConcepts || !problematicNeighbour) { |
| 220 |
changedCompatibleNodeSet->insert(-i); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
incConsData->setAddedNodeCount(addNodeCount); |
| 228 |
incConsData->setPreviousNodeCount(prevNodeCount); |
| 229 |
incConsData->setTotalNodeCount(totalNodeCount); |
| 230 |
} |
| 231 |
|
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
CIndividualProcessNode* CSatisfiableTaskIncrementalConsistencyPreyingAnalyser::getSuccessorIndividual(CIndividualProcessNode*& indi, CIndividualLinkEdge* link, CCalculationAlgorithmContext* calcAlgContext) { |
| 242 |
CIndividualProcessNode* succIndi = nullptr; |
| 243 |
|
| 244 |
if (link->isLocalizationTagUpToDate(calcAlgContext->getUsedProcessTagger()->getCurrentLocalizationTag())) { |
| 245 |
succIndi = link->getOppositeIndividual(indi); |
| 246 |
} else { |
| 247 |
STATINC(INDINODEUPDATELOADCOUNT,calcAlgContext); |
| 248 |
succIndi = link->getOppositeIndividual(indi); |
| 249 |
if (!succIndi->isLocalizationTagUpToDate(calcAlgContext->getUsedProcessTagger()->getCurrentLocalizationTag()) && succIndi->isRelocalized()) { |
| 250 |
cint64 succIndiId = link->getOppositeIndividualID(indi); |
| 251 |
CIndividualProcessNodeVector* indiProcNodeVec = calcAlgContext->getProcessingDataBox()->getIndividualProcessNodeVector(); |
| 252 |
succIndi = indiProcNodeVec->getData(succIndiId); |
| 253 |
} |
| 254 |
} |
| 255 |
return succIndi; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
}; // end namespace Algorithm |
| 262 |
|
| 263 |
}; // end namespace Kernel |
| 264 |
|
| 265 |
}; // end namespace Reasoner |
| 266 |
|
| 267 |
}; // end namespace Konclude |