Debian Codegraph @

Source/Parser/CSPARQLRedlandRasqalQueryParser.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 "CSPARQLRedlandRasqalQueryParser.h"
23
24
25 namespace Konclude {
26
27 namespace Parser {
28
29
30
31 CSPARQLRedlandRasqalQueryParser::CSPARQLRedlandRasqalQueryParser(CConcreteOntology* baseOntology, CConcreteOntology* ontology, CConfigurationBase* config) {
32 mBaseOntology = baseOntology;
33 mOntology = ontology;
34 mConfig = config;
35 mQuery = nullptr;
36 }
37
38
39 CSPARQLRedlandRasqalQueryParser::~CSPARQLRedlandRasqalQueryParser() {
40 }
41
42
43
44 bool CSPARQLRedlandRasqalQueryParser::parseQueryFile(const QString& filename) {
45 bool successfullParsed = true;
46
47 // parse file
48 QFile file(filename);
49 if (file.open(QIODevice::ReadOnly)) {
50
51 QStringList partStringList;
52 while (!file.atEnd()) {
53 QString line(file.readLine());
54 if (!line.isEmpty()) {
55 partStringList.append(line);
56 }
57 }
58 if (!partStringList.isEmpty()) {
59 processQueryText(partStringList.join("\r\n"));
60 }
61
62 file.close();
63
64 } else {
65 successfullParsed = false;
66 LOG(ERROR,"::Konclude::Parser::SPARQLSimpleQueryParser",logTr("File '%1' not found or couldn't be opened.").arg(filename),this);
67 }
68
69 return successfullParsed;
70 }
71
72 bool CSPARQLRedlandRasqalQueryParser::parseQueryTextList(const QStringList& queryTextList) {
73 return processQueryText(queryTextList.join(" "));
74 }
75
76 bool CSPARQLRedlandRasqalQueryParser::parseQueryText(const QString& filetext) {
77 return processQueryText(filetext);
78 }
79
80
81 bool CSPARQLRedlandRasqalQueryParser::parseQueryTextList(const QStringList& queryTextList, const QString& queryName) {
82 return processQueryText(queryTextList.join(" "), queryName);
83 }
84
85 CQuery* CSPARQLRedlandRasqalQueryParser::getQuery() {
86 return mQuery;
87 }
88
89
90 /** A query object */
91 struct librdf_query_s {
92 librdf_world *world;
93 int usage;
94 void *context;
95 struct librdf_query_factory_s* factory;
96
97 /* list of all the results for this query */
98 librdf_query_results* results;
99 };
100
101 typedef struct {
102 librdf_query *query; /* librdf query object */
103 librdf_model *model;
104 rasqal_query *rq;
105 rasqal_query_results *results;
106 char *language; /* rasqal query language name to use */
107 unsigned char *query_string;
108 librdf_uri *uri; /* base URI or NULL */
109
110 int errors;
111 int warnings;
112 } librdf_query_rasqal_context;
113
114
115
116
117 struct QueryGenerationData {
118 QSet<QString> additionalKBDependencies;
119 rasqal_world* rasqal_world_ptr;
120 };
121
122
123
124 int rasqal_graph_pattern_visit_fn(rasqal_query *query, rasqal_graph_pattern *gp, void *user_data) {
125 QueryGenerationData* queryGenData = (QueryGenerationData*)user_data;
126 rasqal_world* rasqal_world_ptr = queryGenData->rasqal_world_ptr;
127 raptor_world* raptor_world_ptr = rasqal_world_get_raptor(rasqal_world_ptr);
128
129 if (RASQAL_GRAPH_PATTERN_OPERATOR_BASIC == rasqal_graph_pattern_get_operator(gp)) {
130 raptor_sequence* tripleSeq = rasqal_graph_pattern_get_triples(query, gp);
131 // check if it can be processed by Konclude
132 bool supported = true;
133 //int seqSize = raptor_sequence_size(tripleSeq);
134 //for (int i = 0; i < seqSize; ++i) {
135 // rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(tripleSeq, i);
136 // if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->predicate)) {
137 // supported = false;
138 // } else if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) {
139 // if (RASQAL_LITERAL_URI == rasqal_literal_get_type(triple->predicate)) {
140 // const char* predicateString = (const char*)rasqal_literal_as_string(triple->predicate);
141 // if (strcmp(predicateString, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") == 0) {
142 // supported = false;
143 // }
144 // }
145 // }
146 //
147 //}
148
149 if (!supported) {
150 return -1;
151 }
152
153 } else if (RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH == rasqal_graph_pattern_get_operator(gp)) {
154 rasqal_literal* lit = rasqal_graph_pattern_get_origin(gp);
155 if (rasqal_literal_get_type(lit) == RASQAL_LITERAL_VARIABLE) {
156 return -1;
157 }
158 const unsigned char* graphUri = rasqal_literal_as_string(lit);
159 QString graphUriString = QString::fromUtf8((const char*)graphUri);
160 queryGenData->additionalKBDependencies.insert(graphUriString);
161 }
162
163 return 0;
164 }
165
166
167
168
169 void rasqal_log_handler_parsing_error_notifier(void *user_data, raptor_log_message *message) {
170 QStringList* messageList = (QStringList*)user_data;
171 if (message->level <= 50 && messageList) {
172 messageList->append(QString(QString("%1").arg(message->text)));
173 }
174 }
175
176
177
178 int redland_log_handler_parsing_error_notifier(void *user_data, librdf_log_message *message) {
179 QStringList* messageList = (QStringList*)user_data;
180 if (message->level <= 50 && messageList) {
181 messageList->append(QString(QString("%1").arg(message->message)));
182 }
183 return 1;
184 }
185
186
187
188
189 bool CSPARQLRedlandRasqalQueryParser::processQueryText(const QString& queryText, const QString& queryName) {
190
191 QList<CQuerySPARQLBasicGraphPatternExpression*> sparqlQueryList;
192
193 librdf_world* world;
194
195 world = librdf_new_world();
196 librdf_world_open(world);
197 rasqal_world* rasqal_world_ptr = librdf_world_get_rasqal(world);
198
199 QueryGenerationData queryGenFunColl;
200 queryGenFunColl.rasqal_world_ptr = rasqal_world_ptr;
201
202
203 QStringList messageList;
204 rasqal_world_set_log_handler(rasqal_world_ptr, &messageList, &rasqal_log_handler_parsing_error_notifier);
205 librdf_world_set_logger(world, &messageList, &redland_log_handler_parsing_error_notifier);
206
207 QByteArray queryByteDataArray = queryText.toUtf8();
208 const unsigned char* queryString = (const unsigned char *)queryByteDataArray.constData();
209 librdf_query* redlandQuery = librdf_new_query(world, "sparql", NULL, queryString, NULL);
210
211
212 rasqal_query *rq;
213
214
215 librdf_query_rasqal_context *context = (librdf_query_rasqal_context*)((librdf_query_s*)redlandQuery)->context;
216 int err = rasqal_query_prepare(context->rq, context->query_string, (raptor_uri*)context->uri);
217
218 rasqal_query *rasqalQuery = context->rq;
219 if (!err && rasqalQuery) {
220 if (rasqal_query_graph_pattern_visit2(rasqalQuery, rasqal_graph_pattern_visit_fn, &queryGenFunColl) == 0) {
221 queryGenFunColl.additionalKBDependencies.remove(mOntology->getOntologyName());
222 mQuery = new CRedlandRasqalBGPsCompositionQuery(mBaseOntology, mOntology, queryGenFunColl.additionalKBDependencies, queryText, world, redlandQuery, rasqalQuery, mConfig, queryName);
223 if (!messageList.isEmpty()) {
224 LOG(WARN, "Konclude::Parser::RedlandRasqalSPARQLQueryParser", logTr("Redland Rasqal query parsing and preparation warnings: '%1'.").arg(messageList.join("; ")), this);
225 }
226 return true;
227 } else {
228 LOG(ERROR, "Konclude::Parser::RedlandRasqalSPARQLQueryParser", logTr("Failed to create query because of unsupported variable/term usage in graph patterns."), this);
229 }
230 } else {
231 if (!messageList.isEmpty()) {
232 LOG(ERROR, "Konclude::Parser::RedlandRasqalSPARQLQueryParser", logTr("Query parsing and preparation failed due to Redland Rasqal parsing errors: '%1'.").arg(messageList.join("; ")), this);
233 } else {
234 LOG(ERROR, "Konclude::Parser::RedlandRasqalSPARQLQueryParser", logTr("Failed to create query because of unknown error."), this);
235 }
236 }
237
238 return false;
239 }
240
241
242
243
244 }; // end namespace Parser
245
246 }; // end namespace Konclude
247
248 #endif // KONCLUDE_REDLAND_INTEGRATION