| 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 |
#ifdef KONCLUDE_REDLAND_INTEGRATION |
| 21 |
|
| 22 |
#include "CRedlandStoredTriplesIndividualAssertionConvertionIndexer.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Triples { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
CRedlandStoredTriplesIndividualAssertionConvertionIndexer::CRedlandStoredTriplesIndividualAssertionConvertionIndexer() { |
| 34 |
} |
| 35 |
|
| 36 |
CRedlandStoredTriplesIndividualAssertionConvertionIndexer::~CRedlandStoredTriplesIndividualAssertionConvertionIndexer() { |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
bool CRedlandStoredTriplesIndividualAssertionConvertionIndexer::indexABoxIndividuals(CConcreteOntology* updateConcreteOntology, COntologyTriplesData* ontologyTripleData) { |
| 41 |
mIndexingTime.start(); |
| 42 |
mUupdatingOntology = updateConcreteOntology; |
| 43 |
mPreprocessContext = new CPreProcessContextBase(updateConcreteOntology, updateConcreteOntology->getConfiguration()); |
| 44 |
|
| 45 |
mAnonymousOntologyIdentifier = updateConcreteOntology->getOntologyName() + ":"; |
| 46 |
if (!mAnonymousOntologyIdentifier.startsWith("_:")) { |
| 47 |
mAnonymousOntologyIdentifier = "_:" + mAnonymousOntologyIdentifier; |
| 48 |
} |
| 49 |
|
| 50 |
bool newTriplesMapped = false; |
| 51 |
for (CTriplesData* tripleData : *ontologyTripleData->getAllTriplesData()) { |
| 52 |
CRedlandStoredTriplesData* redlandTriplesData = dynamic_cast<CRedlandStoredTriplesData*>(tripleData); |
| 53 |
|
| 54 |
librdf_world* world = redlandTriplesData->getRedlandWorld(); |
| 55 |
mNextIndiId = 0; |
| 56 |
mDatatypeHashInitialized = false; |
| 57 |
mMemMan = updateConcreteOntology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager(); |
| 58 |
|
| 59 |
|
| 60 |
librdf_node* rdfTypePredicate = librdf_new_node_from_uri_string(world, (const unsigned char*)PREFIX_RDF_TYPE); |
| 61 |
librdf_node* namedIndividualObject = librdf_new_node_from_uri_string(world, (const unsigned char*)PREFIX_OWL_NAMED_INDIVIDUAL); |
| 62 |
|
| 63 |
|
| 64 |
mRoleNodeDataHash.clear(); |
| 65 |
QSet<CRole*>* activeRoleSet = updateConcreteOntology->getRBox()->getActivePropertyRoleSet(false); |
| 66 |
if (activeRoleSet) { |
| 67 |
for (CRole* role : *activeRoleSet) { |
| 68 |
QByteArray roleCharData = CIRIName::getRecentIRIName(role->getPropertyNameLinker()).toUtf8(); |
| 69 |
librdf_node* roleNode = librdf_new_node_from_uri_string(world, (const unsigned char*)roleCharData.constData()); |
| 70 |
mRoleNodeDataHash.insert(CRedlandNodeHasher(roleNode), new CRoleNodeData(roleNode, role, role->isDataRole())); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
mConceptNodeDataHash.clear(); |
| 76 |
CConceptNodeData* topConceptData = nullptr; |
| 77 |
QSet<CConcept*>* activeConceptSet = updateConcreteOntology->getTBox()->getActiveClassConceptSet(false); |
| 78 |
if (activeConceptSet) { |
| 79 |
for (CConcept* concept : *activeConceptSet) { |
| 80 |
QByteArray conceptCharData = CIRIName::getRecentIRIName(concept->getClassNameLinker()).toUtf8(); |
| 81 |
librdf_node* conceptNode = librdf_new_node_from_uri_string(world, (const unsigned char*)conceptCharData.constData()); |
| 82 |
CConceptNodeData* conceptData = new CConceptNodeData(conceptNode, concept); |
| 83 |
mConceptNodeDataHash.insert(CRedlandNodeHasher(conceptNode), conceptData); |
| 84 |
if (concept->getConceptTag() == 1) { |
| 85 |
topConceptData = conceptData; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
mIndividualNodeDataHash.clear(); |
| 92 |
QSet<CIndividual*>* activeIndiSet = updateConcreteOntology->getABox()->getActiveIndividualSet(false); |
| 93 |
if (activeIndiSet && !activeIndiSet->isEmpty()) { |
| 94 |
cint64 maxIndividualId = updateConcreteOntology->getABox()->getNextIndividualId(false); |
| 95 |
mNextIndiId = qMax(maxIndividualId, mNextIndiId); |
| 96 |
for (CIndividual* indi : *activeIndiSet) { |
| 97 |
maxIndividualId = qMax(indi->getIndividualID(), maxIndividualId); |
| 98 |
QByteArray indiCharData = CIRIName::getRecentIRIName(indi->getIndividualNameLinker()).toUtf8(); |
| 99 |
librdf_node* indiNode = librdf_new_node_from_uri_string(world, (const unsigned char*)indiCharData.constData()); |
| 100 |
|
| 101 |
CIndividualNodeData* indiData = new CIndividualNodeData(indiNode, indi); |
| 102 |
mIndividualNodeDataHash.insert(CRedlandNodeHasher(indiNode), indiData); |
| 103 |
mNextIndiId = qMax(indi->getIndividualID(), mNextIndiId); |
| 104 |
} |
| 105 |
mNextIndiId++; |
| 106 |
} |
| 107 |
|
| 108 |
indexTriples(redlandTriplesData, rdfTypePredicate, namedIndividualObject, updateConcreteOntology); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
LOG(INFO, "::Konclude::Reasoner::TripleEncodedAssertionsIndexer", QString("Identified %1 concept, %2 object property, and %3 data property assertions for overall %4 individuals in %5 ms.").arg(mConceptAssertionCount).arg(mObjectPropertyAssertionCount).arg(mDataPropertyAssertionCount).arg(mNextIndiId).arg(mIndexingTime.elapsed()), this); |
| 113 |
delete mPreprocessContext; |
| 114 |
|
| 115 |
return newTriplesMapped; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
CDataLiteralValue* CRedlandStoredTriplesIndividualAssertionConvertionIndexer::createDataLiteralValue(CDataLiteral* dataLiteral, CConcreteOntology* ontology) { |
| 120 |
CDataLiteralValue* dataLiteralValue = mDataLiteralNormalizer.createPreprocessedDataLiteralValue(dataLiteral, mPreprocessContext, ontology->getDataBoxes()->getBoxContext()); |
| 121 |
return dataLiteralValue; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
CRedlandStoredTriplesIndividualAssertionConvertionIndexer::CIndividualNodeData* CRedlandStoredTriplesIndividualAssertionConvertionIndexer::getIndividualData(librdf_node* indiNode, CConcreteOntology* ontology) { |
| 127 |
CIndividualNodeData* indiNodeData = mIndividualNodeDataHash.value(indiNode); |
| 128 |
if (!indiNodeData) { |
| 129 |
librdf_node* nodeCopy = librdf_new_node_from_node(indiNode); |
| 130 |
CIndividual* nextIndi = CObjectAllocator<CIndividual>::allocateAndConstruct(mMemMan); |
| 131 |
nextIndi->initIndividual(mNextIndiId++); |
| 132 |
nextIndi->setAnonymousIndividual(true); |
| 133 |
indiNodeData = new CIndividualNodeData(nodeCopy, nextIndi); |
| 134 |
mIndividualNodeDataHash.insert(nodeCopy, indiNodeData); |
| 135 |
|
| 136 |
|
| 137 |
const char* indiNameCharPointer = nullptr; |
| 138 |
bool blankNode = false; |
| 139 |
if (!librdf_node_is_blank(indiNode)) { |
| 140 |
librdf_uri* uri = librdf_node_get_uri(indiNode); |
| 141 |
indiNameCharPointer = (const char*)librdf_uri_as_string(uri); |
| 142 |
} else { |
| 143 |
indiNameCharPointer = (const char*)librdf_node_get_blank_identifier(indiNode); |
| 144 |
blankNode = true; |
| 145 |
} |
| 146 |
|
| 147 |
CIRIName* newName = CObjectAllocator<CIRIName>::allocateAndConstruct(mMemMan); |
| 148 |
QString indiName = QString::fromUtf8(indiNameCharPointer); |
| 149 |
if (blankNode) { |
| 150 |
if (!indiName.startsWith("_:")) { |
| 151 |
indiName = mAnonymousOntologyIdentifier + indiName; |
| 152 |
} else { |
| 153 |
indiName = mAnonymousOntologyIdentifier + indiName.mid(2); |
| 154 |
} |
| 155 |
} |
| 156 |
newName->init(indiName); |
| 157 |
CLinker<CName*>* newNameLinker = CObjectAllocator< CLinker<CName*> >::allocateAndConstruct(mMemMan); |
| 158 |
newNameLinker->init(newName); |
| 159 |
nextIndi->addNameLinker(newNameLinker); |
| 160 |
|
| 161 |
CIndividualProcessData* indiProcData = new CIndividualProcessData(); |
| 162 |
indiProcData->initIndividualProcessExtensionData(); |
| 163 |
nextIndi->setIndividualData(indiProcData); |
| 164 |
|
| 165 |
ontology->getABox()->getIndividualVector(true)->setData(nextIndi->getIndividualID(), nextIndi); |
| 166 |
ontology->getABox()->getActiveIndividualSet(true)->insert(nextIndi); |
| 167 |
|
| 168 |
ontology->getStringMapping()->getIndividualNameIndividualMappingHash()->insert(indiName, nextIndi); |
| 169 |
} |
| 170 |
return indiNodeData; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
CRedlandStoredTriplesIndividualAssertionConvertionIndexer::CIndividualNodeData* CRedlandStoredTriplesIndividualAssertionConvertionIndexer::getIndividualData(librdf_node* subjectNode, CIndividualNodeData*& lastIndiData, librdf_node*& lastSubjectNode, CConcreteOntology* ontology) { |
| 175 |
if (!lastIndiData || !librdf_node_equals(lastSubjectNode, subjectNode)) { |
| 176 |
CIndividualNodeData* indiNodeData = getIndividualData(subjectNode, ontology); |
| 177 |
lastIndiData = indiNodeData; |
| 178 |
if (lastSubjectNode) { |
| 179 |
librdf_free_node(lastSubjectNode); |
| 180 |
} |
| 181 |
lastSubjectNode = librdf_new_node_from_node(subjectNode); |
| 182 |
} |
| 183 |
return lastIndiData; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
CDatatype* CRedlandStoredTriplesIndividualAssertionConvertionIndexer::getDatatypeFromDatatypeUri(librdf_uri* uri, CConcreteOntology* ontology, CRedlandStoredTriplesData* ontologyTripleData) { |
| 189 |
CDatatype* datatype = nullptr; |
| 190 |
ensureDatatypeHashInitialized(ontology, ontologyTripleData); |
| 191 |
|
| 192 |
if (uri) { |
| 193 |
datatype = mDatatypeIriDatatypeHash.value(CRedlandUriHasher(uri)); |
| 194 |
} else { |
| 195 |
datatype = mDefaultDatatype; |
| 196 |
} |
| 197 |
return datatype; |
| 198 |
} |
| 199 |
|
| 200 |
void CRedlandStoredTriplesIndividualAssertionConvertionIndexer::ensureDatatypeHashInitialized(CConcreteOntology* ontology, CRedlandStoredTriplesData* ontologyTripleData) { |
| 201 |
if (!mDatatypeHashInitialized) { |
| 202 |
CBUILDHASH<CDatatype*, CDatatypeExpression*>* datatypeDatatypeExpHash = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getDatatypeDatatypeExpessionHash(); |
| 203 |
for (CBUILDHASH<CDatatype*, CDatatypeExpression*>::const_iterator it = datatypeDatatypeExpHash->constBegin(), itEnd = datatypeDatatypeExpHash->constEnd(); it != itEnd; ++it) { |
| 204 |
CDatatype* datatype = it.key(); |
| 205 |
if (datatype->getDatatypeType() == CDatatype::DT_STRING) { |
| 206 |
mDefaultDatatype = datatype; |
| 207 |
} |
| 208 |
QByteArray datatypeCharData = datatype->getDatatypeIRI().toUtf8(); |
| 209 |
librdf_uri* datatypeUri = librdf_new_uri(ontologyTripleData->getRedlandWorld(), (const unsigned char*)datatypeCharData.constData()); |
| 210 |
mDatatypeIriDatatypeHash.insert(CRedlandUriHasher(datatypeUri), datatype); |
| 211 |
} |
| 212 |
mDatatypeHashInitialized = true; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void CRedlandStoredTriplesIndividualAssertionConvertionIndexer::identifyIndividuals(librdf_statement* statement, librdf_node* rdfTypePredicate, librdf_node* namedIndividualObject, CIndividualNodeData* &lastIndiData, librdf_node* lastSubjectNode, CConcreteOntology* updateConcreteOntology, CRedlandStoredTriplesData* redlandTriplesData) { |
| 217 |
librdf_node* predicateNode = librdf_statement_get_predicate(statement); |
| 218 |
librdf_node* objectNode = librdf_statement_get_object(statement); |
| 219 |
librdf_node* subjectNode = librdf_statement_get_subject(statement); |
| 220 |
|
| 221 |
if (librdf_node_equals(predicateNode, rdfTypePredicate)) { |
| 222 |
if (librdf_node_equals(objectNode, namedIndividualObject)) { |
| 223 |
lastIndiData = getIndividualData(subjectNode, lastIndiData, lastSubjectNode, updateConcreteOntology); |
| 224 |
lastIndiData->mIndividual->setAnonymousIndividual(false); |
| 225 |
} else { |
| 226 |
CConceptNodeData* conceptData = mConceptNodeDataHash.value(CRedlandNodeHasher(objectNode)); |
| 227 |
if (conceptData) { |
| 228 |
lastIndiData = getIndividualData(subjectNode, lastIndiData, lastSubjectNode, updateConcreteOntology); |
| 229 |
CConceptAssertionLinker* conceptAssertionLinker = CObjectAllocator<CConceptAssertionLinker>::allocateAndConstruct(mMemMan); |
| 230 |
conceptAssertionLinker->initNegLinker(conceptData->mConcept, false); |
| 231 |
lastIndiData->mIndividual->addAssertionConceptLinker(conceptAssertionLinker); |
| 232 |
mConceptAssertionCount++; |
| 233 |
} |
| 234 |
if (lastIndiData && librdf_node_is_resource(subjectNode)) { |
| 235 |
lastIndiData->mIndividual->setAnonymousIndividual(false); |
| 236 |
} |
| 237 |
} |
| 238 |
} else { |
| 239 |
CRoleNodeData* roleData = mRoleNodeDataHash.value(CRedlandNodeHasher(predicateNode)); |
| 240 |
if (roleData) { |
| 241 |
lastIndiData = getIndividualData(subjectNode, lastIndiData, lastSubjectNode, updateConcreteOntology); |
| 242 |
if (librdf_node_is_resource(subjectNode)) { |
| 243 |
lastIndiData->mIndividual->setAnonymousIndividual(false); |
| 244 |
} |
| 245 |
if (roleData->mDataRole) { |
| 246 |
if (librdf_node_is_literal(objectNode)) { |
| 247 |
const char* literalValue = (const char*)librdf_node_get_literal_value(objectNode); |
| 248 |
librdf_uri* datatypeUri = librdf_node_get_literal_value_datatype_uri(objectNode); |
| 249 |
CDatatype* datatype = getDatatypeFromDatatypeUri(datatypeUri, updateConcreteOntology, redlandTriplesData); |
| 250 |
|
| 251 |
CDataLiteral* dataLiteral = CObjectParameterizingAllocator<CDataLiteral, CContext*>::allocateAndConstructAndParameterize(mMemMan, updateConcreteOntology->getDataBoxes()->getBoxContext()); |
| 252 |
dataLiteral->initDataLiteral(QString::fromUtf8(literalValue), datatype); |
| 253 |
|
| 254 |
createDataLiteralValue(dataLiteral, updateConcreteOntology); |
| 255 |
|
| 256 |
CDataAssertionLinker* dataAssertionLinker = CObjectAllocator<CDataAssertionLinker>::allocateAndConstruct(mMemMan); |
| 257 |
dataAssertionLinker->initDataAssertionLinker(roleData->mRole, dataLiteral); |
| 258 |
|
| 259 |
lastIndiData->mIndividual->addAssertionDataLinker(dataAssertionLinker); |
| 260 |
mDataPropertyAssertionCount++; |
| 261 |
} |
| 262 |
} else { |
| 263 |
if (!librdf_node_is_literal(objectNode)) { |
| 264 |
CIndividualNodeData* targetIndiNode = getIndividualData(objectNode, updateConcreteOntology); |
| 265 |
if (librdf_node_is_resource(objectNode)) { |
| 266 |
targetIndiNode->mIndividual->setAnonymousIndividual(false); |
| 267 |
} |
| 268 |
CRoleAssertionLinker* roleAssertionLinker = CObjectAllocator<CRoleAssertionLinker>::allocateAndConstruct(mMemMan); |
| 269 |
roleAssertionLinker->initRoleAssertionLinker(roleData->mRole, targetIndiNode->mIndividual); |
| 270 |
lastIndiData->mIndividual->addAssertionRoleLinker(roleAssertionLinker); |
| 271 |
CReverseRoleAssertionLinker* reverseRoleAssertionLinker = CObjectAllocator<CReverseRoleAssertionLinker>::allocateAndConstruct(mMemMan); |
| 272 |
reverseRoleAssertionLinker->initReverseRoleAssertionLinker(roleAssertionLinker, lastIndiData->mIndividual); |
| 273 |
targetIndiNode->mIndividual->addReverseAssertionRoleLinker(reverseRoleAssertionLinker); |
| 274 |
mObjectPropertyAssertionCount++; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void CRedlandStoredTriplesIndividualAssertionConvertionIndexer::indexTriples(CRedlandStoredTriplesData* redlandTriplesData, librdf_node* rdfTypePredicate, librdf_node* namedIndividualObject, CConcreteOntology* updateConcreteOntology) { |
| 282 |
|
| 283 |
CIndividualNodeData* lastIndiData = nullptr; |
| 284 |
librdf_node* lastSubjectNode = nullptr; |
| 285 |
|
| 286 |
CXLinker<librdf_statement*>* statementLinker = redlandTriplesData->getRedlandStatementLinker(); |
| 287 |
if (statementLinker) { |
| 288 |
|
| 289 |
while (statementLinker) { |
| 290 |
|
| 291 |
librdf_statement* statement = statementLinker->getData(); |
| 292 |
identifyIndividuals(statement, rdfTypePredicate, namedIndividualObject, lastIndiData, lastSubjectNode, updateConcreteOntology, redlandTriplesData); |
| 293 |
|
| 294 |
|
| 295 |
statementLinker = statementLinker->getNext(); |
| 296 |
} |
| 297 |
} else { |
| 298 |
librdf_stream* stream = librdf_model_as_stream(redlandTriplesData->getRedlandIndexedModel()); |
| 299 |
if (stream) { |
| 300 |
while (!librdf_stream_end(stream)) { |
| 301 |
librdf_statement* statement = librdf_stream_get_object(stream); |
| 302 |
identifyIndividuals(statement, rdfTypePredicate, namedIndividualObject, lastIndiData, lastSubjectNode, updateConcreteOntology, redlandTriplesData); |
| 303 |
librdf_stream_next(stream); |
| 304 |
} |
| 305 |
librdf_free_stream(stream); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
bool deleteIndexedTriplesData = CConfigDataReader::readConfigBoolean(updateConcreteOntology->getConfiguration(), "Konclude.Calculation.Preprocessing.TripleEncodedAssertionsIndexing.DeleteTriplesDataAfterIndexing", false); |
| 311 |
if (deleteIndexedTriplesData) { |
| 312 |
auto tripleDeleteMapFunc = [&](CRedlandStoredTriplesData* redlandTriplesData) { |
| 313 |
if (redlandTriplesData) { |
| 314 |
|
| 315 |
librdf_world* world = redlandTriplesData->getRedlandWorld(); |
| 316 |
mMemMan = updateConcreteOntology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager(); |
| 317 |
|
| 318 |
|
| 319 |
CXLinker<librdf_statement*>* statementLinker = redlandTriplesData->getRedlandStatementLinker(); |
| 320 |
if (statementLinker) { |
| 321 |
|
| 322 |
while (statementLinker) { |
| 323 |
librdf_statement* statement = statementLinker->getData(); |
| 324 |
librdf_free_statement(statement); |
| 325 |
CXLinker<librdf_statement*>* tmpStatementLinker = statementLinker; |
| 326 |
statementLinker = statementLinker->getNext(); |
| 327 |
delete tmpStatementLinker; |
| 328 |
} |
| 329 |
redlandTriplesData->setRedlandStatementLinker(nullptr); |
| 330 |
} else { |
| 331 |
librdf_stream* stream = librdf_model_as_stream(redlandTriplesData->getRedlandIndexedModel()); |
| 332 |
if (stream) { |
| 333 |
while (!librdf_stream_end(stream)) { |
| 334 |
librdf_statement* statement = librdf_stream_get_object(stream); |
| 335 |
librdf_model_remove_statement(redlandTriplesData->getRedlandIndexedModel(), statement); |
| 336 |
librdf_free_statement(statement); |
| 337 |
librdf_stream_next(stream); |
| 338 |
} |
| 339 |
librdf_free_stream(stream); |
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
}; |
| 346 |
tripleDeleteMapFunc(redlandTriplesData); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
updateConcreteOntology->getABox()->setNextIndividualId(mNextIndiId); |
| 351 |
|
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
}; // end namespace Triples |
| 356 |
|
| 357 |
}; // end namespace Reasoner |
| 358 |
|
| 359 |
}; // end namespace Konclude |
| 360 |
|
| 361 |
#endif // !KONCLUDE_REDLAND_INTEGRATION |