Debian Codegraph @

Source/Reasoner/Kernel/Algorithm/CCompletionGraphCacheHandler.cpp

raw
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 "CCompletionGraphCacheHandler.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Algorithm {
31
32 CCompletionGraphCacheHandler::CCompletionGraphCacheHandler(CIndividualNodeBackendCacheHandler* backendCacheHandler) {
33 mLastOntology = nullptr;
34 mLastLoadedCachedCompletionGraph = false;
35 mPreviousConsistencyCachedCompletionGraph = false;
36 mLastConfig = nullptr;
37 mBackendCacheHandler = backendCacheHandler;
38 }
39
40
41 CCompletionGraphCacheHandler::~CCompletionGraphCacheHandler() {
42 }
43
44
45
46 bool CCompletionGraphCacheHandler::loadConsistenceModelData(CCalculationAlgorithmContext* calcAlgContext) {
47 CConcreteOntology* ontology = calcAlgContext->getProcessingDataBox()->getOntology();
48 mCalcTask = calcAlgContext->getUsedSatisfiableCalculationTask();
49 mIndiProcNodeVec = mCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector();
50 CConfiguration* config = calcAlgContext->getSatisfiableCalculationTask()->getCalculationConfiguration();
51 if (mLastOntology != ontology || mLastConfig != config) {
52 mLastOntology = ontology;
53 mLastConfig = config;
54 mIncrementalExpansionCaching = false;
55 mLastLoadedCachedCompletionGraph = false;
56 mPreviousConsistencyCachedCompletionGraph = false;
57 mCurrentIncrementalExpansionID = 0;
58 CConsistence* consistence = ontology->getConsistence();
59 if (consistence) {
60 CConsistenceData* consData = consistence->getConsistenceModelData();
61 if (consData) {
62 CConsistenceTaskData* consTaskData = dynamic_cast<CConsistenceTaskData*>(consData);
63 if (consTaskData) {
64 mDetSatCalcTask = consTaskData->getDeterministicSatisfiableTask();
65 mCompGraphCachedCalcTask = consTaskData->getCompletionGraphCachedSatisfiableTask();
66 if (mDetSatCalcTask && mCompGraphCachedCalcTask) {
67 mDetSatProcNodeVec = mDetSatCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector();
68 mCompGraphCachedProcNodeVec = mCompGraphCachedCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector();
69 mLastLoadedCachedCompletionGraph = true;
70 mDetLocalizationTag = mDetSatCalcTask->getProcessingDataBox()->getProcessContext()->getProcessTagger()->getCurrentLocalizationTag();
71 }
72 }
73 }
74 }
75 if (!mLastLoadedCachedCompletionGraph) {
76 CSatisfiableTaskIncrementalConsistencyTestingAdapter* incConsTestAd = mCalcTask->getSatisfiableTaskIncrementalConsistencyTestingAdapter();
77 if (incConsTestAd) {
78 CConcreteOntology* prevConsOntology = incConsTestAd->getPreviousConsistentOntology();
79 CConsistence* prevConsistence = prevConsOntology->getConsistence();
80 if (prevConsistence) {
81 CConsistenceData* prevConsData = prevConsistence->getConsistenceModelData();
82 if (prevConsData) {
83 CConsistenceTaskData* prevConsTaskData = dynamic_cast<CConsistenceTaskData*>(prevConsData);
84 if (prevConsTaskData) {
85 mDetSatCalcTask = prevConsTaskData->getDeterministicSatisfiableTask();
86 mCompGraphCachedCalcTask = prevConsTaskData->getCompletionGraphCachedSatisfiableTask();
87 if (mDetSatCalcTask && mCompGraphCachedCalcTask) {
88 mDetSatProcNodeVec = mDetSatCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector();
89 mCompGraphCachedProcNodeVec = mCompGraphCachedCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector();
90 mPreviousConsistencyCachedCompletionGraph = true;
91 mIncrementalExpansionCaching = true;
92 mCurrentIncrementalExpansionID = mCalcTask->getProcessingDataBox()->getIncrementalExpansionID();
93 mDetLocalizationTag = mDetSatCalcTask->getProcessingDataBox()->getProcessContext()->getProcessTagger()->getCurrentLocalizationTag();
94 }
95 }
96 }
97 }
98 }
99 }
100 }
101 return mLastLoadedCachedCompletionGraph;
102 }
103
104
105 bool CCompletionGraphCacheHandler::getReactivationIndividuals(CIndividualProcessNode* individualNode, CIndividualReactivationProcessingQueue* reactProcQueue, CCalculationAlgorithmContext* calcAlgContext) {
106 bool addedReactivationIndi = false;
107 if (loadConsistenceModelData(calcAlgContext)) {
108 cint64 indiID = individualNode->getIndividualNodeID();
109 CIndividualProcessNode* compGraphCachedIndiNode = mCompGraphCachedProcNodeVec->getData(indiID);
110 CIndividualProcessNode* detCompGraphCachedIndiNode = mDetSatProcNodeVec->getData(indiID);
111
112 if (compGraphCachedIndiNode && compGraphCachedIndiNode != detCompGraphCachedIndiNode) {
113 cint64 detSatIndiCount = mDetSatProcNodeVec->getItemMaxIndex();
114
115 CPROCESSINGLIST<cint64> reactivateComplGraphCachedIndiList(calcAlgContext->getTaskProcessorContext());
116 CPROCESSINGSET<cint64> reactivateComplGraphCachedIndiSet(calcAlgContext->getTaskProcessorContext());
117
118 CIndividualMergingHash* mergingHash = compGraphCachedIndiNode->getIndividualMergingHash(false);
119 if (mergingHash) {
120 for (CIndividualMergingHash::const_iterator it = mergingHash->constBegin(), itEnd = mergingHash->constEnd(); it != itEnd; ++it) {
121 if (it.value().isMergedWithIndividual()) {
122 cint64 mergedIndiID = it.key();
123 CIndividualProcessNode* compGraphCachedMergedIndiNode = mCompGraphCachedProcNodeVec->getData(-mergedIndiID);
124 CIndividualProcessNode* detSatMergedIndiNode = nullptr;
125 if (-mergedIndiID <= detSatIndiCount) {
126 detSatMergedIndiNode = mDetSatProcNodeVec->getData(-mergedIndiID);
127 }
128 if (compGraphCachedMergedIndiNode != detSatMergedIndiNode && detSatMergedIndiNode) {
129 addedReactivationIndi |= reactProcQueue->insertReactivationIndiviudal(detSatMergedIndiNode, true);
130 }
131 }
132 }
133 }
134
135
136 CConnectionSuccessorSetIterator connSuccSetIt(compGraphCachedIndiNode->getConnectionSuccessorIterator());
137 while (connSuccSetIt.hasNext()) {
138 cint64 connIndiID = connSuccSetIt.next();
139 CIndividualProcessNode* detSatConnIndiNode = nullptr;
140 if (connIndiID <= detSatIndiCount) {
141 detSatConnIndiNode = mDetSatProcNodeVec->getData(connIndiID);
142 }
143 if (detSatConnIndiNode) {
144 CIndividualProcessNode* compGraphCachedConnIndiNode = mCompGraphCachedProcNodeVec->getData(connIndiID);
145 if (compGraphCachedConnIndiNode != detSatConnIndiNode) {
146 addedReactivationIndi |= reactProcQueue->insertReactivationIndiviudal(detSatConnIndiNode,false);
147 }
148 } else {
149 reactivateComplGraphCachedIndiList.append(connIndiID);
150 reactivateComplGraphCachedIndiSet.insert(connIndiID);
151 }
152 }
153
154 while (!reactivateComplGraphCachedIndiList.isEmpty()) {
155 cint64 connIndiID = reactivateComplGraphCachedIndiList.takeFirst();
156 CIndividualProcessNode* detSatConnIndiNode = nullptr;
157 if (connIndiID <= detSatIndiCount) {
158 detSatConnIndiNode = mDetSatProcNodeVec->getData(connIndiID);
159 }
160 if (!detSatConnIndiNode) {
161 CIndividualProcessNode* compGraphCachedConnIndiNode = mCompGraphCachedProcNodeVec->getData(connIndiID);
162 CConnectionSuccessorSetIterator connSuccSetIt(compGraphCachedConnIndiNode->getConnectionSuccessorIterator());
163 while (connSuccSetIt.hasNext()) {
164 cint64 connIndiID = connSuccSetIt.next();
165 if (!reactivateComplGraphCachedIndiSet.contains(connIndiID)) {
166 reactivateComplGraphCachedIndiSet.insert(connIndiID);
167 reactivateComplGraphCachedIndiList.append(connIndiID);
168 }
169 }
170 } else {
171 addedReactivationIndi |= reactProcQueue->insertReactivationIndiviudal(detSatConnIndiNode,true);
172 }
173 }
174 }
175 }
176 return addedReactivationIndi;
177 }
178
179
180 bool CCompletionGraphCacheHandler::isIndividualNodeCompletionGraphConsistenceBlocked(CIndividualProcessNode* individualNode, bool& conceptSetExtended, CCalculationAlgorithmContext* calcAlgContext) {
181 conceptSetExtended = false;
182 if (loadConsistenceModelData(calcAlgContext)) {
183 if (mIncrementalExpansionCaching) {
184 if (individualNode->getIncrementalExpansionID() >= mCurrentIncrementalExpansionID) {
185 return false;
186 }
187 }
188 cint64 detSatIndiCount = mDetSatProcNodeVec->getItemMaxIndex();
189 cint64 indiID = individualNode->getIndividualNodeID();
190 if (indiID <= detSatIndiCount) {
191 CIndividualProcessNode* detSatIndiNode = mDetSatProcNodeVec->getData(indiID);
192 if (detSatIndiNode) {
193 CIndividualProcessNode* compGraphCachedIndiNode = mCompGraphCachedProcNodeVec->getData(indiID);
194 while (compGraphCachedIndiNode->getMergedIntoIndividualNodeID() != compGraphCachedIndiNode->getIndividualNodeID()) {
195 compGraphCachedIndiNode = mCompGraphCachedProcNodeVec->getData(compGraphCachedIndiNode->getMergedIntoIndividualNodeID());
196 }
197 if (testCompletionGraphCached(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext)) {
198 conceptSetExtended = testConceptLabelExtended(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext);
199 return true;
200 }
201 }
202 }
203 }
204 return false;
205 }
206
207
208 bool CCompletionGraphCacheHandler::isIndividualNodeCompletionGraphConsistencePresentNonBlocked(CIndividualProcessNode* individualNode, CCalculationAlgorithmContext* calcAlgContext) {
209 if (loadConsistenceModelData(calcAlgContext)) {
210 if (mIncrementalExpansionCaching) {
211 if (individualNode->getIncrementalExpansionID() >= mCurrentIncrementalExpansionID) {
212 return false;
213 }
214 }
215 cint64 nonDetSatIndiCount = mCompGraphCachedProcNodeVec->getItemMaxIndex();
216 cint64 indiID = individualNode->getIndividualNodeID();
217 if (indiID <= nonDetSatIndiCount) {
218 CIndividualProcessNode* compGraphCachedIndiNode = mCompGraphCachedProcNodeVec->getData(indiID);
219 if (compGraphCachedIndiNode) {
220 bool blocked = compGraphCachedIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFDIRECTBLOCKED | CIndividualProcessNode::PRFINDIRECTBLOCKED | CIndividualProcessNode::PRFPROCESSINGBLOCKED);
221 return !blocked;
222 }
223 }
224 }
225 return false;
226 }
227
228
229
230 bool CCompletionGraphCacheHandler::testConceptLabelExtended(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
231 CReapplyConceptLabelSet* conSet = individualNode->getReapplyConceptLabelSet(false);
232 CReapplyConceptLabelSet* detCachedConSet = detSatIndiNode->getReapplyConceptLabelSet(false);
233 if (conSet->getAddingSortedConceptDescriptionLinker() != detCachedConSet->getAddingSortedConceptDescriptionLinker()) {
234 return true;
235 }
236 return false;
237 }
238
239
240
241 bool CCompletionGraphCacheHandler::testCompletionGraphCached(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
242 bool cached = true;
243 if (cached) {
244 cached &= testConceptLabelSubSetCompletionGraphCached(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext);
245 }
246 if (cached) {
247 cached &= testDistinctSubSetCompletionGraphCached(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext);
248 }
249 if (cached) {
250 cached &= testMergingSubSetCompletionGraphCached(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext);
251 }
252 if (cached) {
253 cached &= testBindingSubSetCompletionGraphCached(individualNode,detSatIndiNode,compGraphCachedIndiNode,calcAlgContext);
254 }
255 return cached;
256 }
257
258
259
260 bool CCompletionGraphCacheHandler::testMergingSubSetCompletionGraphCached(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
261 return true;
262 }
263
264
265 bool CCompletionGraphCacheHandler::testDistinctSubSetCompletionGraphCached(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
266 CDistinctHash* subDistinctHash = individualNode->getDistinctHash(false);
267 CDistinctHash* superDistinctHash = compGraphCachedIndiNode->getDistinctHash(false);
268 if (subDistinctHash) {
269 if (!superDistinctHash) {
270 return false;
271 }
272 for (CDistinctHash::const_iterator it = subDistinctHash->constBegin(), itEnd = subDistinctHash->constEnd(); it != itEnd; ++it) {
273 cint64 disIndiID = it.key();
274 if (!superDistinctHash->isIndividualDistinct(disIndiID)) {
275 return false;
276 }
277 }
278 }
279 return true;
280 }
281
282
283
284 bool CCompletionGraphCacheHandler::testBindingSubSetCompletionGraphCached(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
285
286 CConceptRepresentativePropagationSetHash* compGraphCachedVarRepBindSetHash = compGraphCachedIndiNode->getConceptRepresentativePropagationSetHash(false);
287 CConceptPropagationBindingSetHash* compGraphCachedPropBindSetHash = compGraphCachedIndiNode->getConceptPropagationBindingSetHash(false);
288 CConceptVariableBindingPathSetHash* compGraphCachedVarBindSetHash = compGraphCachedIndiNode->getConceptVariableBindingPathSetHash(false);
289
290
291 CConceptRepresentativePropagationSetHash* indiVarRepBindSetHash = individualNode->getConceptRepresentativePropagationSetHash(false);
292 CConceptPropagationBindingSetHash* indiPropBindSetHash = individualNode->getConceptPropagationBindingSetHash(false);
293 CConceptVariableBindingPathSetHash* indiVarBindSetHash = individualNode->getConceptVariableBindingPathSetHash(false);
294
295 if (indiVarRepBindSetHash || indiPropBindSetHash || indiVarRepBindSetHash) {
296 CReapplyConceptLabelSet* subConceptSet = individualNode->getReapplyConceptLabelSet(false);
297 CReapplyConceptLabelSetIterator subConSetIt = subConceptSet->getConceptLabelSetIterator(true, false, false);
298 while (subConSetIt.hasValue()) {
299 CConceptDescriptor* subConDes = subConSetIt.getConceptDescriptor();
300 CConcept* concept = subConDes->getConcept();
301
302 CConceptOperator* conOperator = concept->getConceptOperator();
303 if (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_PROPAGATION_BIND_TYPE)) {
304 if (indiPropBindSetHash) {
305 CPropagationBindingSet* indiPropBindSet = indiPropBindSetHash->getPropagationBindingSet(concept, false);
306 if (indiPropBindSet) {
307 CPropagationBindingMap* indiPropBindMap = indiPropBindSet->getPropagationBindingMap();
308 if (!indiPropBindMap->empty()) {
309 if (!compGraphCachedPropBindSetHash) {
310 return false;
311 }
312 CPropagationBindingSet* compGraphCachedPropBindSet = compGraphCachedPropBindSetHash->getPropagationBindingSet(concept, false);
313 if (!compGraphCachedPropBindSet) {
314 return false;
315 }
316 CPropagationBindingMap* compGraphCachedPropBindMap = compGraphCachedPropBindSet->getPropagationBindingMap();
317
318 if (!indiPropBindMap->isKeySubSetOf(compGraphCachedPropBindMap)) {
319 return false;
320 }
321 }
322 }
323 }
324 }
325 if (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_VARIABLE_BIND_TYPE)) {
326 if (indiVarBindSetHash) {
327 CVariableBindingPathSet* indiVarBindSet = indiVarBindSetHash->getVariableBindingPathSet(concept, false);
328 if (indiVarBindSet) {
329 CVariableBindingPathMap* indiVarBindMap = indiVarBindSet->getVariableBindingPathMap();
330 if (!indiVarBindMap->empty()) {
331 if (!compGraphCachedVarBindSetHash) {
332 return false;
333 }
334 CVariableBindingPathSet* compGraphCachedVarBindSet = compGraphCachedVarBindSetHash->getVariableBindingPathSet(concept, false);
335 if (!compGraphCachedVarBindSet) {
336 return false;
337 }
338 CVariableBindingPathMap* compGraphCachedVarBindMap = compGraphCachedVarBindSet->getVariableBindingPathMap();
339
340 if (!indiVarBindMap->isKeySubSetOf(compGraphCachedVarBindMap)) {
341 return false;
342 }
343 }
344 }
345 }
346 if (indiVarRepBindSetHash) {
347 CRepresentativePropagationSet* indiVarRepBindSet = indiVarRepBindSetHash->getRepresentativePropagationSet(concept, false);
348 if (indiVarRepBindSet) {
349 CRepresentativePropagationMap* indiVarRepBindMap = indiVarRepBindSet->getRepresentativePropagationMap();
350 if (!indiVarRepBindMap->empty()) {
351 if (!compGraphCachedVarRepBindSetHash) {
352 return false;
353 }
354 CRepresentativePropagationSet* compGraphCachedVarRepBindSet = compGraphCachedVarRepBindSetHash->getRepresentativePropagationSet(concept, false);
355 if (!compGraphCachedVarRepBindSet) {
356 return false;
357 }
358 CRepresentativePropagationMap* compGraphCachedVarRepBindMap = compGraphCachedVarRepBindSet->getRepresentativePropagationMap();
359
360 if (!indiVarRepBindMap->isKeySubSetOf(compGraphCachedVarRepBindMap)) {
361 return false;
362 }
363 }
364 }
365 }
366 }
367
368 subConSetIt.moveNext();
369 }
370 }
371 return true;
372 }
373
374
375 bool CCompletionGraphCacheHandler::testConceptLabelSubSetCompletionGraphCached(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CCalculationAlgorithmContext* calcAlgContext) {
376 CReapplyConceptLabelSet* subConceptSet = individualNode->getReapplyConceptLabelSet(false);
377 CReapplyConceptLabelSet* superConceptSet = compGraphCachedIndiNode->getReapplyConceptLabelSet(false);
378 cint64 subConSetCount = subConceptSet->getConceptCount();
379 cint64 superConSetCount = superConceptSet->getConceptCount();
380 if (subConSetCount > superConSetCount) {
381 return false;
382 }
383 if (superConSetCount == 0) {
384 return true;
385 }
386 CReapplyConceptLabelSetIterator subConSetIt = subConceptSet->getConceptLabelSetIterator(true,false,false);
387 CReapplyConceptLabelSetIterator superConSetIt = superConceptSet->getConceptLabelSetIterator(true,false,false);
388 CConceptDescriptor* superConDes = superConSetIt.getConceptDescriptor();
389 cint64 superConTag = superConDes->getConceptTag();
390 superConSetIt.moveNext();
391 bool contained = false;
392 while (subConSetIt.hasValue()) {
393 CConceptDescriptor* subConDes = subConSetIt.getConceptDescriptor();
394 cint64 subConTag = subConDes->getConceptTag();
395
396 while (superConTag < subConTag) {
397 if (!superConSetIt.hasValue()) {
398 return false;
399 }
400 if (testCriticalConcept(individualNode,detSatIndiNode,compGraphCachedIndiNode,superConDes,contained,calcAlgContext)) {
401 return false;
402 }
403 contained = false;
404 superConDes = superConSetIt.getConceptDescriptor();
405 superConTag = superConDes->getConceptTag();
406 superConSetIt.moveNext();
407 }
408 if (subConTag != superConTag) {
409 return false;
410 } else {
411 if (subConDes->isNegated() != superConDes->isNegated()) {
412 return false;
413 }
414 contained = true;
415 }
416 subConSetIt.moveNext();
417 }
418
419 while (superConSetIt.hasValue()) {
420 if (testCriticalConcept(individualNode,detSatIndiNode,compGraphCachedIndiNode,superConDes,contained,calcAlgContext)) {
421 return false;
422 }
423 contained = false;
424 superConDes = superConSetIt.getConceptDescriptor();
425 superConTag = superConDes->getConceptTag();
426 superConSetIt.moveNext();
427 }
428 if (testCriticalConcept(individualNode,detSatIndiNode,compGraphCachedIndiNode,superConDes,contained,calcAlgContext)) {
429 return false;
430 }
431
432 return true;
433 }
434
435
436 bool CCompletionGraphCacheHandler::testCriticalConcept(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CConceptDescriptor* conDes, bool containedFlag, CCalculationAlgorithmContext* calcAlgContext) {
437 CConcept* concept = conDes->getConcept();
438 bool conNegation = conDes->getNegation();
439 cint64 opCode = concept->getOperatorCode();
440 CRole* role = concept->getRole();
441 CConceptOperator* conOperator = concept->getConceptOperator();
442 if (!containedFlag) {
443 if (!conNegation && (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_ALL_AQALL_TYPE)) || conNegation && opCode == CCSOME || !conNegation && (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_AQAND_TYPE))) {
444 return testCriticalB2AutomateTransitionOperands(individualNode,detSatIndiNode,compGraphCachedIndiNode,concept, conNegation,calcAlgContext);
445 }
446 }
447 if (!conNegation && opCode == CCATMOST || conNegation && opCode == CCATLEAST) {
448 // check cardinality
449 cint64 cardinality = concept->getParameter() - 1*conNegation;
450
451 bool nominalIndiNode = individualNode->isNominalIndividualNode();
452
453 cint64 updatedCardinality = 0;
454 CRoleSuccessorLinkIterator succLinkIt = individualNode->getRoleSuccessorLinkIterator(role);
455 while (updatedCardinality <= cardinality && succLinkIt.hasNext()) {
456 CIndividualLinkEdge* link = succLinkIt.next();
457 CSortedNegLinker<CConcept*>* opConLinkerIt = concept->getOperandList();
458 CIndividualProcessNode* succIndi = link->getOppositeIndividual(individualNode);
459 if (link->isLocalizationTagUpdated(calcAlgContext->getUsedProcessTagger())) {
460 succIndi = mIndiProcNodeVec->getData(link->getOppositeIndividualID(individualNode));
461 }
462 if (nominalIndiNode) {
463 if (link->getCreatorIndividualID() != individualNode->getIndividualNodeID()) {
464 if (succIndi->isBlockableIndividual()) {
465 // requires NN-rule
466 return true;
467 }
468 }
469 }
470 if (succIndi->getLocalizationTag() > mDetLocalizationTag) {
471 if (succIndi->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALIDATED | CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALID)) {
472 bool containsSomeOps = true;
473 if (opConLinkerIt) {
474 containsSomeOps = false;
475 while (opConLinkerIt && !containsSomeOps) {
476 bool opNegated = opConLinkerIt->isNegated() ^ conNegation;
477 CConcept* opCon = opConLinkerIt->getData();
478 bool containsNegated = false;
479 CReapplyConceptLabelSet* succConSet = succIndi->getReapplyConceptLabelSet(false);
480 if (succConSet) {
481 if (succConSet->containsConcept(opCon, &containsNegated)) {
482 if (containsNegated == opNegated) {
483 containsSomeOps = true;
484 }
485 } else {
486 containsSomeOps = true;
487 }
488 }
489 opConLinkerIt = opConLinkerIt->getNext();
490 }
491 if (containsSomeOps) {
492 CIndividualProcessNode* corrCachedSuccIndi = mCompGraphCachedProcNodeVec->getData(succIndi->getIndividualNodeID());
493 if (!corrCachedSuccIndi || corrCachedSuccIndi->getMergedIntoIndividualNodeID() != succIndi->getIndividualNodeID() || corrCachedSuccIndi->getLastMergedIntoIndividualNode() != succIndi->getLastMergedIntoIndividualNode()) {
494 ++updatedCardinality;
495 }
496 }
497 }
498 }
499 }
500 }
501
502 if (updatedCardinality > cardinality) {
503 return true;
504 }
505
506
507
508 CReapplyRoleSuccessorHash* cachedSuccHash = compGraphCachedIndiNode->getReapplyRoleSuccessorHash(false);
509 if (cachedSuccHash) {
510 cint64 linkCount = 0;
511 CRoleSuccessorLinkIterator succLinkIt = cachedSuccHash->getRoleSuccessorLinkIterator(role, &linkCount);
512
513 // check whether some links are lazily expanded from backend cache
514 if (individualNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFSYNCHRONIZEDBACKENNEIGHBOURDPARTIALEXPANSION)) {
515
516 visitMergedIndividualsBackendSynchronisationData(individualNode, [&](CIndividualProcessNode* baseIndiNode, CIndividualProcessNode* mergedDataIndiNode) -> bool {
517 CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData* mergedBackendSyncData = (CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData*)mergedDataIndiNode->getIndividualBackendCacheSynchronisationData(false);
518 if (mergedBackendSyncData) {
519 CBackendRepresentativeMemoryCacheIndividualAssociationData* mergedAssocData = mergedBackendSyncData->getAssocitaionData();
520 mBackendCacheHandler->visitNeighbourIndividualIdsForRole(mergedAssocData, role, [&](cint64 neighbourIndividualId, CBackendRepresentativeMemoryLabelCacheItem* neighbourRoleSetLabel, bool nondeterministic)->bool {
521
522 CIndividualProcessNode* neighbourIndiNode = mIndiProcNodeVec->getData(-neighbourIndividualId);
523 while (neighbourIndiNode->getMergedIntoIndividualNodeID() != neighbourIndiNode->getIndividualNodeID()) {
524 neighbourIndiNode = mIndiProcNodeVec->getData(neighbourIndiNode->getMergedIntoIndividualNodeID());
525 }
526
527 if (!neighbourIndiNode || neighbourIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALIDATED | CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALID)) {
528
529 CIndividualProcessNode* neighbourCompCachedIndiNode = mCompGraphCachedProcNodeVec->getData(-neighbourIndividualId);
530 while (neighbourCompCachedIndiNode && neighbourCompCachedIndiNode->getMergedIntoIndividualNodeID() != neighbourCompCachedIndiNode->getIndividualNodeID()) {
531 neighbourCompCachedIndiNode = mCompGraphCachedProcNodeVec->getData(neighbourCompCachedIndiNode->getMergedIntoIndividualNodeID());
532 }
533
534 if (neighbourCompCachedIndiNode && compGraphCachedIndiNode->hasRoleSuccessorToIndividual(role, neighbourCompCachedIndiNode, false)) {
535
536 bool containsSomeOps = true;
537 CSortedNegLinker<CConcept*>* opConLinkerIt = concept->getOperandList();
538 if (opConLinkerIt) {
539 containsSomeOps = false;
540 while (opConLinkerIt && !containsSomeOps) {
541 bool opNegated = opConLinkerIt->isNegated();
542 CConcept* opCon = opConLinkerIt->getData();
543 CReapplyConceptLabelSet* succConSet = neighbourCompCachedIndiNode->getReapplyConceptLabelSet(false);
544 if (succConSet && succConSet->containsConcept(opCon, opNegated)) {
545 containsSomeOps = true;
546 }
547 opConLinkerIt = opConLinkerIt->getNext();
548 }
549 }
550 if (containsSomeOps && !neighbourIndiNode || !individualNode->hasRoleSuccessorToIndividual(role, neighbourIndiNode, false)) {
551 ++updatedCardinality;
552 }
553
554 }
555 }
556
557 return updatedCardinality <= cardinality;
558 }, true, calcAlgContext);
559 }
560
561 return updatedCardinality <= cardinality;
562 }, calcAlgContext);
563
564
565 if (updatedCardinality > cardinality) {
566 return true;
567 }
568 }
569
570 if (cardinality - linkCount < updatedCardinality) {
571
572 cint64 cachedCardinality = 0;
573 while (succLinkIt.hasNext()) {
574 CIndividualLinkEdge* link = succLinkIt.next();
575 CSortedNegLinker<CConcept*>* opConLinkerIt = concept->getOperandList();
576 if (!opConLinkerIt) {
577 ++cachedCardinality;
578 } else {
579 CIndividualProcessNode* succIndi = link->getOppositeIndividual(individualNode);
580 if (link->isLocalizationTagUpdated(mCompGraphCachedCalcTask->getProcessingDataBox()->getProcessContext()->getUsedProcessTagger())) {
581 succIndi = mCompGraphCachedProcNodeVec->getData(link->getOppositeIndividualID(individualNode));
582 }
583 bool containsSomeOps = false;
584 while (opConLinkerIt && !containsSomeOps) {
585 bool opNegated = opConLinkerIt->isNegated();
586 CConcept* opCon = opConLinkerIt->getData();
587 CReapplyConceptLabelSet* succConSet = succIndi->getReapplyConceptLabelSet(false);
588 if (succConSet && succConSet->containsConcept(opCon,opNegated)) {
589 containsSomeOps = true;
590 }
591 opConLinkerIt = opConLinkerIt->getNext();
592 }
593 if (containsSomeOps) {
594 ++cachedCardinality;
595 }
596 }
597 if (updatedCardinality + cachedCardinality > cardinality) {
598 return true;
599 }
600 }
601
602 }
603 }
604
605 }
606 return false;
607 }
608
609
610
611
612
613
614
615 bool CCompletionGraphCacheHandler::visitMergedIndividualsBackendSynchronisationData(CIndividualProcessNode* indiNode, function<bool(CIndividualProcessNode* baseIndiNode, CIndividualProcessNode* mergedDataIndiNode)> visitFunc, CCalculationAlgorithmContext* calcAlgContext) {
616 bool visited = false;
617 bool continueVisiting = true;
618 CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData* backendSyncData = (CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData*)indiNode->getIndividualBackendCacheSynchronisationData(false);
619 if (backendSyncData) {
620 continueVisiting = visitFunc(indiNode, indiNode);
621 visited = true;
622 }
623 CBackendRepresentativeMemoryCacheIndividualAssociationData* assocData = backendSyncData->getAssocitaionData();
624 CBackendRepresentativeMemoryLabelCacheItem* detSameIndiLabel = assocData->getLabelCacheEntry(CBackendRepresentativeMemoryLabelCacheItem::DETERMINISTIC_SAME_INDIVIDUAL_SET_LABEL);
625 CIndividualMergingHash* mergingHash = indiNode->getIndividualMergingHash(false);
626 if (mergingHash) {
627 for (CXLinker<cint64>* newIndiMergedLinkerIt = mergingHash->getMergedIndividualLinker(); newIndiMergedLinkerIt && continueVisiting; newIndiMergedLinkerIt = newIndiMergedLinkerIt->getNext()) {
628 cint64 newIndiMergedId = newIndiMergedLinkerIt->getData();
629
630 if (!mBackendCacheHandler->hasIndividualIdsInAssociatedIndividualSetLabel(assocData, detSameIndiLabel, newIndiMergedId)) {
631
632 CIndividualProcessNode* backendSyncDataIndiNode = mIndiProcNodeVec->getData(-newIndiMergedId);
633
634 if (backendSyncDataIndiNode->getIndividualBackendCacheSynchronisationData(false)) {
635 continueVisiting = visitFunc(indiNode, backendSyncDataIndiNode);
636 visited = true;
637 }
638 }
639 }
640 }
641 return visited;
642 }
643
644
645
646
647 bool CCompletionGraphCacheHandler::testCriticalB2AutomateTransitionOperands(CIndividualProcessNode* individualNode, CIndividualProcessNode* detSatIndiNode, CIndividualProcessNode* compGraphCachedIndiNode, CConcept* concept, bool conNegation, CCalculationAlgorithmContext* calcAlgContext) {
648 cint64 opCode = concept->getOperatorCode();
649 CConceptOperator* conOperator = concept->getConceptOperator();
650 if (!conNegation && conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_AQAND_TYPE)) {
651 // recursive
652 CSortedNegLinker<CConcept*>* opLinkerIt = concept->getOperandList();
653 while (opLinkerIt) {
654 CConcept* opConcept = opLinkerIt->getData();
655 if (testCriticalB2AutomateTransitionOperands(individualNode,detSatIndiNode,compGraphCachedIndiNode,opConcept, opLinkerIt->isNegated() ^ conNegation,calcAlgContext)) {
656 return true;
657 }
658 opLinkerIt = opLinkerIt->getNext();
659 }
660 } else if (!conNegation && (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_ALL_AQALL_TYPE)) || conNegation && opCode == CCSOME) {
661 CRole* conRole = concept->getRole();
662 CRoleSuccessorLinkIterator succLinkIt = individualNode->getRoleSuccessorLinkIterator(conRole);
663 while (succLinkIt.hasNext()) {
664 CIndividualLinkEdge* link = succLinkIt.next();
665 CIndividualProcessNode* succIndi = link->getOppositeIndividual(individualNode);
666 if (link->isLocalizationTagUpdated(calcAlgContext->getUsedProcessTagger())) {
667 succIndi = mIndiProcNodeVec->getData(link->getOppositeIndividualID(individualNode));
668 }
669 if (succIndi->getLocalizationTag() > mDetLocalizationTag && !succIndi->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHED)
670 || succIndi->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALIDATED | CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALID)) {
671 return checkPropagatedConceptsMissing(concept, conNegation, succIndi, conOperator, compGraphCachedIndiNode);
672 }
673
674 }
675
676
677
678 if (individualNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFSYNCHRONIZEDBACKENNEIGHBOURDPARTIALEXPANSION)) {
679
680 bool criticalMissingLink = false;
681 visitMergedIndividualsBackendSynchronisationData(individualNode, [&](CIndividualProcessNode* baseIndiNode, CIndividualProcessNode* mergedDataIndiNode) -> bool {
682 CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData* mergedBackendSyncData = (CIndividualNodeRepresentativeMemoryBackendCacheSynchronisationData*)mergedDataIndiNode->getIndividualBackendCacheSynchronisationData(false);
683 if (mergedBackendSyncData) {
684 CBackendRepresentativeMemoryCacheIndividualAssociationData* mergedAssocData = mergedBackendSyncData->getAssocitaionData();
685 mBackendCacheHandler->visitNeighbourIndividualIdsForRole(mergedAssocData, conRole, [&](cint64 neighbourIndividualId, CBackendRepresentativeMemoryLabelCacheItem* neighbourRoleSetLabel, bool nondeterministic)->bool {
686
687
688 CIndividualProcessNode* neighbourIndiNode = mIndiProcNodeVec->getData(-neighbourIndividualId);
689 while (neighbourIndiNode && neighbourIndiNode->getMergedIntoIndividualNodeID() != neighbourIndiNode->getIndividualNodeID()) {
690 neighbourIndiNode = mIndiProcNodeVec->getData(neighbourIndiNode->getMergedIntoIndividualNodeID());
691 }
692
693
694 if (!neighbourIndiNode || neighbourIndiNode->hasPartialProcessingRestrictionFlags(CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALIDATED | CIndividualProcessNode::PRFCOMPLETIONGRAPHCACHINGINVALID)) {
695
696 CIndividualProcessNode* neighbourCompCachedIndiNode = mCompGraphCachedProcNodeVec->getData(-neighbourIndividualId);
697 while (neighbourCompCachedIndiNode && neighbourCompCachedIndiNode->getMergedIntoIndividualNodeID() != neighbourCompCachedIndiNode->getIndividualNodeID()) {
698 neighbourCompCachedIndiNode = mCompGraphCachedProcNodeVec->getData(neighbourCompCachedIndiNode->getMergedIntoIndividualNodeID());
699 }
700
701 if (neighbourCompCachedIndiNode && compGraphCachedIndiNode->hasRoleSuccessorToIndividual(conRole, neighbourCompCachedIndiNode, false)) {
702
703 if (!neighbourIndiNode) {
704 criticalMissingLink = true;
705 } else if (!individualNode->hasRoleSuccessorToIndividual(conRole, neighbourIndiNode, false)) {
706 if (checkPropagatedConceptsMissing(concept, conNegation, neighbourIndiNode, conOperator, neighbourCompCachedIndiNode)) {
707 criticalMissingLink = true;
708 }
709 }
710
711
712 }
713 }
714
715 return !criticalMissingLink;
716 }, true, calcAlgContext);
717 }
718
719 return !criticalMissingLink;
720 }, calcAlgContext);
721
722
723 if (criticalMissingLink) {
724 return true;
725 }
726 }
727 }
728 return true;
729 }
730
731
732 bool CCompletionGraphCacheHandler::checkPropagatedConceptsMissing(CConcept* concept, bool conNegation, CIndividualProcessNode* succIndi, CConceptOperator* conOperator, CIndividualProcessNode* compGraphCachedIndiNode) {
733 CSortedNegLinker<CConcept*>* opConLinkerIt = concept->getOperandList();
734 while (opConLinkerIt) {
735 bool opNegated = opConLinkerIt->isNegated() ^ conNegation;
736 CConcept* opCon = opConLinkerIt->getData();
737 CReapplyConceptLabelSet* succConSet = succIndi->getReapplyConceptLabelSet(false);
738 if (!succConSet || !succConSet->containsConcept(opCon, opNegated)) {
739 return true;
740 }
741 if (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCF_PBINDAQALL | CConceptOperator::CCF_PBINDALL)) {
742 CConceptPropagationBindingSetHash* compGraphCachedPropBindSetHash = compGraphCachedIndiNode->getConceptPropagationBindingSetHash(false);
743 if (compGraphCachedPropBindSetHash) {
744 CPropagationBindingSet* compGraphCachedPropBindSet = compGraphCachedPropBindSetHash->getPropagationBindingSet(concept, false);
745 if (compGraphCachedPropBindSet) {
746 CPropagationBindingMap* compGraphCachedPropBindMap = compGraphCachedPropBindSet->getPropagationBindingMap();
747 if (!compGraphCachedPropBindMap->empty()) {
748 CConceptPropagationBindingSetHash* succPropBindSetHash = succIndi->getConceptPropagationBindingSetHash(false);
749 if (!succPropBindSetHash) {
750 return true;
751 }
752 CPropagationBindingSet* succPropBindSet = succPropBindSetHash->getPropagationBindingSet(opCon, false);
753 if (!succPropBindSet) {
754 return true;
755 }
756 CPropagationBindingMap* succPropBindMap = succPropBindSet->getPropagationBindingMap();
757
758 if (!compGraphCachedPropBindMap->isKeySubSetOf(succPropBindMap)) {
759 return true;
760 }
761 }
762 }
763 }
764 }
765 if (conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCF_VARBINDAQALL | CConceptOperator::CCF_VARBINDALL)) {
766 CConceptVariableBindingPathSetHash* compGraphCachedVarBindSetHash = compGraphCachedIndiNode->getConceptVariableBindingPathSetHash(false);
767 if (compGraphCachedVarBindSetHash) {
768 CVariableBindingPathSet* compGraphCachedVarBindSet = compGraphCachedVarBindSetHash->getVariableBindingPathSet(concept, false);
769 if (compGraphCachedVarBindSet) {
770 CVariableBindingPathMap* compGraphCachedVarBindMap = compGraphCachedVarBindSet->getVariableBindingPathMap();
771 if (!compGraphCachedVarBindMap->empty()) {
772 CConceptVariableBindingPathSetHash* succVarBindSetHash = succIndi->getConceptVariableBindingPathSetHash(false);
773 if (!succVarBindSetHash) {
774 return true;
775 }
776 CVariableBindingPathSet* succVarBindSet = succVarBindSetHash->getVariableBindingPathSet(opCon, false);
777 if (!succVarBindSet) {
778 return true;
779 }
780 CVariableBindingPathMap* succVarBindMap = succVarBindSet->getVariableBindingPathMap();
781
782 if (!compGraphCachedVarBindMap->isKeySubSetOf(succVarBindMap)) {
783 return true;
784 }
785 }
786 }
787 }
788 CConceptRepresentativePropagationSetHash* compGraphCachedVarRepBindSetHash = compGraphCachedIndiNode->getConceptRepresentativePropagationSetHash(false);
789 if (compGraphCachedVarRepBindSetHash) {
790 CRepresentativePropagationSet* compGraphCachedVarRepBindSet = compGraphCachedVarRepBindSetHash->getRepresentativePropagationSet(concept, false);
791 if (compGraphCachedVarRepBindSet) {
792 CRepresentativePropagationMap* compGraphCachedVarRepBindMap = compGraphCachedVarRepBindSet->getRepresentativePropagationMap();
793 if (!compGraphCachedVarRepBindMap->empty()) {
794 CConceptRepresentativePropagationSetHash* succVarRepBindSetHash = succIndi->getConceptRepresentativePropagationSetHash(false);
795 if (!succVarRepBindSetHash) {
796 return true;
797 }
798 CRepresentativePropagationSet* succVarRepBindSet = succVarRepBindSetHash->getRepresentativePropagationSet(opCon, false);
799 if (!succVarRepBindSet) {
800 return true;
801 }
802 CRepresentativePropagationMap* succVarRepBindMap = succVarRepBindSet->getRepresentativePropagationMap();
803
804 if (!compGraphCachedVarRepBindMap->isKeySubSetOf(succVarRepBindMap)) {
805 return true;
806 }
807 }
808 }
809 }
810 }
811 opConLinkerIt = opConLinkerIt->getNext();
812 }
813 return false;
814 }
815
816 }; // end namespace Algorithm
817
818 }; // end namespace Kernel
819
820 }; // end namespace Reasoner
821
822 }; // end namespace Konclude