| 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 "CRedlandStoredTriplesIndividualAssertionIndexer.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Triples { |
| 30 |
|
| 31 |
|
| 32 |
CRedlandStoredTriplesIndividualAssertionIndexer::CRedlandStoredTriplesIndividualAssertionIndexer() { |
| 33 |
} |
| 34 |
|
| 35 |
CRedlandStoredTriplesIndividualAssertionIndexer::~CRedlandStoredTriplesIndividualAssertionIndexer() { |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
bool CRedlandStoredTriplesIndividualAssertionIndexer::indexABoxIndividuals(CConcreteOntology* updateConcreteOntology, COntologyTriplesData* ontologyTripleData) { |
| 40 |
|
| 41 |
bool newTriplesMapped = false; |
| 42 |
for (CTriplesData* tripleData : *ontologyTripleData->getAllTriplesData()) { |
| 43 |
CRedlandStoredTriplesData* redlandTriplesData = dynamic_cast<CRedlandStoredTriplesData*>(tripleData); |
| 44 |
|
| 45 |
mIndividualAssertionIndexCache = new CRedlandStoredTriplesIndividualAssertionIndexCache(updateConcreteOntology->getOntologyName(), redlandTriplesData, updateConcreteOntology->getConfiguration()); |
| 46 |
|
| 47 |
librdf_world* world = redlandTriplesData->getRedlandWorld(); |
| 48 |
|
| 49 |
|
| 50 |
librdf_node* rdfTypePredicate = librdf_new_node_from_uri_string(world, (const unsigned char*)PREFIX_RDF_TYPE); |
| 51 |
librdf_node* namedIndividualObject = librdf_new_node_from_uri_string(world, (const unsigned char*)PREFIX_OWL_NAMED_INDIVIDUAL); |
| 52 |
|
| 53 |
|
| 54 |
QSet<CRole*>* activeRoleSet = updateConcreteOntology->getRBox()->getActivePropertyRoleSet(false); |
| 55 |
if (activeRoleSet) { |
| 56 |
for (CRole* role : *activeRoleSet) { |
| 57 |
QByteArray roleCharData = CIRIName::getRecentIRIName(role->getPropertyNameLinker()).toUtf8(); |
| 58 |
librdf_node* roleNode = librdf_new_node_from_uri_string(world, (const unsigned char*)roleCharData.constData()); |
| 59 |
mRoleNodeDataHash.insert(CRedlandNodeHasher(roleNode), new CRoleNodeData(roleNode, role, role->isDataRole())); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
CConceptNodeData* topConceptData = nullptr; |
| 65 |
QSet<CConcept*>* activeConceptSet = updateConcreteOntology->getTBox()->getActiveClassConceptSet(false); |
| 66 |
if (activeConceptSet) { |
| 67 |
for (CConcept* concept : *activeConceptSet) { |
| 68 |
QByteArray conceptCharData = CIRIName::getRecentIRIName(concept->getClassNameLinker()).toUtf8(); |
| 69 |
librdf_node* conceptNode = librdf_new_node_from_uri_string(world, (const unsigned char*)conceptCharData.constData()); |
| 70 |
CConceptNodeData* conceptData = new CConceptNodeData(conceptNode, concept); |
| 71 |
mConceptNodeDataHash.insert(CRedlandNodeHasher(conceptNode), conceptData); |
| 72 |
if (concept->getConceptTag() == 1) { |
| 73 |
topConceptData = conceptData; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
QSet<CIndividual*>* activeIndiSet = updateConcreteOntology->getABox()->getActiveIndividualSet(false); |
| 80 |
if (activeIndiSet && !activeIndiSet->isEmpty()) { |
| 81 |
cint64 maxIndividualId = updateConcreteOntology->getABox()->getNextIndividualId(false); |
| 82 |
for (CIndividual* indi : *activeIndiSet) { |
| 83 |
maxIndividualId = qMax(indi->getIndividualID(), maxIndividualId); |
| 84 |
QByteArray indiCharData = CIRIName::getRecentIRIName(indi->getIndividualNameLinker()).toUtf8(); |
| 85 |
librdf_node* indiNode = librdf_new_node_from_uri_string(world, (const unsigned char*)indiCharData.constData()); |
| 86 |
mIndividualAssertionIndexCache->associateIndividualWithId(indiNode, indi->getIndividualID()); |
| 87 |
} |
| 88 |
mIndividualAssertionIndexCache->setNextIndividualId(maxIndividualId + 1); |
| 89 |
} |
| 90 |
|
| 91 |
mIndividualAssertionIndexCache->beginIndexing(); |
| 92 |
|
| 93 |
//for (cint64 loop = 0; true; loop++) { |
| 94 |
// bool debug = true; |
| 95 |
|
| 96 |
CRedlandStoredTriplesIndividualAssertionIndexCacheData* lastIndiCacheData = nullptr; |
| 97 |
librdf_node* lastSubjectNode = nullptr; |
| 98 |
|
| 99 |
CXLinker<librdf_statement*>* statementLinkerIt = redlandTriplesData->getRedlandStatementLinker(); |
| 100 |
if (statementLinkerIt) { |
| 101 |
for (; statementLinkerIt; statementLinkerIt = statementLinkerIt->getNext()) { |
| 102 |
|
| 103 |
librdf_statement* statement = statementLinkerIt->getData(); |
| 104 |
lastIndiCacheData = handleStatement(statement, rdfTypePredicate, namedIndividualObject, lastIndiCacheData, lastSubjectNode); |
| 105 |
} |
| 106 |
} else { |
| 107 |
librdf_stream* stream = librdf_model_as_stream(redlandTriplesData->getRedlandIndexedModel()); |
| 108 |
if (stream) { |
| 109 |
while (!librdf_stream_end(stream)) { |
| 110 |
|
| 111 |
librdf_statement* statement = librdf_stream_get_object(stream); |
| 112 |
lastIndiCacheData = handleStatement(statement, rdfTypePredicate, namedIndividualObject, lastIndiCacheData, lastSubjectNode); |
| 113 |
librdf_stream_next(stream); |
| 114 |
} |
| 115 |
librdf_free_stream(stream); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
mIndividualAssertionIndexCache->completeIndexing(); |
| 120 |
|
| 121 |
|
| 122 |
updateConcreteOntology->getABox()->setNextIndividualId(mIndividualAssertionIndexCache->getNextIndividualId()); |
| 123 |
|
| 124 |
ontologyTripleData->setTripleAssertionAccessor(new CRedlandStoredTriplesSimpleABoxAssertionsAccessor(updateConcreteOntology, mIndividualAssertionIndexCache, redlandTriplesData)); |
| 125 |
|
| 126 |
|
| 127 |
#ifndef KONCLUDE_FORCE_ALL_DEBUG_DEACTIVATED |
| 128 |
QString modelString = mIndividualAssertionIndexCache->getRepresentativeCacheModelString(); |
| 129 |
QFile modelFile("./Debugging/RepresentativeCache/assertion-index-data.txt"); |
| 130 |
if (modelFile.open(QIODevice::WriteOnly)) { |
| 131 |
modelFile.write(modelString.toUtf8()); |
| 132 |
} |
| 133 |
#endif |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
return newTriplesMapped; |
| 138 |
} |
| 139 |
|
| 140 |
CRedlandStoredTriplesIndividualAssertionIndexCacheData* CRedlandStoredTriplesIndividualAssertionIndexer::getIndividualAssertionIndexcacheData(librdf_node* subjectNode, CRedlandStoredTriplesIndividualAssertionIndexCacheData*& lastIndiCacheData, librdf_node*& lastSubjectNode) { |
| 141 |
if (!lastIndiCacheData || !librdf_node_equals(lastSubjectNode, subjectNode)) { |
| 142 |
lastIndiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(subjectNode); |
| 143 |
if (lastSubjectNode) { |
| 144 |
librdf_free_node(lastSubjectNode); |
| 145 |
} |
| 146 |
lastSubjectNode = librdf_new_node_from_node(subjectNode); |
| 147 |
} |
| 148 |
return lastIndiCacheData; |
| 149 |
} |
| 150 |
|
| 151 |
Konclude::Reasoner::Triples::CRedlandStoredTriplesIndividualAssertionIndexCacheData* CRedlandStoredTriplesIndividualAssertionIndexer::handleStatement(librdf_statement* statement, librdf_node* rdfTypePredicate, librdf_node* namedIndividualObject, CRedlandStoredTriplesIndividualAssertionIndexCacheData* lastIndiCacheData, librdf_node* lastSubjectNode) { |
| 152 |
librdf_node* predicateNode = librdf_statement_get_predicate(statement); |
| 153 |
librdf_node* objectNode = librdf_statement_get_object(statement); |
| 154 |
librdf_node* subjectNode = librdf_statement_get_subject(statement); |
| 155 |
|
| 156 |
if (librdf_node_equals(predicateNode, rdfTypePredicate)) { |
| 157 |
if (librdf_node_equals(objectNode, namedIndividualObject)) { |
| 158 |
lastIndiCacheData = getIndividualAssertionIndexcacheData(subjectNode, lastIndiCacheData, lastSubjectNode); |
| 159 |
lastIndiCacheData->setAnonymous(false); |
| 160 |
} else { |
| 161 |
CConceptNodeData* conceptData = mConceptNodeDataHash.value(CRedlandNodeHasher(objectNode)); |
| 162 |
if (conceptData) { |
| 163 |
lastIndiCacheData = getIndividualAssertionIndexcacheData(subjectNode, lastIndiCacheData, lastSubjectNode); |
| 164 |
if (librdf_node_is_resource(subjectNode)) { |
| 165 |
lastIndiCacheData->setAnonymous(false); |
| 166 |
} |
| 167 |
mIndividualAssertionIndexCache->extendIndividualAssertionIndexCacheDataByTypeConcept(lastIndiCacheData, conceptData->mConcept); |
| 168 |
} |
| 169 |
} |
| 170 |
} else { |
| 171 |
CRoleNodeData* roleData = mRoleNodeDataHash.value(CRedlandNodeHasher(predicateNode)); |
| 172 |
if (roleData) { |
| 173 |
lastIndiCacheData = getIndividualAssertionIndexcacheData(subjectNode, lastIndiCacheData, lastSubjectNode); |
| 174 |
if (librdf_node_is_resource(subjectNode)) { |
| 175 |
lastIndiCacheData->setAnonymous(false); |
| 176 |
} |
| 177 |
if (roleData->mDataRole) { |
| 178 |
if (librdf_node_is_literal(objectNode)) { |
| 179 |
mIndividualAssertionIndexCache->extendIndividualAssertionIndexCacheDataByOutgoingDataRole(lastIndiCacheData, roleData->mRole); |
| 180 |
} |
| 181 |
} else { |
| 182 |
if (!librdf_node_is_literal(objectNode)) { |
| 183 |
mIndividualAssertionIndexCache->extendIndividualAssertionIndexCacheDataByOutgoingObjectRole(lastIndiCacheData, roleData->mRole); |
| 184 |
CRedlandStoredTriplesIndividualAssertionIndexCacheData* otherIndiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(objectNode); |
| 185 |
if (librdf_node_is_resource(objectNode)) { |
| 186 |
otherIndiCacheData->setAnonymous(false); |
| 187 |
} |
| 188 |
mIndividualAssertionIndexCache->extendIndividualAssertionIndexCacheDataByIncomingObjectRole(otherIndiCacheData, roleData->mRole); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
return lastIndiCacheData; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
}; // end namespace Triples |
| 198 |
|
| 199 |
}; // end namespace Reasoner |
| 200 |
|
| 201 |
}; // end namespace Konclude |
| 202 |
|
| 203 |
#endif // !KONCLUDE_REDLAND_INTEGRATION |