| 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 "CAbstractVariableMappingsCompositionPropagationComputator.h" |
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Reasoner { |
| 26 |
|
| 27 |
namespace Answerer { |
| 28 |
|
| 29 |
namespace Composition { |
| 30 |
|
| 31 |
CAbstractVariableMappingsCompositionPropagationComputator::CAbstractVariableMappingsCompositionPropagationComputator() : CLogIdentifier("::Konclude::Reasoner::Kernel::Answerer", this) { |
| 32 |
mComputerName = "Propagation"; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
CAbstractVariableMappingsCompositionComputator* CAbstractVariableMappingsCompositionPropagationComputator::configureComputator(COptimizedComplexExpressionOntologyAnsweringItem* ontoAnsweringItem, CAbstractVariableMappingsCompositionItemRequirementProcessor* reqProcessor, CAnswererContext* answererContext) { |
| 38 |
CAbstractVariableMappingsCompositionComputator::configureComputator(ontoAnsweringItem, reqProcessor, answererContext); |
| 39 |
|
| 40 |
mConfExtendedLogging = CConfigDataReader::readConfigBoolean(ontoAnsweringItem->getCalculationConfiguration(), "Konclude.Answering.ExtendedLogging", false); |
| 41 |
|
| 42 |
mConfMappingsComputationUnlimitedInterpretationSize = Q_UINT64_C(100000000000000); |
| 43 |
mConfMappingsComputationUnlimitedInterpretationSize = CConfigDataReader::readConfigInteger(ontoAnsweringItem->getCalculationConfiguration(), "Konclude.Answering.MappingsComputationUnlimitedInterpretationSize", Q_UINT64_C(100000000000000)); |
| 44 |
|
| 45 |
mRoleFillerRealizationSorting = CRealizationIndividualSorting(CRealizationIndividualSorting::SORT_TYPE_NOTHING, CRealizationIndividualSorting::SORT_ORDER_NOTHING, CRealizationIndividualSorting::SORT_GROUP_SAME_UNIQUE); |
| 46 |
return this; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
CAbstractVariableMappingsCompositionPropagationComputator* CAbstractVariableMappingsCompositionPropagationComputator::computeVariableMappingsComposition(COptimizedComplexVariableCompositionItem* compVarItem, COptimizedComplexBuildingVariableCompositionsItem* buildVarItem, CAnswererContext* answererContext, bool& processing) { |
| 51 |
COptimizedComplexBuildingIndividualVariableCompositionsItem* buildingVarItem = (COptimizedComplexBuildingIndividualVariableCompositionsItem*)buildVarItem; |
| 52 |
COptimizedComplexVariableRolePropagationAbstractItem* rolePropItem = (COptimizedComplexVariableRolePropagationAbstractItem*)compVarItem; |
| 53 |
COptimizedComplexVariableCompositionItem* baseVarItem = rolePropItem->getBaseItem(); |
| 54 |
CRole* role = rolePropItem->getRole(); |
| 55 |
bool inversed = rolePropItem->getInversed(); |
| 56 |
COptimizedComplexVariableIndividualMappings* baseVariableMapping = baseVarItem->getVariableMapping(); |
| 57 |
COptimizedComplexVariableCompositionItemDependence* baseItemDep = rolePropItem->getBaseItemDependence(); |
| 58 |
COptimizedComplexVariableIndividualMappings* variableMapping = rolePropItem->getVariableMapping(); |
| 59 |
|
| 60 |
|
| 61 |
bool mappingProcessed = computeVariableMappingsPropagationComposition(rolePropItem, buildingVarItem, answererContext, processing); |
| 62 |
|
| 63 |
|
| 64 |
QList<COptimizedComplexVariableRolePropagationProcessingRealizationIteratorDataBase*>* waitingProcessedRealizationIteratorDataList = rolePropItem->getWaitingProcessedRealizationIteratorDataList(); |
| 65 |
QList<COntologyProcessingRequirement*>* schedulingReqList = rolePropItem->geSchedulingRealizationRequirementIteratorDataList(); |
| 66 |
QList<COptimizedComplexVariableRolePropagationProcessingRealizationIteratorDataBase*>* schedulingRealizationIteratorDataList = rolePropItem->getSchedulingRealizationIteratorDataList(); |
| 67 |
|
| 68 |
|
| 69 |
bool dependentItemRescheduled = false; |
| 70 |
bool realizationScheduled = false; |
| 71 |
|
| 72 |
|
| 73 |
if (rolePropItem->requiresMoreVariableMappingsComputation()) { |
| 74 |
// determine for how many instances role realization has to be scheduled |
| 75 |
cint64 requiredMappingCount = rolePropItem->getVariableMappingsComputationRequirement(); |
| 76 |
double requiredProcessingCount = 0; |
| 77 |
|
| 78 |
// estimated realization count based on expected fillers per propagation item |
| 79 |
double exprectedFillerPerPropagationItemCount = rolePropItem->getExpectedFillerPerPropagationItemCount(); |
| 80 |
|
| 81 |
|
| 82 |
cint64 propagatedInstanceItemCount = rolePropItem->getPropagatedInstanceItemCount(); |
| 83 |
double propagatedInstanceItemCreatedVariableMappingsFactor = 0.1; |
| 84 |
if (propagatedInstanceItemCount > 0) { |
| 85 |
double varMapCount = variableMapping->getBindingCount(); |
| 86 |
if (variableMapping->getBindingCount() <= 0) { |
| 87 |
varMapCount = qMax(0.01, 10. / ((double)propagatedInstanceItemCount + 10)); |
| 88 |
} |
| 89 |
propagatedInstanceItemCreatedVariableMappingsFactor = (double)propagatedInstanceItemCount / varMapCount; |
| 90 |
} |
| 91 |
|
| 92 |
requiredProcessingCount = qMax(requiredMappingCount * propagatedInstanceItemCreatedVariableMappingsFactor + 1., propagatedInstanceItemCount + 1.); |
| 93 |
|
| 94 |
requiredProcessingCount -= rolePropItem->getScheduledRealizationCount(); |
| 95 |
|
| 96 |
|
| 97 |
requiredProcessingCount = requiredProcessingCount * rolePropItem->repeatedRequirementInsufficientDependencyComputationIncreaseFactor(); |
| 98 |
|
| 99 |
cint64 requiredProcessingCountInteger = requiredProcessingCount; |
| 100 |
requiredProcessingCountInteger = qMax((cint64)-1, requiredProcessingCountInteger); |
| 101 |
if (requiredMappingCount == -1 || mConfMappingsComputationUnlimitedInterpretationSize >= 0 && requiredProcessingCount >= mConfMappingsComputationUnlimitedInterpretationSize) { |
| 102 |
requiredProcessingCountInteger = -1; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if ((requiredProcessingCountInteger > 0 || requiredProcessingCountInteger == -1) && !schedulingReqList->isEmpty()) { |
| 107 |
|
| 108 |
QList<COntologyProcessingRequirement*> realList; |
| 109 |
while ((requiredProcessingCountInteger > 0 || requiredProcessingCountInteger == -1) && !schedulingReqList->isEmpty()) { |
| 110 |
COntologyProcessingRequirement* realReq = schedulingReqList->takeFirst(); |
| 111 |
if (requiredProcessingCountInteger != -1) { |
| 112 |
requiredProcessingCountInteger--; |
| 113 |
} |
| 114 |
realList.append(realReq); |
| 115 |
COptimizedComplexVariableRolePropagationProcessingRealizationIteratorDataBase* itData = schedulingRealizationIteratorDataList->takeFirst(); |
| 116 |
waitingProcessedRealizationIteratorDataList->append(itData); |
| 117 |
} |
| 118 |
|
| 119 |
if (!realList.isEmpty()) { |
| 120 |
realizationScheduled = true; |
| 121 |
rolePropItem->incScheduledRealizationCount(realList.size()); |
| 122 |
processRequirements(realList, compVarItem, buildingVarItem, answererContext); |
| 123 |
processing = true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if ((requiredProcessingCountInteger > 0 || requiredProcessingCountInteger == -1) && !baseVarItem->isVariableMappingsComputed() && !baseItemDep->hasMappingsCountComputationRequested(requiredProcessingCountInteger)) { |
| 128 |
baseItemDep->setLastRequestedMappingsComputationCount(requiredProcessingCountInteger); |
| 129 |
dependentItemRescheduled = rescheduleVariableCompositionItemComputation(buildingVarItem, compVarItem, baseVarItem, requiredProcessingCountInteger); |
| 130 |
|
| 131 |
if (dependentItemRescheduled && mConfExtendedLogging) { |
| 132 |
LOG(INFO, getLogDomain(), logTr("Computation step %1 (attempt %2): Requesting %3 mappings from step %4.").arg(compVarItem->getComputationStepId()).arg(compVarItem->getComputationAttempt(false)).arg(requiredProcessingCountInteger).arg(baseVarItem->getComputationStepId()), this); |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
if (variableMapping->getBindingCount() > 0) { |
| 141 |
cint64 propagatedInstanceItemCount = rolePropItem->getPropagatedInstanceItemCount(); |
| 142 |
double propagatedInstanceItemCreatedVariableMappingsFactor = (double)propagatedInstanceItemCount / variableMapping->getBindingCount(); |
| 143 |
rolePropItem->setVariableMappingsExpectedCount(baseVarItem->getVariableMappingsExpectedCount() / propagatedInstanceItemCreatedVariableMappingsFactor); |
| 144 |
} else { |
| 145 |
rolePropItem->setVariableMappingsExpectedCount(0); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
bool computationRunning = realizationScheduled || dependentItemRescheduled || rolePropItem->getScheduledRealizationCount() > 0 || !schedulingReqList->isEmpty(); |
| 150 |
if (!rolePropItem->requiresMoreVariableMappingsComputation() || !computationRunning && rolePropItem->getDependencyUpdatingCount() <= 0) { |
| 151 |
bool computationFinished = !computationRunning && baseItemDep->isProcessingFinished(); |
| 152 |
finishVariableCompositionItemComputation(buildingVarItem, compVarItem, computationFinished); |
| 153 |
|
| 154 |
if (mConfExtendedLogging) { |
| 155 |
QString roleName = "?"; |
| 156 |
if (role->getPropertyNameLinker()) { |
| 157 |
roleName = CIRIName::getRecentIRIName(role->getPropertyNameLinker()); |
| 158 |
if (inversed) { |
| 159 |
roleName = roleName + " inversed"; |
| 160 |
} |
| 161 |
} else if (role->getInverseRole() && role->getInverseRole()->getPropertyNameLinker()) { |
| 162 |
roleName = CIRIName::getRecentIRIName(role->getInverseRole()->getPropertyNameLinker()); |
| 163 |
if (!inversed) { |
| 164 |
roleName = roleName + " inversed"; |
| 165 |
} |
| 166 |
} |
| 167 |
LOG(INFO, getLogDomain(), logTr("Computation step %4 (attempt %5): %6 of %1 mappings over role %2 resulted in %3 mappings.").arg(baseVariableMapping->getBindingCount()).arg(roleName).arg(compVarItem->getVariableMapping()->getBindingCount()).arg(compVarItem->getComputationStepId()).arg(compVarItem->getComputationAttempt()).arg(mComputerName), this); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
configureDependentComputationIncreaseFactor(dependentItemRescheduled, mappingProcessed, compVarItem); |
| 172 |
|
| 173 |
|
| 174 |
//if (CIRIName::getRecentIRIName(rolePropItem->getRole()->getNameLinker()) == "http://ontology.dumontierlab.com/isPartOf" && rolePropItem->isVariableMappingsComputed()) { |
| 175 |
// debugCheckVariableMappingFromFile(buildingVarItem, compVarItem); |
| 176 |
|
| 177 |
// debugWriteVariableMappingToFile(buildingVarItem, compVarItem); |
| 178 |
//} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
//if (variableMapping->count() == 0) { |
| 183 |
// bool debug = true; |
| 184 |
//} |
| 185 |
//debugCheckVariableMappingContainsSolution(buildingVarItem, compVarItem); |
| 186 |
|
| 187 |
return this; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
}; // end namespace Composition |
| 197 |
|
| 198 |
}; // end namespace Answerer |
| 199 |
|
| 200 |
}; // end namespace Reasoner |
| 201 |
|
| 202 |
}; // end namespace Konclude |