| 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 "CBackendRepresentativeMemoryCacheUtilities.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Cache { |
| 31 |
|
| 32 |
|
| 33 |
CBackendRepresentativeMemoryCacheUtilities::CBackendRepresentativeMemoryCacheUtilities() { |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getConceptDescriptorSignature(CConceptSaturationDescriptor* conDesLinker, cint64 count, CConcept* exclusionConcept) { |
| 38 |
cint64 sigValue = 0; |
| 39 |
for (CConceptSaturationDescriptor* conDesLinkerIt = conDesLinker; conDesLinkerIt; conDesLinkerIt = conDesLinkerIt->getNext()) { |
| 40 |
CConcept* concept = conDesLinkerIt->getConcept(); |
| 41 |
bool negated = conDesLinkerIt->isNegated(); |
| 42 |
|
| 43 |
if (negated || concept != exclusionConcept) { |
| 44 |
|
| 45 |
cint64 tag = concept->getConceptTag(); |
| 46 |
if (negated) { |
| 47 |
tag = -tag; |
| 48 |
} |
| 49 |
sigValue += qHash((cint64)tag); |
| 50 |
} |
| 51 |
} |
| 52 |
return sigValue; |
| 53 |
} |
| 54 |
|
| 55 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getConceptDescriptorSignature(CConceptDescriptor* conDesLinker, cint64& count, bool deterministic, cint64 maxDeterministicBranchTag, bool excludePositiveNominalConcepts) { |
| 56 |
cint64 sigValue = 0; |
| 57 |
count = 0; |
| 58 |
for (CConceptDescriptor* conDesLinkerIt = conDesLinker; conDesLinkerIt; conDesLinkerIt = conDesLinkerIt->getNext()) { |
| 59 |
CConcept* concept = conDesLinkerIt->getConcept(); |
| 60 |
bool negated = conDesLinkerIt->isNegated(); |
| 61 |
|
| 62 |
|
| 63 |
bool deterministicallyDerived = true; |
| 64 |
if (!conDesLinkerIt->getDependencyTrackPoint() || conDesLinkerIt->getDependencyTrackPoint()->getBranchingTag() > maxDeterministicBranchTag) { |
| 65 |
deterministicallyDerived = false; |
| 66 |
} |
| 67 |
bool considerConcept = deterministicallyDerived == deterministic; |
| 68 |
if (excludePositiveNominalConcepts && concept->getOperatorCode() == CCNOMINAL && !negated) { |
| 69 |
considerConcept = false; |
| 70 |
} |
| 71 |
|
| 72 |
if (considerConcept) { |
| 73 |
cint64 tag = concept->getConceptTag(); |
| 74 |
if (negated) { |
| 75 |
tag = -tag; |
| 76 |
} |
| 77 |
sigValue += qHash((cint64)tag); |
| 78 |
++count; |
| 79 |
} |
| 80 |
} |
| 81 |
return sigValue; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getIndividualSetSignature(cint64 indiId, CIndividualMergingHash* indiMerginghash, cint64& count, bool onlyDeterministic, cint64 maxDeterministicBranchTag) { |
| 86 |
cint64 sigValue = 0; |
| 87 |
count = 0; |
| 88 |
for (CIndividualMergingHash::const_iterator it = indiMerginghash->constBegin(), itEnd = indiMerginghash->constEnd(); it != itEnd; ++it) { |
| 89 |
cint64 mergedIndiId = it.key(); |
| 90 |
if (mergedIndiId != indiId && it.value().isMergedWithIndividual()) { |
| 91 |
CIndividualMergingHashData mergedData = it.value(); |
| 92 |
|
| 93 |
bool deterministicallyDerived = true; |
| 94 |
if (!mergedData.getDependencyTrackPoint() || mergedData.getDependencyTrackPoint()->getBranchingTag() > maxDeterministicBranchTag) { |
| 95 |
deterministicallyDerived = false; |
| 96 |
} |
| 97 |
bool considerIndividual = deterministicallyDerived || !onlyDeterministic; |
| 98 |
|
| 99 |
if (considerIndividual) { |
| 100 |
cint64 tag = mergedIndiId; |
| 101 |
sigValue += qHash((cint64)tag); |
| 102 |
++count; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if (count > 0 && indiId >= 0) { |
| 107 |
sigValue += qHash((cint64)indiId); |
| 108 |
++count; |
| 109 |
} |
| 110 |
return sigValue; |
| 111 |
} |
| 112 |
|
| 113 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getIndividualSetSignature(cint64 indiId, CDistinctHash* indiDistinctHash, cint64& count, bool onlyDeterministic, cint64 maxDeterministicBranchTag) { |
| 114 |
cint64 sigValue = 0; |
| 115 |
count = 0; |
| 116 |
for (CDistinctHash::const_iterator it = indiDistinctHash->constBegin(), itEnd = indiDistinctHash->constEnd(); it != itEnd; ++it) { |
| 117 |
cint64 distinctIndiId = -it.key(); |
| 118 |
if (distinctIndiId >= 0 && distinctIndiId != indiId) { |
| 119 |
CDistinctEdge* distinctEdge = it.value(); |
| 120 |
|
| 121 |
bool deterministicallyDerived = true; |
| 122 |
if (!distinctEdge->getDependencyTrackPoint() || distinctEdge->getDependencyTrackPoint()->getBranchingTag() > maxDeterministicBranchTag) { |
| 123 |
deterministicallyDerived = false; |
| 124 |
} |
| 125 |
bool considerIndividual = deterministicallyDerived || !onlyDeterministic; |
| 126 |
|
| 127 |
if (considerIndividual) { |
| 128 |
cint64 tag = distinctIndiId; |
| 129 |
sigValue += qHash((cint64)tag); |
| 130 |
++count; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if (count > 0 && indiId >= 0) { |
| 135 |
sigValue += qHash((cint64)indiId); |
| 136 |
++count; |
| 137 |
} |
| 138 |
return sigValue; |
| 139 |
} |
| 140 |
|
| 141 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getIndividualSetSignature(CPROCESSSET<cint64>* individualSet, cint64& count) { |
| 142 |
cint64 sigValue = 0; |
| 143 |
count = 0; |
| 144 |
for (CPROCESSSET<cint64>::const_iterator it = individualSet->constBegin(), itEnd = individualSet->constEnd(); it != itEnd; ++it) { |
| 145 |
cint64 indiId = *it; |
| 146 |
sigValue += qHash((cint64)indiId); |
| 147 |
++count; |
| 148 |
} |
| 149 |
return sigValue; |
| 150 |
} |
| 151 |
|
| 152 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getConceptDescriptorSignature(CConceptDescriptor* conDesLinker, cint64& count, function<bool(CConcept* concept, bool negation)> exclusionDetermineFunction) { |
| 153 |
cint64 sigValue = 0; |
| 154 |
count = 0; |
| 155 |
for (CConceptDescriptor* conDesLinkerIt = conDesLinker; conDesLinkerIt; conDesLinkerIt = conDesLinkerIt->getNext()) { |
| 156 |
CConcept* concept = conDesLinkerIt->getConcept(); |
| 157 |
bool negated = conDesLinkerIt->isNegated(); |
| 158 |
|
| 159 |
bool considerConcept = exclusionDetermineFunction(concept, negated); |
| 160 |
|
| 161 |
if (considerConcept) { |
| 162 |
cint64 tag = concept->getConceptTag(); |
| 163 |
if (negated) { |
| 164 |
tag = -tag; |
| 165 |
} |
| 166 |
sigValue += qHash((cint64)tag); |
| 167 |
++count; |
| 168 |
} |
| 169 |
} |
| 170 |
return sigValue; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getConceptDescriptorSignature(CConceptDescriptor* conDesLinker, cint64& count, function<bool(CConcept* concept, bool negation)> exclusionDetermineFunction, function<bool(CConcept* concept, bool negation, CDependencyTrackPoint* depTrackPoint)> deterministicDetermineFunction) { |
| 175 |
cint64 sigValue = 0; |
| 176 |
count = 0; |
| 177 |
for (CConceptDescriptor* conDesLinkerIt = conDesLinker; conDesLinkerIt; conDesLinkerIt = conDesLinkerIt->getNext()) { |
| 178 |
CConcept* concept = conDesLinkerIt->getConcept(); |
| 179 |
bool negated = conDesLinkerIt->isNegated(); |
| 180 |
|
| 181 |
bool considerConcept = exclusionDetermineFunction(concept, negated); |
| 182 |
if (considerConcept) { |
| 183 |
bool deterministic = deterministicDetermineFunction(concept, negated, conDesLinkerIt->getDependencyTrackPoint()); |
| 184 |
|
| 185 |
cint64 tag = concept->getConceptTag(); |
| 186 |
if (negated) { |
| 187 |
tag = -tag; |
| 188 |
} |
| 189 |
sigValue += qHash((cint64)tag); |
| 190 |
++count; |
| 191 |
} |
| 192 |
} |
| 193 |
return sigValue; |
| 194 |
} |
| 195 |
|
| 196 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getRoleInversedLinkerSignature(CSortedNegLinker<CRole*>* roleLinker, bool inversed, cint64 count) { |
| 197 |
cint64 sigValue = 0; |
| 198 |
for (CSortedNegLinker<CRole*>* roleLinkerIt = roleLinker; roleLinkerIt; roleLinkerIt = roleLinkerIt->getNext()) { |
| 199 |
CRole* role = roleLinkerIt->getData(); |
| 200 |
bool roleInversed = roleLinkerIt->isNegated() ^ inversed; |
| 201 |
|
| 202 |
cint64 tag = role->getRoleTag(); |
| 203 |
if (roleInversed) { |
| 204 |
tag = -tag; |
| 205 |
} |
| 206 |
sigValue += qHash((cint64)tag); |
| 207 |
} |
| 208 |
return sigValue; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getNeighbourRoleInstantiatedSetLinkerSignature(CBackendRepresentativeMemoryCacheTemporaryLabelReferenceDataLinker* neighbourRoleSetLinker, cint64 count) { |
| 213 |
cint64 signature = 0; |
| 214 |
for (CBackendRepresentativeMemoryCacheTemporaryLabelReferenceDataLinker* neighbourRoleSetLinkerIt = neighbourRoleSetLinker; neighbourRoleSetLinkerIt; neighbourRoleSetLinkerIt = neighbourRoleSetLinkerIt->getNext()) { |
| 215 |
CBackendRepresentativeMemoryLabelCacheItem* labelCacheItem = neighbourRoleSetLinkerIt->getReferredLabelData(); |
| 216 |
CBackendRepresentativeMemoryCacheTemporaryLabelWriteDataLinker* tmpLabelWriteData = neighbourRoleSetLinkerIt->getReferredTemporaryLabelData(); |
| 217 |
|
| 218 |
cint64 value = 0; |
| 219 |
if (tmpLabelWriteData) { |
| 220 |
value = (cint64)tmpLabelWriteData; |
| 221 |
} else { |
| 222 |
value = (cint64)labelCacheItem->getCacheEntryID(); |
| 223 |
} |
| 224 |
signature += qHash((qint64)value); |
| 225 |
} |
| 226 |
return signature; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
cint64 CBackendRepresentativeMemoryCacheUtilities::getSignatureExtensionByCacheValue(cint64 signature, CCacheValue& cacheValue) { |
| 231 |
signature += qHash((qint64)cacheValue.getTag()); |
| 232 |
return signature; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
}; // end namespace Cache |
| 237 |
|
| 238 |
}; // end namespace Kernel |
| 239 |
|
| 240 |
}; // end namespace Reasoner |
| 241 |
|
| 242 |
}; // end namespace Konclude |