| 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 "CMappingUpdaterPreProcess.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Preprocess { |
| 29 |
|
| 30 |
|
| 31 |
CMappingUpdaterPreProcess::CMappingUpdaterPreProcess() { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
CMappingUpdaterPreProcess::~CMappingUpdaterPreProcess() { |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
CConcreteOntology *CMappingUpdaterPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) { |
| 40 |
if (ontology) { |
| 41 |
tbox = ontology->getDataBoxes()->getTBox(); |
| 42 |
abox = ontology->getDataBoxes()->getABox(); |
| 43 |
rbox = ontology->getDataBoxes()->getRBox(); |
| 44 |
mBox = ontology->getDataBoxes()->getMBox(); |
| 45 |
|
| 46 |
|
| 47 |
mBuildConstructFlags = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getBuildConstructFlags(); |
| 48 |
|
| 49 |
mRoleVec = rbox->getRoleVector(); |
| 50 |
mConceptVec = tbox->getConceptVector(); |
| 51 |
mIndiVec = abox->getIndividualVector(); |
| 52 |
|
| 53 |
mRoleObjPropTermHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getRoleObjectPropertyTermMappingHash(); |
| 54 |
mObjPropTermRoleHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getObjectPropertyTermRoleMappingHash(); |
| 55 |
|
| 56 |
mRoleDataPropTermHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getRoleDataPropertyTermMappingHash(); |
| 57 |
mDataPropTermRoleHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getDataPropertyTermRoleMappingHash(); |
| 58 |
|
| 59 |
mClassTermConceptHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getClassTermConceptMappingHash(); |
| 60 |
mConceptClassTermHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getConceptClassTermMappingHash(); |
| 61 |
|
| 62 |
mIndiTermIndiHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getIndividulTermIndiMappingHash(); |
| 63 |
mIndiIndiTermHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getIndiIndividulTermMappingHash(); |
| 64 |
|
| 65 |
mDeclarationAxiomSet = ontology->getBuildData()->getDeclarationAxiomSet(); |
| 66 |
|
| 67 |
mClassTermClassAxiomHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getClassTermExpressionClassAxiomExpressionHash(); |
| 68 |
mObjPropTermObjPropAxiomHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getObjectPropertyTermObjectPropertyAxiomHash(); |
| 69 |
|
| 70 |
mNameConceptHash = ontology->getStringMapping()->getClassNameConceptMappingHash(); |
| 71 |
mNameRoleHash = ontology->getStringMapping()->getPropertyNameRoleMappingHash(); |
| 72 |
mNameIndiHash = ontology->getStringMapping()->getIndividualNameIndividualMappingHash(); |
| 73 |
|
| 74 |
mActiveEntityVector = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getActiveEntityVector(); |
| 75 |
|
| 76 |
qint64 rolesStartUpdateInd = mRoleVec->getReferencedItemCount(); |
| 77 |
qint64 rolesCounts = mRoleVec->getItemCount(); |
| 78 |
for (qint64 i = rolesStartUpdateInd; i < rolesCounts; ++i) { |
| 79 |
CRole *role = mRoleVec->getLocalData(i); |
| 80 |
if (role) { |
| 81 |
if (role->hasPropertyName()) { |
| 82 |
mNameRoleHash->insert(CIRIName::getRecentIRIName(role->getPropertyNameLinker()),role); |
| 83 |
} |
| 84 |
CObjectPropertyTermExpression* objPropTerm = mRoleObjPropTermHash->value(role,nullptr); |
| 85 |
if (objPropTerm) { |
| 86 |
CRole* prevRole = mRoleVec->getReferenceData(i); |
| 87 |
if (prevRole) { |
| 88 |
objPropTerm = mRoleObjPropTermHash->value(prevRole,nullptr); |
| 89 |
} |
| 90 |
if (objPropTerm) { |
| 91 |
mRoleObjPropTermHash->insert(role,objPropTerm); |
| 92 |
mObjPropTermRoleHash->insert(objPropTerm,role); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
qint64 conStartUpdateInd = mConceptVec->getReferencedItemCount(); |
| 99 |
qint64 conCounts = mConceptVec->getItemCount(); |
| 100 |
for (qint64 i = 0; i < conCounts; ++i) { |
| 101 |
CConcept *concept = mConceptVec->getLocalData(i); |
| 102 |
if (concept) { |
| 103 |
if (concept->hasClassName()) { |
| 104 |
mNameConceptHash->insert(CIRIName::getRecentIRIName(concept->getClassNameLinker()),concept); |
| 105 |
} |
| 106 |
if (i < conStartUpdateInd) { |
| 107 |
CClassTermExpression* classTerm = mConceptClassTermHash->value(concept,nullptr); |
| 108 |
if (classTerm) { |
| 109 |
CConcept* prevConcept = mConceptVec->getReferenceData(i); |
| 110 |
if (prevConcept) { |
| 111 |
classTerm = mConceptClassTermHash->value(prevConcept,nullptr); |
| 112 |
} |
| 113 |
if (classTerm) { |
| 114 |
mConceptClassTermHash->insert(concept,classTerm); |
| 115 |
mClassTermConceptHash->insert(classTerm,concept); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
qint64 indiStartUpdateInd = mIndiVec->getReferencedItemCount(); |
| 125 |
qint64 indiCounts = mIndiVec->getItemCount(); |
| 126 |
for (qint64 i = 0; i < indiCounts; ++i) { |
| 127 |
CIndividual *indi = mIndiVec->getLocalData(i); |
| 128 |
if (indi) { |
| 129 |
if (indi->hasIndividualName()) { |
| 130 |
mNameIndiHash->insert(CIRIName::getRecentIRIName(indi->getIndividualNameLinker()),indi); |
| 131 |
} |
| 132 |
if (i < indiStartUpdateInd) { |
| 133 |
CIndividualTermExpression* indiTerm = mIndiIndiTermHash->value(indi,nullptr); |
| 134 |
if (indiTerm) { |
| 135 |
CIndividual* prevIndi = mIndiVec->getReferenceData(i); |
| 136 |
if (prevIndi) { |
| 137 |
indiTerm = mIndiIndiTermHash->value(prevIndi,nullptr); |
| 138 |
} |
| 139 |
if (indiTerm) { |
| 140 |
mIndiIndiTermHash->insert(indi,indiTerm); |
| 141 |
mIndiTermIndiHash->insert(indiTerm,indi); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
collectActiveOntologyItemsFromActiveEntities(); |
| 149 |
|
| 150 |
return ontology; |
| 151 |
} |
| 152 |
|
| 153 |
bool CMappingUpdaterPreProcess::collectActiveOntologyItemsFromActiveEntities() { |
| 154 |
CBOXSET<CConcept*>* activeClassConceptSet = tbox->getActiveClassConceptSet(); |
| 155 |
activeClassConceptSet->clear(); |
| 156 |
|
| 157 |
CBOXSET<CIndividual*>* activeIndividualSet = abox->getActiveIndividualSet(); |
| 158 |
activeIndividualSet->clear(); |
| 159 |
|
| 160 |
CBOXSET<CRole*>* activeRoleSet = rbox->getActivePropertyRoleSet(); |
| 161 |
activeRoleSet->clear(); |
| 162 |
|
| 163 |
CActiveEntityIterator activeEntityIterator(mActiveEntityVector->getActiveEntityIterator()); |
| 164 |
while (activeEntityIterator.hasNextActiveEntity()) { |
| 165 |
CExpressionEntity* expressionEntity = activeEntityIterator.getNextActiveEntity(true); |
| 166 |
if (expressionEntity) { |
| 167 |
CClassExpression* classExpression = dynamic_cast<CClassExpression*>(expressionEntity); |
| 168 |
if (classExpression) { |
| 169 |
CConcept* concept = mClassTermConceptHash->value(classExpression); |
| 170 |
if (concept) { |
| 171 |
activeClassConceptSet->insert(concept); |
| 172 |
} |
| 173 |
} else { |
| 174 |
CNamedIndividualExpression* namedIndiExpression = dynamic_cast<CNamedIndividualExpression*>(expressionEntity); |
| 175 |
if (namedIndiExpression) { |
| 176 |
CIndividual* individual = mIndiTermIndiHash->value(namedIndiExpression); |
| 177 |
if (individual) { |
| 178 |
activeIndividualSet->insert(individual); |
| 179 |
} |
| 180 |
} else { |
| 181 |
CAnonymousIndividualExpression* anoIndiExpression = dynamic_cast<CAnonymousIndividualExpression*>(expressionEntity); |
| 182 |
if (anoIndiExpression) { |
| 183 |
CIndividual* individual = mIndiTermIndiHash->value(anoIndiExpression); |
| 184 |
if (individual) { |
| 185 |
activeIndividualSet->insert(individual); |
| 186 |
} |
| 187 |
} else { |
| 188 |
CObjectPropertyExpression* objPropertyExpression = dynamic_cast<CObjectPropertyExpression*>(expressionEntity); |
| 189 |
if (objPropertyExpression) { |
| 190 |
CRole* role = mObjPropTermRoleHash->value(objPropertyExpression); |
| 191 |
if (role) { |
| 192 |
activeRoleSet->insert(role); |
| 193 |
} |
| 194 |
} else { |
| 195 |
CDataPropertyExpression* dataPropertyExpression = dynamic_cast<CDataPropertyExpression*>(expressionEntity); |
| 196 |
if (dataPropertyExpression) { |
| 197 |
CRole* role = mDataPropTermRoleHash->value(dataPropertyExpression); |
| 198 |
if (role) { |
| 199 |
activeRoleSet->insert(role); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
LOG(INFO,"::Konclude::Reasoner::Preprocess::MappingUpdater",logTr("Found %1 active classes, %2 active object/data properties, %3 active individuals.").arg(activeClassConceptSet->count()).arg(activeRoleSet->count()).arg(activeIndividualSet->count()),this); |
| 209 |
|
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
bool CMappingUpdaterPreProcess::collectActiveOntologyItems() { |
| 214 |
|
| 215 |
mUpdateConceptSet.insert(tbox->getTopConcept()); |
| 216 |
mUpdateConceptList.append(tbox->getTopConcept()); |
| 217 |
mUpdateConceptSet.insert(tbox->getBottomConcept()); |
| 218 |
mUpdateConceptList.append(tbox->getBottomConcept()); |
| 219 |
|
| 220 |
mUpdateRoleSet.insert(rbox->getTopObjectRole()); |
| 221 |
mUpdateRoleList.append(rbox->getTopObjectRole()); |
| 222 |
mUpdateRoleSet.insert(rbox->getBottomObjectRole()); |
| 223 |
mUpdateRoleList.append(rbox->getBottomObjectRole()); |
| 224 |
|
| 225 |
|
| 226 |
if (!mBuildConstructFlags->isRetractionUsed()) { |
| 227 |
qint64 conCounts = mConceptVec->getItemCount(); |
| 228 |
for (qint64 i = 2; i < conCounts; ++i) { |
| 229 |
CConcept *concept = mConceptVec->getLocalData(i); |
| 230 |
if (concept) { |
| 231 |
if (concept->hasClassName()) { |
| 232 |
if (!mUpdateConceptSet.contains(concept)) { |
| 233 |
mUpdateConceptSet.insert(concept); |
| 234 |
mUpdateConceptList.append(concept); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} else { |
| 240 |
FOREACHIT (CDeclarationAxiomExpression* declAxiom, *mDeclarationAxiomSet) { |
| 241 |
CExpressionEntity* entity = declAxiom->getEntity(); |
| 242 |
CClassExpression* classExp = dynamic_cast<CClassExpression*>(entity); |
| 243 |
if (classExp) { |
| 244 |
CConcept* concept = mClassTermConceptHash->value(classExp,nullptr); |
| 245 |
if (concept) { |
| 246 |
if (!mUpdateConceptSet.contains(concept)) { |
| 247 |
mUpdateConceptSet.insert(concept); |
| 248 |
mUpdateConceptList.append(concept); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
CObjectPropertyExpression* objPropExp = dynamic_cast<CObjectPropertyExpression*>(entity); |
| 253 |
if (objPropExp) { |
| 254 |
CRole* role = mObjPropTermRoleHash->value(objPropExp,nullptr); |
| 255 |
if (role) { |
| 256 |
if (!mUpdateRoleSet.contains(role)) { |
| 257 |
mUpdateRoleSet.insert(role); |
| 258 |
mUpdateRoleList.append(role); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
//CIndividualTermExpression* indiExp = dynamic_cast<CIndividualTermExpression*>(entity); |
| 263 |
//if (indiExp) { |
| 264 |
// CIndividual* individual = mIndiTermIndiHash->value(indiExp); |
| 265 |
// if (individual) { |
| 266 |
// if (!mUpdateIndividualSet.contains(individual)) { |
| 267 |
// mUpdateIndividualSet.insert(individual); |
| 268 |
// mUpdateIndividualList.append(individual); |
| 269 |
// } |
| 270 |
// } |
| 271 |
//} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
// consider all Abox individual, not only those which are referenced by definitions |
| 276 |
// if an individual is removed from the KB, then there are no asserted concepts |
| 277 |
cint64 indiCount = abox->getIndividualCount(); |
| 278 |
CIndividualVector* indiVec = abox->getIndividualVector(false); |
| 279 |
if (indiVec) { |
| 280 |
for (cint64 indiID = 0; indiID < indiCount; ++indiID) { |
| 281 |
CIndividual* individual = indiVec->getData(indiID); |
| 282 |
if (individual) { |
| 283 |
for (CConceptAssertionLinker* conAssIt = individual->getAssertionConceptLinker(); conAssIt; conAssIt = conAssIt->getNext()) { |
| 284 |
CConcept* concept = conAssIt->getData(); |
| 285 |
if (!mUpdateConceptSet.contains(concept)) { |
| 286 |
mUpdateConceptSet.insert(concept); |
| 287 |
mUpdateConceptList.append(concept); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
FOREACHIT (CClassTermExpression* classTermExp, mClassTermClassAxiomHash->keys()) { |
| 295 |
if (mClassTermClassAxiomHash->value(classTermExp) != nullptr) { |
| 296 |
CConcept* concept = mClassTermConceptHash->value(classTermExp,nullptr); |
| 297 |
if (concept) { |
| 298 |
if (!mUpdateConceptSet.contains(concept)) { |
| 299 |
mUpdateConceptSet.insert(concept); |
| 300 |
mUpdateConceptList.append(concept); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
FOREACHIT (CObjectPropertyTermExpression* objPropTermExp, mObjPropTermObjPropAxiomHash->keys()) { |
| 306 |
if (mObjPropTermObjPropAxiomHash->value(objPropTermExp) != nullptr) { |
| 307 |
CRole* role = mObjPropTermRoleHash->value(objPropTermExp,nullptr); |
| 308 |
if (role) { |
| 309 |
if (!mUpdateRoleSet.contains(role)) { |
| 310 |
mUpdateRoleSet.insert(role); |
| 311 |
mUpdateRoleList.append(role); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
while (!mUpdateConceptList.isEmpty() || !mUpdateRoleList.isEmpty() || !mUpdateIndividualList.isEmpty()) { |
| 318 |
while (!mUpdateConceptList.isEmpty()) { |
| 319 |
CConcept* concept = mUpdateConceptList.takeFirst(); |
| 320 |
CSortedNegLinker<CConcept*>* opLinkerIt = concept->getOperandList(); |
| 321 |
while (opLinkerIt) { |
| 322 |
CConcept* opCon = opLinkerIt->getData(); |
| 323 |
if (!mUpdateConceptSet.contains(opCon)) { |
| 324 |
mUpdateConceptSet.insert(opCon); |
| 325 |
mUpdateConceptList.append(opCon); |
| 326 |
} |
| 327 |
opLinkerIt = opLinkerIt->getNext(); |
| 328 |
} |
| 329 |
CRole* role = concept->getRole(); |
| 330 |
if (role && !mUpdateRoleSet.contains(role)) { |
| 331 |
mUpdateRoleSet.insert(role); |
| 332 |
mUpdateRoleList.append(role); |
| 333 |
} |
| 334 |
//CIndividual* individual = concept->getNominalIndividual(); |
| 335 |
//if (individual && !mUpdateIndividualSet.contains(individual)) { |
| 336 |
// mUpdateIndividualSet.insert(individual); |
| 337 |
// mUpdateIndividualList.append(individual); |
| 338 |
//} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
//while (!mUpdateIndividualList.isEmpty()) { |
| 344 |
// CIndividual* individual = mUpdateIndividualList.takeFirst(); |
| 345 |
|
| 346 |
// for (CConceptAssertionLinker* conAssIt = individual->getAssertionConceptLinker(); conAssIt; conAssIt = conAssIt->getNext()) { |
| 347 |
// CConcept* concept = conAssIt->getData(); |
| 348 |
// if (!mUpdateConceptSet.contains(concept)) { |
| 349 |
// mUpdateConceptSet.insert(concept); |
| 350 |
// mUpdateConceptList.append(concept); |
| 351 |
// } |
| 352 |
// } |
| 353 |
// for (CRoleAssertionLinker* roleAssIt = individual->getAssertionRoleLinker(); roleAssIt; roleAssIt = roleAssIt->getNext()) { |
| 354 |
// CRole* role = roleAssIt->getRole(); |
| 355 |
// if (!mUpdateRoleSet.contains(role)) { |
| 356 |
// mUpdateRoleSet.insert(role); |
| 357 |
// mUpdateRoleList.append(role); |
| 358 |
// } |
| 359 |
// CIndividual* individual2 = roleAssIt->getIndividual(); |
| 360 |
// if (!mUpdateIndividualSet.contains(individual2)) { |
| 361 |
// mUpdateIndividualSet.insert(individual2); |
| 362 |
// mUpdateIndividualList.append(individual2); |
| 363 |
// } |
| 364 |
// } |
| 365 |
//} |
| 366 |
|
| 367 |
|
| 368 |
while (!mUpdateRoleList.isEmpty()) { |
| 369 |
CRole* role = mUpdateRoleList.takeFirst(); |
| 370 |
CSortedNegLinker<CRole*>* roleLinkerIt = role->getIndirectSuperRoleList(); |
| 371 |
while (roleLinkerIt) { |
| 372 |
CRole* superRole = roleLinkerIt->getData(); |
| 373 |
if (!mUpdateRoleSet.contains(superRole)) { |
| 374 |
mUpdateRoleSet.insert(superRole); |
| 375 |
mUpdateRoleList.append(superRole); |
| 376 |
} |
| 377 |
roleLinkerIt = roleLinkerIt->getNext(); |
| 378 |
} |
| 379 |
CSortedNegLinker<CRole*>* disRoleLinkerIt = role->getDisjointRoleList(); |
| 380 |
while (disRoleLinkerIt) { |
| 381 |
CRole* disRole = disRoleLinkerIt->getData(); |
| 382 |
if (!mUpdateRoleSet.contains(disRole)) { |
| 383 |
mUpdateRoleSet.insert(disRole); |
| 384 |
mUpdateRoleList.append(disRole); |
| 385 |
} |
| 386 |
disRoleLinkerIt = disRoleLinkerIt->getNext(); |
| 387 |
} |
| 388 |
CSortedNegLinker<CConcept*>* domainConLinkerIt = role->getDomainConceptList(); |
| 389 |
while (domainConLinkerIt) { |
| 390 |
CConcept* opCon = domainConLinkerIt->getData(); |
| 391 |
if (!mUpdateConceptSet.contains(opCon)) { |
| 392 |
mUpdateConceptSet.insert(opCon); |
| 393 |
mUpdateConceptList.append(opCon); |
| 394 |
} |
| 395 |
domainConLinkerIt = domainConLinkerIt->getNext(); |
| 396 |
} |
| 397 |
CSortedNegLinker<CConcept*>* rangeConLinkerIt = role->getRangeConceptList(); |
| 398 |
while (rangeConLinkerIt) { |
| 399 |
CConcept* opCon = rangeConLinkerIt->getData(); |
| 400 |
if (!mUpdateConceptSet.contains(opCon)) { |
| 401 |
mUpdateConceptSet.insert(opCon); |
| 402 |
mUpdateConceptList.append(opCon); |
| 403 |
} |
| 404 |
rangeConLinkerIt = rangeConLinkerIt->getNext(); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
CBOXSET<CConcept*>* activeClassConceptSet = tbox->getActiveClassConceptSet(); |
| 411 |
activeClassConceptSet->clear(); |
| 412 |
FOREACHIT (CConcept* concept, mUpdateConceptSet) { |
| 413 |
if (concept->hasClassName()) { |
| 414 |
activeClassConceptSet->insert(concept); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
}; // end namespace Preprocess |
| 424 |
|
| 425 |
}; // end namespace Reasoner |
| 426 |
|
| 427 |
}; // end namespace Konclude |