Debian Codegraph @

Source/Reasoner/Kernel/Cache/COccurrenceUnsatisfiableCache.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 "COccurrenceUnsatisfiableCache.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Cache {
31
32
33 COccurrenceUnsatisfiableCache::COccurrenceUnsatisfiableCache(qint64 writeUpdateSlotCount, const QString &threadIdentifierName, CWatchDog *watchDogThread) : CThread(threadIdentifierName,watchDogThread),updatesSlotItemVector(writeUpdateSlotCount,0) {
34 cacheWritingRequested = false;
35 updateSlotCount = writeUpdateSlotCount;
36
37 writeOperationsCount = 0;
38 mCachingTag = 0;
39 lastUpdateSlot = nullptr;
40
41 startThread();
42 }
43
44
45 COccurrenceUnsatisfiableCache::~COccurrenceUnsatisfiableCache() {
46 qDeleteAll(cacheReaderList);
47 qDeleteAll(cacheWriterList);
48 }
49
50
51
52 COccurrenceUnsatisfiableCacheReader *COccurrenceUnsatisfiableCache::getCacheReader() {
53 cacheReaderSyncMutex.lock();
54 COccurrenceUnsatisfiableCacheReader *reader = new COccurrenceUnsatisfiableCacheReader(this);
55 cacheReaderList.append(reader);
56 cacheReaderSyncMutex.unlock();
57 return reader;
58 }
59
60
61 COccurrenceUnsatisfiableCacheWriter *COccurrenceUnsatisfiableCache::getCacheWriter() {
62 cacheWriterSyncMutex.lock();
63 COccurrenceUnsatisfiableCacheWriter *writer = new COccurrenceUnsatisfiableCacheWriter(this);
64 cacheWriterList.append(writer);
65 cacheWriterSyncMutex.unlock();
66 return writer;
67 }
68
69
70 bool COccurrenceUnsatisfiableCache::isCacheWritePending() {
71 return cacheWritingRequested;
72 }
73
74
75 cint64 COccurrenceUnsatisfiableCache::getCurrentCachingTag() {
76 return mCachingTag;
77 }
78
79 CCacheStatistics* COccurrenceUnsatisfiableCache::getCacheStatistics() {
80 return &mCachStat;
81 }
82
83
84 COccurrenceUnsatisfiableCacheEntry *COccurrenceUnsatisfiableCache::getPrimarCacheEntry() {
85 if (!CThread::isThreadRunning()) {
86 CThread::waitSynchronization();
87 }
88 return primarCacheEntry;
89 }
90
91
92 void COccurrenceUnsatisfiableCache::addUnsatisfiableCacheEntry(QList<CCacheValue> &itemList) {
93 // item list is and has to be copied in event constructor!
94 CThread::postEvent(new CWriteUnsatisfiableCacheEntryEvent(itemList));
95 }
96
97
98
99
100 void COccurrenceUnsatisfiableCache::threadStarted() {
101 primarCacheEntry = new COccurrenceUnsatisfiableCacheEntry(CCacheValue(0,0,CCacheValue::CACHEVALCONCEPTONTOLOGYTAG),0,updateSlotCount,0);
102 container.append(primarCacheEntry);
103
104 for (qint64 idx = 0; idx < updateSlotCount; ++idx) {
105 updatesSlotItemVector[idx] = new COccurrenceUnsatisfiableCacheUpdateSlotItem(idx);
106 notusedUpdatesSlotsList.append(updatesSlotItemVector[idx]);
107 }
108 }
109
110 void COccurrenceUnsatisfiableCache::threadStopped() {
111 for (qint64 idx = 0; idx < updateSlotCount; ++idx) {
112 delete updatesSlotItemVector[idx];
113 }
114 primarCacheEntry = 0;
115 qDeleteAll(container);
116 container.clear();
117 }
118
119
120 bool COccurrenceUnsatisfiableCache::waitCacheWritePrepared() {
121 for (qint64 i = usedUpdatesSlotsList.count(); i > 0; --i) {
122 COccurrenceUnsatisfiableCacheUpdateSlotItem *slotItem = usedUpdatesSlotsList.takeFirst();
123 if (!slotItem->hasCacheReaders()) {
124 slotItem->cleanSlotUpdateItems();
125 notusedUpdatesSlotsList.append(slotItem);
126 } else {
127 usedUpdatesSlotsList.append(slotItem);
128 }
129 }
130 while (notusedUpdatesSlotsList.isEmpty()) {
131 // wait until one update slot is not longer used
132 QThread::msleep(10);
133 for (qint64 i = usedUpdatesSlotsList.count(); i > 0; --i) {
134 COccurrenceUnsatisfiableCacheUpdateSlotItem *slotItem = usedUpdatesSlotsList.takeFirst();
135 if (!slotItem->hasCacheReaders()) {
136 slotItem->cleanSlotUpdateItems();
137 notusedUpdatesSlotsList.append(slotItem);
138 } else {
139 usedUpdatesSlotsList.append(slotItem);
140 }
141 }
142 }
143 return true;
144 }
145
146
147
148 bool COccurrenceUnsatisfiableCache::activateCacheUpdate(COccurrenceUnsatisfiableCacheUpdateSlotItem *updateSlot) {
149
150 qint64 slotIndex = updateSlot->getSlotIndex();
151
152 updateSlot->activateSlotUpdateItems();
153 usedUpdatesSlotsList.append(updateSlot);
154
155 foreach (COccurrenceUnsatisfiableCacheEntry *entry, container) {
156 COccurrenceUnsatisfiableCacheEntriesHash *prevDelHash = entry->updateSlotCacheHashGetPrevious(slotIndex);
157 if (prevDelHash) {
158 updateSlot->addCacheEntriesHash(prevDelHash);
159 }
160 }
161
162 lastUpdateSlot = updateSlot;
163
164 cacheReaderSyncMutex.lock();
165 foreach (COccurrenceUnsatisfiableCacheReader *reader, cacheReaderList) {
166 reader->changeUpdateSlot(updateSlot);
167 }
168 cacheReaderSyncMutex.unlock();
169
170 return true;
171 }
172
173
174 bool COccurrenceUnsatisfiableCache::writeCacheTags(CCacheValue* cacheValue, cint64 cachingTag, cint64 cachedTag, cint64 cachingSize) {
175 CCacheValue::CACHEVALUEIDENTIFIER valId = cacheValue->getCacheValueIdentifier();
176 bool hasConcept = false;
177 bool conceptNeg = false;
178 if (valId == CCacheValue::CACHEVALTAGANDCONCEPT) {
179 hasConcept = true;
180 conceptNeg = false;
181 } else if (valId == CCacheValue::CACHEVALTAGANDNEGATEDCONCEPT) {
182 hasConcept = true;
183 conceptNeg = true;
184 }
185 if (hasConcept) {
186 cint64 identification = cacheValue->getIdentification();
187 CConcept* concept = (CConcept*)identification;
188 if (concept) {
189 CConceptProcessData* conProData = (CConceptProcessData*)concept->getConceptData();
190 if (conProData) {
191 CUnsatisfiableCachingTags* unsatCachingTags = conProData->getUnsatisfiableCachingTags(conceptNeg);
192 if (!unsatCachingTags) {
193 unsatCachingTags = new CUnsatisfiableCachingTags();
194 conProData->setUnsatisfiableCachingTags(conceptNeg,unsatCachingTags);
195 }
196 unsatCachingTags->updateCachingTags(cachedTag,cachingTag,cachingSize);
197 return true;
198 }
199 }
200 }
201 return false;
202 }
203
204
205 bool COccurrenceUnsatisfiableCache::writeCacheValues(COccurrenceUnsatisfiableCacheUpdateSlotItem *updateSlot, QList<CCacheValue> *cacheValueList) {
206 qint64 slotIndex = updateSlot->getSlotIndex();
207 COccurrenceUnsatisfiableCacheEntry *cache = primarCacheEntry;
208
209 cint64 cachingTag = mCachingTag+1;
210 cint64 cachedTag = mCacheTaggingPool.takeNextTag();
211
212 cint64 cachingSize = cacheValueList->count();
213
214 foreach (CCacheValue cacheValue, *cacheValueList) {
215
216 COccurrenceUnsatisfiableCacheEntriesHash *cacheHash = cache->getCacheEntriesHash();
217 if (!cacheHash || !cacheHash->contains(cacheValue)) {
218
219 COccurrenceUnsatisfiableCacheEntriesHash *updatedCacheHash = 0;
220 if (!updatedCacheHash) {
221 if (cacheHash) {
222 updatedCacheHash = new COccurrenceUnsatisfiableCacheEntriesHash(*cacheHash);
223 } else {
224 updatedCacheHash = new COccurrenceUnsatisfiableCacheEntriesHash();
225 }
226 updateSlot->addCacheEntry(cache);
227 }
228
229 COccurrenceUnsatisfiableCacheEntry *nextCache = new COccurrenceUnsatisfiableCacheEntry(cacheValue,0,updateSlotCount,slotIndex);
230 container.append(nextCache);
231
232 qint64 tag = cacheValue.getTag();
233 updatedCacheHash->insert(cacheValue,nextCache);
234 cache->setMinimumCandidate(tag);
235 cache->setMaximumCandidate(tag);
236
237 COccurrenceUnsatisfiableCacheEntriesHash *prevDelHash = cache->setCacheEntriesHashSlotGetPrevious(slotIndex,updatedCacheHash);
238 if (prevDelHash) {
239 updateSlot->addCacheEntriesHash(prevDelHash);
240 }
241
242 cache = nextCache;
243 } else {
244 cache = cacheHash->value(cacheValue);
245 }
246
247 writeCacheTags(&cacheValue,cachingTag,cachedTag,cachingSize);
248 }
249 if (cache) {
250 cache->copyCacheTerminationValuesList(cacheValueList);
251 }
252 ++mCachingTag;
253
254 return true;
255 }
256
257
258
259 bool COccurrenceUnsatisfiableCache::testAlreadyCached(COccurrenceUnsatisfiableCacheUpdateSlotItem *updateSlot, QList<CCacheValue> *cacheValueList) {
260 qint64 slotIndex = updateSlot->getSlotIndex();
261 COccurrenceUnsatisfiableCacheEntry *cache = primarCacheEntry;
262 foreach (CCacheValue cacheValue, *cacheValueList) {
263
264 if (cache->isUnsatisfiableTermination()) {
265 return true;
266 }
267
268 COccurrenceUnsatisfiableCacheEntriesHash *cacheHash = cache->getCacheEntriesHash();
269
270 if (!cacheHash) {
271 return false;
272 }
273
274 cache = cacheHash->value(cacheValue);
275 if (!cache) {
276 return false;
277 }
278 }
279 if (cache->isUnsatisfiableTermination()) {
280 return true;
281 }
282 return false;
283 }
284
285
286
287 QString COccurrenceUnsatisfiableCache::getCachingConceptsDebugString(QList<CCacheValue> &itemList) {
288 QString cachingConString;
289 for (QList<CCacheValue>::const_iterator it = itemList.constBegin(), itEnd = itemList.constEnd(); it != itEnd; ++it) {
290 CCacheValue cacheValue(*it);
291 QString conceptString;
292 if (cacheValue.getCacheValueIdentifier() == CCacheValue::CACHEVALTAGANDCONCEPT) {
293 conceptString = CConceptTextFormater::getConceptString((CConcept*)cacheValue.getIdentification(),false);
294 } else if (cacheValue.getCacheValueIdentifier() == CCacheValue::CACHEVALTAGANDNEGATEDCONCEPT) {
295 conceptString = CConceptTextFormater::getConceptString((CConcept*)cacheValue.getIdentification(),true);
296 }
297 if (!cachingConString.isEmpty()) {
298 cachingConString += QString(", ");
299 }
300 cachingConString += conceptString;
301 }
302 return cachingConString;
303 }
304
305
306 bool COccurrenceUnsatisfiableCache::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
307 if (CThread::processCustomsEvents(type,event)) {
308 return true;
309 } if (type == EVENTWRITEUNSATISFIABLECACHEENTRY) {
310 CWriteUnsatisfiableCacheEntryEvent *wuc = (CWriteUnsatisfiableCacheEntryEvent *)event;
311
312 QList<CCacheValue> *cEL = wuc->getCacheEntryList();
313
314
315 if (!lastUpdateSlot || !testAlreadyCached(lastUpdateSlot,cEL)) {
316 cacheWritingRequested = true;
317
318 if (waitCacheWritePrepared()) {
319 COccurrenceUnsatisfiableCacheUpdateSlotItem *updateSlot = notusedUpdatesSlotsList.takeFirst();
320
321 writeOperationsCount++;
322
323 writeCacheValues(updateSlot,cEL);
324 mCachStat.incCacheEntriesCount();
325
326 activateCacheUpdate(updateSlot);
327
328
329
330 for (qint64 i = usedUpdatesSlotsList.count(); i > 0; --i) {
331 COccurrenceUnsatisfiableCacheUpdateSlotItem *slotItem = usedUpdatesSlotsList.takeFirst();
332 if (!slotItem->hasCacheReaders()) {
333 slotItem->cleanSlotUpdateItems();
334 notusedUpdatesSlotsList.append(slotItem);
335 } else {
336 usedUpdatesSlotsList.append(slotItem);
337 }
338 }
339
340 //KONCLUCE_OCCURUNSATCACHE_CACHING_STRING_INSTRUCTION(mCachingString = getCachingConceptsDebugString(*cEL));
341 //KONCLUCE_OCCURUNSATCACHE_CACHING_STRING_INSTRUCTION(mCachedStringList.append(mCachingString));
342 //KONCLUCE_OCCURUNSATCACHE_CACHING_STRING_INSTRUCTION(std::cout<<"\nUnsatisfiable cached concepts: "<<mCachingString.toLocal8Bit().data()<<"\n\n");
343 //KONCLUCE_OCCURUNSATCACHE_CACHING_STRING_INSTRUCTION(LOG(WARNING,"::Konclude::Reasoner::Cache::OccurenceUnsatisfiableCache",logTr("Unsatisfiable cached concepts: %1").arg(mCachingString),this));
344
345 }
346
347 cacheWritingRequested = false;
348 }
349
350
351 return true;
352 }
353 return false;
354 }
355
356
357
358
359 }; // end namespace Cache
360
361 }; // end namespace Kernel
362
363 }; // end namespace Reasoner
364
365 }; // end namespace Konclude