Debian Codegraph @

Source/Reasoner/Kernel/Cache/CSaturationNodeAssociatedExpansionCache.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 "CSaturationNodeAssociatedExpansionCache.h"
22 #include "CSaturationNodeAssociatedExpansionCacheWriter.h"
23
24
25 namespace Konclude {
26
27 namespace Reasoner {
28
29 namespace Kernel {
30
31 namespace Cache {
32
33
34 CSaturationNodeAssociatedExpansionCache::CSaturationNodeAssociatedExpansionCache(CConfiguration* config, QString threadIdentifierName, CWatchDog *watchDogThread) : CThread(threadIdentifierName,watchDogThread) {
35
36
37 mReaderLinker = nullptr;
38
39 CMemoryAllocationManager* memMan = mContext.getMemoryAllocationManager();
40 mSaturationNodeCacheUpdate = new CSaturationNodeCacheUpdater(&mContext);
41
42 mConfAllowedNonDetExpansionCount = 1;
43 if (config) {
44 mConfAllowedNonDetExpansionCount = CConfigDataReader::readConfigInteger(config,"Konclude.Calculation.Optimization.SaturationExpansionSatisfiabilityCacheCount",1);
45 }
46
47 startThread(QThread::HighestPriority);
48 }
49
50
51 CSaturationNodeAssociatedExpansionCache::~CSaturationNodeAssociatedExpansionCache() {
52 delete mSaturationNodeCacheUpdate;
53 }
54
55
56
57 CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::writeCacheData(CSaturationNodeAssociatedExpansionCacheWriteData* writeData, CMemoryPool* memoryPools) {
58 postEvent(new CWriteSaturationCacheDataEvent(writeData,memoryPools));
59 return this;
60 }
61
62
63
64 CCacheStatistics* CSaturationNodeAssociatedExpansionCache::getCacheStatistics() {
65 return &mCacheStat;
66 }
67
68
69
70 CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::installWriteCacheData(CSaturationNodeAssociatedExpansionCacheWriteData* writeData, CSaturationNodeAssociatedExpansionCacheContext* context) {
71 CSaturationNodeAssociatedExpansionCacheWriteData* writeDataLinker = writeData;
72 while (writeDataLinker) {
73 if (writeDataLinker->getWriteDataType() == CSaturationNodeAssociatedExpansionCacheWriteData::SNAECWT_UNSAT) {
74 CSaturationNodeAssociatedExpansionCacheUnsatisfiabilityWriteData* snaecuwd = (CSaturationNodeAssociatedExpansionCacheUnsatisfiabilityWriteData*)writeDataLinker;
75 CIndividualSaturationProcessNode* node = snaecuwd->getUnsatisfiableSaturationIndividualNode();
76 propagateUnsatisfibility(node,context);
77 } else if (writeDataLinker->getWriteDataType() == CSaturationNodeAssociatedExpansionCacheWriteData::SNAECWT_EXPAND) {
78 CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd = (CSaturationNodeAssociatedExpansionCacheExpansionWriteData*)writeDataLinker;
79 addNodeExpansionData(snaecewd,context);
80 }
81 writeDataLinker = (CSaturationNodeAssociatedExpansionCacheWriteData*)writeDataLinker->getNext();
82 }
83 return this;
84 }
85
86
87
88 CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::addNodeExpansionData(CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
89 CIndividualSaturationProcessNode* node = snaecewd->getSaturationIndividualNode();
90 CSaturationNodeAssociatedExpansionCacheEntry* cacheEntry = getCacheEntryForNode(node,context,true);
91 if (cacheEntry) {
92
93 if (snaecewd->isDeterministicExpansion()) {
94 if (!cacheEntry->hasDeterministicConceptExpansion()) {
95 CSaturationNodeAssociatedDeterministicConceptExpansion* detExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
96 detExpansion->initDeterministicConceptExpansion();
97
98 fillExpansionData(detExpansion,snaecewd,context);
99
100 detExpansion->setNonDeterministicExpansionRequired(snaecewd->requiresNondeterministicExpansion());
101 cacheEntry->setDeterministicConceptExpansion(detExpansion);
102
103 } else {
104 CSaturationNodeAssociatedDeterministicConceptExpansion* detExpansion = extendDeterministicExpansionData(cacheEntry->getDeterministicConceptExpansion(),snaecewd,context);
105 detExpansion->setNonDeterministicExpansionRequired(snaecewd->requiresNondeterministicExpansion());
106 cacheEntry->setDeterministicConceptExpansion(detExpansion);
107
108 }
109 } else {
110 if (cacheEntry->areMoreNondeterministicExpansionAllowed()) {
111 cacheEntry->decRemainingAllowedNondeterministicExpansionCount();
112
113 CSaturationNodeAssociatedNondeterministicConceptExpansion* nondetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedNondeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
114 nondetExpansion->initNondeterministicConceptExpansion();
115
116 fillExpansionData(nondetExpansion,snaecewd,context);
117
118 cacheEntry->addNondeterministicConceptExpansion(nondetExpansion);
119 }
120 }
121 }
122 return this;
123 }
124
125
126
127 CSaturationNodeAssociatedDeterministicConceptExpansion* CSaturationNodeAssociatedExpansionCache::extendDeterministicExpansionData(CSaturationNodeAssociatedConceptExpansion* prevConceptExpansion, CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
128 CSaturationNodeAssociatedDeterministicConceptExpansion* newDetExpansion = nullptr;
129 for (CSaturationNodeAssociatedConceptLinker* conceptExpansionLinkerIt = snaecewd->getExpansionConceptLinker(); conceptExpansionLinkerIt; conceptExpansionLinkerIt = conceptExpansionLinkerIt->getNext()) {
130 CCacheValue* cacheValue = conceptExpansionLinkerIt->getCacheValue();
131
132 if (!prevConceptExpansion->hasConceptExpansionLinker(cacheValue)) {
133 if (!newDetExpansion) {
134 newDetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
135 newDetExpansion->initDeterministicConceptExpansion();
136 }
137 CSaturationNodeAssociatedConceptLinker* conceptExpansionLinker = CObjectAllocator< CSaturationNodeAssociatedConceptLinker >::allocateAndConstruct(context->getMemoryAllocationManager());
138 conceptExpansionLinker->initConceptLinker(*cacheValue);
139 newDetExpansion->addConceptExpansionLinker(conceptExpansionLinker);
140 }
141 }
142 if (!newDetExpansion && !snaecewd->requiresNondeterministicExpansion()) {
143 newDetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
144 newDetExpansion->initDeterministicConceptExpansion();
145 }
146
147 if (newDetExpansion) {
148 CSaturationNodeAssociatedDependentNominalSet* nominalDependentSet = snaecewd->getDependentNominalSet();
149 if (nominalDependentSet && !nominalDependentSet->isEmpty()) {
150 for (CSaturationNodeAssociatedDependentNominalSet::const_iterator it = nominalDependentSet->constBegin(), itEnd = nominalDependentSet->constEnd(); it != itEnd; ++it) {
151 cint64 nominalID = *it;
152 newDetExpansion->getDependentNominalSet(true)->insert(nominalID);
153 }
154 }
155 newDetExpansion->setHasTightCardinalityRestriction(snaecewd->hasTightAtMostRestriction());
156 newDetExpansion->setConceptSetSignature(snaecewd->getConceptSetSignature());
157 newDetExpansion->setTotalConceptCount(snaecewd->getTotalConceptCount());
158 }
159
160 return newDetExpansion;
161 }
162
163
164
165 CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::fillExpansionData(CSaturationNodeAssociatedConceptExpansion* conceptExpansion, CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
166 for (CSaturationNodeAssociatedConceptLinker* conceptExpansionLinkerIt = snaecewd->getExpansionConceptLinker(); conceptExpansionLinkerIt; conceptExpansionLinkerIt = conceptExpansionLinkerIt->getNext()) {
167 CCacheValue* cacheValue = conceptExpansionLinkerIt->getCacheValue();
168 CSaturationNodeAssociatedConceptLinker* conceptExpansionLinker = CObjectAllocator< CSaturationNodeAssociatedConceptLinker >::allocateAndConstruct(context->getMemoryAllocationManager());
169 conceptExpansionLinker->initConceptLinker(*cacheValue);
170 conceptExpansion->addConceptExpansionLinker(conceptExpansionLinker);
171 }
172
173 CSaturationNodeAssociatedDependentNominalSet* nominalDependentSet = snaecewd->getDependentNominalSet();
174 if (nominalDependentSet && !nominalDependentSet->isEmpty()) {
175 for (CSaturationNodeAssociatedDependentNominalSet::const_iterator it = nominalDependentSet->constBegin(), itEnd = nominalDependentSet->constEnd(); it != itEnd; ++it) {
176 cint64 nominalID = *it;
177 conceptExpansion->getDependentNominalSet(true)->insert(nominalID);
178 }
179 }
180
181 conceptExpansion->setHasTightCardinalityRestriction(snaecewd->hasTightAtMostRestriction());
182 conceptExpansion->setConceptSetSignature(snaecewd->getConceptSetSignature());
183 conceptExpansion->setTotalConceptCount(snaecewd->getTotalConceptCount());
184
185 return this;
186 }
187
188
189
190
191
192 CSaturationNodeAssociatedExpansionCacheEntry* CSaturationNodeAssociatedExpansionCache::getCacheEntryForNode(CIndividualSaturationProcessNode* node, CSaturationNodeAssociatedExpansionCacheContext* context, bool create) {
193 CSaturationNodeAssociatedExpansionCacheEntry* cacheEntry = (CSaturationNodeAssociatedExpansionCacheEntry*)node->getCacheExpansionData();
194 if (!cacheEntry) {
195 cacheEntry = CObjectParameterizingAllocator< CSaturationNodeAssociatedExpansionCacheEntry,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
196 cacheEntry->initCacheEntry(node,mConfAllowedNonDetExpansionCount);
197 node->setCacheExpansionData(cacheEntry);
198 }
199 return cacheEntry;
200 }
201
202
203
204
205 CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::propagateUnsatisfibility(CIndividualSaturationProcessNode* node, CSaturationNodeAssociatedExpansionCacheContext* context) {
206 mSaturationNodeCacheUpdate->propagateUnsatisfibility(node,context);
207 return this;
208 }
209
210
211
212 CSaturationNodeAssociatedExpansionCacheReader* CSaturationNodeAssociatedExpansionCache::createCacheReader() {
213 CSaturationNodeAssociatedExpansionCacheReader* readerLinker = new CSaturationNodeAssociatedExpansionCacheReader();
214 mReaderSyncMutex.lock();
215 mReaderLinker = readerLinker->append(mReaderLinker);
216 mReaderSyncMutex.unlock();
217 return readerLinker;
218 }
219
220
221 CSaturationNodeAssociatedExpansionCacheWriter* CSaturationNodeAssociatedExpansionCache::createCacheWriter() {
222 return new CSaturationNodeAssociatedExpansionCacheWriter(this);
223 }
224
225
226
227
228 bool CSaturationNodeAssociatedExpansionCache::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
229 if (CThread::processCustomsEvents(type,event)) {
230 return true;
231 } else if (type == EVENTWRITESATURATIONCACHEDATAENTRY) {
232 CWriteSaturationCacheDataEvent* wscde = (CWriteSaturationCacheDataEvent*)event;
233 CMemoryPool* memoryPools = wscde->getMemoryPools();
234 CSaturationNodeAssociatedExpansionCacheWriteData* writeData = wscde->getWriteData();
235 installWriteCacheData(writeData,&mContext);
236 mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools);
237 return true;
238
239 }
240 return false;
241 }
242
243
244
245
246 }; // end namespace Cache
247
248 }; // end namespace Kernel
249
250 }; // end namespace Reasoner
251
252 }; // end namespace Konclude