| 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 "CReapplyRoleSuccessorHash.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Process { |
| 31 |
|
| 32 |
|
| 33 |
CReapplyRoleSuccessorHash::CReapplyRoleSuccessorHash(CProcessContext* context) : mContext(context),mRoleSuccessorDataHash(context) { |
| 34 |
mLinkCount = 0; |
| 35 |
} |
| 36 |
|
| 37 |
CReapplyRoleSuccessorHash* CReapplyRoleSuccessorHash::initRoleSuccessorHash(CReapplyRoleSuccessorHash* prevRoleSuccHash) { |
| 38 |
if (prevRoleSuccHash) { |
| 39 |
mRoleSuccessorDataHash = prevRoleSuccHash->mRoleSuccessorDataHash; |
| 40 |
mLinkCount = prevRoleSuccHash->mLinkCount; |
| 41 |
} else { |
| 42 |
mRoleSuccessorDataHash.clear(); |
| 43 |
mLinkCount = 0; |
| 44 |
} |
| 45 |
return this; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
cint64 CReapplyRoleSuccessorHash::insertRoleSuccessorLink(CRole* role, CIndividualLinkEdge* link, CReapplyQueueIterator* reapplyQueueIterator) { |
| 50 |
CReapplyRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 51 |
if (roleSuccData.mLinkSet) { |
| 52 |
cint64 coupId = getCoupledIndividualID(link); |
| 53 |
ensureRoleSuccessorDataLocalated(roleSuccData); |
| 54 |
eliminateRoleSuccessorPreviousShareData(roleSuccData, coupId); |
| 55 |
roleSuccData.mLinkSet->insert(coupId, link); |
| 56 |
} |
| 57 |
roleSuccData.mLinkLinker = link->append(roleSuccData.mLinkLinker); |
| 58 |
roleSuccData.mLinkCount++; |
| 59 |
++mLinkCount; |
| 60 |
if (reapplyQueueIterator) { |
| 61 |
*reapplyQueueIterator = roleSuccData.mReapplyQueue.getIterator(true); |
| 62 |
} |
| 63 |
return roleSuccData.mLinkCount; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void CReapplyRoleSuccessorHash::ensureRoleSuccessorDataLocalated(CReapplyRoleSuccessorData &roleSuccData) { |
| 69 |
if (!roleSuccData.mLocatedLinkSet) { |
| 70 |
|
| 71 |
CPROCESSHASH<cint64, CIndividualLinkEdge*>* referredLinkSet = roleSuccData.mLinkSet; |
| 72 |
CPROCESSHASH<cint64, CIndividualLinkEdge*>* referredPrevLinkSet = roleSuccData.mPrevLinkSet; |
| 73 |
|
| 74 |
|
| 75 |
CPROCESSHASH<cint64, CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64, CIndividualLinkEdge*>, CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext), mContext); |
| 76 |
roleSuccData.mLinkSet = newLinkSet; |
| 77 |
roleSuccData.mPrevLinkSet = nullptr; |
| 78 |
roleSuccData.mLocatedLinkSet = true; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if (!referredPrevLinkSet && referredLinkSet->size() <= 100) { |
| 83 |
newLinkSet->init(referredLinkSet, mContext); |
| 84 |
} else if (!referredPrevLinkSet && referredLinkSet->size() > 100) { |
| 85 |
roleSuccData.mPrevLinkSet = referredLinkSet; |
| 86 |
} else { |
| 87 |
|
| 88 |
if (referredLinkSet->size() * 10 > referredPrevLinkSet->size()) { |
| 89 |
newLinkSet->init(referredPrevLinkSet, mContext); |
| 90 |
|
| 91 |
for (CPROCESSHASH<cint64, CIndividualLinkEdge*>::const_iterator it = referredLinkSet->constBegin(), itEnd = referredLinkSet->constEnd(); it != itEnd; ++it) { |
| 92 |
cint64 neighbourId = it.key(); |
| 93 |
CIndividualLinkEdge* link = it.value(); |
| 94 |
newLinkSet->insert(neighbourId, link); |
| 95 |
} |
| 96 |
} else { |
| 97 |
newLinkSet->init(referredLinkSet, mContext); |
| 98 |
roleSuccData.mPrevLinkSet = referredPrevLinkSet; |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void CReapplyRoleSuccessorHash::eliminateRoleSuccessorPreviousShareData(CReapplyRoleSuccessorData &roleSuccData, cint64 coupId) { |
| 107 |
if (roleSuccData.mPrevLinkSet && roleSuccData.mPrevLinkSet->contains(coupId)) { |
| 108 |
for (CPROCESSHASH<cint64, CIndividualLinkEdge*>::const_iterator it = roleSuccData.mPrevLinkSet->constBegin(), itEnd = roleSuccData.mPrevLinkSet->constEnd(); it != itEnd; ++it) { |
| 109 |
cint64 neighbourId = it.key(); |
| 110 |
CIndividualLinkEdge* link = it.value(); |
| 111 |
roleSuccData.mLinkSet->insert(neighbourId, link); |
| 112 |
} |
| 113 |
roleSuccData.mPrevLinkSet = nullptr; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
CReapplyRoleSuccessorHash* CReapplyRoleSuccessorHash::removeRoleSuccessorLink(CRole* role, CIndividualLinkEdge* link) { |
| 118 |
CReapplyRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 119 |
if (roleSuccData.mLinkSet) { |
| 120 |
cint64 coupId = getCoupledIndividualID(link); |
| 121 |
ensureRoleSuccessorDataLocalated(roleSuccData); |
| 122 |
eliminateRoleSuccessorPreviousShareData(roleSuccData, coupId); |
| 123 |
roleSuccData.mLinkSet->remove(coupId); |
| 124 |
} else { |
| 125 |
if (roleSuccData.mLinkLinker) { |
| 126 |
// replace by set |
| 127 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 128 |
roleSuccData.mLinkSet = newLinkSet; |
| 129 |
roleSuccData.mLocatedLinkSet = true; |
| 130 |
|
| 131 |
CIndividualLinkEdge* linkIt = roleSuccData.mLinkLinker; |
| 132 |
while (linkIt) { |
| 133 |
if (linkIt != link) { |
| 134 |
newLinkSet->insert(getCoupledIndividualID(linkIt),linkIt); |
| 135 |
} |
| 136 |
linkIt = linkIt->getNext(); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
--roleSuccData.mLinkCount; |
| 141 |
--mLinkCount; |
| 142 |
return this; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
CReapplyRoleSuccessorHash* CReapplyRoleSuccessorHash::removeRoleSuccessorLink(CRole* role, cint64 sourceIndiID, cint64 destinationIndiID) { |
| 148 |
CReapplyRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 149 |
if (roleSuccData.mLinkSet) { |
| 150 |
cint64 searchedCoupledID = getCoupledIndividualID(sourceIndiID, destinationIndiID); |
| 151 |
ensureRoleSuccessorDataLocalated(roleSuccData); |
| 152 |
eliminateRoleSuccessorPreviousShareData(roleSuccData, searchedCoupledID); |
| 153 |
roleSuccData.mLinkSet->remove(searchedCoupledID); |
| 154 |
} else { |
| 155 |
if (roleSuccData.mLinkLinker) { |
| 156 |
// replace by set |
| 157 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 158 |
roleSuccData.mLinkSet = newLinkSet; |
| 159 |
roleSuccData.mLocatedLinkSet = true; |
| 160 |
|
| 161 |
cint64 searchedCoupledID = getCoupledIndividualID(sourceIndiID,destinationIndiID); |
| 162 |
CIndividualLinkEdge* linkIt = roleSuccData.mLinkLinker; |
| 163 |
while (linkIt) { |
| 164 |
cint64 coupledID = getCoupledIndividualID(linkIt); |
| 165 |
if (coupledID != searchedCoupledID) { |
| 166 |
newLinkSet->insert(coupledID,linkIt); |
| 167 |
} |
| 168 |
linkIt = linkIt->getNext(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
--roleSuccData.mLinkCount; |
| 173 |
--mLinkCount; |
| 174 |
return this; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
CIndividualLinkEdge* CReapplyRoleSuccessorHash::getRoleSuccessorToIndividualLink(CRole* role, cint64 sourceIndiID, cint64 destinationIndiID, bool locateable) { |
| 180 |
CReapplyRoleSuccessorData* roleSuccData = mRoleSuccessorDataHash.valuePointer(role); |
| 181 |
if (roleSuccData && roleSuccData->mLinkLinker) { |
| 182 |
if (locateable && !roleSuccData->mLinkSet && roleSuccData->mLinkCount >= 5) { |
| 183 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 184 |
roleSuccData->mLinkSet = newLinkSet; |
| 185 |
roleSuccData->mLocatedLinkSet = true; |
| 186 |
|
| 187 |
CIndividualLinkEdge* linkIt = roleSuccData->mLinkLinker; |
| 188 |
CIndividualLinkEdge* searchedLink = nullptr; |
| 189 |
cint64 searchedCoupledID = getCoupledIndividualID(sourceIndiID,destinationIndiID); |
| 190 |
while (linkIt) { |
| 191 |
cint64 coupledID = getCoupledIndividualID(linkIt); |
| 192 |
newLinkSet->insert(coupledID,linkIt); |
| 193 |
if (coupledID == searchedCoupledID) { |
| 194 |
searchedLink = linkIt; |
| 195 |
} |
| 196 |
linkIt = linkIt->getNext(); |
| 197 |
} |
| 198 |
return searchedLink; |
| 199 |
|
| 200 |
} else if (!roleSuccData->mLinkSet) { |
| 201 |
cint64 searchedCoupledID = getCoupledIndividualID(sourceIndiID,destinationIndiID); |
| 202 |
CIndividualLinkEdge* linkIt = roleSuccData->mLinkLinker; |
| 203 |
while (linkIt) { |
| 204 |
cint64 coupledID = getCoupledIndividualID(linkIt); |
| 205 |
if (coupledID == searchedCoupledID) { |
| 206 |
return linkIt; |
| 207 |
} |
| 208 |
linkIt = linkIt->getNext(); |
| 209 |
} |
| 210 |
|
| 211 |
} else { |
| 212 |
cint64 searchedCoupledID = getCoupledIndividualID(sourceIndiID,destinationIndiID); |
| 213 |
CIndividualLinkEdge* link = roleSuccData->mLinkSet->value(searchedCoupledID,nullptr); |
| 214 |
if (!link && roleSuccData->mPrevLinkSet) { |
| 215 |
link = roleSuccData->mPrevLinkSet->value(searchedCoupledID, nullptr); |
| 216 |
} |
| 217 |
return link; |
| 218 |
} |
| 219 |
} |
| 220 |
return nullptr; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
bool CReapplyRoleSuccessorHash::hasRoleSuccessorToIndividual(CRole* role, cint64 sourceIndiID, cint64 destinationIndiID, bool locateable) { |
| 226 |
CIndividualLinkEdge* searchedLink = getRoleSuccessorToIndividualLink(role,sourceIndiID,destinationIndiID,locateable); |
| 227 |
return searchedLink != nullptr; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
bool CReapplyRoleSuccessorHash::hasRoleSuccessor(CRole* role) { |
| 233 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 234 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 235 |
if (roleSuccData->mPrevLinkSet) { |
| 236 |
return !roleSuccData->mLinkSet->isEmpty() || !roleSuccData->mPrevLinkSet->isEmpty(); |
| 237 |
} else if(roleSuccData->mLinkSet) { |
| 238 |
return !roleSuccData->mLinkSet->isEmpty(); |
| 239 |
} else { |
| 240 |
return roleSuccData->mLinkLinker != nullptr; |
| 241 |
} |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
CRoleSuccessorLinkIterator CReapplyRoleSuccessorHash::getRoleSuccessorLinkIterator(CRole* role) { |
| 248 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 249 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 250 |
if (roleSuccData->mPrevLinkSet) { |
| 251 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(), (roleSuccData->mLinkSet)->end(), (roleSuccData->mPrevLinkSet)->begin(), (roleSuccData->mPrevLinkSet)->end()); |
| 252 |
} else if (roleSuccData->mLinkSet) { |
| 253 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(),(roleSuccData->mLinkSet)->end()); |
| 254 |
} else { |
| 255 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker); |
| 256 |
} |
| 257 |
} |
| 258 |
return CRoleSuccessorLinkIterator(nullptr); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
CRoleSuccessorLinkIterator CReapplyRoleSuccessorHash::getRoleSuccessorLinkIterator(CRole* role, cint64* linkCount, CIndividualLinkEdge*& lastLink) { |
| 263 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 264 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 265 |
lastLink = roleSuccData->mLinkLinker; |
| 266 |
if (linkCount) { |
| 267 |
*linkCount = roleSuccData->mLinkCount; |
| 268 |
} |
| 269 |
if (roleSuccData->mPrevLinkSet) { |
| 270 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(), (roleSuccData->mLinkSet)->end(), (roleSuccData->mPrevLinkSet)->begin(), (roleSuccData->mPrevLinkSet)->end()); |
| 271 |
} else if (roleSuccData->mLinkSet) { |
| 272 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(),(roleSuccData->mLinkSet)->end()); |
| 273 |
} else { |
| 274 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker); |
| 275 |
} |
| 276 |
} |
| 277 |
return CRoleSuccessorLinkIterator(nullptr); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
CRoleSuccessorLinkIterator CReapplyRoleSuccessorHash::getRoleSuccessorLinkIterator(CRole* role, cint64* linkCount) { |
| 283 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 284 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 285 |
if (linkCount) { |
| 286 |
*linkCount = roleSuccData->mLinkCount; |
| 287 |
} |
| 288 |
if (roleSuccData->mPrevLinkSet) { |
| 289 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(), (roleSuccData->mLinkSet)->end(), (roleSuccData->mPrevLinkSet)->begin(), (roleSuccData->mPrevLinkSet)->end()); |
| 290 |
} else if (roleSuccData->mLinkSet) { |
| 291 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(),(roleSuccData->mLinkSet)->end()); |
| 292 |
} else { |
| 293 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker); |
| 294 |
} |
| 295 |
} |
| 296 |
return CRoleSuccessorLinkIterator(nullptr); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
cint64 CReapplyRoleSuccessorHash::getRoleSuccessorCount(CRole* role) { |
| 301 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 302 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 303 |
return roleSuccData->mLinkCount; |
| 304 |
} |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
CRoleSuccessorLinkIterator CReapplyRoleSuccessorHash::getRoleSuccessorHistoryLinkIterator(CRole* role, CIndividualLinkEdge* lastLink) { |
| 311 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 312 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 313 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker,lastLink); |
| 314 |
} |
| 315 |
return CRoleSuccessorLinkIterator(nullptr); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
CRoleSuccessorLinkIterator CReapplyRoleSuccessorHash::getRoleSuccessorHistoryLinkIterator(CRole* role, CIndividualLinkEdge* lastLink, cint64* linkCount) { |
| 320 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 321 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 322 |
if (linkCount) { |
| 323 |
*linkCount = roleSuccData->mLinkCount; |
| 324 |
} |
| 325 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker,lastLink); |
| 326 |
} |
| 327 |
return CRoleSuccessorLinkIterator(nullptr); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
CReapplyQueue* CReapplyRoleSuccessorHash::getRoleReapplyQueue(CRole* role, bool create) { |
| 333 |
if (create) { |
| 334 |
CReapplyRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 335 |
return &roleSuccData.mReapplyQueue; |
| 336 |
} else { |
| 337 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 338 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 339 |
return &roleSuccData->mReapplyQueue; |
| 340 |
} |
| 341 |
} |
| 342 |
return nullptr; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
bool CReapplyRoleSuccessorHash::containsRoleReapplyQueue(CRole* role) { |
| 347 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 348 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 349 |
return !roleSuccData->mReapplyQueue.isEmpty(); |
| 350 |
} |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
CReapplyQueueIterator CReapplyRoleSuccessorHash::getRoleReapplyIterator(CRole* role, bool clearDynamicReapplyQueue) { |
| 356 |
CReapplyRoleSuccessorData* roleSuccData = nullptr; |
| 357 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 358 |
return roleSuccData->mReapplyQueue.getIterator(clearDynamicReapplyQueue); |
| 359 |
} |
| 360 |
return CReapplyQueueIterator(nullptr,nullptr); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
cint64 CReapplyRoleSuccessorHash::getCoupledIndividualID(CIndividualLinkEdge* link) { |
| 365 |
return link->getCoupledIndividualID(); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
cint64 CReapplyRoleSuccessorHash::getCoupledIndividualID(cint64 indi1ID, cint64 indi2ID) { |
| 370 |
return indi1ID + indi2ID; |
| 371 |
} |
| 372 |
|
| 373 |
CRoleSuccessorIterator CReapplyRoleSuccessorHash::getRoleIterator() { |
| 374 |
return CRoleSuccessorIterator(mRoleSuccessorDataHash.begin(),mRoleSuccessorDataHash.end()); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
}; // end namespace Process |
| 379 |
|
| 380 |
}; // end namespace Kernel |
| 381 |
|
| 382 |
}; // end namespace Reasoner |
| 383 |
|
| 384 |
}; // end namespace Konclude |