| 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 "CClassAssertionQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
#include "Reasoner/Kernel/Task/CConsistenceTaskData.h" |
| 25 |
#include "Reasoner/Kernel/Task/CSatisfiableCalculationTask.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Reasoner { |
| 31 |
|
| 32 |
using namespace Kernel::Task; |
| 33 |
|
| 34 |
namespace Query { |
| 35 |
|
| 36 |
|
| 37 |
CClassAssertionQuery::CClassAssertionQuery(CConcreteOntology* ontology, CConfigurationBase* configuration, const QSet<CIndividual*>& individualSet, const QString& queryName) |
| 38 |
: CConsistencePremisingQuery(ontology,configuration) { |
| 39 |
mQueryName = queryName; |
| 40 |
if (mQueryName.isEmpty()) { |
| 41 |
mQueryName = QString("Get Concept Assertions"); |
| 42 |
} |
| 43 |
mResult = nullptr; |
| 44 |
|
| 45 |
mUseAbbreviatedIRIs = CConfigDataReader::readConfigBoolean(configuration,"Konclude.OWLlink.AbbreviatedIRIs",false); |
| 46 |
|
| 47 |
mConsistencyCalcError = false; |
| 48 |
mQueryConstructError = false; |
| 49 |
mDeterministicAssertions = false; |
| 50 |
mNondeterministicAssertions = false; |
| 51 |
|
| 52 |
mIndividualSet = individualSet; |
| 53 |
|
| 54 |
mCalcConfig = configuration; |
| 55 |
} |
| 56 |
|
| 57 |
CClassAssertionQuery::~CClassAssertionQuery() { |
| 58 |
if (mResult) { |
| 59 |
delete mResult; |
| 60 |
} |
| 61 |
delete mCalcConfig; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
CQueryResult* CClassAssertionQuery::getQueryResult() { |
| 66 |
return mResult; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
CQueryResult *CClassAssertionQuery::constructResult(CConsistence* consistence) { |
| 71 |
return constructAssertionResult(consistence); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
bool CClassAssertionQuery::addIndividualResultData(cint64 indiID, CIndividualProcessNodeVector* indiNodeVec) { |
| 76 |
CBOXSET<CConcept*>* eqNonCanSet = ontology->getTBox()->getEquivalentConceptNonCandidateSet(false); |
| 77 |
bool indiConAssertionAdded = false; |
| 78 |
CIndividualProcessNode* baseIndiNode = indiNodeVec->getData(indiID); |
| 79 |
CIndividualProcessNode* indiNode = baseIndiNode; |
| 80 |
if (indiNode && indiNode->getNominalIndividual()) { |
| 81 |
cint64 mergedIndiID = indiID; |
| 82 |
bool nonDeterministicMerged = false; |
| 83 |
while (mergedIndiID != indiNode->getMergedIntoIndividualNodeID()) { |
| 84 |
mergedIndiID = indiNode->getMergedIntoIndividualNodeID(); |
| 85 |
CDependencyTrackPoint* mergedDepTrackPoint = indiNode->getMergedDependencyTrackPoint(); |
| 86 |
if (!mergedDepTrackPoint || mergedDepTrackPoint->getBranchingTag() > 0) { |
| 87 |
nonDeterministicMerged = true; |
| 88 |
} |
| 89 |
indiNode = indiNodeVec->getData(mergedIndiID); |
| 90 |
} |
| 91 |
|
| 92 |
CReapplyConceptLabelSet* conLabelSet = indiNode->getReapplyConceptLabelSet(false); |
| 93 |
if (conLabelSet) { |
| 94 |
bool allNonDeterministic = nonDeterministicMerged; |
| 95 |
QString indiName; |
| 96 |
if (mUseAbbreviatedIRIs) { |
| 97 |
indiName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(baseIndiNode->getNominalIndividual()->getIndividualNameLinker()); |
| 98 |
} else { |
| 99 |
indiName = CIRIName::getRecentIRIName(baseIndiNode->getNominalIndividual()->getIndividualNameLinker()); |
| 100 |
} |
| 101 |
|
| 102 |
for (CConceptDescriptor* conDesIt = conLabelSet->getAddingSortedConceptDescriptionLinker(); conDesIt; conDesIt = conDesIt->getNext()) { |
| 103 |
CDependencyTrackPoint* depTrackPoint = conDesIt->getDependencyTrackPoint(); |
| 104 |
bool conNegation = conDesIt->isNegated(); |
| 105 |
CConcept* concept = conDesIt->getConcept(); |
| 106 |
if (mDeterministicAssertions && depTrackPoint && depTrackPoint->getBranchingTag() <= 0 && !allNonDeterministic || mNondeterministicAssertions && (allNonDeterministic || !depTrackPoint || depTrackPoint->getBranchingTag() > 0)) { |
| 107 |
if (!conNegation && concept && concept->hasClassName()) { |
| 108 |
|
| 109 |
QString conceptName; |
| 110 |
if (mUseAbbreviatedIRIs) { |
| 111 |
conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 112 |
} else { |
| 113 |
conceptName = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 114 |
} |
| 115 |
mResult->addIndividualConceptAssertion(indiName,conceptName); |
| 116 |
indiConAssertionAdded = true; |
| 117 |
|
| 118 |
} |
| 119 |
} |
| 120 |
if (mNondeterministicAssertions && concept->getOperatorCode() == CCEQCAND) { |
| 121 |
CConcept* eqConcept = concept->getOperandList()->getData(); |
| 122 |
bool negation = false; |
| 123 |
if (!conLabelSet->containsConcept(eqConcept,&negation)) { |
| 124 |
|
| 125 |
QString conceptName; |
| 126 |
if (mUseAbbreviatedIRIs) { |
| 127 |
conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(eqConcept->getClassNameLinker()); |
| 128 |
} else { |
| 129 |
conceptName = CIRIName::getRecentIRIName(eqConcept->getClassNameLinker()); |
| 130 |
} |
| 131 |
mResult->addIndividualConceptAssertion(indiName,conceptName); |
| 132 |
indiConAssertionAdded = true; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (eqNonCanSet) { |
| 138 |
for (CBOXSET<CConcept*>::const_iterator it = eqNonCanSet->constBegin(), itEnd = eqNonCanSet->constEnd(); it != itEnd; ++it) { |
| 139 |
CConcept* eqConcept(*it); |
| 140 |
bool negation = false; |
| 141 |
if (!conLabelSet->containsConcept(eqConcept,&negation)) { |
| 142 |
|
| 143 |
QString conceptName; |
| 144 |
if (mUseAbbreviatedIRIs) { |
| 145 |
conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(eqConcept->getClassNameLinker()); |
| 146 |
} else { |
| 147 |
conceptName = CIRIName::getRecentIRIName(eqConcept->getClassNameLinker()); |
| 148 |
} |
| 149 |
mResult->addIndividualConceptAssertion(indiName,conceptName); |
| 150 |
indiConAssertionAdded = true; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return indiConAssertionAdded; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
CIndividualClassAssertionsResult *CClassAssertionQuery::constructAssertionResult(CConsistence* consistence) { |
| 161 |
if (mResult) { |
| 162 |
delete mResult; |
| 163 |
mResult = 0; |
| 164 |
} |
| 165 |
if (consistence) { |
| 166 |
mResult = new CIndividualClassAssertionsResult(); |
| 167 |
CConsistenceTaskData* consTaskData = (CConsistenceTaskData*)consistence->getConsistenceModelData(); |
| 168 |
if (consTaskData) { |
| 169 |
CSatisfiableCalculationTask* detTask = consTaskData->getDeterministicSatisfiableTask(); |
| 170 |
CSatisfiableCalculationTask* nonDetTask = consTaskData->getCompletionGraphCachedSatisfiableTask(); |
| 171 |
if (detTask != nonDetTask) { |
| 172 |
CProcessingDataBox* dataBox = nonDetTask->getProcessingDataBox(); |
| 173 |
if (dataBox) { |
| 174 |
CIndividualProcessNodeVector* indiNodeVec = dataBox->getIndividualProcessNodeVector(); |
| 175 |
cint64 indiNodeMinIdx = indiNodeVec->getItemMinIndex(); |
| 176 |
if (mIndividualSet.isEmpty()) { |
| 177 |
for (cint64 indiID = indiNodeMinIdx; indiID <= 0; ++indiID) { |
| 178 |
addIndividualResultData(indiID,indiNodeVec); |
| 179 |
} |
| 180 |
} else { |
| 181 |
for (QSet<CIndividual*>::const_iterator it = mIndividualSet.constBegin(), itEnd = mIndividualSet.constEnd(); it != itEnd; ++it) { |
| 182 |
CIndividual* individual(*it); |
| 183 |
cint64 indiID = individual->getIndividualID(); |
| 184 |
addIndividualResultData(-indiID,indiNodeVec); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
return mResult; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
QString CClassAssertionQuery::getQueryName() { |
| 196 |
return mQueryName; |
| 197 |
} |
| 198 |
|
| 199 |
QString CClassAssertionQuery::getQueryString() { |
| 200 |
return mQueryString; |
| 201 |
} |
| 202 |
|
| 203 |
bool CClassAssertionQuery::hasAnswer() { |
| 204 |
return mResult != 0; |
| 205 |
} |
| 206 |
|
| 207 |
QString CClassAssertionQuery::getAnswerString() { |
| 208 |
if (!mResult) { |
| 209 |
return QString("Unknown Concept Assertions"); |
| 210 |
} else { |
| 211 |
return mResult->getQueryResultString(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
bool CClassAssertionQuery::hasError() { |
| 216 |
return mConsistencyCalcError || mQueryConstructError || CQuery::hasError(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
}; // end namespace Query |
| 221 |
|
| 222 |
}; // end namespace Reasoner |
| 223 |
|
| 224 |
}; // end namespace Konclude |