| 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 "CSPARQLRedlandRasqalReasoningBGPQueryParser.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Parser { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::CSPARQLRedlandRasqalReasoningBGPQueryParser(CSPARQLQueryBuilder *queryBuilder, COntologyBuilder* ontoBuilder, CConcreteOntology* ontology, CConfigurationBase* config) : CSPARQLExtendedMappingBuildingParser(ontoBuilder, ontology) { |
| 32 |
mQueryBuilder = queryBuilder; |
| 33 |
mConfig = config; |
| 34 |
mConfWriteQueryStructure = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Debugging.WriteRedlandRasqalQueryStructure", false); |
| 35 |
mConfForceDistinctSubQueries = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalReasoningSubQueriesForceDistinct", false); |
| 36 |
|
| 37 |
mConfMinimizeBGPTriples = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalMinimizeBGPSubQueryTriples", true); |
| 38 |
mConfPropagateBGPSubQueryDistinct = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalReasoningSubBGPQueryDistinctPropagation", true); |
| 39 |
mConfReduceBGPSubQueryAnswerVariables = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalReasoningSubBGPQueryAnswerVariableReduction", true); |
| 40 |
mConfAllowResultStreamingFromSingleSubQuery = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalSingleSubBGPQueryAllowsDirectResultStreaming", true); |
| 41 |
|
| 42 |
|
| 43 |
mConfSubQueriesDependencyIdentification = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalSubQueriesDependencyIdentification", true); |
| 44 |
mConfSubQueriesFollowingRestrictionsIntegration = CConfigDataReader::readConfigBoolean(mConfig, "Konclude.Answering.RedlandRasqalSubQueriesFollowingRestrictionsIntegration", true); |
| 45 |
|
| 46 |
mConfWriteSubQueryTriples = mConfWriteQueryStructure; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::~CSPARQLRedlandRasqalReasoningBGPQueryParser() { |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
struct rasqal_literal_s { |
| 56 |
rasqal_world *world; |
| 57 |
|
| 58 |
int usage; |
| 59 |
|
| 60 |
rasqal_literal_type type; |
| 61 |
|
| 62 |
/* UTF-8 string, pattern, qname, blank, double, float, decimal, datetime */ |
| 63 |
const unsigned char *string; |
| 64 |
unsigned int string_len; |
| 65 |
|
| 66 |
union { |
| 67 |
/* integer and boolean types */ |
| 68 |
int integer; |
| 69 |
/* double and float */ |
| 70 |
double floating; |
| 71 |
/* uri (can be temporarily NULL if a qname, see flags below) */ |
| 72 |
raptor_uri* uri; |
| 73 |
/* variable */ |
| 74 |
rasqal_variable* variable; |
| 75 |
/* decimal */ |
| 76 |
rasqal_xsd_decimal* decimal; |
| 77 |
/* datetime */ |
| 78 |
rasqal_xsd_datetime* datetime; |
| 79 |
/* date */ |
| 80 |
rasqal_xsd_date* date; |
| 81 |
} value; |
| 82 |
|
| 83 |
/* for string */ |
| 84 |
char *language; |
| 85 |
raptor_uri *datatype; |
| 86 |
|
| 87 |
/* various flags for literal types: |
| 88 |
* pattern regex flags |
| 89 |
* string datatype of qname |
| 90 |
* uri qname of URI not yet expanded (temporary) |
| 91 |
*/ |
| 92 |
const unsigned char *flags; |
| 93 |
|
| 94 |
rasqal_literal_type parent_type; |
| 95 |
|
| 96 |
int valid; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
QString getRasqalVariableString(rasqal_query *query, rasqal_variable* variable) { |
| 101 |
QString string; |
| 102 |
if (variable->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) { |
| 103 |
string = QString("_:") + QString::fromUtf8((const char*)variable->name); |
| 104 |
} else { |
| 105 |
string = QString("?") + QString::fromUtf8((const char*)variable->name); |
| 106 |
} |
| 107 |
return string; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
QString getRasqalLiteralString(rasqal_query *query, rasqal_literal* literal) { |
| 112 |
QString string; |
| 113 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(literal)) { |
| 114 |
rasqal_literal_s* var = (rasqal_literal_s*)literal; |
| 115 |
if (var->value.variable->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) { |
| 116 |
string = QString("_:") + QString::fromUtf8((const char*)var->value.variable->name); |
| 117 |
} else { |
| 118 |
string = QString("?") + QString::fromUtf8((const char*)var->value.variable->name); |
| 119 |
} |
| 120 |
// does not work somehow, seems to be a bug |
| 121 |
//string = QString::fromUtf8((const char*)rasqal_literal_as_string(literal)); |
| 122 |
} else if (RASQAL_LITERAL_URI == rasqal_literal_get_type(literal)) { |
| 123 |
string = QString::fromUtf8((const char*)rasqal_literal_as_string(literal)); |
| 124 |
} else if (RASQAL_LITERAL_STRING == rasqal_literal_get_type(literal)) { |
| 125 |
string = QString("\"%1\"").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))); |
| 126 |
} else if (RASQAL_LITERAL_XSD_STRING == rasqal_literal_get_type(literal)) { |
| 127 |
string = QString("\"%1\"^^xsd:string").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))); |
| 128 |
} else if (RASQAL_LITERAL_BOOLEAN == rasqal_literal_get_type(literal)) { |
| 129 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_BOOLEAN_DATATYPE); |
| 130 |
} else if (RASQAL_LITERAL_INTEGER == rasqal_literal_get_type(literal)) { |
| 131 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_INTEGER_DATATYPE); |
| 132 |
} else if (RASQAL_LITERAL_FLOAT == rasqal_literal_get_type(literal)) { |
| 133 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_FLOAT_DATATYPE); |
| 134 |
} else if (RASQAL_LITERAL_DOUBLE == rasqal_literal_get_type(literal)) { |
| 135 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_DOUBLE_DATATYPE); |
| 136 |
} else if (RASQAL_LITERAL_DECIMAL == rasqal_literal_get_type(literal)) { |
| 137 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_DECIMAL_DATATYPE); |
| 138 |
} else if (RASQAL_LITERAL_DATETIME == rasqal_literal_get_type(literal)) { |
| 139 |
string = QString("\"%1\"^^%2").arg(QString::fromUtf8((const char*)rasqal_literal_as_string(literal))).arg(PREFIX_XML_DATETIME_DATATYPE); |
| 140 |
} |
| 141 |
return string; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
typedef struct { |
| 147 |
rasqal_query* query; |
| 148 |
|
| 149 |
raptor_sequence* variables; |
| 150 |
|
| 151 |
unsigned int wildcard : 1; |
| 152 |
|
| 153 |
int distinct; |
| 154 |
} rasqal_projection; |
| 155 |
|
| 156 |
|
| 157 |
struct rasqal_query_s { |
| 158 |
rasqal_world* world; /* world object */ |
| 159 |
|
| 160 |
int usage; /* reference count - 1 for itself, plus for query_results */ |
| 161 |
|
| 162 |
unsigned char* query_string; |
| 163 |
size_t query_string_length; /* length including NULs */ |
| 164 |
|
| 165 |
raptor_namespace_stack* namespaces; |
| 166 |
|
| 167 |
/* query graph pattern, containing the sequence of graph_patterns below */ |
| 168 |
rasqal_graph_pattern* query_graph_pattern; |
| 169 |
|
| 170 |
/* the query verb - in SPARQL terms: SELECT, CONSTRUCT, DESCRIBE or ASK */ |
| 171 |
rasqal_query_verb verb; |
| 172 |
|
| 173 |
/* WAS: selects - sequence of rasqal_variable# */ |
| 174 |
raptor_sequence* unused10; /* ... rasqal_variable* names only */ |
| 175 |
|
| 176 |
/* sequences of ... */ |
| 177 |
raptor_sequence* data_graphs; /* ... rasqal_data_graph* */ |
| 178 |
/* NOTE: Cannot assume that triples are in any of |
| 179 |
* graph pattern use / query execution / document order |
| 180 |
*/ |
| 181 |
raptor_sequence* triples; /* ... rasqal_triple* */ |
| 182 |
raptor_sequence* prefixes; /* ... rasqal_prefix* */ |
| 183 |
raptor_sequence* constructs; /* ... rasqal_triple* SPARQL */ |
| 184 |
raptor_sequence* optional_triples; /* ... rasqal_triple* SPARQL */ |
| 185 |
raptor_sequence* describes; /* ... rasqal_literal* (var or URIs) SPARQL */ |
| 186 |
|
| 187 |
/* WAS: distinct 0..2; now projection->distinct */ |
| 188 |
unsigned int unused9; |
| 189 |
|
| 190 |
/* WAS: result limit LIMIT (>=0) or <0 if not given */ |
| 191 |
int unused4; |
| 192 |
|
| 193 |
/* WAS: result offset OFFSET (>=0) or <0 if not given */ |
| 194 |
int unused5; |
| 195 |
|
| 196 |
/* WAS: wildcard flag; now projection->wildcard */ |
| 197 |
int unused12; |
| 198 |
|
| 199 |
/* flag: non-0 if query has been prepared */ |
| 200 |
int prepared; |
| 201 |
|
| 202 |
rasqal_variables_table* vars_table; |
| 203 |
|
| 204 |
/* WAS: The number of selected variables: these are always the first |
| 205 |
* in the variables table and are the ones returned to the user. |
| 206 |
*/ |
| 207 |
int unused11; |
| 208 |
|
| 209 |
/* INTERNAL 2D array of: |
| 210 |
* width (number of total variables) |
| 211 |
* height (number of triples) |
| 212 |
* marking how a variable is mentioned/used in a TRIPLE PATTERN |
| 213 |
* Each triple pattern has a row and the short values are per-variable |
| 214 |
* with flags from #rasqal_triples_use_map_flags |
| 215 |
*/ |
| 216 |
unsigned short* triples_use_map; |
| 217 |
|
| 218 |
/* can be filled with error location information */ |
| 219 |
raptor_locator locator; |
| 220 |
|
| 221 |
/* base URI of this query for resolving relative URIs in queries */ |
| 222 |
raptor_uri* base_uri; |
| 223 |
|
| 224 |
/* non 0 if query had fatal error in parsing and cannot be executed */ |
| 225 |
int failed; |
| 226 |
|
| 227 |
/* stuff for our user */ |
| 228 |
void* user_data; |
| 229 |
|
| 230 |
/* former state for generating blank node IDs; now in world object */ |
| 231 |
int unused1; |
| 232 |
char *unused2; |
| 233 |
size_t unused3; |
| 234 |
|
| 235 |
/* query engine specific stuff */ |
| 236 |
void* context; |
| 237 |
|
| 238 |
struct rasqal_query_language_factory_s* factory; |
| 239 |
|
| 240 |
rasqal_triples_source_factory* triples_source_factory; |
| 241 |
|
| 242 |
/* sequence of query results made from this query */ |
| 243 |
raptor_sequence* results; |
| 244 |
|
| 245 |
/* incrementing counter for declaring prefixes in order of appearance */ |
| 246 |
int prefix_depth; |
| 247 |
|
| 248 |
/* WAS: sequence of order condition expressions */ |
| 249 |
void* unused6; |
| 250 |
|
| 251 |
/* WAS: sequence of group by condition expressions */ |
| 252 |
void* unused7; |
| 253 |
|
| 254 |
/* INTERNAL rasqal_literal_compare / rasqal_expression_evaluate flags */ |
| 255 |
int compare_flags; |
| 256 |
|
| 257 |
/* Number of graph patterns in this query */ |
| 258 |
int graph_pattern_count; |
| 259 |
|
| 260 |
/* Graph pattern shared pointers by gp index (after prepare) */ |
| 261 |
raptor_sequence* graph_patterns_sequence; |
| 262 |
|
| 263 |
/* Features */ |
| 264 |
int features[RASQAL_FEATURE_LAST + 1]; |
| 265 |
|
| 266 |
/* Name of requested query results syntax. If present, this |
| 267 |
* is the name used for constructing a rasqal_query_formatter |
| 268 |
* from the results. |
| 269 |
*/ |
| 270 |
char* query_results_formatter_name; |
| 271 |
|
| 272 |
/* flag: non-0 if EXPLAIN was given */ |
| 273 |
int explain; |
| 274 |
|
| 275 |
/* INTERNAL lexer internal data */ |
| 276 |
void* lexer_user_data; |
| 277 |
|
| 278 |
/* INTERNAL flag for now: non-0 to store results otherwise lazy eval results */ |
| 279 |
int store_results; |
| 280 |
|
| 281 |
/* INTERNAL 2D array of: |
| 282 |
* width (number of total variables) |
| 283 |
* height (number of graph patterns) |
| 284 |
* marking how a variable is mentioned/used in a GP |
| 285 |
* Each graph pattern has a row and the short values are per-variable |
| 286 |
* with flags: |
| 287 |
* 1 RASQAL_USE_IN_SCOPE |
| 288 |
* 2 RASQAL_USE_MENTIONED_HERE |
| 289 |
* 4 RASQAL_USE_BOUND_HERE |
| 290 |
*/ |
| 291 |
unsigned short* variables_use_map; |
| 292 |
|
| 293 |
/* sequence of #rasqal_update_operation when @verb is |
| 294 |
* INSERT (deprecated), DELETE (deprecated) or UPDATE |
| 295 |
*/ |
| 296 |
raptor_sequence* updates; |
| 297 |
|
| 298 |
/* WAS: sequence of (group by ...) having condition expressions */ |
| 299 |
raptor_sequence* unused8; |
| 300 |
|
| 301 |
/* INTERNAL solution modifier */ |
| 302 |
//rasqal_solution_modifier* modifier; |
| 303 |
void* modifier; |
| 304 |
|
| 305 |
/* INTERNAL SELECT bindings */ |
| 306 |
//rasqal_bindings* bindings; |
| 307 |
void* bindings; |
| 308 |
|
| 309 |
/* INTERNAL static structure for expression evaluation*/ |
| 310 |
rasqal_evaluation_context *eval_context; |
| 311 |
|
| 312 |
/* INTERNAL flag: non-0 if user set a random seed via RASQAL_FEATURE_RAND_SEED */ |
| 313 |
unsigned int user_set_rand : 1; |
| 314 |
|
| 315 |
/* Variable projection (or NULL when invalid such as for ASK) */ |
| 316 |
rasqal_projection* projection; |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
struct rasqal_graph_pattern_s { |
| 321 |
rasqal_query* query; |
| 322 |
|
| 323 |
/* operator for this graph pattern's contents */ |
| 324 |
rasqal_graph_pattern_operator op; |
| 325 |
|
| 326 |
raptor_sequence* triples; /* ... rasqal_triple* */ |
| 327 |
raptor_sequence* graph_patterns; /* ... rasqal_graph_pattern* */ |
| 328 |
|
| 329 |
int start_column; |
| 330 |
int end_column; |
| 331 |
|
| 332 |
/* the FILTER / LET expression */ |
| 333 |
rasqal_expression* filter_expression; |
| 334 |
|
| 335 |
/* index of the graph pattern in the query (0.. query->graph_pattern_count-1) */ |
| 336 |
int gp_index; |
| 337 |
|
| 338 |
/* Graph literal / SERVICE literal */ |
| 339 |
rasqal_literal *origin; |
| 340 |
|
| 341 |
/* Variable for LET graph pattern */ |
| 342 |
rasqal_variable *var; |
| 343 |
|
| 344 |
/* SELECT projection */ |
| 345 |
rasqal_projection* projection; |
| 346 |
|
| 347 |
/* SELECT modifiers */ |
| 348 |
//rasqal_solution_modifier* modifier; |
| 349 |
void* modifier; |
| 350 |
|
| 351 |
/* SILENT flag for SERVICE graph pattern */ |
| 352 |
unsigned int silent : 1; |
| 353 |
|
| 354 |
/* SELECT graph pattern: sequence of #rasqal_data_graph */ |
| 355 |
raptor_sequence* data_graphs; |
| 356 |
|
| 357 |
/* VALUES bindings for VALUES and sub-SELECT graph patterns */ |
| 358 |
//rasqal_bindings* bindings; |
| 359 |
void* bindings; |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
/** A query object */ |
| 364 |
struct librdf_query_s { |
| 365 |
librdf_world *world; |
| 366 |
int usage; |
| 367 |
void *context; |
| 368 |
struct librdf_query_factory_s* factory; |
| 369 |
|
| 370 |
/* list of all the results for this query */ |
| 371 |
librdf_query_results* results; |
| 372 |
}; |
| 373 |
|
| 374 |
typedef struct { |
| 375 |
librdf_query *query; /* librdf query object */ |
| 376 |
librdf_model *model; |
| 377 |
rasqal_query *rq; |
| 378 |
rasqal_query_results *results; |
| 379 |
char *language; /* rasqal query language name to use */ |
| 380 |
unsigned char *query_string; |
| 381 |
librdf_uri *uri; /* base URI or NULL */ |
| 382 |
|
| 383 |
int errors; |
| 384 |
int warnings; |
| 385 |
} librdf_query_rasqal_context; |
| 386 |
|
| 387 |
|
| 388 |
class QueryGenerationData { |
| 389 |
public: |
| 390 |
QString knowledgeBase; |
| 391 |
rasqal_world* rasqal_world_ptr = nullptr; |
| 392 |
bool defaultKnowledgeBase = false; |
| 393 |
QSet<rasqal_graph_pattern*> knowledgeBaseRelevantBGPsSet; |
| 394 |
function<cint64(rasqal_query *query, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData, QList<CRDFStringTriple>* tripleStringList, QStringList* varStringList, QHash<QString, cint64>* varStringUsageCountHash)> selectQueryGenerationFn; |
| 395 |
function<cint64(rasqal_query *query, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData, QList<CRDFStringTriple>* tripleStringList)> askQueryGenerationFn; |
| 396 |
|
| 397 |
|
| 398 |
QueryGenerationData(const QString& kb, bool defaultKB, const QSet<rasqal_graph_pattern*>& kbRelevantBGPsSet) { |
| 399 |
knowledgeBase = kb; |
| 400 |
defaultKnowledgeBase = defaultKB; |
| 401 |
rasqal_world_ptr = nullptr; |
| 402 |
knowledgeBaseRelevantBGPsSet = kbRelevantBGPsSet; |
| 403 |
} |
| 404 |
}; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
int get_connected_triples_and_variables(const QString startVariableString, const QSet<QString>& variableSet, QList<CRDFStringTriple*>& remainingTripleStringList, QSet<QString>& connectedVariableSet, QList<CRDFStringTriple*>& connectedTripleStringList) { |
| 412 |
int connectedVariableCount = 0; |
| 413 |
QHash<QString, cint64> variableTriplePosHash; |
| 414 |
cint64 triplePos = 0; |
| 415 |
for (CRDFStringTriple* stringTriple : remainingTripleStringList) { |
| 416 |
for (cint64 i = 0; stringTriple && i < 3; ++i) { |
| 417 |
const QString& term = stringTriple->string[i]; |
| 418 |
if (variableSet.contains(term)) { |
| 419 |
variableTriplePosHash.insertMulti(term, triplePos); |
| 420 |
} |
| 421 |
} |
| 422 |
triplePos++; |
| 423 |
} |
| 424 |
|
| 425 |
// handle blank nodes |
| 426 |
|
| 427 |
QList<cint64> processingTripleIds = variableTriplePosHash.values(startVariableString); |
| 428 |
if (!processingTripleIds.isEmpty()) { |
| 429 |
connectedVariableSet.insert(startVariableString); |
| 430 |
QSet<cint64> processedTripleIds; |
| 431 |
while (!processingTripleIds.isEmpty()) { |
| 432 |
cint64 tripleId = processingTripleIds.takeFirst(); |
| 433 |
if (!processedTripleIds.contains(tripleId)) { |
| 434 |
processedTripleIds.insert(tripleId); |
| 435 |
|
| 436 |
CRDFStringTriple* stringTriple = remainingTripleStringList.at(tripleId); |
| 437 |
remainingTripleStringList.replace(tripleId, nullptr); |
| 438 |
connectedTripleStringList.replace(tripleId, stringTriple); |
| 439 |
for (cint64 i = 0; i < 3; ++i) { |
| 440 |
const QString& term = stringTriple->string[i]; |
| 441 |
if (variableSet.contains(term) && !connectedVariableSet.contains(term)) { |
| 442 |
connectedVariableSet.insert(term); |
| 443 |
processingTripleIds.append(variableTriplePosHash.values(term)); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
return connectedVariableSet.count(); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::CBGPSubQueryGenerationData* CSPARQLRedlandRasqalReasoningBGPQueryParser::createSubQueryGenerationData(rasqal_query *query, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData) { |
| 458 |
CBGPSubQueryGenerationData* genData = new CBGPSubQueryGenerationData(); |
| 459 |
genData->bgpData = bgpData; |
| 460 |
genData->bgpData->subQueryId = mNextSubQueryId++; |
| 461 |
genData->subQueryId = genData->bgpData->subQueryId; |
| 462 |
mBGPSubQueryIdDataHash.insertMulti(bgpData->bgp, genData); |
| 463 |
mIdSubQueryIdDataHash.insert(genData->subQueryId, genData); |
| 464 |
return genData; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::identifyBGPBasedSubQueryTriples(rasqal_graph_pattern* gp) { |
| 472 |
|
| 473 |
raptor_sequence* tripleSeq = rasqal_graph_pattern_get_triples(mRasqalQuery, gp); |
| 474 |
// check if it can be processed by Konclude |
| 475 |
bool supported = true; |
| 476 |
int seqSize = raptor_sequence_size(tripleSeq); |
| 477 |
//for (int i = 0; i < seqSize; ++i) { |
| 478 |
// rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(tripleSeq, i); |
| 479 |
// if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->predicate)) { |
| 480 |
// supported = false; |
| 481 |
// } else if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) { |
| 482 |
// if (RASQAL_LITERAL_URI == rasqal_literal_get_type(triple->predicate)) { |
| 483 |
// const char* predicateString = (const char*)rasqal_literal_as_string(triple->predicate); |
| 484 |
// if (strcmp(predicateString, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") == 0) { |
| 485 |
// supported = false; |
| 486 |
// } |
| 487 |
// } |
| 488 |
// } |
| 489 |
|
| 490 |
//} |
| 491 |
|
| 492 |
if (supported) { |
| 493 |
QList<CRDFStringTriple*> tripleStringList; |
| 494 |
QHash<QString, rasqal_literal*> varStringLitHash; |
| 495 |
QHash<rasqal_literal*, QString> varLitStringHash; |
| 496 |
QHash<QString, cint64> varStringUsageCountHash; |
| 497 |
QStringList varStringList; |
| 498 |
for (int i = 0; i < seqSize; ++i) { |
| 499 |
rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(tripleSeq, i); |
| 500 |
CRDFStringTriple* stringTriple = new CRDFStringTriple(); |
| 501 |
stringTriple->string[0] = getRasqalLiteralString(mRasqalQuery, triple->subject); |
| 502 |
stringTriple->string[1] = getRasqalLiteralString(mRasqalQuery, triple->predicate); |
| 503 |
stringTriple->string[2] = getRasqalLiteralString(mRasqalQuery, triple->object); |
| 504 |
tripleStringList.append(stringTriple); |
| 505 |
|
| 506 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->subject)) { |
| 507 |
if (!varStringLitHash.contains(stringTriple->string[0])) { |
| 508 |
varStringList.append(stringTriple->string[0]); |
| 509 |
} |
| 510 |
varStringUsageCountHash[stringTriple->string[0]]++; |
| 511 |
varStringLitHash.insert(stringTriple->string[0], triple->subject); |
| 512 |
varLitStringHash.insert(triple->subject, stringTriple->string[0]); |
| 513 |
} |
| 514 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->predicate)) { |
| 515 |
if (!varStringLitHash.contains(stringTriple->string[1])) { |
| 516 |
varStringList.append(stringTriple->string[1]); |
| 517 |
} |
| 518 |
varStringUsageCountHash[stringTriple->string[1]]++; |
| 519 |
varStringLitHash.insert(stringTriple->string[1], triple->predicate); |
| 520 |
varLitStringHash.insert(triple->predicate, stringTriple->string[1]); |
| 521 |
} |
| 522 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) { |
| 523 |
if (!varStringLitHash.contains(stringTriple->string[2])) { |
| 524 |
varStringList.append(stringTriple->string[2]); |
| 525 |
} |
| 526 |
varStringUsageCountHash[stringTriple->string[2]]++; |
| 527 |
varStringLitHash.insert(stringTriple->string[2], triple->object); |
| 528 |
varLitStringHash.insert(triple->object, stringTriple->string[2]); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
QByteArray defaultURIBytes = QString("x").toUtf8(); |
| 538 |
|
| 539 |
rasqal_query_s* squery = (rasqal_query_s*)mRasqalQuery; |
| 540 |
raptor_sequence* queryTripleSeq = squery->triples; |
| 541 |
|
| 542 |
rasqal_graph_pattern_s *gps = (rasqal_graph_pattern_s*)gp; |
| 543 |
int triplesStartColumn = gps->start_column; |
| 544 |
int triplesEndColumn = gps->end_column; |
| 545 |
int triplesCount = triplesEndColumn - triplesStartColumn + 1; |
| 546 |
|
| 547 |
int minUsedTriplesStartColumn = gps->end_column; |
| 548 |
int maxUsedTriplesEndColumn = gps->start_column; |
| 549 |
|
| 550 |
QSet<QString> variableSet = varStringList.toSet(); |
| 551 |
QSet<QString> usedVariableSet; |
| 552 |
QSet<QString> remainVariableSet = variableSet; |
| 553 |
|
| 554 |
for (const QString& varString : varStringList) { |
| 555 |
if (!usedVariableSet.contains(varString)) { |
| 556 |
remainVariableSet.remove(varString); |
| 557 |
QSet<QString> connectedVariableSet; |
| 558 |
QList<CRDFStringTriple*> connectedTripleStringList; |
| 559 |
for (cint64 i = 0; i < triplesCount; ++i) { |
| 560 |
connectedTripleStringList.append(nullptr); |
| 561 |
} |
| 562 |
cint64 connectedVarCount = get_connected_triples_and_variables(varString, variableSet, tripleStringList, connectedVariableSet, connectedTripleStringList); |
| 563 |
usedVariableSet += connectedVariableSet; |
| 564 |
|
| 565 |
QSet<cint64> usedtriplePosSet; |
| 566 |
QList<CRDFStringTriple> convertingStringTriplesList; |
| 567 |
for (cint64 i = 0; i < triplesCount; ++i) { |
| 568 |
CRDFStringTriple* tripleString = connectedTripleStringList.at(i); |
| 569 |
if (tripleString) { |
| 570 |
usedtriplePosSet.insert(i); |
| 571 |
CRDFStringTriple convTripleString(*tripleString); |
| 572 |
convertingStringTriplesList.append(convTripleString); |
| 573 |
} |
| 574 |
} |
| 575 |
//if (connectedVariableSet.count() == variableSet.count()) { |
| 576 |
// // last variable, i.e., last query, also integrate fact entailment checking triples, e.g., A(a) |
| 577 |
// for (CRDFStringTriple* tripleString : tripleStringList) { |
| 578 |
// if (tripleString) { |
| 579 |
// CRDFStringTriple convTripleString(*tripleString); |
| 580 |
// convertingStringTriplesList.append(convTripleString); |
| 581 |
// } |
| 582 |
// } |
| 583 |
//} |
| 584 |
|
| 585 |
QSet<QString> distinguishedVariableSet; |
| 586 |
QStringList distinguishedVariableStringList; |
| 587 |
QStringList subQueryVarStringList; |
| 588 |
for (QString varString : varStringList) { |
| 589 |
if (connectedVariableSet.contains(varString)) { |
| 590 |
subQueryVarStringList.append(varString); |
| 591 |
|
| 592 |
QString nameOnlyReducedVarString = varString; |
| 593 |
if (nameOnlyReducedVarString.startsWith("$") || nameOnlyReducedVarString.startsWith("?")) { |
| 594 |
nameOnlyReducedVarString = nameOnlyReducedVarString.mid(1); |
| 595 |
|
| 596 |
cint64 count1 = 0; |
| 597 |
cint64 count2 = 0; |
| 598 |
if (!mQueryContainsSelectAll && mConfReduceBGPSubQueryAnswerVariables) { |
| 599 |
QString pattern = "(" + QRegularExpression::escape("?") + "|" + QRegularExpression::escape("$") + ")" + QRegularExpression::escape(nameOnlyReducedVarString) + "(?!\\w)"; |
| 600 |
count1 = mQueryText.count(QRegularExpression(pattern)); |
| 601 |
} |
| 602 |
cint64 bgpCount = varStringUsageCountHash.value("?" + nameOnlyReducedVarString) + varStringUsageCountHash.value("$" + nameOnlyReducedVarString); |
| 603 |
cint64 totalCount = count1 + count2; |
| 604 |
if (mQueryContainsSelectAll || mConfReduceBGPSubQueryAnswerVariables && totalCount > bgpCount) { |
| 605 |
distinguishedVariableSet.insert(varString); |
| 606 |
distinguishedVariableStringList.append(varString); |
| 607 |
} |
| 608 |
|
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData = new CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData(); |
| 615 |
bgpData->bgp = gp; |
| 616 |
bgpData->tripleStartPos = gps->end_column; |
| 617 |
bgpData->tripleEndPos = gps->start_column; |
| 618 |
|
| 619 |
CBGPSubQueryGenerationData* subQueryGenData = createSubQueryGenerationData(mRasqalQuery, bgpData); |
| 620 |
subQueryGenData->connectedTripleStringList = connectedTripleStringList; |
| 621 |
subQueryGenData->convertingStringTriplesList = convertingStringTriplesList; |
| 622 |
subQueryGenData->usedtriplePosSet = usedtriplePosSet; |
| 623 |
subQueryGenData->connectedVariableSet = connectedVariableSet; |
| 624 |
subQueryGenData->subQueryVarStringList = subQueryVarStringList; |
| 625 |
subQueryGenData->distinguishedVariableSet = distinguishedVariableSet; |
| 626 |
subQueryGenData->distinguishedVariableStringList = distinguishedVariableStringList; |
| 627 |
|
| 628 |
|
| 629 |
cint64 subQueryId = subQueryGenData->bgpData->subQueryId; |
| 630 |
|
| 631 |
|
| 632 |
QStringList::const_iterator varIt = distinguishedVariableStringList.constBegin(), varItEnd = distinguishedVariableStringList.constEnd(); |
| 633 |
QSet<QString> replacedVarLitStringSet; |
| 634 |
if (varIt != varItEnd) { |
| 635 |
QString varString = *varIt; |
| 636 |
|
| 637 |
bool startTriplePos = true; |
| 638 |
for (int i = 0; i < triplesCount && (varIt != varItEnd || !remainVariableSet.isEmpty()); ++i) { |
| 639 |
if (connectedTripleStringList.at(i)) { |
| 640 |
int triplePos = triplesStartColumn + i; |
| 641 |
rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(queryTripleSeq, triplePos); |
| 642 |
if (varIt != varItEnd) { |
| 643 |
minUsedTriplesStartColumn = qMin(minUsedTriplesStartColumn, triplePos); |
| 644 |
maxUsedTriplesEndColumn = qMax(maxUsedTriplesEndColumn, triplePos); |
| 645 |
} |
| 646 |
if (varIt != varItEnd || !remainVariableSet.isEmpty()) { |
| 647 |
bgpData->tripleStartPos = qMin(bgpData->tripleStartPos, triplePos); |
| 648 |
bgpData->tripleEndPos = qMax(bgpData->tripleEndPos, triplePos); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
cint64 encodedSubQueryId = subQueryId + 1; |
| 653 |
if (startTriplePos) { |
| 654 |
encodedSubQueryId = -encodedSubQueryId; |
| 655 |
startTriplePos = false; |
| 656 |
} |
| 657 |
triple->origin = rasqal_new_integer_literal(mRasqalWorld, RASQAL_LITERAL_INTEGER, encodedSubQueryId); |
| 658 |
|
| 659 |
|
| 660 |
//triple->origin = (rasqal_literal*)encodedSubQueryId; |
| 661 |
|
| 662 |
rasqal_literal* lastVarLit = varStringLitHash.value(varString); |
| 663 |
|
| 664 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->subject)) { |
| 665 |
QString varLitString = varLitStringHash.value(triple->subject); |
| 666 |
if (!replacedVarLitStringSet.contains(varLitString) && varLitString == varString) { |
| 667 |
replacedVarLitStringSet.insert(varLitString); |
| 668 |
triple->subject = lastVarLit; |
| 669 |
if (varIt != varItEnd) { |
| 670 |
++varIt; |
| 671 |
if (varIt != varItEnd) { |
| 672 |
varString = *varIt; |
| 673 |
lastVarLit = varStringLitHash.value(varString); |
| 674 |
} |
| 675 |
} |
| 676 |
} else { |
| 677 |
triple->subject = rasqal_new_uri_literal(mRasqalWorld, raptor_new_uri(mRaptorWorld, (const unsigned char*)defaultURIBytes.constData())); |
| 678 |
} |
| 679 |
} |
| 680 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) { |
| 681 |
QString varLitString = varLitStringHash.value(triple->object); |
| 682 |
if (!replacedVarLitStringSet.contains(varLitString) && varLitString == varString) { |
| 683 |
triple->object = lastVarLit; |
| 684 |
replacedVarLitStringSet.insert(varLitString); |
| 685 |
if (varIt != varItEnd) { |
| 686 |
++varIt; |
| 687 |
if (varIt != varItEnd) { |
| 688 |
varString = *varIt; |
| 689 |
lastVarLit = varStringLitHash.value(varString); |
| 690 |
} |
| 691 |
} |
| 692 |
} else { |
| 693 |
triple->object = rasqal_new_uri_literal(mRasqalWorld, raptor_new_uri(mRaptorWorld, (const unsigned char*)defaultURIBytes.constData())); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
} |
| 699 |
} |
| 700 |
} else { |
| 701 |
bool hasTriple = false; |
| 702 |
bool startTriplePos = true; |
| 703 |
for (int i = 0; i < triplesCount && (!hasTriple || !remainVariableSet.isEmpty()); ++i) { |
| 704 |
if (connectedTripleStringList.at(i)) { |
| 705 |
int triplePos = triplesStartColumn + i; |
| 706 |
minUsedTriplesStartColumn = qMin(minUsedTriplesStartColumn, triplePos); |
| 707 |
maxUsedTriplesEndColumn = qMax(maxUsedTriplesEndColumn, triplePos); |
| 708 |
|
| 709 |
bgpData->tripleStartPos = qMin(bgpData->tripleStartPos, triplePos); |
| 710 |
bgpData->tripleEndPos = qMax(bgpData->tripleEndPos, triplePos); |
| 711 |
|
| 712 |
rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(queryTripleSeq, triplePos); |
| 713 |
|
| 714 |
cint64 encodedSubQueryId = subQueryId + 1; |
| 715 |
if (startTriplePos) { |
| 716 |
encodedSubQueryId = -encodedSubQueryId; |
| 717 |
startTriplePos = false; |
| 718 |
} |
| 719 |
triple->origin = rasqal_new_integer_literal(mRasqalWorld, RASQAL_LITERAL_INTEGER, encodedSubQueryId); |
| 720 |
|
| 721 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->subject)) { |
| 722 |
hasTriple = true; |
| 723 |
if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) { |
| 724 |
triple->object = rasqal_new_uri_literal(mRasqalWorld, raptor_new_uri(mRaptorWorld, (const unsigned char*)defaultURIBytes.constData())); |
| 725 |
} |
| 726 |
} else if (RASQAL_LITERAL_VARIABLE == rasqal_literal_get_type(triple->object)) { |
| 727 |
hasTriple = true; |
| 728 |
} |
| 729 |
|
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
// separate query for fact entailment checking triples, e.g., A(a) |
| 742 |
QList<CRDFStringTriple> convertingStringTriplesList; |
| 743 |
QSet<cint64> usedtriplePosSet; |
| 744 |
cint64 i = 0; |
| 745 |
for (CRDFStringTriple* tripleString : tripleStringList) { |
| 746 |
if (tripleString) { |
| 747 |
usedtriplePosSet.insert(i); |
| 748 |
CRDFStringTriple convTripleString(*tripleString); |
| 749 |
convertingStringTriplesList.append(convTripleString); |
| 750 |
} |
| 751 |
++i; |
| 752 |
} |
| 753 |
if (!convertingStringTriplesList.isEmpty()) { |
| 754 |
QStringList emptyVarList; |
| 755 |
bool canExtendBeginning = minUsedTriplesStartColumn > gps->start_column; |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData* bgpData = new CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData(); |
| 760 |
bgpData->bgp = gp; |
| 761 |
bgpData->tripleStartPos = gps->end_column; |
| 762 |
bgpData->tripleEndPos = gps->start_column; |
| 763 |
|
| 764 |
CBGPSubQueryGenerationData* subQueryGenData = createSubQueryGenerationData(mRasqalQuery, bgpData); |
| 765 |
subQueryGenData->connectedTripleStringList = tripleStringList; |
| 766 |
subQueryGenData->convertingStringTriplesList = convertingStringTriplesList; |
| 767 |
subQueryGenData->usedtriplePosSet = usedtriplePosSet; |
| 768 |
|
| 769 |
cint64 subQueryId = subQueryGenData->bgpData->subQueryId; |
| 770 |
|
| 771 |
bool startTriplePos = true; |
| 772 |
for (int i = 0; i < triplesCount; ++i) { |
| 773 |
int triplePos = triplesStartColumn + i; |
| 774 |
if (triplePos >= minUsedTriplesStartColumn - 1 && canExtendBeginning || triplePos <= maxUsedTriplesEndColumn + 1 && !canExtendBeginning) { |
| 775 |
minUsedTriplesStartColumn = qMin(minUsedTriplesStartColumn, triplePos); |
| 776 |
maxUsedTriplesEndColumn = qMax(maxUsedTriplesEndColumn, triplePos); |
| 777 |
|
| 778 |
bgpData->tripleStartPos = qMin(bgpData->tripleStartPos, triplePos); |
| 779 |
bgpData->tripleEndPos = qMax(bgpData->tripleEndPos, triplePos); |
| 780 |
|
| 781 |
|
| 782 |
rasqal_triple* triple = (rasqal_triple*)raptor_sequence_get_at(queryTripleSeq, triplePos); |
| 783 |
cint64 encodedSubQueryId = subQueryId + 1; |
| 784 |
if (startTriplePos) { |
| 785 |
encodedSubQueryId = -encodedSubQueryId; |
| 786 |
startTriplePos = false; |
| 787 |
} |
| 788 |
triple->origin = rasqal_new_integer_literal(mRasqalWorld, RASQAL_LITERAL_INTEGER, encodedSubQueryId); |
| 789 |
|
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
if (mConfMinimizeBGPTriples) { |
| 796 |
if (maxUsedTriplesEndColumn < minUsedTriplesStartColumn) { |
| 797 |
gps->end_column = gps->start_column; |
| 798 |
} else { |
| 799 |
gps->start_column = minUsedTriplesStartColumn; |
| 800 |
gps->end_column = maxUsedTriplesEndColumn; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
} |
| 805 |
|
| 806 |
return true; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::identifyBGPBasedSubQueries(rasqal_graph_pattern* gp) { |
| 812 |
bool subQueryGenerated = false; |
| 813 |
bool kbRelevantTriples = false; |
| 814 |
if (mKnowledgeBaseRelevantBGPsSet.contains(gp) && rasqal_graph_pattern_get_operator(gp) == RASQAL_GRAPH_PATTERN_OPERATOR_BASIC) { |
| 815 |
kbRelevantTriples = true; |
| 816 |
} |
| 817 |
if (kbRelevantTriples) { |
| 818 |
subQueryGenerated |= identifyBGPBasedSubQueryTriples(gp); |
| 819 |
} |
| 820 |
|
| 821 |
raptor_sequence* subGrapPatternSeq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp); |
| 822 |
if (subGrapPatternSeq) { |
| 823 |
int subGrapPatternSeqSize = raptor_sequence_size(subGrapPatternSeq); |
| 824 |
int subBGPsSubQueryIdentificationCount = 0; |
| 825 |
for (int i = 0; i < subGrapPatternSeqSize; ++i) { |
| 826 |
rasqal_graph_pattern* subGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subGrapPatternSeq, i); |
| 827 |
if (identifyBGPBasedSubQueries(subGP)) { |
| 828 |
++subBGPsSubQueryIdentificationCount; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
if (mConfSubQueriesDependencyIdentification && rasqal_graph_pattern_get_operator(gp) == RASQAL_GRAPH_PATTERN_OPERATOR_GROUP && subBGPsSubQueryIdentificationCount >= 1 && subGrapPatternSeqSize >= 2) { |
| 833 |
|
| 834 |
for (int i = 1; i < subGrapPatternSeqSize; ++i) { |
| 835 |
rasqal_graph_pattern* subGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subGrapPatternSeq, i); |
| 836 |
QList<rasqal_graph_pattern*> subQueryBGPs; |
| 837 |
if (rasqal_graph_pattern_get_operator(subGP) == RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL) { |
| 838 |
raptor_sequence* optSubGrapPatternSeq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(subGP); |
| 839 |
if (optSubGrapPatternSeq) { |
| 840 |
int optSubGrapPatternSeqSize = raptor_sequence_size(optSubGrapPatternSeq); |
| 841 |
if (optSubGrapPatternSeqSize == 1) { |
| 842 |
rasqal_graph_pattern* optSubGP = (rasqal_graph_pattern*)raptor_sequence_get_at(optSubGrapPatternSeq, 0); |
| 843 |
subQueryBGPs.append(optSubGP); |
| 844 |
} |
| 845 |
} |
| 846 |
} else if (rasqal_graph_pattern_get_operator(subGP) == RASQAL_GRAPH_PATTERN_OPERATOR_UNION) { |
| 847 |
raptor_sequence* optSubGrapPatternSeq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp); |
| 848 |
if (optSubGrapPatternSeq) { |
| 849 |
int optSubGrapPatternSeqSize = raptor_sequence_size(optSubGrapPatternSeq); |
| 850 |
if (optSubGrapPatternSeqSize >= 1) { |
| 851 |
for (int j = 0; j < optSubGrapPatternSeqSize; ++j) { |
| 852 |
rasqal_graph_pattern* optSubGP = (rasqal_graph_pattern*)raptor_sequence_get_at(optSubGrapPatternSeq, j); |
| 853 |
subQueryBGPs.append(optSubGP); |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
} else if (rasqal_graph_pattern_get_operator(subGP) == RASQAL_GRAPH_PATTERN_OPERATOR_BASIC) { |
| 858 |
subQueryBGPs.append(subGP); |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
for (rasqal_graph_pattern* sgp : subQueryBGPs) { |
| 863 |
QList<CBGPSubQueryGenerationData*> subQueryBGPDataList = mBGPSubQueryIdDataHash.values(sgp); |
| 864 |
for (CBGPSubQueryGenerationData* subQueryBGPData : subQueryBGPDataList) { |
| 865 |
|
| 866 |
|
| 867 |
for (int j = i - 1; j >= 0; --j) { |
| 868 |
rasqal_graph_pattern* prevSubQueryBGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subGrapPatternSeq, j); |
| 869 |
QList<CBGPSubQueryGenerationData*> prevSubQueryBGPDataList = mBGPSubQueryIdDataHash.values(prevSubQueryBGP); |
| 870 |
for (CBGPSubQueryGenerationData* prevSubQueryBGPData : prevSubQueryBGPDataList) { |
| 871 |
|
| 872 |
bool commonVars = subQueryBGPData->distinguishedVariableSet.intersects(prevSubQueryBGPData->distinguishedVariableSet); |
| 873 |
if (commonVars) { |
| 874 |
subQueryBGPData->bgpData->dependentSubQueryIds.prepend(prevSubQueryBGPData->bgpData->subQueryId); |
| 875 |
} |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
if (mConfSubQueriesFollowingRestrictionsIntegration && rasqal_graph_pattern_get_operator(gp) == RASQAL_GRAPH_PATTERN_OPERATOR_GROUP) { |
| 887 |
for (int i = 0; i < subGrapPatternSeqSize - 1; ++i) { |
| 888 |
rasqal_graph_pattern* subGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subGrapPatternSeq, i); |
| 889 |
QList<CBGPSubQueryGenerationData*> subQueryBGPDataList = mBGPSubQueryIdDataHash.values(subGP); |
| 890 |
|
| 891 |
for (int j = i + 1; j < subGrapPatternSeqSize; ++j) { |
| 892 |
rasqal_graph_pattern* followingSubGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subGrapPatternSeq, j); |
| 893 |
QList<CBGPSubQueryGenerationData*> followingSubQueryBGPDataList = mBGPSubQueryIdDataHash.values(followingSubGP); |
| 894 |
|
| 895 |
|
| 896 |
for (CBGPSubQueryGenerationData* subQueryBGPData : subQueryBGPDataList) { |
| 897 |
|
| 898 |
for (CBGPSubQueryGenerationData* followingSubQueryBGPData : followingSubQueryBGPDataList) { |
| 899 |
|
| 900 |
bool commonVars = subQueryBGPData->distinguishedVariableSet.intersects(followingSubQueryBGPData->distinguishedVariableSet); |
| 901 |
commonVars |= subQueryBGPData->additionalIntegratedVariableSet.intersects(followingSubQueryBGPData->distinguishedVariableSet); |
| 902 |
if (commonVars) { |
| 903 |
integrateFollowingSubQueryRestrictions(subQueryBGPData, followingSubQueryBGPData); |
| 904 |
} |
| 905 |
|
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
} |
| 911 |
|
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
} |
| 916 |
|
| 917 |
return subQueryGenerated; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::integrateFollowingSubQueryRestrictions(CBGPSubQueryGenerationData* subQueryBGPData, CBGPSubQueryGenerationData* followingSubQueryBGPData) { |
| 922 |
subQueryBGPData->additionalIntegratingStringTriplesList.append(followingSubQueryBGPData->convertingStringTriplesList); |
| 923 |
|
| 924 |
for (QString varString : followingSubQueryBGPData->connectedVariableSet) { |
| 925 |
if (!subQueryBGPData->connectedVariableSet.contains(varString)) { |
| 926 |
subQueryBGPData->additionalIntegratedVariableSet.insert(varString); |
| 927 |
} |
| 928 |
} |
| 929 |
return true; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::generateBGPBasedSubQueries(const QStringList& projectedVarList) { |
| 937 |
if (mConfWriteSubQueryTriples) { |
| 938 |
QFile file("./Debugging/Answering/RASQAL-Sub-Query-Triples.txt"); |
| 939 |
if (file.open(QIODevice::WriteOnly)) { |
| 940 |
file.write(QString("Sub-Query Count: %1 \r\n").arg(mIdSubQueryIdDataHash.count()).toUtf8()); |
| 941 |
file.close(); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
for (QHash<cint64, CBGPSubQueryGenerationData*>::const_iterator it = mIdSubQueryIdDataHash.constBegin(), itEnd = mIdSubQueryIdDataHash.constEnd(); it != itEnd; ++it) { |
| 946 |
CBGPSubQueryGenerationData* subQueryGenData = it.value(); |
| 947 |
|
| 948 |
CRedlandRasqalBGPSubQueryData* bgpData = subQueryGenData->bgpData; |
| 949 |
QSet<CExpressionVariable*> varExpSet; |
| 950 |
QList<CRDFStringTriple> stringTriplesList = subQueryGenData->convertingStringTriplesList; |
| 951 |
stringTriplesList.append(subQueryGenData->additionalIntegratingStringTriplesList); |
| 952 |
|
| 953 |
QList<CAxiomExpression*> axiomList = getTripleOWLAxioms(&stringTriplesList, true, &varExpSet); |
| 954 |
|
| 955 |
if (mConfWriteSubQueryTriples) { |
| 956 |
QFile file("./Debugging/Answering/RASQAL-Sub-Query-Triples.txt"); |
| 957 |
if (file.open(QIODevice::Append)) { |
| 958 |
file.write(QString("\r\n\r\nSub-Query %1 \r\n").arg(subQueryGenData->bgpData->subQueryId).toUtf8()); |
| 959 |
for (CRDFStringTriple stringTriples : stringTriplesList) { |
| 960 |
file.write(stringTriples.string[0].toUtf8()); |
| 961 |
file.write(" "); |
| 962 |
file.write(stringTriples.string[1].toUtf8()); |
| 963 |
file.write(" "); |
| 964 |
file.write(stringTriples.string[2].toUtf8()); |
| 965 |
file.write(" .\r\n"); |
| 966 |
} |
| 967 |
file.close(); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
bool allIndiVars = true; |
| 973 |
bool allClassVars = true; |
| 974 |
bool allObjPropVars = true; |
| 975 |
bool allDataPropVars = true; |
| 976 |
|
| 977 |
bool hasIndiVars = false; |
| 978 |
bool hasClassVars = false; |
| 979 |
bool hasObjPropVars = false; |
| 980 |
bool hasDataPropVars = false; |
| 981 |
|
| 982 |
for (CExpressionVariable* var : varExpSet) { |
| 983 |
if (dynamic_cast<CClassVariableExpression*>(var)) { |
| 984 |
allIndiVars = false; |
| 985 |
allObjPropVars = false; |
| 986 |
allDataPropVars = false; |
| 987 |
hasClassVars = true; |
| 988 |
} else if (dynamic_cast<CIndividualVariableExpression*>(var)) { |
| 989 |
allClassVars = false; |
| 990 |
allObjPropVars = false; |
| 991 |
allDataPropVars = false; |
| 992 |
hasIndiVars = true; |
| 993 |
} else if (dynamic_cast<CObjectPropertyVariableExpression*>(var)) { |
| 994 |
allIndiVars = false; |
| 995 |
allClassVars = false; |
| 996 |
allDataPropVars = false; |
| 997 |
hasObjPropVars = true; |
| 998 |
} else if (dynamic_cast<CDataPropertyVariableExpression*>(var)) { |
| 999 |
allIndiVars = false; |
| 1000 |
allClassVars = false; |
| 1001 |
allObjPropVars = false; |
| 1002 |
hasDataPropVars = true; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
QHash<QString, CExpressionVariable*> disVarNameExpHash; |
| 1008 |
QList<CExpressionVariable*> ignoreCardVarExpList; |
| 1009 |
|
| 1010 |
for (CExpressionVariable* var : varExpSet) { |
| 1011 |
if (subQueryGenData->distinguishedVariableSet.contains("?" + var->getName()) || subQueryGenData->distinguishedVariableSet.contains("$" + var->getName())) { |
| 1012 |
disVarNameExpHash.insert("?" + var->getName(), var); |
| 1013 |
} else if (subQueryGenData->additionalIntegratedVariableSet.contains("?" + var->getName()) || subQueryGenData->additionalIntegratedVariableSet.contains("$" + var->getName())) { |
| 1014 |
ignoreCardVarExpList.append(var); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
QList<CExpressionVariable*> disVarList; |
| 1019 |
for (QStringList::iterator varIt = subQueryGenData->distinguishedVariableStringList.begin(); varIt != subQueryGenData->distinguishedVariableStringList.end(); ++varIt) { |
| 1020 |
QString varString(*varIt); |
| 1021 |
if (disVarNameExpHash.contains(varString)) { |
| 1022 |
disVarList.append(disVarNameExpHash.value(varString)); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
QList<CExpressionVariable*> sortedDisVarList; |
| 1027 |
sortedDisVarList.reserve(disVarList.size()); |
| 1028 |
QHash<QString, cint64> projVarPosHash; |
| 1029 |
cint64 nextProjVarPos = 0; |
| 1030 |
for (CExpressionVariable* disVar : disVarList) { |
| 1031 |
projVarPosHash.insert("?" + disVar->getName(),-1); |
| 1032 |
sortedDisVarList.append(nullptr); |
| 1033 |
} |
| 1034 |
for (const QString& projVarString : projectedVarList) { |
| 1035 |
if (projVarPosHash.contains(projVarString)) { |
| 1036 |
projVarPosHash.insert(projVarString, nextProjVarPos++); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
for (CExpressionVariable* disVar : disVarList) { |
| 1040 |
if (projVarPosHash.contains("?" + disVar->getName())) { |
| 1041 |
cint64 pos = projVarPosHash.value("?" + disVar->getName()); |
| 1042 |
if (pos >= 0) { |
| 1043 |
sortedDisVarList[pos] = disVar; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
for (CExpressionVariable* disVar : disVarList) { |
| 1048 |
if (projVarPosHash.value("?" + disVar->getName(), -1) < 0) { |
| 1049 |
sortedDisVarList[nextProjVarPos++] = disVar; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
cint64 limit = -1; |
| 1055 |
cint64 offset = 0; |
| 1056 |
if (mSingleBGPResultsDirectlyUsable && mConfPropagateBGPSubQueryDistinct) { |
| 1057 |
cint64 queryOffset = rasqal_query_get_offset(mRasqalQuery); |
| 1058 |
cint64 queryLimit = rasqal_query_get_limit(mRasqalQuery); |
| 1059 |
limit = queryLimit; |
| 1060 |
offset = queryOffset; |
| 1061 |
bgpData->completelyHandledResultsTransfer = true; |
| 1062 |
rasqal_query_set_offset(mRasqalQuery, 0); |
| 1063 |
if (queryLimit != -1) { |
| 1064 |
rasqal_query_set_limit(mRasqalQuery, 0); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
if (hasObjPropVars && allObjPropVars) { |
| 1071 |
if (!sortedDisVarList.isEmpty()) { |
| 1072 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-SELECT-Object-Properties-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1073 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternObjectPropertiesSelectQuery(axiomList, sortedDisVarList, limit, offset, subQueryName); |
| 1074 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1075 |
} else { |
| 1076 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-ASK-Object-Properties-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1077 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternObjectPropertiesAskQuery(axiomList, subQueryName); |
| 1078 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1079 |
} |
| 1080 |
} else if (hasDataPropVars && allDataPropVars) { |
| 1081 |
if (!sortedDisVarList.isEmpty()) { |
| 1082 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-SELECT-Data-Properties-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1083 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternDataPropertiesSelectQuery(axiomList, sortedDisVarList, limit, offset, subQueryName); |
| 1084 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1085 |
} else { |
| 1086 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-ASK-Data-Properties-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1087 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternDataPropertiesAskQuery(axiomList, subQueryName); |
| 1088 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1089 |
} |
| 1090 |
} else if (hasClassVars && allClassVars) { |
| 1091 |
if (!sortedDisVarList.isEmpty()) { |
| 1092 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-SELECT-Classes-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1093 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternClassSelectQuery(axiomList, sortedDisVarList, limit, offset, subQueryName); |
| 1094 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1095 |
} else { |
| 1096 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-ASK-Classes-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1097 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternClassAskQuery(axiomList, subQueryName); |
| 1098 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1099 |
} |
| 1100 |
} else if (hasIndiVars && allIndiVars || varExpSet.isEmpty()) { |
| 1101 |
|
| 1102 |
CEXPRESSIONLIST<COrderingTermExpression*> orderingList; |
| 1103 |
QList<CFilteringTermExpression*> filteringList; |
| 1104 |
|
| 1105 |
bool distinctModifier = mConfForceDistinctSubQueries; |
| 1106 |
if (!distinctModifier) { |
| 1107 |
if (mDistinctSubBGPsSet.contains(bgpData->bgp)) { |
| 1108 |
distinctModifier = true; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
if (!sortedDisVarList.isEmpty()) { |
| 1114 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-SELECT-Individuals-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1115 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternIndividualSelectQuery(axiomList, sortedDisVarList, ignoreCardVarExpList, distinctModifier, limit, offset, subQueryName); |
| 1116 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1117 |
} else { |
| 1118 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-ASK-Individuals-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1119 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternIndividualAskQuery(axiomList, filteringList, subQueryName); |
| 1120 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1121 |
} |
| 1122 |
} else if (hasIndiVars && (hasObjPropVars || hasDataPropVars || hasClassVars) || varExpSet.isEmpty()) { |
| 1123 |
|
| 1124 |
CEXPRESSIONLIST<COrderingTermExpression*> orderingList; |
| 1125 |
QList<CFilteringTermExpression*> filteringList; |
| 1126 |
|
| 1127 |
bool distinctModifier = mConfForceDistinctSubQueries; |
| 1128 |
if (!distinctModifier) { |
| 1129 |
if (mDistinctSubBGPsSet.contains(bgpData->bgp)) { |
| 1130 |
distinctModifier = true; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
if (!sortedDisVarList.isEmpty()) { |
| 1136 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-SELECT-Individuals-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1137 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternIndividualMixedSelectQuery(axiomList, sortedDisVarList, ignoreCardVarExpList, distinctModifier, limit, offset, subQueryName); |
| 1138 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1139 |
} else { |
| 1140 |
QString subQueryName = QString("Redland-Rasqal-BGP-Reasoning-ASK-Individuals-Sub-Query-%1").arg(subQueryGenData->bgpData->subQueryId); |
| 1141 |
CQuerySPARQLBasicGraphPatternExpression* sparqlQuery = mQueryBuilder->getSPARQLBasicGraphPatternIndividualAskQuery(axiomList, filteringList, subQueryName); |
| 1142 |
mBGPSubQueryNameHash.insert(subQueryName, bgpData); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
} |
| 1148 |
return true; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
bool rasqal_expression_is_aggregate(rasqal_expression* e) { |
| 1160 |
if (e->op == RASQAL_EXPR_COUNT || |
| 1161 |
e->op == RASQAL_EXPR_SUM || |
| 1162 |
e->op == RASQAL_EXPR_AVG || |
| 1163 |
e->op == RASQAL_EXPR_MIN || |
| 1164 |
e->op == RASQAL_EXPR_MAX || |
| 1165 |
e->op == RASQAL_EXPR_SAMPLE || |
| 1166 |
e->op == RASQAL_EXPR_GROUP_CONCAT) |
| 1167 |
return true; |
| 1168 |
|
| 1169 |
if (e->op != RASQAL_EXPR_FUNCTION) |
| 1170 |
return false; |
| 1171 |
|
| 1172 |
return (e->flags & RASQAL_EXPR_FLAG_AGGREGATE) != 0; |
| 1173 |
} |
| 1174 |
|
| 1175 |
int rasqal_algebra_extract_aggregate_expression_visit(void *user_data, rasqal_expression *e) { |
| 1176 |
|
| 1177 |
/* If an aggregate expression, return found */ |
| 1178 |
if (rasqal_expression_is_aggregate(e)) { |
| 1179 |
return 1; |
| 1180 |
} |
| 1181 |
|
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
bool hasProjectionAggregate(rasqal_projection* projection) { |
| 1189 |
if (projection) { |
| 1190 |
raptor_sequence* seq = projection->variables; |
| 1191 |
int seqSize = raptor_sequence_size(seq); |
| 1192 |
bool hasAggregate = false; |
| 1193 |
for (int i = 0; i < seqSize; ++i) { |
| 1194 |
rasqal_variable* v = (rasqal_variable*)raptor_sequence_get_at(seq, i); |
| 1195 |
rasqal_expression* expr = v->expression; |
| 1196 |
|
| 1197 |
if (!expr) |
| 1198 |
continue; |
| 1199 |
|
| 1200 |
if (rasqal_expression_visit(expr, rasqal_algebra_extract_aggregate_expression_visit, &hasAggregate)) { |
| 1201 |
return true; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
return false; |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
bool hasProjectionNonVariableExpression(rasqal_projection* projection) { |
| 1212 |
if (projection) { |
| 1213 |
raptor_sequence* seq = projection->variables; |
| 1214 |
int seqSize = raptor_sequence_size(seq); |
| 1215 |
bool hasAggregate = false; |
| 1216 |
for (int i = 0; i < seqSize; ++i) { |
| 1217 |
rasqal_variable* v = (rasqal_variable*)raptor_sequence_get_at(seq, i); |
| 1218 |
rasqal_expression* expr = v->expression; |
| 1219 |
|
| 1220 |
if (expr) { |
| 1221 |
return true; |
| 1222 |
} |
| 1223 |
|
| 1224 |
} |
| 1225 |
} |
| 1226 |
return false; |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::identifySubBGPsDistinctComputable(rasqal_graph_pattern* basicGrapPattern, QSet<rasqal_graph_pattern*>& distinctSubBGPsSet) { |
| 1232 |
|
| 1233 |
if (hasProjectionAggregate(((rasqal_graph_pattern_s*)basicGrapPattern)->projection)) { |
| 1234 |
return false; |
| 1235 |
} |
| 1236 |
|
| 1237 |
rasqal_graph_pattern_operator op = rasqal_graph_pattern_get_operator(basicGrapPattern); |
| 1238 |
if (op != RASQAL_GRAPH_PATTERN_OPERATOR_GROUP && op != RASQAL_GRAPH_PATTERN_OPERATOR_BASIC && op != RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL && op != RASQAL_GRAPH_PATTERN_OPERATOR_UNION) { |
| 1239 |
return false; |
| 1240 |
} |
| 1241 |
|
| 1242 |
rasqal_expression* filterExp = rasqal_graph_pattern_get_filter_expression(basicGrapPattern); |
| 1243 |
if (filterExp) { |
| 1244 |
return false; |
| 1245 |
} |
| 1246 |
|
| 1247 |
distinctSubBGPsSet.insert(basicGrapPattern); |
| 1248 |
raptor_sequence* subBGPSequence = rasqal_graph_pattern_get_sub_graph_pattern_sequence(basicGrapPattern); |
| 1249 |
if (subBGPSequence) { |
| 1250 |
int seqSize = raptor_sequence_size(subBGPSequence); |
| 1251 |
for (int i = 0; i < seqSize; ++i) { |
| 1252 |
rasqal_graph_pattern* subBGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subBGPSequence, i); |
| 1253 |
identifySubBGPsDistinctComputable(subBGP, distinctSubBGPsSet); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
return true; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::identifyHandlingKnowledgeBaseRelevantBGPs(rasqal_graph_pattern* grapPattern, bool rootPattern, QSet<rasqal_graph_pattern*>& kbRelevantBGPsSet, const QString& kbName, bool defaultKB) { |
| 1264 |
|
| 1265 |
rasqal_graph_pattern_operator op = rasqal_graph_pattern_get_operator(grapPattern); |
| 1266 |
if (rootPattern && op != RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH && defaultKB) { |
| 1267 |
kbRelevantBGPsSet.insert(grapPattern); |
| 1268 |
} |
| 1269 |
if (op == RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH) { |
| 1270 |
rasqal_literal* graphLit = rasqal_graph_pattern_get_origin(grapPattern); |
| 1271 |
const unsigned char* graphUrl = rasqal_literal_as_string(graphLit); |
| 1272 |
QString graphUrlString = QString::fromUtf8((const char*)graphUrl); |
| 1273 |
if (graphUrlString == kbName) { |
| 1274 |
kbRelevantBGPsSet.insert(grapPattern); |
| 1275 |
} |
| 1276 |
|
| 1277 |
} |
| 1278 |
|
| 1279 |
raptor_sequence* subBGPSequence = rasqal_graph_pattern_get_sub_graph_pattern_sequence(grapPattern); |
| 1280 |
if (subBGPSequence) { |
| 1281 |
int seqSize = raptor_sequence_size(subBGPSequence); |
| 1282 |
for (int i = 0; i < seqSize; ++i) { |
| 1283 |
rasqal_graph_pattern* subBGP = (rasqal_graph_pattern*)raptor_sequence_get_at(subBGPSequence, i); |
| 1284 |
|
| 1285 |
rasqal_graph_pattern_operator subOp = rasqal_graph_pattern_get_operator(subBGP); |
| 1286 |
if (subOp != RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH && kbRelevantBGPsSet.contains(grapPattern)) { |
| 1287 |
kbRelevantBGPsSet.insert(subBGP); |
| 1288 |
} |
| 1289 |
identifyHandlingKnowledgeBaseRelevantBGPs(subBGP, false, kbRelevantBGPsSet, kbName, defaultKB); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
return true; |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
QHash<QString, CSPARQLRedlandRasqalReasoningBGPQueryParser::CRedlandRasqalBGPSubQueryData*> CSPARQLRedlandRasqalReasoningBGPQueryParser::parseBGPReasoningSubQueries(librdf_world* world, librdf_query* redlandQuery, rasqal_query* rasqalQuery, const QString& queryText, const QString& knowledgeBase, bool defaultKnowledgeBase, cint64& nextSubQueryId) { |
| 1301 |
QList<CQuerySPARQLBasicGraphPatternExpression*> sparqlQueryList; |
| 1302 |
|
| 1303 |
mQueryText = queryText; |
| 1304 |
mRedlandWorld = world; |
| 1305 |
mRedlandQuery = redlandQuery; |
| 1306 |
mRasqalQuery = rasqalQuery; |
| 1307 |
mRasqalWorld = librdf_world_get_rasqal(world); |
| 1308 |
mRaptorWorld = rasqal_world_get_raptor(mRasqalWorld); |
| 1309 |
mNextSubQueryId = nextSubQueryId; |
| 1310 |
|
| 1311 |
rasqal_graph_pattern * basicGrapPattern = rasqal_query_get_query_graph_pattern(rasqalQuery); |
| 1312 |
|
| 1313 |
if (mConfWriteQueryStructure) { |
| 1314 |
raptor_iostream * raptorIoStream = raptor_new_iostream_to_filename(mRaptorWorld, "./Debugging/Answering/raptor-query.txt"); |
| 1315 |
rasqal_query_write(raptorIoStream, rasqalQuery, NULL, NULL); |
| 1316 |
raptor_iostream_write_end(raptorIoStream); |
| 1317 |
raptor_free_iostream(raptorIoStream); |
| 1318 |
|
| 1319 |
|
| 1320 |
FILE* fh = fopen("./Debugging/Answering/raptor-query-graph-pattern.txt", "w"); |
| 1321 |
rasqal_graph_pattern_print(basicGrapPattern, fh); |
| 1322 |
fclose(fh); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
bool queryDistinct = rasqal_query_get_distinct(rasqalQuery); |
| 1327 |
if (queryDistinct && mConfPropagateBGPSubQueryDistinct && !hasProjectionAggregate(((rasqal_query_s*)rasqalQuery)->projection)) { |
| 1328 |
identifySubBGPsDistinctComputable(basicGrapPattern, mDistinctSubBGPsSet); |
| 1329 |
} |
| 1330 |
|
| 1331 |
QStringList selectProjVarList; |
| 1332 |
raptor_sequence* projectVarSeq = rasqal_query_get_bound_variable_sequence(rasqalQuery); |
| 1333 |
if (projectVarSeq) { |
| 1334 |
int seqSize = raptor_sequence_size(projectVarSeq); |
| 1335 |
for (int i = 0; i < seqSize; ++i) { |
| 1336 |
rasqal_variable* v = (rasqal_variable*)raptor_sequence_get_at(projectVarSeq, i); |
| 1337 |
|
| 1338 |
QString varString = getRasqalVariableString(rasqalQuery, v); |
| 1339 |
if (!varString.isEmpty()) { |
| 1340 |
selectProjVarList.append(varString); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
mQueryContainsSelectAll = queryText.contains("*"); |
| 1347 |
|
| 1348 |
mSingleBGPResultsDirectlyUsable = mConfAllowResultStreamingFromSingleSubQuery; |
| 1349 |
mSingleBGPResultsDirectlyUsable = checkSingleBGPResultsDirectUsable(mSingleBGPResultsDirectlyUsable, basicGrapPattern, rasqalQuery); |
| 1350 |
|
| 1351 |
identifyHandlingKnowledgeBaseRelevantBGPs(basicGrapPattern, true, mKnowledgeBaseRelevantBGPsSet, knowledgeBase, defaultKnowledgeBase); |
| 1352 |
|
| 1353 |
identifyBGPBasedSubQueries(basicGrapPattern); |
| 1354 |
|
| 1355 |
generateBGPBasedSubQueries(selectProjVarList); |
| 1356 |
|
| 1357 |
if (mConfWriteQueryStructure) { |
| 1358 |
raptor_iostream * raptorIoStream = raptor_new_iostream_to_filename(mRaptorWorld, "./Debugging/Answering/raptor-query.txt"); |
| 1359 |
rasqal_query_write(raptorIoStream, rasqalQuery, NULL, NULL); |
| 1360 |
raptor_iostream_write_end(raptorIoStream); |
| 1361 |
raptor_free_iostream(raptorIoStream); |
| 1362 |
|
| 1363 |
FILE* fh = fopen("./Debugging/Answering/raptor-query-graph-pattern.txt", "w"); |
| 1364 |
rasqal_graph_pattern_print(basicGrapPattern, fh); |
| 1365 |
fclose(fh); |
| 1366 |
} |
| 1367 |
|
| 1368 |
nextSubQueryId = mNextSubQueryId; |
| 1369 |
|
| 1370 |
return mBGPSubQueryNameHash; |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
bool CSPARQLRedlandRasqalReasoningBGPQueryParser::checkSingleBGPResultsDirectUsable(bool singleBGPResultsDirectlyUsable, rasqal_graph_pattern * basicGrapPattern, rasqal_query* rasqalQuery) { |
| 1378 |
if (singleBGPResultsDirectlyUsable && rasqal_graph_pattern_get_operator(basicGrapPattern) != RASQAL_GRAPH_PATTERN_OPERATOR_BASIC) { |
| 1379 |
singleBGPResultsDirectlyUsable = false; |
| 1380 |
} |
| 1381 |
if (singleBGPResultsDirectlyUsable && rasqal_graph_pattern_get_filter_expression(basicGrapPattern)) { |
| 1382 |
singleBGPResultsDirectlyUsable = false; |
| 1383 |
} |
| 1384 |
if (singleBGPResultsDirectlyUsable && ((rasqal_graph_pattern_s*)basicGrapPattern)->projection) { |
| 1385 |
singleBGPResultsDirectlyUsable = false; |
| 1386 |
} |
| 1387 |
if (singleBGPResultsDirectlyUsable && ((rasqal_graph_pattern_s*)basicGrapPattern)->modifier) { |
| 1388 |
singleBGPResultsDirectlyUsable = false; |
| 1389 |
} |
| 1390 |
if (singleBGPResultsDirectlyUsable && hasProjectionNonVariableExpression(((rasqal_query_s*)rasqalQuery)->projection)) { |
| 1391 |
singleBGPResultsDirectlyUsable = false; |
| 1392 |
} |
| 1393 |
if (singleBGPResultsDirectlyUsable && rasqal_query_get_order_conditions_sequence(rasqalQuery)) { |
| 1394 |
singleBGPResultsDirectlyUsable = false; |
| 1395 |
} |
| 1396 |
if (singleBGPResultsDirectlyUsable) { |
| 1397 |
rasqal_query_verb verb = rasqal_query_get_verb(rasqalQuery); |
| 1398 |
if (verb != RASQAL_QUERY_VERB_SELECT && verb != RASQAL_QUERY_VERB_ASK) { |
| 1399 |
singleBGPResultsDirectlyUsable = false; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
if (singleBGPResultsDirectlyUsable && rasqal_query_get_group_conditions_sequence(rasqalQuery)) { |
| 1403 |
singleBGPResultsDirectlyUsable = false; |
| 1404 |
} |
| 1405 |
if (singleBGPResultsDirectlyUsable && rasqal_query_get_having_conditions_sequence(rasqalQuery)) { |
| 1406 |
singleBGPResultsDirectlyUsable = false; |
| 1407 |
} |
| 1408 |
return singleBGPResultsDirectlyUsable; |
| 1409 |
} |
| 1410 |
|
| 1411 |
}; // end namespace Parser |
| 1412 |
|
| 1413 |
}; // end namespace Konclude |
| 1414 |
|
| 1415 |
#endif // KONCLUDE_REDLAND_INTEGRATION |