Debian Codegraph @

Source/Reasoner/Triples/CRedlandStoredTriplesSimpleABoxAssertionsAccessor.cpp

raw
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 "CRedlandStoredTriplesSimpleABoxAssertionsAccessor.h"
23
24
25 namespace Konclude {
26
27 namespace Reasoner {
28
29 namespace Triples {
30
31
32 CRedlandStoredTriplesSimpleABoxAssertionsAccessor::CRedlandStoredTriplesSimpleABoxAssertionsAccessor(CConcreteOntology* ontology, CRedlandStoredTriplesIndividualAssertionIndexCache* individualAssertionIndexCache, CRedlandStoredTriplesData* tripleData) {
33 mTripleData = tripleData;
34 mIndividualAssertionIndexCache = individualAssertionIndexCache;
35 mOntology = ontology;
36
37 mAnonymousOntologyIdentifier = mOntology->getOntologyName() + ":";
38 if (!mAnonymousOntologyIdentifier.startsWith("_:")) {
39 mAnonymousOntologyIdentifier = "_:" + mAnonymousOntologyIdentifier;
40 }
41 }
42
43 CRedlandStoredTriplesSimpleABoxAssertionsAccessor::~CRedlandStoredTriplesSimpleABoxAssertionsAccessor() {
44 }
45
46
47
48 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::hasIndividualId(const QString& indiName) {
49 cint64 indiId = mIndividualAssertionIndexCache->getIndividualIdFromIndividualName(indiName);
50 return indiId >= 0;
51 }
52
53
54 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitIndividualId(const QString& indiName, function<bool(cint64 indiId)> visitFunc) {
55 cint64 indiId = mIndividualAssertionIndexCache->getIndividualIdFromIndividualName(indiName);
56 if (indiId >= 0) {
57 visitFunc(indiId);
58 return true;
59 }
60 return false;
61 }
62
63 QString CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getAnonymousResolvedIndividualName(CRedlandStoredTriplesIndividualAssertionIndexCacheData* indiCacheData) {
64 QString indiName = indiCacheData->getIndividualName();
65 if (indiCacheData->isAnonymous()) {
66 if (indiName.startsWith("_:")) {
67 indiName = indiName.mid(2);
68 }
69 return mAnonymousOntologyIdentifier + indiName;
70 }
71 return indiName;
72 }
73
74
75 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitIndividualName(cint64 individualId, COntologyTriplesIndividualNamesVisitor* visitor) {
76 CRedlandStoredTriplesIndividualAssertionIndexCacheData* indiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(individualId);
77 if (indiCacheData) {
78 visitor->visitIndividualName(getAnonymousResolvedIndividualName(indiCacheData), this);
79 return true;
80 }
81 return false;
82 }
83
84
85 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitIndividualName(cint64 individualId, function<bool(const QString& indiName)> visitFunc) {
86 CRedlandStoredTriplesIndividualAssertionIndexCacheData* indiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(individualId);
87 if (indiCacheData) {
88 visitFunc(getAnonymousResolvedIndividualName(indiCacheData));
89 return true;
90 }
91 return false;
92 }
93
94
95 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitIndividualAnonymity(cint64 individualId, function<bool(bool anonymous)> visitFunc) {
96 CRedlandStoredTriplesIndividualAssertionIndexCacheData* indiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(individualId);
97 if (indiCacheData) {
98 visitFunc(indiCacheData->isAnonymous());
99 return true;
100 }
101 return false;
102 }
103
104
105
106
107 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitIndividualAssertions(cint64 individualId, COntologyTriplesIndividualAssertionsVisitor* visitor, bool visitConceptAssertions, bool visitRoleAssertions, bool visitReverseRoleAssertions, bool visitDataAssertions) {
108 CRedlandStoredTriplesIndividualAssertionIndexCacheData* indiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(individualId);
109 if (indiCacheData) {
110
111 librdf_node* indiNode = librdf_new_node_from_node(indiCacheData->getIndividualNode());
112
113 bool continueVisiting = true;
114
115 #ifndef KONCLUDE_FORCE_ALL_DEBUG_DEACTIVATED
116 continueVisiting &= visitor->visitIndividualName(indiCacheData->getIndividualName(), this);
117 #endif
118
119 CRedlandStoredTriplesRepresentativeIndividualAssertionsIndexData* repIndiAssData = indiCacheData->getCurrentRepresentativeIndividualAssertionsIndexData();
120 if (repIndiAssData) {
121 CRedlandStoredTriplesRepresentativeTagTypeConceptsSet* repConSet = repIndiAssData->getRepresentativeTypeConceptsSet();
122 if (visitConceptAssertions && repConSet && repConSet->getTagElementCount() > 0) {
123 for (QMap<cint64, CConcept*>::const_iterator it = repConSet->getTagDataMap()->constBegin(), itEnd = repConSet->getTagDataMap()->constEnd(); continueVisiting && it != itEnd; ++it) {
124 CConcept* concept = it.value();
125 continueVisiting &= visitor->visitConceptAssertion(concept, this);
126 }
127 }
128
129 CRedlandStoredTriplesRepresentativeTagRolesSet* repOutRoleSet = repIndiAssData->getRepresentativeOutgoingObjectRolesSet();
130 if (visitRoleAssertions && repOutRoleSet && repOutRoleSet->getTagElementCount() > 0) {
131 for (QMap<cint64, CRole*>::const_iterator it = repOutRoleSet->getTagDataMap()->constBegin(), itEnd = repOutRoleSet->getTagDataMap()->constEnd(); continueVisiting && it != itEnd; ++it) {
132 CRole* role = it.value();
133
134 librdf_model* model = mTripleData->getRedlandIndexedModel();
135 librdf_statement *partial_statement = librdf_new_statement(mTripleData->getRedlandWorld());
136
137 librdf_statement_set_predicate(partial_statement, librdf_new_node_from_node(getRolePredicateNode(role)));
138 librdf_statement_set_subject(partial_statement, librdf_new_node_from_node(indiNode));
139
140 librdf_stream* stream = librdf_model_find_statements(model, partial_statement);
141 while (!librdf_stream_end(stream) && continueVisiting) {
142 librdf_statement* statement = librdf_stream_get_object(stream);
143 librdf_node* objectNode = librdf_statement_get_object(statement);
144
145 CRedlandStoredTriplesIndividualAssertionIndexCacheData* otherIndiCacheData = mIndividualAssertionIndexCache->getIndividualAssertionIndexCacheData(objectNode, false);
146 cint64 otherIndiId = otherIndiCacheData->getIndividualId();
147 continueVisiting &= visitor->visitRoleAssertion(role, otherIndiId, this);
148 librdf_stream_next(stream);
149 }
150 librdf_free_stream(stream);
151 librdf_free_statement(partial_statement);
152 }
153 }
154
155
156 CRedlandStoredTriplesRepresentativeTagRolesSet* repIncRoleSet = repIndiAssData->getRepresentativeIncomingObjectRolesSet();
157 if (visitReverseRoleAssertions && repIncRoleSet && repIncRoleSet->getTagElementCount() > 0) {
158 for (QMap<cint64, CRole*>::const_iterator it = repIncRoleSet->getTagDataMap()->constBegin(), itEnd = repIncRoleSet->getTagDataMap()->constEnd(); continueVisiting && it != itEnd; ++it) {
159 CRole* role = it.value();
160
161 librdf_model* model = mTripleData->getRedlandIndexedModel();
162 librdf_statement *partial_statement = librdf_new_statement(mTripleData->getRedlandWorld());
163
164 librdf_statement_set_predicate(partial_statement, librdf_new_node_from_node(getRolePredicateNode(role)));
165 librdf_statement_set_object(partial_statement, librdf_new_node_from_node(indiNode));
166
167 librdf_stream* stream = librdf_model_find_statements(model, partial_statement);
168 while (!librdf_stream_end(stream) && continueVisiting) {
169 librdf_statement* statement = librdf_stream_get_object(stream);
170 librdf_node* subjectNode = librdf_statement_get_subject(statement);
171
172 cint64 otherIndiId = mIndividualAssertionIndexCache->getIndividualIdFromIndividualNode(subjectNode);
173 continueVisiting &= visitor->visitReverseRoleAssertion(role, otherIndiId, this);
174 librdf_stream_next(stream);
175 }
176 librdf_free_stream(stream);
177 librdf_free_statement(partial_statement);
178 }
179 }
180
181
182
183 CRedlandStoredTriplesRepresentativeTagRolesSet* repDataRoleSet = repIndiAssData->getRepresentativeOutgoingDataRolesSet();
184 if (visitDataAssertions && repDataRoleSet && repDataRoleSet->getTagElementCount() > 0) {
185
186
187 for (QMap<cint64, CRole*>::const_iterator it = repDataRoleSet->getTagDataMap()->constBegin(), itEnd = repDataRoleSet->getTagDataMap()->constEnd(); continueVisiting && it != itEnd; ++it) {
188 CRole* role = it.value();
189
190 librdf_model* model = mTripleData->getRedlandIndexedModel();
191 librdf_statement *partial_statement = librdf_new_statement(mTripleData->getRedlandWorld());
192
193 librdf_statement_set_predicate(partial_statement, librdf_new_node_from_node(getRolePredicateNode(role)));
194 librdf_statement_set_subject(partial_statement, librdf_new_node_from_node(indiNode));
195
196 librdf_stream* stream = librdf_model_find_statements(model, partial_statement);
197 while (!librdf_stream_end(stream) && continueVisiting) {
198 librdf_statement* statement = librdf_stream_get_object(stream);
199 librdf_node* objectNode = librdf_statement_get_object(statement);
200
201 const char* literalValue = (const char*)librdf_node_get_literal_value(objectNode);
202 librdf_uri* datatypeUri = librdf_node_get_literal_value_datatype_uri(objectNode);
203 CDatatype* datatype = getDatatypeFromDatatypeUri(datatypeUri);
204
205 CDataLiteral dataLiteral(nullptr);
206 dataLiteral.initDataLiteral(QString::fromUtf8(literalValue), datatype);
207
208 continueVisiting &= visitor->visitDataAssertion(role, &dataLiteral, this);
209 librdf_stream_next(stream);
210 }
211 librdf_free_stream(stream);
212 librdf_free_statement(partial_statement);
213 }
214 }
215
216 }
217 librdf_free_node(indiNode);
218 return true;
219 }
220 return false;
221 }
222
223
224 librdf_node* CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getRolePredicateNode(CRole* role) {
225 librdf_node*& predicateNode = mRolePredicateNodeHash[role];
226 if (!predicateNode) {
227 QByteArray predicateCharData = CIRIName::getRecentIRIName(role->getNameLinker()).toUtf8();
228 predicateNode = librdf_new_node_from_uri_string(mTripleData->getRedlandWorld(), (const unsigned char*)predicateCharData.constData());
229 }
230 return predicateNode;
231 }
232
233
234 CDatatype* CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getDatatypeFromDatatypeUri(librdf_uri* uri) {
235 CDatatype* datatype = nullptr;
236 initializeDatatypeMaps();
237 if (uri) {
238 datatype = mDatatypeIriDatatypeHash.value(CRedlandUriHasher(uri));
239 } else {
240 datatype = mDefaultDatatype;
241 }
242 return datatype;
243 }
244
245
246
247 librdf_uri* CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getDatatypeUriFromDatatype(CDatatype* datatype) {
248 librdf_uri* datatypeUri = nullptr;
249 initializeDatatypeMaps();
250 if (datatype) {
251 datatypeUri = mDatatypeDatatypeIriHash.value(datatype);
252 } else {
253 datatypeUri = mDefaultDatatypeUri;
254 }
255 return datatypeUri;
256 }
257
258 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::initializeDatatypeMaps() {
259 if (!mDatatypeHashInitialized) {
260 CBUILDHASH<CDatatype*, CDatatypeExpression*>* datatypeDatatypeExpHash = mOntology->getDataBoxes()->getExpressionDataBoxMapping()->getDatatypeDatatypeExpessionHash();
261 for (CBUILDHASH<CDatatype*, CDatatypeExpression*>::const_iterator it = datatypeDatatypeExpHash->constBegin(), itEnd = datatypeDatatypeExpHash->constEnd(); it != itEnd; ++it) {
262 CDatatype* datatype = it.key();
263
264 QByteArray datatypeCharData = datatype->getDatatypeIRI().toUtf8();
265 librdf_uri* datatypeUri = librdf_new_uri(mTripleData->getRedlandWorld(), (const unsigned char*)datatypeCharData.constData());
266 mDatatypeIriDatatypeHash.insert(CRedlandUriHasher(datatypeUri), datatype);
267
268 mDatatypeDatatypeIriHash.insert(datatype, datatypeUri);
269
270 if (datatype->getDatatypeType() == CDatatype::DT_STRING) {
271 mDefaultDatatype = datatype;
272 mDefaultDatatypeUri = datatypeUri;
273 }
274 }
275 mDatatypeHashInitialized = true;
276 return true;
277 }
278 return false;
279 }
280
281
282
283 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitAllAssertedConcepts(COntologyTriplesAssertedTypesVisitor* visitor) {
284 bool visited = false;
285 for (CConcept* concept : *mIndividualAssertionIndexCache->getAssertedConceptSet()) {
286 visited = true;
287 if (!visitor->visitAssertedConcept(concept, this)) {
288 return visited;
289 }
290 }
291 return visited;
292 }
293
294 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitAllAssertedObjectRoles(COntologyTriplesAssertedTypesVisitor* visitor) {
295 bool visited = false;
296 for (CRole* role : *mIndividualAssertionIndexCache->getAssertedObjectRoleSet()) {
297 visited = true;
298 if (!visitor->visitAssertedObjectRole(role, this)) {
299 return visited;
300 }
301 }
302 return visited;
303 }
304
305 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitAllAssertedDataRoles(COntologyTriplesAssertedTypesVisitor* visitor) {
306 bool visited = false;
307 for (CRole* role : *mIndividualAssertionIndexCache->getAssertedDataRoleSet()) {
308 visited = true;
309 if (!visitor->visitAssertedDataRole(role, this)) {
310 return visited;
311 }
312 }
313 return visited;
314 }
315
316
317 cint64 CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getMaxIndexedIndividualId() {
318 return mIndividualAssertionIndexCache->getNextIndividualId() - 1;
319 }
320
321 cint64 CRedlandStoredTriplesSimpleABoxAssertionsAccessor::getIndexedIndividualCount() {
322 return mIndividualAssertionIndexCache->getNextIndividualId();
323 }
324
325
326 bool CRedlandStoredTriplesSimpleABoxAssertionsAccessor::visitDataValueIndividuals(CRole* dataRole, CDataLiteral* dataLiteral, function<bool(cint64 indiId)> visitFunc) {
327
328 bool continueVisiting = true;
329
330 librdf_model* model = mTripleData->getRedlandIndexedModel();
331 librdf_statement *partial_statement = librdf_new_statement(mTripleData->getRedlandWorld());
332
333 librdf_statement_set_predicate(partial_statement, librdf_new_node_from_node(getRolePredicateNode(dataRole)));
334
335 QByteArray dataLiteralCharData = dataLiteral->getLexicalDataLiteralValueString().toUtf8();
336
337 librdf_uri* datatypeUri = getDatatypeUriFromDatatype(dataLiteral->getDatatype());
338 if (datatypeUri == mDefaultDatatypeUri) {
339 datatypeUri = nullptr;
340 }
341
342 librdf_statement_set_object(partial_statement, librdf_new_node_from_typed_literal(mTripleData->getRedlandWorld(), (const unsigned char*)dataLiteralCharData.constData(), nullptr, datatypeUri));
343
344 librdf_stream* stream = librdf_model_find_statements(model, partial_statement);
345 while (!librdf_stream_end(stream) && continueVisiting) {
346 librdf_statement* statement = librdf_stream_get_object(stream);
347 librdf_node* subjectNode = librdf_statement_get_subject(statement);
348
349 cint64 indiId = mIndividualAssertionIndexCache->getIndividualIdFromIndividualNode(subjectNode);
350
351 continueVisiting &= visitFunc(indiId);
352 librdf_stream_next(stream);
353 }
354 librdf_free_stream(stream);
355 librdf_free_statement(partial_statement);
356 return true;
357
358 }
359
360
361
362 }; // end namespace Triples
363
364 }; // end namespace Reasoner
365
366 }; // end namespace Konclude
367
368 #endif // !KONCLUDE_REDLAND_INTEGRATION