| 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 "CComputedConsequencesCacheHandler.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Algorithm { |
| 31 |
|
| 32 |
CComputedConsequencesCacheHandler::CComputedConsequencesCacheHandler(CComputedConsequencesCacheReader* satCacheReader, CComputedConsequencesCacheWriter* satCacheWriter) { |
| 33 |
mSatCacheReader = satCacheReader; |
| 34 |
mSatCacheWriter = satCacheWriter; |
| 35 |
|
| 36 |
mTmpContext = nullptr; |
| 37 |
mMemAllocMan = nullptr; |
| 38 |
mWriteData = nullptr; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
bool CComputedConsequencesCacheHandler::canCacheTypeConcept(CIndividual* individual, CConcept* concept, bool negation, CCalculationAlgorithmContext* calcAlgContext) { |
| 46 |
CConcreteOntology* ontology = calcAlgContext->getProcessingDataBox()->getOntology(); |
| 47 |
CConsistence* consistence = ontology->getConsistence(); |
| 48 |
CBOXHASH<CConcept*,CConcept*>* eqConCandHash = ontology->getTBox()->getEquivalentConceptCandidateHash(false); |
| 49 |
if (consistence && concept->getTerminology()) { |
| 50 |
CConsistenceData* consData = consistence->getConsistenceModelData(); |
| 51 |
if (consData) { |
| 52 |
CConsistenceTaskData* consTaskData = dynamic_cast<CConsistenceTaskData*>(consData); |
| 53 |
if (consTaskData) { |
| 54 |
CSatisfiableCalculationTask* detSatCalcTask = consTaskData->getDeterministicSatisfiableTask(); |
| 55 |
if (detSatCalcTask) { |
| 56 |
CIndividualProcessNodeVector* detSatProcNodeVec = detSatCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector(); |
| 57 |
CIndividualProcessNode* detIndiNode = detSatProcNodeVec->getData(-individual->getIndividualID()); |
| 58 |
while (detIndiNode->getIndividualNodeID() != detIndiNode->getMergedIntoIndividualNodeID()) { |
| 59 |
detIndiNode = detSatProcNodeVec->getData(detIndiNode->getMergedIntoIndividualNodeID()); |
| 60 |
} |
| 61 |
CReapplyConceptLabelSet* detConSet = detIndiNode->getReapplyConceptLabelSet(false); |
| 62 |
if (detConSet && !detConSet->containsConcept(concept,negation)) { |
| 63 |
//if (concept->getTerminology() == ontology) { |
| 64 |
// return true; |
| 65 |
//} |
| 66 |
CSatisfiableCalculationTask* compGraphCachedCalcTask = consTaskData->getCompletionGraphCachedSatisfiableTask(); |
| 67 |
CIndividualProcessNodeVector* compGraphCachedProcNodeVec = compGraphCachedCalcTask->getProcessingDataBox()->getIndividualProcessNodeVector(); |
| 68 |
CIndividualProcessNode* compGraphCachedIndiNode = compGraphCachedProcNodeVec->getData(-individual->getIndividualID()); |
| 69 |
while (compGraphCachedIndiNode->getIndividualNodeID() != compGraphCachedIndiNode->getMergedIntoIndividualNodeID()) { |
| 70 |
compGraphCachedIndiNode = detSatProcNodeVec->getData(compGraphCachedIndiNode->getIndividualNodeID()); |
| 71 |
} |
| 72 |
CReapplyConceptLabelSet* compGraphCachedConSet = compGraphCachedIndiNode->getReapplyConceptLabelSet(false); |
| 73 |
if (compGraphCachedConSet && compGraphCachedConSet->containsConcept(concept,negation)) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
if (!negation && eqConCandHash) { |
| 77 |
CConcept* candidateConcept = eqConCandHash->value(concept); |
| 78 |
if (compGraphCachedConSet && candidateConcept && compGraphCachedConSet->containsConcept(candidateConcept,false)) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
bool CComputedConsequencesCacheHandler::tryCacheTypeConcept(CIndividual* individual, CConcept* concept, bool negation, CCalculationAlgorithmContext* calcAlgContext) { |
| 94 |
CComputedConsequencesTypesCacheEntry* typeCacheEntry = mSatCacheReader->getTypesCacheEntry(individual); |
| 95 |
|
| 96 |
if ((!typeCacheEntry || !typeCacheEntry->hasConcept(concept,negation)) && canCacheTypeConcept(individual,concept,negation,calcAlgContext)) { |
| 97 |
// try caching |
| 98 |
prepareCacheMessages(calcAlgContext); |
| 99 |
CTaskMemoryPoolAllocationManager* satCacheMemMan = mMemAllocMan; |
| 100 |
CContextBase* tmpContext = mTmpContext; |
| 101 |
|
| 102 |
CComputedConsequencesCacheWriteTypesData* unsatWriteData = CObjectAllocator< CComputedConsequencesCacheWriteTypesData >::allocateAndConstruct(satCacheMemMan); |
| 103 |
unsatWriteData->initTypesCacheWriteData(individual,concept,negation); |
| 104 |
addCacheMessages(unsatWriteData,calcAlgContext); |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
CSortedNegLinker<CConcept*>* CComputedConsequencesCacheHandler::getCachedTypesConceptLinker(CIndividual* individual, CCalculationAlgorithmContext* calcAlgContext) { |
| 113 |
CSortedNegLinker<CConcept*>* typeConceptLinker = nullptr; |
| 114 |
CComputedConsequencesTypesCacheEntry* typeCacheEntry = mSatCacheReader->getTypesCacheEntry(individual); |
| 115 |
if (typeCacheEntry) { |
| 116 |
typeConceptLinker = typeCacheEntry->getConceptLinker(); |
| 117 |
} |
| 118 |
return typeConceptLinker; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
bool CComputedConsequencesCacheHandler::prepareCacheMessages(CCalculationAlgorithmContext* calcAlgContext) { |
| 125 |
if (!mMemAllocMan) { |
| 126 |
CTaskMemoryPoolAllocationManager satCacheMemManCreaterMemMan(&mMemPoolCon,calcAlgContext->getTemporaryMemoryAllocationManager()); |
| 127 |
mMemAllocMan = CObjectAllocator<CTaskMemoryPoolAllocationManager>::allocateAndConstruct(&satCacheMemManCreaterMemMan); |
| 128 |
mMemAllocMan->initTaskMemoryPoolAllocationManager(&mMemPoolCon,calcAlgContext->getTemporaryMemoryAllocationManager()); |
| 129 |
mTmpContext = CObjectParameterizingAllocator< CContextBase,CMemoryAllocationManager* >::allocateAndConstructAndParameterize(mMemAllocMan,mMemAllocMan); |
| 130 |
return true; |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
bool CComputedConsequencesCacheHandler::commitCacheMessages(CCalculationAlgorithmContext* calcAlgContext) { |
| 136 |
if (mWriteData) { |
| 137 |
CComputedConsequencesCacheWriteData* commitWriteData = nullptr; |
| 138 |
CComputedConsequencesCacheWriteData* writeDataIt = mWriteData; |
| 139 |
while (writeDataIt) { |
| 140 |
CComputedConsequencesCacheWriteData* tmpWriteData = writeDataIt; |
| 141 |
writeDataIt = (CComputedConsequencesCacheWriteData*)writeDataIt->getNext(); |
| 142 |
tmpWriteData->clearNext(); |
| 143 |
if (commitWriteData) { |
| 144 |
commitWriteData = (CComputedConsequencesCacheWriteData*)tmpWriteData->append(commitWriteData); |
| 145 |
} else { |
| 146 |
commitWriteData = tmpWriteData; |
| 147 |
} |
| 148 |
} |
| 149 |
mSatCacheWriter->writeCacheData(commitWriteData,mMemPoolCon.takeMemoryPools()); |
| 150 |
mWriteData = nullptr; |
| 151 |
mMemAllocMan = nullptr; |
| 152 |
mTmpContext = nullptr; |
| 153 |
return true; |
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
bool CComputedConsequencesCacheHandler::addCacheMessages(CComputedConsequencesCacheWriteData* expandWriteData, CCalculationAlgorithmContext* calcAlgContext) { |
| 161 |
if (mWriteData) { |
| 162 |
mWriteData = (CComputedConsequencesCacheWriteData*)expandWriteData->append(mWriteData); |
| 163 |
} else { |
| 164 |
mWriteData = expandWriteData; |
| 165 |
} |
| 166 |
commitCacheMessages(calcAlgContext); |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
}; // end namespace Algorithm |
| 175 |
|
| 176 |
}; // end namespace Kernel |
| 177 |
|
| 178 |
}; // end namespace Reasoner |
| 179 |
|
| 180 |
}; // end namespace Konclude |