File: CSaturationNodeAssociatedExpansionCache.cpp

package info (click to toggle)
konclude 0.6.2~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,976 kB
  • ctags: 43,332
  • sloc: cpp: 250,898; xml: 54,573; makefile: 29; ansic: 3; sh: 3
file content (252 lines) | stat: -rw-r--r-- 12,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 *		Copyright (C) 2013, 2014, 2015 by the Konclude Developer Team.
 *
 *		This file is part of the reasoning system Konclude.
 *		For details and support, see <http://konclude.com/>.
 *
 *		Konclude is free software: you can redistribute it and/or modify it under
 *		the terms of version 2.1 of the GNU Lesser General Public License (LGPL2.1)
 *		as published by the Free Software Foundation.
 *
 *		You should have received a copy of the GNU Lesser General Public License
 *		along with Konclude. If not, see <http://www.gnu.org/licenses/>.
 *
 *		Konclude is distributed in the hope that it will be useful,
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more
 *		details, see GNU Lesser General Public License.
 *
 */

#include "CSaturationNodeAssociatedExpansionCache.h"
#include "CSaturationNodeAssociatedExpansionCacheWriter.h"


namespace Konclude {

	namespace Reasoner {

		namespace Kernel {

			namespace Cache {


				CSaturationNodeAssociatedExpansionCache::CSaturationNodeAssociatedExpansionCache(CConfiguration* config, QString threadIdentifierName, CWatchDog *watchDogThread) : CThread(threadIdentifierName,watchDogThread) {


					mReaderLinker = nullptr;

					CMemoryAllocationManager* memMan = mContext.getMemoryAllocationManager();
					mSaturationNodeCacheUpdate = new CSaturationNodeCacheUpdater(&mContext);

					mConfAllowedNonDetExpansionCount = 1;
					if (config) {
						mConfAllowedNonDetExpansionCount = CConfigDataReader::readConfigInteger(config,"Konclude.Calculation.Optimization.SaturationExpansionSatisfiabilityCacheCount",1);
					}

					startThread(QThread::HighestPriority);
				}


				CSaturationNodeAssociatedExpansionCache::~CSaturationNodeAssociatedExpansionCache() {
					delete mSaturationNodeCacheUpdate;
				}



				CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::writeCacheData(CSaturationNodeAssociatedExpansionCacheWriteData* writeData, CMemoryPool* memoryPools) {
					postEvent(new CWriteSaturationCacheDataEvent(writeData,memoryPools));
					return this;
				}



				CCacheStatistics* CSaturationNodeAssociatedExpansionCache::getCacheStatistics() {
					return &mCacheStat;
				}



				CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::installWriteCacheData(CSaturationNodeAssociatedExpansionCacheWriteData* writeData, CSaturationNodeAssociatedExpansionCacheContext* context) {
					CSaturationNodeAssociatedExpansionCacheWriteData* writeDataLinker = writeData;
					while (writeDataLinker) {
						if (writeDataLinker->getWriteDataType() == CSaturationNodeAssociatedExpansionCacheWriteData::SNAECWT_UNSAT) {
							CSaturationNodeAssociatedExpansionCacheUnsatisfiabilityWriteData* snaecuwd = (CSaturationNodeAssociatedExpansionCacheUnsatisfiabilityWriteData*)writeDataLinker;
							CIndividualSaturationProcessNode* node = snaecuwd->getUnsatisfiableSaturationIndividualNode();
							propagateUnsatisfibility(node,context);							
						} else if (writeDataLinker->getWriteDataType() == CSaturationNodeAssociatedExpansionCacheWriteData::SNAECWT_EXPAND) {
							CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd = (CSaturationNodeAssociatedExpansionCacheExpansionWriteData*)writeDataLinker;
							addNodeExpansionData(snaecewd,context);
						}
						writeDataLinker = (CSaturationNodeAssociatedExpansionCacheWriteData*)writeDataLinker->getNext();
					}
					return this;
				}



				CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::addNodeExpansionData(CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
					CIndividualSaturationProcessNode* node = snaecewd->getSaturationIndividualNode();
					CSaturationNodeAssociatedExpansionCacheEntry* cacheEntry = getCacheEntryForNode(node,context,true);
					if (cacheEntry) {

						if (snaecewd->isDeterministicExpansion()) {
							if (!cacheEntry->hasDeterministicConceptExpansion()) {
								CSaturationNodeAssociatedDeterministicConceptExpansion* detExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
								detExpansion->initDeterministicConceptExpansion();

								fillExpansionData(detExpansion,snaecewd,context);

								detExpansion->setNonDeterministicExpansionRequired(snaecewd->requiresNondeterministicExpansion());
								cacheEntry->setDeterministicConceptExpansion(detExpansion);

							} else {
								CSaturationNodeAssociatedDeterministicConceptExpansion* detExpansion = extendDeterministicExpansionData(cacheEntry->getDeterministicConceptExpansion(),snaecewd,context);
								detExpansion->setNonDeterministicExpansionRequired(snaecewd->requiresNondeterministicExpansion());
								cacheEntry->setDeterministicConceptExpansion(detExpansion);

							}
						} else {
							if (cacheEntry->areMoreNondeterministicExpansionAllowed()) {
								cacheEntry->decRemainingAllowedNondeterministicExpansionCount();

								CSaturationNodeAssociatedNondeterministicConceptExpansion* nondetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedNondeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
								nondetExpansion->initNondeterministicConceptExpansion();

								fillExpansionData(nondetExpansion,snaecewd,context);

								cacheEntry->addNondeterministicConceptExpansion(nondetExpansion);
							}
						}
					}
					return this;
				}



				CSaturationNodeAssociatedDeterministicConceptExpansion* CSaturationNodeAssociatedExpansionCache::extendDeterministicExpansionData(CSaturationNodeAssociatedConceptExpansion* prevConceptExpansion, CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
					CSaturationNodeAssociatedDeterministicConceptExpansion* newDetExpansion = nullptr;
					for (CSaturationNodeAssociatedConceptLinker* conceptExpansionLinkerIt = snaecewd->getExpansionConceptLinker(); conceptExpansionLinkerIt; conceptExpansionLinkerIt = conceptExpansionLinkerIt->getNext()) {
						CCacheValue* cacheValue = conceptExpansionLinkerIt->getCacheValue();

						if (!prevConceptExpansion->hasConceptExpansionLinker(cacheValue)) {
							if (!newDetExpansion) {
								newDetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
								newDetExpansion->initDeterministicConceptExpansion();
							}
							CSaturationNodeAssociatedConceptLinker* conceptExpansionLinker = CObjectAllocator< CSaturationNodeAssociatedConceptLinker >::allocateAndConstruct(context->getMemoryAllocationManager());
							conceptExpansionLinker->initConceptLinker(*cacheValue);
							newDetExpansion->addConceptExpansionLinker(conceptExpansionLinker);
						}
					}
					if (!newDetExpansion && !snaecewd->requiresNondeterministicExpansion()) {
						newDetExpansion = CObjectParameterizingAllocator< CSaturationNodeAssociatedDeterministicConceptExpansion,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
						newDetExpansion->initDeterministicConceptExpansion();
					}

					if (newDetExpansion) {
						CSaturationNodeAssociatedDependentNominalSet* nominalDependentSet = snaecewd->getDependentNominalSet();
						if (nominalDependentSet && !nominalDependentSet->isEmpty()) {
							for (CSaturationNodeAssociatedDependentNominalSet::const_iterator it = nominalDependentSet->constBegin(), itEnd = nominalDependentSet->constEnd(); it != itEnd; ++it) {
								cint64 nominalID = *it;
								newDetExpansion->getDependentNominalSet(true)->insert(nominalID);
							}
						}
						newDetExpansion->setHasTightCardinalityRestriction(snaecewd->hasTightAtMostRestriction());
						newDetExpansion->setConceptSetSignature(snaecewd->getConceptSetSignature());
						newDetExpansion->setTotalConceptCount(snaecewd->getTotalConceptCount());
					}

					return newDetExpansion;
				}



				CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::fillExpansionData(CSaturationNodeAssociatedConceptExpansion* conceptExpansion, CSaturationNodeAssociatedExpansionCacheExpansionWriteData* snaecewd, CSaturationNodeAssociatedExpansionCacheContext* context) {
					for (CSaturationNodeAssociatedConceptLinker* conceptExpansionLinkerIt = snaecewd->getExpansionConceptLinker(); conceptExpansionLinkerIt; conceptExpansionLinkerIt = conceptExpansionLinkerIt->getNext()) {
						CCacheValue* cacheValue = conceptExpansionLinkerIt->getCacheValue();
						CSaturationNodeAssociatedConceptLinker* conceptExpansionLinker = CObjectAllocator< CSaturationNodeAssociatedConceptLinker >::allocateAndConstruct(context->getMemoryAllocationManager());
						conceptExpansionLinker->initConceptLinker(*cacheValue);
						conceptExpansion->addConceptExpansionLinker(conceptExpansionLinker);
					}

					CSaturationNodeAssociatedDependentNominalSet* nominalDependentSet = snaecewd->getDependentNominalSet();
					if (nominalDependentSet && !nominalDependentSet->isEmpty()) {
						for (CSaturationNodeAssociatedDependentNominalSet::const_iterator it = nominalDependentSet->constBegin(), itEnd = nominalDependentSet->constEnd(); it != itEnd; ++it) {
							cint64 nominalID = *it;
							conceptExpansion->getDependentNominalSet(true)->insert(nominalID);
						}
					}

					conceptExpansion->setHasTightCardinalityRestriction(snaecewd->hasTightAtMostRestriction());
					conceptExpansion->setConceptSetSignature(snaecewd->getConceptSetSignature());
					conceptExpansion->setTotalConceptCount(snaecewd->getTotalConceptCount());
					
					return this;
				}





				CSaturationNodeAssociatedExpansionCacheEntry* CSaturationNodeAssociatedExpansionCache::getCacheEntryForNode(CIndividualSaturationProcessNode* node, CSaturationNodeAssociatedExpansionCacheContext* context, bool create) {
					CSaturationNodeAssociatedExpansionCacheEntry* cacheEntry = (CSaturationNodeAssociatedExpansionCacheEntry*)node->getCacheExpansionData();
					if (!cacheEntry) {
						cacheEntry = CObjectParameterizingAllocator< CSaturationNodeAssociatedExpansionCacheEntry,CSaturationNodeAssociatedExpansionCacheContext* >::allocateAndConstructAndParameterize(context->getMemoryAllocationManager(),context);
						cacheEntry->initCacheEntry(node,mConfAllowedNonDetExpansionCount);
						node->setCacheExpansionData(cacheEntry);
					}
					return cacheEntry;
				}




				CSaturationNodeAssociatedExpansionCache* CSaturationNodeAssociatedExpansionCache::propagateUnsatisfibility(CIndividualSaturationProcessNode* node, CSaturationNodeAssociatedExpansionCacheContext* context) {
					mSaturationNodeCacheUpdate->propagateUnsatisfibility(node,context);
					return this;
				}



				CSaturationNodeAssociatedExpansionCacheReader* CSaturationNodeAssociatedExpansionCache::createCacheReader() {
					CSaturationNodeAssociatedExpansionCacheReader* readerLinker = new CSaturationNodeAssociatedExpansionCacheReader();
					mReaderSyncMutex.lock();
					mReaderLinker = readerLinker->append(mReaderLinker);
					mReaderSyncMutex.unlock();
					return readerLinker;
				}


				CSaturationNodeAssociatedExpansionCacheWriter* CSaturationNodeAssociatedExpansionCache::createCacheWriter() {
					return new CSaturationNodeAssociatedExpansionCacheWriter(this);
				}




				bool CSaturationNodeAssociatedExpansionCache::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
					if (CThread::processCustomsEvents(type,event)) {
						return true;
					} else if (type == EVENTWRITESATURATIONCACHEDATAENTRY) {
						CWriteSaturationCacheDataEvent* wscde = (CWriteSaturationCacheDataEvent*)event;
						CMemoryPool* memoryPools = wscde->getMemoryPools();
						CSaturationNodeAssociatedExpansionCacheWriteData* writeData = wscde->getWriteData();
						installWriteCacheData(writeData,&mContext);
						mContext.getMemoryPoolAllocationManager()->releaseTemporaryMemoryPools(memoryPools);
						return true;

					}
					return false;
				}




			}; // end namespace Cache

		}; // end namespace Kernel

	}; // end namespace Reasoner

}; // end namespace Konclude