| 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 "CComputedConsequencesCache.h" |
| 22 |
#include "CComputedConsequencesCacheWriter.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Kernel { |
| 30 |
|
| 31 |
namespace Cache { |
| 32 |
|
| 33 |
|
| 34 |
CComputedConsequencesCache::CComputedConsequencesCache(CConfiguration* config, QString threadIdentifierName, CWatchDog *watchDogThread) : CThread(threadIdentifierName,watchDogThread) { |
| 35 |
|
| 36 |
|
| 37 |
mReaderLinker = nullptr; |
| 38 |
|
| 39 |
CMemoryAllocationManager* memMan = mContext.getMemoryAllocationManager(); |
| 40 |
|
| 41 |
mConfAllowedNonDetExpansionCount = 1; |
| 42 |
if (config) { |
| 43 |
mConfAllowedNonDetExpansionCount = CConfigDataReader::readConfigInteger(config,"Konclude.Calculation.Optimization.SaturationExpansionSatisfiabilityCacheCount",1); |
| 44 |
} |
| 45 |
|
| 46 |
startThread(QThread::HighestPriority); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
CComputedConsequencesCache::~CComputedConsequencesCache() { |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
CComputedConsequencesCache* CComputedConsequencesCache::writeCacheData(CComputedConsequencesCacheWriteData* writeData, CMemoryPool* memoryPools) { |
| 56 |
postEvent(new CWriteComputedConcequencesCacheEntryEvent(writeData,memoryPools)); |
| 57 |
return this; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
CCacheStatistics* CComputedConsequencesCache::getCacheStatistics() { |
| 63 |
return &mCacheStat; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
CComputedConsequencesCache* CComputedConsequencesCache::installWriteCacheData(CComputedConsequencesCacheWriteData* writeData, CComputedConsequencesCacheContext* context) { |
| 69 |
CComputedConsequencesCacheWriteData* writeDataLinker = writeData; |
| 70 |
while (writeDataLinker) { |
| 71 |
if (writeDataLinker->getWriteDataType() == CComputedConsequencesCacheWriteData::CCWT_TYPE) { |
| 72 |
CComputedConsequencesCacheWriteTypesData* cccwtd = (CComputedConsequencesCacheWriteTypesData*)writeDataLinker; |
| 73 |
addTypesExpansionData(cccwtd,context); |
| 74 |
} |
| 75 |
writeDataLinker = (CComputedConsequencesCacheWriteData*)writeDataLinker->getNext(); |
| 76 |
} |
| 77 |
return this; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
CComputedConsequencesCache* CComputedConsequencesCache::addTypesExpansionData(CComputedConsequencesCacheWriteTypesData* cccwtd, CComputedConsequencesCacheContext* context) { |
| 83 |
CIndividual* individual = cccwtd->getIndividual(); |
| 84 |
CConcept* conceptType = cccwtd->getConcept(); |
| 85 |
bool conceptNegation = cccwtd->getNegation(); |
| 86 |
CComputedConsequencesTypesCacheEntry* cacheEntry = getComputedTypesCacheEntryForNode(individual,context,true); |
| 87 |
if (cacheEntry) { |
| 88 |
CSortedNegLinker<CConcept*>* conceptLinker = context->createConceptLinker(); |
| 89 |
conceptLinker->init(conceptType,conceptNegation); |
| 90 |
cacheEntry->addConceptLinker(conceptLinker); |
| 91 |
} |
| 92 |
return this; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
CComputedConsequencesTypesCacheEntry* CComputedConsequencesCache::getComputedTypesCacheEntryForNode(CIndividual* individual, CComputedConsequencesCacheContext* context, bool create) { |
| 100 |
CIndividualProcessData* indProData = (CIndividualProcessData*)individual->getIndividualData(); |
| 101 |
CComputedConsequencesTypesCacheEntry* cacheEntry = nullptr; |
| 102 |
if (indProData) { |
| 103 |
cacheEntry = (CComputedConsequencesTypesCacheEntry*)indProData->getComputedConsequencesCachingData(); |
| 104 |
} |
| 105 |
if (indProData && !cacheEntry && create) { |
| 106 |
cacheEntry = CObjectParameterizingAllocator<CComputedConsequencesTypesCacheEntry,CComputedConsequencesCacheContext*>::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context); |
| 107 |
cacheEntry->initCacheEntry(individual); |
| 108 |
indProData->setComputedConsequencesCachingData(cacheEntry); |
| 109 |
} |
| 110 |
return cacheEntry; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
CComputedConsequencesCacheReader* CComputedConsequencesCache::createCacheReader() { |
| 118 |
CComputedConsequencesCacheReader* readerLinker = new CComputedConsequencesCacheReader(); |
| 119 |
mReaderSyncMutex.lock(); |
| 120 |
mReaderLinker = readerLinker->append(mReaderLinker); |
| 121 |
mReaderSyncMutex.unlock(); |
| 122 |
return readerLinker; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
CComputedConsequencesCacheWriter* CComputedConsequencesCache::createCacheWriter() { |
| 127 |
return new CComputedConsequencesCacheWriter(this); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
bool CComputedConsequencesCache::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 134 |
if (CThread::processCustomsEvents(type,event)) { |
| 135 |
return true; |
| 136 |
} else if (type == EVENTWRITECOMPUTEDCONSEQUENCESCACHEDATAENTRY) { |
| 137 |
CWriteComputedConcequencesCacheEntryEvent* wscde = (CWriteComputedConcequencesCacheEntryEvent*)event; |
| 138 |
CMemoryPool* memoryPools = wscde->getMemoryPools(); |
| 139 |
CComputedConsequencesCacheWriteData* writeData = wscde->getWriteData(); |
| 140 |
installWriteCacheData(writeData,&mContext); |
| 141 |
mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools); |
| 142 |
return true; |
| 143 |
|
| 144 |
} |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
}; // end namespace Cache |
| 152 |
|
| 153 |
}; // end namespace Kernel |
| 154 |
|
| 155 |
}; // end namespace Reasoner |
| 156 |
|
| 157 |
}; // end namespace Konclude |