Debian Codegraph @

Source/Reasoner/Answerer/COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler.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 "COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Answerer {
29
30
31 COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler(COptimizedComplexVariableAbsorptionBasedHandlingQueryPartData* queryPart, COptimizedComplexExpressionOntologyAnsweringItem* ontoAnsweringItem, COccurrenceStatisticsCacheReader* occStatsCacheReader) : COptimizedComplexVariableAbstractAbsorptionBasedQueryPartHandler(queryPart, ontoAnsweringItem) {
32 mOntology = mOntoAnsweringItem->getOntology();
33 mOccStatsCacheReader = occStatsCacheReader;
34
35 mConfOccurrenceStatisticsCollection = true;
36 mConfOccurrenceStatisticsBasedAbsorptionOrder = true;
37 mConfPrioritizePrepareVariables = false;
38 }
39
40
41
42 COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::readConfig(CCalculationConfigurationExtension* config) {
43 COptimizedComplexVariableAbstractAbsorptionBasedQueryPartHandler::readConfig(config);
44 mConfOccurrenceStatisticsBasedAbsorptionOrder = CConfigDataReader::readConfigInteger(config, "Konclude.Answering.QueryAbsorption.OccurrenceStatisticsBasedAbsorptionOrder", true);
45 mConfOccurrenceStatisticsCollection = CConfigDataReader::readConfigBoolean(config, "Konclude.Calculation.Optimization.OccurrenceStatisticsCollecting", true);
46 return this;
47 }
48
49
50
51 CVariable* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::getPropagationVariableForVariableExpression(CExpressionVariable* expVar) {
52 cint64 nextVariableId = mOntoAnsweringItem->getNextVariableId();
53 QHash<CExpressionVariable*, CVariable*>* variableExpressionVariableHash = mQueryPart->getVariableExpressionVariableHash();
54 QHash<CVariable*, CExpressionVariable*>* expressionVariableVariableHash = mQueryPart->getExpressionVariableVariableHash();
55 CVariable* variable = variableExpressionVariableHash->value(expVar);
56 if (!variable) {
57 variable = new CVariable();
58 variable->initVariable(nullptr, nextVariableId++);
59 variableExpressionVariableHash->insert(expVar, variable);
60 expressionVariableVariableHash->insert(variable, expVar);
61 #ifndef KONCLUDE_FORCE_ALL_DEBUG_DEACTIVATED
62 variable->mDebugVariableName = expVar->getName();
63 #endif
64 }
65 return variable;
66 }
67
68
69
70
71 COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::absorbQueryPart() {
72 QSet<CExpressionVariable*>* absorptionVariableSet = mQueryPart->getAbsorptionVariableSet();
73 QSet<CExpressionVariable*>* anonymousVariableSet = mQueryPart->getAnonymousVariableSet();
74
75 CExpressionVariable* currentVariable = mQueryPart->getLastTriggerVariable();
76 if (!currentVariable) {
77 QList<CExpressionVariable*> remainingVarList(getAbsorptionVariableList());
78
79 bool hasIndividualVariables = false;
80 for (CExpressionVariable* variable : remainingVarList) {
81 if (!anonymousVariableSet->contains(variable)) {
82 hasIndividualVariables = true;
83 }
84 }
85 mQueryPart->setHasIndividualVariables(hasIndividualVariables);
86
87 if (!mOntoAnsweringItem->getTestingOntology()->getABox()->hasUniversalConnectionIndividual()) {
88 bool topObjProProp = checkHasTopObjectPropertyPropagation();
89 if (topObjProProp) {
90 mQueryPart->setTopObjectPropertyUsage(topObjProProp);
91 }
92 }
93
94 currentVariable = remainingVarList.takeFirst();
95 }
96
97 while (currentVariable) {
98
99 handleCurrentVariableSwitch(currentVariable);
100
101 QList<CObjectPropertyAssertionExpression*> selfPropAssList = mQueryPart->getSelfPropertyAssertions(currentVariable);
102 while (!selfPropAssList.isEmpty()) {
103 CObjectPropertyAssertionExpression* propAss = selfPropAssList.takeFirst();
104 absorbObjectPropertyAssertion(currentVariable, propAss);
105 }
106
107 QList<CExpressionVariable*> propagationVarList;
108 QList<CObjectPropertyAssertionExpression*> propAssList = mQueryPart->getNeighbouringPropertyAssertions(currentVariable);
109 while (!propAssList.isEmpty()) {
110 propAssList = getOrderedObjectPropertyAbsorptionList(currentVariable, propAssList);
111 CObjectPropertyAssertionExpression* propAss = propAssList.takeFirst();
112 if (propAssList.isEmpty() || !mQueryPart->isPropertyNeighbouringRemovalDisconnecting(currentVariable, propAss)) {
113 CExpressionVariable* otherVar = absorbStraightObjectPropertyAssertionPath(currentVariable, propAss);
114 if (otherVar != currentVariable) {
115 propagationVarList.append(otherVar);
116 }
117 propAssList = mQueryPart->getNeighbouringPropertyAssertions(currentVariable);
118 }
119 }
120
121 QList<CObjectPropertyAssertionExpression*> objectPropertyPath;
122 CExpressionVariable* nextVariable = findNextAbsorptionVariable(currentVariable, propagationVarList, objectPropertyPath);
123
124 propagateAbsorptionOverObjectPropertyAssertionPath(currentVariable, objectPropertyPath);
125
126 currentVariable = nextVariable;
127 }
128
129
130
131 return this;
132 }
133
134
135
136
137
138 QList<CExpressionVariable*> COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::getAbsorptionVariableList() {
139 QSet<CExpressionVariable*>* absorbtionVariableSet = mQueryPart->getAbsorptionVariableSet();
140 return getFilteredOrderedVariableAbsorptionList(absorbtionVariableSet->toList());
141 }
142
143
144 QList<CExpressionVariable*> COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::getFilteredOrderedVariableAbsorptionList(QList<CExpressionVariable*> varExpList) {
145 QSet<CExpressionVariable*>* anonymousVariableSet = mQueryPart->getAnonymousVariableSet();
146 QList<CExpressionVariable*> absorbableDistVarList;
147 QList<CExpressionVariable*> absorbableDistVarMultiList;
148 QList<CExpressionVariable*> absorbableNonDistVarList;
149 QList<CExpressionVariable*> absorbableNonDistVarMultiList;
150 QList<CExpressionVariable*> remainingVarList;
151 for (CExpressionVariable* var : varExpList) {
152 QList<CObjectPropertyAssertionExpression*> neighPropAssList = mQueryPart->getNeighbouringPropertyAssertions(var);
153 QList<CObjectPropertyAssertionExpression*> selfPropAssList = mQueryPart->getSelfPropertyAssertions(var);
154 bool hasAbsorbableProperty = false;
155 if (selfPropAssList.isEmpty()) {
156 for (CObjectPropertyAssertionExpression* propAss : neighPropAssList) {
157 if (neighPropAssList.size() == 1 || !mQueryPart->isPropertyNeighbouringRemovalDisconnecting(var, propAss)) {
158 hasAbsorbableProperty = true;
159 break;
160 }
161 }
162 }
163 bool prioritizedVariable = !anonymousVariableSet->contains(var);
164 if (mConfPrioritizePrepareVariables && !prioritizedVariable) {
165 if (mQueryPart->getPrepareVariableSet()->contains(var)) {
166 prioritizedVariable = true;
167 }
168 }
169 if (prioritizedVariable) {
170 if (hasAbsorbableProperty) {
171 if (neighPropAssList.size() > 2) {
172 absorbableDistVarMultiList.append(var);
173 } else {
174 absorbableDistVarList.append(var);
175 }
176 } else {
177 remainingVarList.append(var);
178 }
179 } else {
180 if (hasAbsorbableProperty) {
181 if (neighPropAssList.size() > 2 || !selfPropAssList.isEmpty()) {
182 absorbableNonDistVarMultiList.append(var);
183 } else {
184 absorbableNonDistVarList.append(var);
185 }
186 } else {
187 remainingVarList.append(var);
188 }
189 }
190 }
191 QList<CExpressionVariable*> absorbVarList;
192 absorbVarList.append(getOrderedVariableAbsorptionList(absorbableDistVarMultiList));
193 absorbVarList.append(getOrderedVariableAbsorptionList(absorbableDistVarList));
194 absorbVarList.append(getOrderedVariableAbsorptionList(absorbableNonDistVarMultiList));
195 absorbVarList.append(getOrderedVariableAbsorptionList(absorbableNonDistVarList));
196 absorbVarList.append(getOrderedVariableAbsorptionList(remainingVarList));
197 return absorbVarList;
198
199 }
200
201
202
203
204 CExpressionVariable* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::findNextAbsorptionVariable(CExpressionVariable* currentVariable, QList<CExpressionVariable*>& absorbedPropagatedVariables, QList<CObjectPropertyAssertionExpression*>& objectPropertyPath) {
205
206 QList<CExpressionVariable*> testingVariableList;
207 QSet<CExpressionVariable*> testingVariableSet;
208 QHash<CExpressionVariable*, CObjectPropertyAssertionExpression*> variablePredecessorObjectPropertyAssertionHash;
209 testingVariableSet.insert(currentVariable);
210 testingVariableList.append(currentVariable);
211 for (CExpressionVariable* absorbedPropagatedVariable : absorbedPropagatedVariables) {
212 if (!testingVariableSet.contains(absorbedPropagatedVariable)) {
213 testingVariableSet.insert(absorbedPropagatedVariable);
214 testingVariableList.append(absorbedPropagatedVariable);
215 }
216 }
217 testingVariableList = getFilteredOrderedVariableAbsorptionList(testingVariableList);
218 while (!testingVariableList.isEmpty()) {
219 CExpressionVariable* nextVariable = testingVariableList.takeFirst();
220 QList<CObjectPropertyAssertionExpression*> selfPropAssList = mQueryPart->getSelfPropertyAssertions(nextVariable);
221 bool absorbable = false;
222 if (!selfPropAssList.isEmpty()) {
223 absorbable = true;
224 }
225 QList<CObjectPropertyAssertionExpression*> propAssList = mQueryPart->getNeighbouringPropertyAssertions(nextVariable);
226 for (CObjectPropertyAssertionExpression* propAss : propAssList) {
227 if (propAssList.size() == 1 || !mQueryPart->isPropertyNeighbouringRemovalDisconnecting(nextVariable, propAss)) {
228 absorbable = true;
229 break;
230 } else {
231 CExpressionVariable* otherVariable = mQueryPart->getOtherVariableExpression(nextVariable, propAss);
232 if (!testingVariableSet.contains(otherVariable)) {
233 testingVariableSet.insert(otherVariable);
234 variablePredecessorObjectPropertyAssertionHash.insert(otherVariable, propAss);
235 testingVariableList.append(otherVariable);
236 }
237 }
238 }
239 if (absorbable) {
240 objectPropertyPath.clear();
241 CExpressionVariable* predecessorVariable = nextVariable;
242 while (predecessorVariable) {
243 CObjectPropertyAssertionExpression* predecessorObjectPropertyAss = variablePredecessorObjectPropertyAssertionHash.value(predecessorVariable);
244 if (predecessorObjectPropertyAss) {
245 objectPropertyPath.append(predecessorObjectPropertyAss);
246 predecessorVariable = mQueryPart->getOtherVariableExpression(nextVariable, predecessorObjectPropertyAss);
247 }
248 else {
249 predecessorVariable = nullptr;
250 }
251 }
252 return nextVariable;
253 }
254
255 }
256 return nullptr;
257
258 }
259
260
261
262
263
264 bool COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::checkVariableCompletelyHandled(CExpressionVariable* currentVariable) {
265 if (mQueryPart->isVariableHandled(currentVariable)) {
266 return true;
267 }
268 QList<CObjectPropertyAssertionExpression*> selfPropAssList = mQueryPart->getSelfPropertyAssertions(currentVariable);
269 QList<CObjectPropertyAssertionExpression*> propAssList = mQueryPart->getNeighbouringPropertyAssertions(currentVariable);
270 bool completelyHandled = propAssList.size() <= 0 && selfPropAssList.isEmpty();
271 if (completelyHandled) {
272 mQueryPart->setVariableHandled(currentVariable);
273 }
274 return completelyHandled;
275 }
276
277
278
279
280 CExpressionVariable* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::propagateAbsorptionOverObjectPropertyAssertionPath(CExpressionVariable* currentVariable, const QList<CObjectPropertyAssertionExpression*>& objectPropertyPath) {
281 CExpressionVariable* nextVariable = currentVariable;
282 for (CObjectPropertyAssertionExpression* objectProperty : objectPropertyPath) {
283 nextVariable = propagateAbsorptionOverObjectPropertyAssertion(nextVariable, objectProperty);
284
285 }
286 return nextVariable;
287 }
288
289
290
291 CExpressionVariable* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::absorbStraightObjectPropertyAssertionPath(CExpressionVariable* currentVariable, CObjectPropertyAssertionExpression* startingProperty) {
292
293 CExpressionVariable* nextVariable = absorbObjectPropertyAssertion(currentVariable, startingProperty);
294
295 QSet<CExpressionVariable*>* anonymousVarSet = mQueryPart->getAnonymousVariableSet();
296
297 while (nextVariable) {
298 QList<CObjectPropertyAssertionExpression*> neighbouringPropAssList = mQueryPart->getNeighbouringPropertyAssertions(nextVariable);
299 QList<CObjectPropertyAssertionExpression*> selfPropAssList = mQueryPart->getSelfPropertyAssertions(nextVariable);
300 if (neighbouringPropAssList.size() == 1 && selfPropAssList.isEmpty()) {
301 CObjectPropertyAssertionExpression* nextObjectPropertyAssertion = neighbouringPropAssList.first();
302 nextVariable = absorbObjectPropertyAssertion(nextVariable, nextObjectPropertyAssertion);
303 } else {
304 break;
305 }
306 }
307
308 return nextVariable;
309 }
310
311
312 CExpressionVariable* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::absorbObjectPropertyAssertion(CExpressionVariable* currentVariable, CObjectPropertyAssertionExpression* startingProperty) {
313 CExpressionVariable* variable = propagateAbsorptionOverObjectPropertyAssertion(currentVariable, startingProperty);
314 mQueryPart->removePropertyAssertion(startingProperty);
315 return variable;
316 }
317
318 static bool itemSortLessObjectPropertyTermOccurrenceScoreThan(const TObjectPropertyOccurrenceScoreSortingItem& item1, const TObjectPropertyOccurrenceScoreSortingItem& item2) {
319 return item1.second < item2.second;
320 }
321
322 static bool itemSortLessVariableTermsOccurrenceScoreThan(const TVariableTermsOccurrenceScoreSortingItem& item1, const TVariableTermsOccurrenceScoreSortingItem& item2) {
323 return item1.second < item2.second;
324 }
325
326
327 QList<CExpressionVariable*> COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::getOrderedVariableAbsorptionList(QList<CExpressionVariable*> varExpList) {
328 if (!mConfOccurrenceStatisticsBasedAbsorptionOrder || !mConfOccurrenceStatisticsCollection || !mOccStatsCacheReader) {
329 return varExpList;
330 }
331
332 QList<CExpressionVariable*> orderedVarExpList;
333 QList<TVariableTermsOccurrenceScoreSortingItem> varTermsOccScoreList;
334 cint64 ontologyId = mOntology->getOntologyID();
335
336 for (CExpressionVariable* varExp : varExpList) {
337 double varOccScore = 0.;
338
339 double varClassAssOccScoreMin = 0;
340 double varClassAssOccScore = 0.;
341 cint64 varClassAssOccCount = 0;
342 for (CBuildExpression* classBuildExp : mQueryPart->getVariableClassExpressions(varExp)) {
343 CClassTermExpression* classExp = dynamic_cast<CClassTermExpression*>(classBuildExp);
344 if (dynamic_cast<CClassExpression*>(classExp)) {
345 CConcept* concept = mOntology->getDataBoxes()->getExpressionDataBoxMapping()->getClassTermConceptMappingHash()->value((CClassTermExpression*)classExp);
346 if (concept->getOperatorCode() == CCEQ) {
347 concept = mOntology->getDataBoxes()->getTBox()->getEquivalentConceptCandidateHash(false)->value(concept);
348 }
349 COccurrenceStatisticsConceptData conceptOccStatsData = mOccStatsCacheReader->getAccummulatedConceptDataOccurrenceStatistics(ontologyId, concept->getConceptTag());
350
351 double conceptOccFactor = conceptOccStatsData.getDeterministicInstanceOccurrencesCount() + conceptOccStatsData.getNonDeterministicInstanceOccurrencesCount() * 2 + conceptOccStatsData.getExistentialInstanceOccurrencesCount() * 1.5 + conceptOccStatsData.getIndividualInstanceOccurrencesCount();
352 if (varClassAssOccCount != 0) {
353 varClassAssOccScoreMin = qMin(varClassAssOccScoreMin, conceptOccFactor);
354 } else {
355 varClassAssOccScoreMin = conceptOccFactor;
356 }
357 varClassAssOccCount++;
358 }
359 }
360 varClassAssOccScore = varClassAssOccScoreMin * (1. / (varClassAssOccCount + 1) + 0.5);
361 varOccScore += varClassAssOccScore;
362
363
364 QList<CObjectPropertyAssertionExpression*> propAssList = mQueryPart->getNeighbouringPropertyAssertions(varExp);
365 propAssList.append(mQueryPart->getSelfPropertyAssertions(varExp));
366 if (!propAssList.isEmpty()) {
367 double varPropAssOccScore = 0.;
368 getOrderedObjectPropertyAbsorptionList(varExp, propAssList, &varPropAssOccScore);
369 varOccScore += varPropAssOccScore / 3.;
370 }
371
372 varTermsOccScoreList.append(TVariableTermsOccurrenceScoreSortingItem(varExp, varOccScore));
373 }
374
375 qSort(varTermsOccScoreList.begin(), varTermsOccScoreList.end(), itemSortLessVariableTermsOccurrenceScoreThan);
376 for (TVariableTermsOccurrenceScoreSortingItem varScoreItem : varTermsOccScoreList) {
377 CExpressionVariable* varExp = varScoreItem.first;
378 orderedVarExpList.append(varExp);
379 }
380
381 return orderedVarExpList;
382 }
383
384 QList<CObjectPropertyAssertionExpression*> COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::getOrderedObjectPropertyAbsorptionList(CExpressionVariable* currentVariable, QList<CObjectPropertyAssertionExpression*> objectPropertyList, double* totalOccScore) {
385 if (!mConfOccurrenceStatisticsBasedAbsorptionOrder || !mConfOccurrenceStatisticsCollection || !mOccStatsCacheReader) {
386 return objectPropertyList;
387 }
388
389 QList<CObjectPropertyAssertionExpression*> orderedList;
390 QList<TObjectPropertyOccurrenceScoreSortingItem> propOccScoreList;
391 cint64 ontologyId = mOntology->getOntologyID();
392
393 //bool hasLikeRole = false;
394
395 double totalScore = 0;
396 for (CObjectPropertyAssertionExpression* propAssExp : objectPropertyList) {
397
398 CRole* role = mOntology->getDataBoxes()->getExpressionDataBoxMapping()->getObjectPropertyTermRoleMappingHash()->value((CObjectPropertyTermExpression*)propAssExp->getObjectPropertyTermExpression());
399 if (role) {
400 //if (CIRIName::getRecentIRIName(role->getPropertyNameLinker()) == "http://semantics.crl.ibm.com/univ-bench-dl.owl#like") {
401 // hasLikeRole = true;
402 //}
403
404
405 double complexFactor = 1.;
406 if (role->isComplexRole()) {
407 complexFactor = 2.5;
408 }
409
410 COccurrenceStatisticsRoleData roleOccStatsData = mOccStatsCacheReader->getAccummulatedRoleDataOccurrenceStatistics(ontologyId, role->getRoleTag());
411 double roleOccFactor = complexFactor * (roleOccStatsData.getDeterministicInstanceOccurrencesCount() + roleOccStatsData.getNonDeterministicInstanceOccurrencesCount() * 2 + roleOccStatsData.getExistentialInstanceOccurrencesCount() * 1.5 + roleOccStatsData.getIndividualInstanceOccurrencesCount());
412 totalScore += roleOccFactor;
413 propOccScoreList.append(TObjectPropertyOccurrenceScoreSortingItem(propAssExp, roleOccFactor));
414 }
415
416 }
417
418 qSort(propOccScoreList.begin(), propOccScoreList.end(), itemSortLessObjectPropertyTermOccurrenceScoreThan);
419 //if (!propOccScoreList.isEmpty() && hasLikeRole) {
420 // CRole* role = mOntology->getDataBoxes()->getExpressionDataBoxMapping()->getObjectPropertyTermRoleMappingHash()->value((CObjectPropertyTermExpression*)propOccScoreList.last().first->getObjectPropertyTermExpression());
421 // if (CIRIName::getRecentIRIName(role->getPropertyNameLinker()) != "http://semantics.crl.ibm.com/univ-bench-dl.owl#like") {
422 // bool debug = true;
423 // if (debug) {
424 // getOrderedObjectPropertyAbsorptionList(currentVariable, objectPropertyList, totalOccScore);
425 // }
426 // }
427 //}
428 for (TObjectPropertyOccurrenceScoreSortingItem propScoreItem : propOccScoreList) {
429 CObjectPropertyAssertionExpression* propAssExp = propScoreItem.first;
430 orderedList.append(propAssExp);
431 }
432
433 if (totalOccScore) {
434 *totalOccScore = totalScore;
435 }
436
437 return orderedList;
438 }
439
440
441 CConcept* COptimizedComplexVariableAbstractNonDisconnectingAbsorptionBasedQueryPartHandler::createNewConcept() {
442 CConcept* concept = CObjectAllocator< CConcept >::allocateAndConstruct(mMemMan);
443 concept->initConcept();
444 cint64 nextConTag = mTBox->getNextConceptID();
445 concept->initTag(nextConTag);
446 mConceptVec->setLocalData(nextConTag, concept);
447 return concept;
448 }
449
450
451 }; // end namespace Answerer
452
453 }; // end namespace Reasoner
454
455 }; // end namespace Konclude