| 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 "CRDFRedlandRaptorParser.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Parser { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
CRDFRedlandRaptorParser::CRDFRedlandRaptorParser(COntologyBuilder* ontologyBuilder, CTRIPLES_DATA_UPDATE_TYPE updateType, QString redlandParsingFormat, CConfiguration* configuration) : CLogIdentifier("::Konclude::Parser::RDFRedlandRaptorParser", this) { |
| 32 |
mOntologyBuilder = ontologyBuilder; |
| 33 |
mUpdateType = updateType; |
| 34 |
mRedlandParsingFormat = redlandParsingFormat; |
| 35 |
mConfiguration = configuration; |
| 36 |
|
| 37 |
mConfLoadTriplesIntoStore = true; |
| 38 |
if (mConfiguration) { |
| 39 |
mConfLoadTriplesIntoStore = CConfigDataReader::readConfigBoolean(mConfiguration, "Konclude.Parser.RedlandRaptor.LoadTriplesIntoStore", true); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
CRDFRedlandRaptorParser::~CRDFRedlandRaptorParser() { |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
CRedlandStoredTriplesData* CRDFRedlandRaptorParser::getUpdatingTripleData(bool forceNew) { |
| 50 |
|
| 51 |
bool localTriplesDataFlag = false; |
| 52 |
CRedlandStoredTriplesData* prevTripleData = (CRedlandStoredTriplesData*)mOntologyBuilder->getLatestTriplesData(false, &localTriplesDataFlag); |
| 53 |
CRedlandStoredTriplesData* tripleData = nullptr; |
| 54 |
if (!forceNew && localTriplesDataFlag && prevTripleData->getUpdateType() == mUpdateType) { |
| 55 |
return prevTripleData; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if (!tripleData) { |
| 60 |
tripleData = new CRedlandStoredTriplesData(); |
| 61 |
tripleData->initTriplesData(mUpdateType, prevTripleData); |
| 62 |
} |
| 63 |
|
| 64 |
librdf_world* world = tripleData->getRedlandWorld(); |
| 65 |
if (!world) { |
| 66 |
world = librdf_new_world(); |
| 67 |
librdf_world_open(world); |
| 68 |
tripleData->setRedlandWorldData(world); |
| 69 |
} |
| 70 |
|
| 71 |
librdf_storage* indexedStorage = tripleData->getRedlandIndexedStorage(); |
| 72 |
if (!indexedStorage && world) { |
| 73 |
indexedStorage = librdf_new_storage(world, "hashes", NULL, "hash-type='memory',index-predicates='yes'"); |
| 74 |
tripleData->setRedlandIndexedStorageData(indexedStorage); |
| 75 |
} |
| 76 |
|
| 77 |
librdf_model* indexedModel = tripleData->getRedlandIndexedModel(); |
| 78 |
if (!indexedModel && indexedStorage) { |
| 79 |
indexedModel = librdf_new_model(world, indexedStorage, NULL); |
| 80 |
tripleData->setRedlandIndexedModelData(indexedModel); |
| 81 |
} |
| 82 |
/*else { |
| 83 |
model = librdf_new_model_from_model(prevModel); |
| 84 |
tripleData->setRedlandModelData(model); |
| 85 |
}*/ |
| 86 |
|
| 87 |
|
| 88 |
if (!indexedModel) { |
| 89 |
delete tripleData; |
| 90 |
tripleData = nullptr; |
| 91 |
} |
| 92 |
|
| 93 |
return tripleData; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
bool CRDFRedlandRaptorParser::parseTriplesFile(const QString& filename) { |
| 102 |
|
| 103 |
|
| 104 |
mParsingError = false; |
| 105 |
CRedlandStoredTriplesData* tripleData = getUpdatingTripleData(); |
| 106 |
|
| 107 |
QString filenameUri = QString("file://" + filename); |
| 108 |
QByteArray filenameUriByteArray(filenameUri.toUtf8()); |
| 109 |
const char* filenameBaseUri = filenameUriByteArray.constData(); |
| 110 |
librdf_uri *baseUri = librdf_new_uri(tripleData->getRedlandWorld(), (const unsigned char*)filenameBaseUri); |
| 111 |
|
| 112 |
|
| 113 |
QByteArray mRedlandParsingFormatByteArray = mRedlandParsingFormat.toUtf8(); |
| 114 |
const char* parser_name = mRedlandParsingFormatByteArray.constData(); |
| 115 |
librdf_parser* parser = librdf_new_parser(tripleData->getRedlandWorld(), parser_name, NULL, NULL); |
| 116 |
if (!parser) { |
| 117 |
mParsingError = true; |
| 118 |
} |
| 119 |
|
| 120 |
QByteArray filenameByteArray(filename.toUtf8()); |
| 121 |
FILE* file = fopen(filenameByteArray.constData(), "r"); |
| 122 |
// parser closes file |
| 123 |
if (parser && librdf_parser_parse_file_handle_into_model(parser, file, 1, baseUri, tripleData->getRedlandIndexedModel())) { |
| 124 |
mParsingError = true; |
| 125 |
} |
| 126 |
if (parser) { |
| 127 |
librdf_free_parser(parser); |
| 128 |
} |
| 129 |
|
| 130 |
if (mParsingError) { |
| 131 |
delete tripleData; |
| 132 |
tripleData = nullptr; |
| 133 |
} else { |
| 134 |
mOntologyBuilder->addTriplesData(tripleData); |
| 135 |
} |
| 136 |
|
| 137 |
return !mParsingError; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
int raptor_iostream_read_bytes_from_qiodevice(void *context, void *ptr, size_t size, size_t nmemb) { |
| 143 |
QIODevice* ioDevice = (QIODevice*)context; |
| 144 |
qint64 readElements = 0; |
| 145 |
if (size == 1) { |
| 146 |
readElements = ioDevice->read((char*)ptr, nmemb); |
| 147 |
} else { |
| 148 |
for (size_t i = 0; i < nmemb && !ioDevice->atEnd(); ++i) { |
| 149 |
qint64 read = ioDevice->read((char*)ptr, size); |
| 150 |
if (read >= size) { |
| 151 |
ptr = (void*)((char*)ptr + size); |
| 152 |
readElements++; |
| 153 |
} else { |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
return readElements; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
int raptor_iostream_read_eof_func_from_qiodevice(void *context) { |
| 163 |
QIODevice* ioDevice = (QIODevice*)context; |
| 164 |
if (ioDevice->atEnd()) { |
| 165 |
return 1; |
| 166 |
} |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void raptor_log_handler_error_notifier(void *user_data, raptor_log_message *message) { |
| 172 |
QString** raptorParsingErrorString = (QString**)user_data; |
| 173 |
if (message->level >= RAPTOR_LOG_LEVEL_ERROR && *raptorParsingErrorString == nullptr) { |
| 174 |
*raptorParsingErrorString = new QString(QString("%1").arg(message->text)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
QString CRDFRedlandRaptorParser::getErrorString() { |
| 180 |
return mErrorString; |
| 181 |
} |
| 182 |
|
| 183 |
bool CRDFRedlandRaptorParser::hasError() { |
| 184 |
return mParsingError; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
bool CRDFRedlandRaptorParser::parseTriples(QIODevice* ioDevice, const QString& baseUriString) { |
| 189 |
raptor_iostream_handler qioDeviceIOStreamHandler; |
| 190 |
qioDeviceIOStreamHandler.version = 2; |
| 191 |
qioDeviceIOStreamHandler.read_bytes = &raptor_iostream_read_bytes_from_qiodevice; |
| 192 |
qioDeviceIOStreamHandler.read_eof = &raptor_iostream_read_eof_func_from_qiodevice; |
| 193 |
qioDeviceIOStreamHandler.init = nullptr; |
| 194 |
qioDeviceIOStreamHandler.finish = nullptr; |
| 195 |
qioDeviceIOStreamHandler.write_byte = nullptr; |
| 196 |
qioDeviceIOStreamHandler.write_bytes = nullptr; |
| 197 |
qioDeviceIOStreamHandler.write_end = nullptr; |
| 198 |
|
| 199 |
CRedlandStoredTriplesData* tripleData = getUpdatingTripleData(false); |
| 200 |
raptor_world* raptor_world_ptr = librdf_world_get_raptor(tripleData->getRedlandWorld()); |
| 201 |
|
| 202 |
cint64 bytesAvailable = ioDevice->bytesAvailable(); |
| 203 |
|
| 204 |
|
| 205 |
QString* raptorParsingErrorString = nullptr; |
| 206 |
raptor_world_set_log_handler(raptor_world_ptr, &raptorParsingErrorString, &raptor_log_handler_error_notifier); |
| 207 |
|
| 208 |
raptor_iostream* raptor_stream = raptor_new_iostream_from_handler(raptor_world_ptr, ioDevice, &qioDeviceIOStreamHandler); |
| 209 |
|
| 210 |
|
| 211 |
QByteArray baseUriStringByteArray(baseUriString.toUtf8()); |
| 212 |
librdf_uri *baseUri = librdf_new_uri(tripleData->getRedlandWorld(), (const unsigned char*)baseUriStringByteArray.constData()); |
| 213 |
|
| 214 |
|
| 215 |
QByteArray mRedlandParsingFormatByteArray = mRedlandParsingFormat.toUtf8(); |
| 216 |
const char* parser_name = mRedlandParsingFormatByteArray.constData(); |
| 217 |
librdf_parser* parser = librdf_new_parser(tripleData->getRedlandWorld(), parser_name, NULL, NULL); |
| 218 |
if (!parser) { |
| 219 |
mErrorString = QString("Failed to create parser"); |
| 220 |
mParsingError = true; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
//if (parser && raptor_stream && librdf_parser_parse_iostream_into_model(parser, raptor_stream, baseUri, tripleData->getRedlandStreamedModel())) { |
| 225 |
// mParsingError = true; |
| 226 |
//} |
| 227 |
|
| 228 |
librdf_stream* tripleStream = nullptr; |
| 229 |
if (parser && raptor_stream) { |
| 230 |
tripleStream = librdf_parser_parse_iostream_as_stream(parser, raptor_stream, baseUri); |
| 231 |
if (!tripleStream) { |
| 232 |
mErrorString = QString("Failed to load data stream"); |
| 233 |
mParsingError = true; |
| 234 |
} |
| 235 |
|
| 236 |
librdf_node* critPredicate1 = librdf_new_node_from_uri_string(tripleData->getRedlandWorld(), (const unsigned char*)PREFIX_RDFS_SUBCLASS_OF); |
| 237 |
librdf_node* critPredicate2 = librdf_new_node_from_uri_string(tripleData->getRedlandWorld(), (const unsigned char*)PREFIX_OWL_COMPLEMENT_OF); |
| 238 |
bool warningNoStore = false; |
| 239 |
|
| 240 |
CXLinker<librdf_statement*>* statementLinker = tripleData->getRedlandStatementLinker(); |
| 241 |
CXLinker<librdf_statement*>* lastStatementLinker = nullptr; |
| 242 |
if (statementLinker) { |
| 243 |
lastStatementLinker = statementLinker->getLastListLink(); |
| 244 |
} |
| 245 |
while (!librdf_stream_end(tripleStream)) { |
| 246 |
librdf_statement* statement = librdf_stream_get_object(tripleStream); |
| 247 |
|
| 248 |
if (!librdf_model_contains_statement(tripleData->getRedlandIndexedModel(), statement)) { |
| 249 |
librdf_statement* statementCopy = librdf_new_statement_from_statement(statement); |
| 250 |
CXLinker<librdf_statement*>* newStatementLinker = new CXLinker<librdf_statement*>(); |
| 251 |
newStatementLinker->initLinker(statementCopy, nullptr); |
| 252 |
if (statementLinker) { |
| 253 |
lastStatementLinker->setNext(newStatementLinker); |
| 254 |
lastStatementLinker = newStatementLinker; |
| 255 |
} else { |
| 256 |
statementLinker = newStatementLinker; |
| 257 |
lastStatementLinker = newStatementLinker; |
| 258 |
} |
| 259 |
if (mConfLoadTriplesIntoStore) { |
| 260 |
librdf_model_add_statement(tripleData->getRedlandIndexedModel(), statement); |
| 261 |
} else { |
| 262 |
if (!warningNoStore && (librdf_node_equals(critPredicate1, librdf_statement_get_predicate(statementCopy)) || librdf_node_equals(critPredicate2, librdf_statement_get_predicate(statementCopy)))) { |
| 263 |
warningNoStore = true; |
| 264 |
LOG(WARN, getLogDomain(), logTr("(Nontrivial) OWL axioms and expressions cannot be extracted from triples since they are not loaded into the triple store."), this); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
librdf_stream_next(tripleStream); |
| 269 |
} |
| 270 |
librdf_free_node(critPredicate1); |
| 271 |
librdf_free_node(critPredicate2); |
| 272 |
tripleData->setRedlandStatementLinker(statementLinker); |
| 273 |
} |
| 274 |
|
| 275 |
if (raptorParsingErrorString) { |
| 276 |
mParsingError = true; |
| 277 |
mErrorString = *raptorParsingErrorString; |
| 278 |
delete raptorParsingErrorString; |
| 279 |
raptorParsingErrorString = nullptr; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
if (raptor_stream) { |
| 284 |
raptor_free_iostream(raptor_stream); |
| 285 |
} |
| 286 |
|
| 287 |
if (parser) { |
| 288 |
librdf_free_parser(parser); |
| 289 |
} |
| 290 |
|
| 291 |
if (mParsingError) { |
| 292 |
delete tripleData; |
| 293 |
tripleData = nullptr; |
| 294 |
} else { |
| 295 |
mOntologyBuilder->addTriplesData(tripleData); |
| 296 |
} |
| 297 |
|
| 298 |
return !mParsingError; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
}; // end namespace Parser |
| 304 |
|
| 305 |
}; // end namespace Konclude |
| 306 |
|
| 307 |
#endif // !KONCLUDE_REDLAND_INTEGRATION |