| 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 "CAnsweringSplitPropagationTestingVariableData.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Answerer { |
| 29 |
|
| 30 |
|
| 31 |
CAnsweringSplitPropagationTestingVariableData::CAnsweringSplitPropagationTestingVariableData(cint64 variableDataId, CVariable* variable, CExpressionVariable* variableExpression, QSet<CIndividualReference>* bindingSet) { |
| 32 |
mVariableDataId = variableDataId; |
| 33 |
mIntegratedDataLinker = nullptr; |
| 34 |
mIntegratedBindingsCount = 0; |
| 35 |
mIntegrationBindingsLimit = 10; |
| 36 |
mVariable = variable; |
| 37 |
mVariableExpression = variableExpression; |
| 38 |
mVariableDataIntegrationId = -1; |
| 39 |
mDisabledBindingsCount = 0; |
| 40 |
mLastIntegratedBindingsCount = 0; |
| 41 |
mBindingSet = bindingSet; |
| 42 |
mRemainingIntegrationRequirementCount = 0; |
| 43 |
|
| 44 |
if (bindingSet) { |
| 45 |
addIntegrateableVariableBindings(bindingSet); |
| 46 |
} else { |
| 47 |
mDynamicBindings = true; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
QSet<CIndividualReference>* CAnsweringSplitPropagationTestingVariableData::getVariableBindingIndividualSet() { |
| 53 |
return mBindingSet; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::setVariableBindingsIntegrationLimit(cint64 limit) { |
| 58 |
mIntegrationBindingsLimit = limit; |
| 59 |
return this; |
| 60 |
} |
| 61 |
|
| 62 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::addIntegrateableVariableBinding(const CIndividualReference& indiRef) { |
| 63 |
mBindingIndiDataHash[indiRef]; |
| 64 |
mDynamicBindings = false; |
| 65 |
return this; |
| 66 |
} |
| 67 |
|
| 68 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::addIntegrateableVariableBindings(QSet<CIndividualReference>* bindingSet) { |
| 69 |
if (bindingSet) { |
| 70 |
for (CIndividualReference indiRef : *bindingSet) { |
| 71 |
CAnsweringSplitPropagationTestingVariableBindingData& bindingData = mBindingIndiDataHash[indiRef]; |
| 72 |
} |
| 73 |
mDynamicBindings = false; |
| 74 |
} |
| 75 |
return this; |
| 76 |
} |
| 77 |
|
| 78 |
cint64 CAnsweringSplitPropagationTestingVariableData::getIntegrateableVariableBindingCount() { |
| 79 |
return mBindingIndiDataHash.size(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
bool CAnsweringSplitPropagationTestingVariableData::visitIntegrateableVariableBindings(function<bool(const CIndividualReference& indiRef)> visitFunc) { |
| 84 |
bool continueVisiting = true; |
| 85 |
bool visited = false; |
| 86 |
for (QHash<CIndividualReference, CAnsweringSplitPropagationTestingVariableBindingData>::const_iterator it = mBindingIndiDataHash.constBegin(), itEnd = mBindingIndiDataHash.constEnd(); it != itEnd && continueVisiting; ++it) { |
| 87 |
continueVisiting = visitFunc(it.key()); |
| 88 |
visited = true; |
| 89 |
} |
| 90 |
return visited; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
cint64 CAnsweringSplitPropagationTestingVariableData::getVariableDataId() { |
| 95 |
return mVariableDataId; |
| 96 |
} |
| 97 |
|
| 98 |
CVariable* CAnsweringSplitPropagationTestingVariableData::getVariable() { |
| 99 |
return mVariable; |
| 100 |
} |
| 101 |
|
| 102 |
CExpressionVariable* CAnsweringSplitPropagationTestingVariableData::getVariableExpression() { |
| 103 |
return mVariableExpression; |
| 104 |
} |
| 105 |
|
| 106 |
QHash<CIndividualReference, CAnsweringSplitPropagationTestingVariableBindingData>* CAnsweringSplitPropagationTestingVariableData::getBindingIndividualDataHash() { |
| 107 |
return &mBindingIndiDataHash; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
bool CAnsweringSplitPropagationTestingVariableData::canIntegrateBinding(const CIndividualReference& indiRef, cint64* integationCounter) { |
| 112 |
bool checkIntegrationData = false; |
| 113 |
if (mDynamicBindings && (mIntegrationBindingsLimit < 0 || mIntegratedBindingsCount < mIntegrationBindingsLimit)) { |
| 114 |
checkIntegrationData = true; |
| 115 |
} else if (mBindingIndiDataHash.contains(indiRef)) { |
| 116 |
checkIntegrationData = true; |
| 117 |
} |
| 118 |
if (checkIntegrationData) { |
| 119 |
CAnsweringSplitPropagationTestingVariableBindingData& bindingData = mBindingIndiDataHash[indiRef]; |
| 120 |
if (bindingData.isIntegrated()) { |
| 121 |
return true; |
| 122 |
} else { |
| 123 |
if (!bindingData.isDisabled()) { |
| 124 |
if (mIntegrationBindingsLimit < 0 || mIntegratedBindingsCount < mIntegrationBindingsLimit) { |
| 125 |
bindingData.setIntegrated(); |
| 126 |
if (integationCounter) { |
| 127 |
(*integationCounter) += 1; |
| 128 |
} |
| 129 |
++mIntegratedBindingsCount; |
| 130 |
bindingData.setNext(mIntegratedDataLinker); |
| 131 |
mIntegratedDataLinker = &bindingData; |
| 132 |
return true; |
| 133 |
} else { |
| 134 |
++mRemainingIntegrationRequirementCount; |
| 135 |
return false; |
| 136 |
} |
| 137 |
} else { |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} else { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::resetIntegrations() { |
| 148 |
for (CAnsweringSplitPropagationTestingVariableBindingData* integratedDataLinkerIt = mIntegratedDataLinker; integratedDataLinkerIt; integratedDataLinkerIt = integratedDataLinkerIt->getNext()) { |
| 149 |
integratedDataLinkerIt->resetIntegration(); |
| 150 |
} |
| 151 |
mIntegratedBindingsCount = 0; |
| 152 |
mDisabledBindingsCount = 0; |
| 153 |
mIntegratedDataLinker = nullptr; |
| 154 |
mRemainingIntegrationRequirementCount = 0; |
| 155 |
return this; |
| 156 |
} |
| 157 |
|
| 158 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::disableIntegrations() { |
| 159 |
for (CAnsweringSplitPropagationTestingVariableBindingData* integratedDataLinkerIt = mIntegratedDataLinker; integratedDataLinkerIt; integratedDataLinkerIt = integratedDataLinkerIt->getNext()) { |
| 160 |
if (integratedDataLinkerIt->isDisabled()) { |
| 161 |
break; |
| 162 |
} |
| 163 |
integratedDataLinkerIt->disableIntegration(); |
| 164 |
++mDisabledBindingsCount; |
| 165 |
} |
| 166 |
mIntegratedBindingsCount = 0; |
| 167 |
mIntegratedDataLinker = nullptr; |
| 168 |
mRemainingIntegrationRequirementCount = 0; |
| 169 |
return this; |
| 170 |
} |
| 171 |
|
| 172 |
cint64 CAnsweringSplitPropagationTestingVariableData::getVariableDataIntegrationId() { |
| 173 |
return mVariableDataIntegrationId; |
| 174 |
} |
| 175 |
|
| 176 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::setVariableDataIntegrationId(cint64 id) { |
| 177 |
mVariableDataIntegrationId = id; |
| 178 |
return this; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
bool CAnsweringSplitPropagationTestingVariableData::hasVariableDataIntegrationId() { |
| 183 |
return mVariableDataIntegrationId >= 0; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestingVariableData::incVariableBindingsRemainingIntegrationRequirementCount(cint64 count) { |
| 188 |
mRemainingIntegrationRequirementCount += count; |
| 189 |
return this; |
| 190 |
} |
| 191 |
|
| 192 |
cint64 CAnsweringSplitPropagationTestingVariableData::getVariableBindingsIntegrationCount() { |
| 193 |
return mIntegratedBindingsCount; |
| 194 |
} |
| 195 |
|
| 196 |
cint64 CAnsweringSplitPropagationTestingVariableData::getVariableBindingsRemainingIntegrationRequirementCount() { |
| 197 |
return mRemainingIntegrationRequirementCount; |
| 198 |
} |
| 199 |
|
| 200 |
cint64 CAnsweringSplitPropagationTestingVariableData::getVariableBindingsIntegrationLimit() { |
| 201 |
return mIntegrationBindingsLimit; |
| 202 |
} |
| 203 |
|
| 204 |
bool CAnsweringSplitPropagationTestingVariableData::hasVariableBiningsIntegrationLimitReached() { |
| 205 |
return mIntegrationBindingsLimit >= 0 && mIntegratedBindingsCount >= mIntegrationBindingsLimit; |
| 206 |
} |
| 207 |
|
| 208 |
cint64 CAnsweringSplitPropagationTestingVariableData::getDisabledVariableBindingsCount() { |
| 209 |
return mDisabledBindingsCount; |
| 210 |
} |
| 211 |
|
| 212 |
bool CAnsweringSplitPropagationTestingVariableData::hasAllVariableBindingsDisabled() { |
| 213 |
if (mDisabledBindingsCount >= mBindingIndiDataHash.size()) { |
| 214 |
return true; |
| 215 |
} |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
cint64 CAnsweringSplitPropagationTestingVariableData::getLastVariableBindingsIntegrationCount(bool updateLast) { |
| 220 |
cint64 tmpBindingsCount = mLastIntegratedBindingsCount; |
| 221 |
if (updateLast) { |
| 222 |
mLastIntegratedBindingsCount = mIntegratedBindingsCount; |
| 223 |
} |
| 224 |
return tmpBindingsCount; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
}; // end namespace Answerer |
| 229 |
|
| 230 |
}; // end namespace Reasoner |
| 231 |
|
| 232 |
}; // end namespace Konclude |