| 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 "CRoleSuccessorHash.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Process { |
| 31 |
|
| 32 |
|
| 33 |
CRoleSuccessorHash::CRoleSuccessorHash(CProcessContext* context) : mContext(context),mRoleSuccessorDataHash(context) { |
| 34 |
} |
| 35 |
|
| 36 |
CRoleSuccessorHash::~CRoleSuccessorHash() { |
| 37 |
} |
| 38 |
|
| 39 |
CRoleSuccessorHash* CRoleSuccessorHash::initRoleSuccessorHash(CRoleSuccessorHash* prevRoleSuccHash) { |
| 40 |
if (prevRoleSuccHash) { |
| 41 |
mRoleSuccessorDataHash = prevRoleSuccHash->mRoleSuccessorDataHash; |
| 42 |
} else { |
| 43 |
mRoleSuccessorDataHash.clear(); |
| 44 |
} |
| 45 |
return this; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
CRoleSuccessorHash* CRoleSuccessorHash::insertRoleSuccessorLink(CRole* role, CIndividualLinkEdge* link) { |
| 50 |
CRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 51 |
if (roleSuccData.mLinkSet) { |
| 52 |
if (!roleSuccData.mLocatedLinkSet) { |
| 53 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 54 |
newLinkSet->init(roleSuccData.mLinkSet,mContext); |
| 55 |
roleSuccData.mLinkSet = newLinkSet; |
| 56 |
roleSuccData.mLocatedLinkSet = true; |
| 57 |
} |
| 58 |
roleSuccData.mLinkSet->insert(link->getCoupledIndividualID(),link); |
| 59 |
} else { |
| 60 |
roleSuccData.mLinkLinker = link->append(roleSuccData.mLinkLinker); |
| 61 |
} |
| 62 |
return this; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
CRoleSuccessorHash* CRoleSuccessorHash::removeRoleSuccessorLink(CRole* role, CIndividualLinkEdge* link) { |
| 67 |
CRoleSuccessorData& roleSuccData = mRoleSuccessorDataHash[role]; |
| 68 |
if (roleSuccData.mLinkSet) { |
| 69 |
if (!roleSuccData.mLocatedLinkSet) { |
| 70 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 71 |
newLinkSet->init(roleSuccData.mLinkSet,mContext); |
| 72 |
roleSuccData.mLinkSet = newLinkSet; |
| 73 |
roleSuccData.mLocatedLinkSet = true; |
| 74 |
} |
| 75 |
roleSuccData.mLinkSet->remove(link->getCoupledIndividualID()); |
| 76 |
} else { |
| 77 |
if (roleSuccData.mLinkLinker) { |
| 78 |
// replace by set |
| 79 |
CPROCESSHASH<cint64,CIndividualLinkEdge*>* newLinkSet = CObjectParameterizingAllocator< CPROCESSHASH<cint64,CIndividualLinkEdge*>,CContext* >::allocateAndConstructAndParameterize(CContext::getMemoryAllocationManager(mContext),mContext); |
| 80 |
roleSuccData.mLinkSet = newLinkSet; |
| 81 |
roleSuccData.mLocatedLinkSet = true; |
| 82 |
|
| 83 |
CIndividualLinkEdge* linkIt = roleSuccData.mLinkLinker; |
| 84 |
while (linkIt) { |
| 85 |
if (linkIt != link) { |
| 86 |
newLinkSet->insert(linkIt->getCoupledIndividualID(),linkIt); |
| 87 |
} |
| 88 |
linkIt = linkIt->getNext(); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
return this; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
CRoleSuccessorLinkIterator CRoleSuccessorHash::getRoleSuccessorLinkIterator(CRole* role) { |
| 97 |
CRoleSuccessorData* roleSuccData = nullptr; |
| 98 |
if (mRoleSuccessorDataHash.tryGetValuePointer(role,roleSuccData)) { |
| 99 |
if (roleSuccData->mLinkSet) { |
| 100 |
return CRoleSuccessorLinkIterator((roleSuccData->mLinkSet)->begin(),(roleSuccData->mLinkSet)->end()); |
| 101 |
} else { |
| 102 |
return CRoleSuccessorLinkIterator(roleSuccData->mLinkLinker); |
| 103 |
} |
| 104 |
} |
| 105 |
return CRoleSuccessorLinkIterator(nullptr); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
}; // end namespace Process |
| 111 |
|
| 112 |
}; // end namespace Kernel |
| 113 |
|
| 114 |
}; // end namespace Reasoner |
| 115 |
|
| 116 |
}; // end namespace Konclude |