| 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 |
|
| 21 |
#include "CSPARQLKnowledgeBaseSplittingOperationParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CSPARQLKnowledgeBaseSplittingOperationParser::CSPARQLKnowledgeBaseSplittingOperationParser() { |
| 31 |
mKeywordSet.insert("BASE"); |
| 32 |
mKeywordSet.insert("@BASE"); |
| 33 |
mKeywordSet.insert("PREFIX"); |
| 34 |
mKeywordSet.insert("@PREFIX"); |
| 35 |
mKeywordSet.insert("SELECT"); |
| 36 |
mKeywordSet.insert("CONSTRUCT"); |
| 37 |
mKeywordSet.insert("DESCRIBE"); |
| 38 |
mKeywordSet.insert("ASK"); |
| 39 |
mKeywordSet.insert("INSERT"); |
| 40 |
mKeywordSet.insert("LOAD"); |
| 41 |
mKeywordSet.insert("CREATE"); |
| 42 |
mKeywordSet.insert("DROP"); |
| 43 |
mKeywordSet.insert("PREPARE"); |
| 44 |
mKeywordSet.insert("CONNECT"); |
| 45 |
mSubKeywordSet.insert("SELECT"); |
| 46 |
|
| 47 |
mPrefixHash.insert("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
CSPARQLKnowledgeBaseSplittingOperationParser::~CSPARQLKnowledgeBaseSplittingOperationParser() { |
| 53 |
qDeleteAll(mDataContainer); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
bool CSPARQLKnowledgeBaseSplittingOperationParser::parseQueryFile(const QString& filename) { |
| 59 |
bool successfullParsed = true; |
| 60 |
|
| 61 |
// parse file |
| 62 |
QFile file(filename); |
| 63 |
if (file.open(QIODevice::ReadOnly)) { |
| 64 |
|
| 65 |
QStringList partStringList; |
| 66 |
while (!file.atEnd()) { |
| 67 |
QString line(file.readLine()); |
| 68 |
cint64 linePos = 0; |
| 69 |
cint64 bracketDepth = 0; |
| 70 |
while (linePos != line.length()) { |
| 71 |
QString partString = getNextPart(line, linePos); |
| 72 |
if (!partString.isEmpty()) { |
| 73 |
if (partString == "{") { |
| 74 |
bracketDepth++; |
| 75 |
} else if (partString == "}") { |
| 76 |
bracketDepth--; |
| 77 |
} |
| 78 |
if (isKeyword(partString) && (bracketDepth == 0 || !isSubKeyword(partString)) && !partStringList.isEmpty()) { |
| 79 |
processParts(partStringList); |
| 80 |
partStringList.clear(); |
| 81 |
bracketDepth = 0; |
| 82 |
} |
| 83 |
partStringList.append(partString); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
if (!partStringList.isEmpty()) { |
| 88 |
processParts(partStringList); |
| 89 |
} |
| 90 |
|
| 91 |
file.close(); |
| 92 |
|
| 93 |
} else { |
| 94 |
successfullParsed = false; |
| 95 |
LOG(ERROR,"::Konclude::Parser::SPARQLSimpleQueryParser",logTr("File '%1' not found or couldn't be opened.").arg(filename),this); |
| 96 |
} |
| 97 |
|
| 98 |
return successfullParsed; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
bool CSPARQLKnowledgeBaseSplittingOperationParser::parseQueryText(const QString& filetext) { |
| 103 |
QStringList partStringList; |
| 104 |
QString line(filetext); |
| 105 |
cint64 linePos = 0; |
| 106 |
cint64 bracketDepth = 0; |
| 107 |
while (linePos != line.length()) { |
| 108 |
QString partString = getNextPart(line, linePos); |
| 109 |
if (!partString.isEmpty()) { |
| 110 |
if (partString == "{") { |
| 111 |
bracketDepth++; |
| 112 |
} else if (partString == "}") { |
| 113 |
bracketDepth--; |
| 114 |
} |
| 115 |
if (isKeyword(partString) && (bracketDepth == 0 || !isSubKeyword(partString)) && !partStringList.isEmpty()) { |
| 116 |
processParts(partStringList); |
| 117 |
partStringList.clear(); |
| 118 |
bracketDepth = 0; |
| 119 |
} |
| 120 |
partStringList.append(partString); |
| 121 |
} |
| 122 |
} |
| 123 |
if (!partStringList.isEmpty()) { |
| 124 |
processParts(partStringList); |
| 125 |
} |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
bool CSPARQLKnowledgeBaseSplittingOperationParser::isKeyword(const QString& part) { |
| 132 |
return mKeywordSet.contains(part.toUpper()); |
| 133 |
} |
| 134 |
|
| 135 |
bool CSPARQLKnowledgeBaseSplittingOperationParser::isSubKeyword(const QString& part) { |
| 136 |
return mSubKeywordSet.contains(part.toUpper()); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
bool CSPARQLKnowledgeBaseSplittingOperationParser::processParts(QStringList& parsedParts) { |
| 141 |
if (parsedParts.size() > 0) { |
| 142 |
QString keyword = parsedParts.takeFirst().toUpper(); |
| 143 |
if (keyword == "BASE" || keyword == "@BASE") { |
| 144 |
mBaseIRI = ""; |
| 145 |
mCommonPartStringList.append(keyword); |
| 146 |
if (parsedParts.size() >= 1) { |
| 147 |
QString prefixIriString = parsedParts.takeFirst(); |
| 148 |
mBaseIRI = getIRI(prefixIriString); |
| 149 |
mCommonPartStringList.append(prefixIriString); |
| 150 |
} |
| 151 |
} else if (keyword == "PREFIX" || keyword == "@PREFIX") { |
| 152 |
mCommonPartStringList.append(keyword); |
| 153 |
if (parsedParts.size() >= 2) { |
| 154 |
QString prefix = parsedParts.takeFirst(); |
| 155 |
QString resolvedPrefix = prefix; |
| 156 |
while (resolvedPrefix.endsWith(":")) { |
| 157 |
resolvedPrefix = resolvedPrefix.mid(0, resolvedPrefix.length() - 1); |
| 158 |
} |
| 159 |
QString prefixIriString = parsedParts.takeFirst(); |
| 160 |
QString iri = getIRI(prefixIriString); |
| 161 |
mPrefixHash.insert(resolvedPrefix,iri); |
| 162 |
mCommonPartStringList.append(prefix); |
| 163 |
mCommonPartStringList.append(prefixIriString); |
| 164 |
} |
| 165 |
} else if (keyword == "SELECT" || keyword == "ASK") { |
| 166 |
QStringList queryStringList; |
| 167 |
queryStringList += mCommonPartStringList; |
| 168 |
queryStringList.append(keyword); |
| 169 |
QString fromIRIString; |
| 170 |
bool fromClauseFound = false; |
| 171 |
bool whereClauseStarted = false; |
| 172 |
while (!parsedParts.isEmpty() && !fromClauseFound && !whereClauseStarted) { |
| 173 |
QString partString = parsedParts.takeFirst(); |
| 174 |
queryStringList.append(partString); |
| 175 |
|
| 176 |
|
| 177 |
partString = partString.toUpper(); |
| 178 |
|
| 179 |
fromIRIString = getGraphName(partString, parsedParts, queryStringList, false); |
| 180 |
if (!fromIRIString.isEmpty()) { |
| 181 |
fromClauseFound = true; |
| 182 |
} |
| 183 |
|
| 184 |
if (partString == "{") { |
| 185 |
whereClauseStarted = true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
QStringList whereClauseStringList = getBracketContent(parsedParts, '{', '}', whereClauseStarted, true); |
| 191 |
queryStringList += whereClauseStringList; |
| 192 |
queryStringList += parsedParts; |
| 193 |
if (!mKnowledgeBaseQueryStringListHash.contains(fromIRIString)) { |
| 194 |
mKnowledgeBaseList.append(fromIRIString); |
| 195 |
} |
| 196 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_QUERY, fromIRIString, queryStringList)); |
| 197 |
|
| 198 |
} else if (keyword == "INSERT" || keyword == "DELETE") { |
| 199 |
QStringList updateStringList; |
| 200 |
updateStringList += mCommonPartStringList; |
| 201 |
updateStringList.append(keyword); |
| 202 |
QString graphIRIString; |
| 203 |
bool fromClauseFound = false; |
| 204 |
bool whereClauseStarted = false; |
| 205 |
bool whereClauseFinished = false; |
| 206 |
cint64 openBrackets = 0; |
| 207 |
while (!parsedParts.isEmpty() && !fromClauseFound && !whereClauseFinished) { |
| 208 |
QString partString = parsedParts.takeFirst(); |
| 209 |
updateStringList.append(partString); |
| 210 |
|
| 211 |
|
| 212 |
partString = partString.toUpper(); |
| 213 |
|
| 214 |
graphIRIString = getGraphName(partString, parsedParts, updateStringList, true); |
| 215 |
if (!graphIRIString.isEmpty()) { |
| 216 |
fromClauseFound = true; |
| 217 |
} |
| 218 |
|
| 219 |
if (partString == "{") { |
| 220 |
whereClauseStarted = true; |
| 221 |
++openBrackets; |
| 222 |
} else if (partString == "}") { |
| 223 |
openBrackets--; |
| 224 |
if (openBrackets <= 0) { |
| 225 |
whereClauseFinished = true; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if (!whereClauseFinished) { |
| 233 |
QStringList whereClauseStringList = getBracketContent(parsedParts, '{', '}', whereClauseStarted, false); |
| 234 |
updateStringList += whereClauseStringList; |
| 235 |
} |
| 236 |
if (!mKnowledgeBaseQueryStringListHash.contains(graphIRIString)) { |
| 237 |
mKnowledgeBaseList.append(graphIRIString); |
| 238 |
} |
| 239 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_UPDATE_MODIFY, graphIRIString, updateStringList)); |
| 240 |
|
| 241 |
} else if (keyword == "LOAD") { |
| 242 |
QStringList manageStringList; |
| 243 |
manageStringList += mCommonPartStringList; |
| 244 |
manageStringList.append(keyword); |
| 245 |
QString graphIRIString; |
| 246 |
QString addressIRIString; |
| 247 |
while (!parsedParts.isEmpty() && graphIRIString.isEmpty()) { |
| 248 |
QString partString = parsedParts.takeFirst(); |
| 249 |
manageStringList.append(partString); |
| 250 |
|
| 251 |
QString upperPartString = partString.toUpper(); |
| 252 |
if (upperPartString != "SILENT") { |
| 253 |
if (addressIRIString.isEmpty()) { |
| 254 |
addressIRIString = getIRI(partString); |
| 255 |
} else { |
| 256 |
graphIRIString = getGraphName(partString, parsedParts, manageStringList, false); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if (!mKnowledgeBaseQueryStringListHash.contains(graphIRIString)) { |
| 263 |
mKnowledgeBaseList.append(graphIRIString); |
| 264 |
} |
| 265 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_UPDATE_MANAGE, graphIRIString, manageStringList)); |
| 266 |
|
| 267 |
} else if (keyword == "CREATE" || keyword == "DROP") { |
| 268 |
QStringList manageStringList; |
| 269 |
manageStringList += mCommonPartStringList; |
| 270 |
manageStringList.append(keyword); |
| 271 |
QString graphIRIString; |
| 272 |
while (!parsedParts.isEmpty() && graphIRIString.isEmpty()) { |
| 273 |
QString partString = parsedParts.takeFirst(); |
| 274 |
manageStringList.append(partString); |
| 275 |
|
| 276 |
QString upperPartString = partString.toUpper(); |
| 277 |
if (upperPartString != "SILENT") { |
| 278 |
graphIRIString = getGraphName(partString, parsedParts, manageStringList, false); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if (!mKnowledgeBaseQueryStringListHash.contains(graphIRIString)) { |
| 283 |
mKnowledgeBaseList.append(graphIRIString); |
| 284 |
} |
| 285 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_UPDATE_MANAGE, graphIRIString, manageStringList)); |
| 286 |
|
| 287 |
} else if (keyword == "PREPARE") { |
| 288 |
QStringList manageStringList; |
| 289 |
manageStringList += mCommonPartStringList; |
| 290 |
manageStringList.append(keyword); |
| 291 |
QString graphIRIString; |
| 292 |
while (!parsedParts.isEmpty() && graphIRIString.isEmpty()) { |
| 293 |
QString partString = parsedParts.takeFirst(); |
| 294 |
manageStringList.append(partString); |
| 295 |
|
| 296 |
QString upperPartString = partString.toUpper(); |
| 297 |
if (upperPartString != "QUERYING") { |
| 298 |
graphIRIString = getGraphName(partString, parsedParts, manageStringList, false); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if (!mKnowledgeBaseQueryStringListHash.contains(graphIRIString)) { |
| 303 |
mKnowledgeBaseList.append(graphIRIString); |
| 304 |
} |
| 305 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_UPDATE_MANAGE, graphIRIString, manageStringList)); |
| 306 |
|
| 307 |
} else if (keyword == "CONNECT") { |
| 308 |
QStringList manageStringList; |
| 309 |
manageStringList += mCommonPartStringList; |
| 310 |
manageStringList.append(keyword); |
| 311 |
QString graphIRIString; |
| 312 |
while (!parsedParts.isEmpty() && graphIRIString.isEmpty()) { |
| 313 |
QString partString = parsedParts.takeFirst(); |
| 314 |
manageStringList.append(partString); |
| 315 |
|
| 316 |
QString upperPartString = partString.toUpper(); |
| 317 |
if (upperPartString != "WITH") { |
| 318 |
graphIRIString = getGraphName(partString, parsedParts, manageStringList, false); |
| 319 |
} else if (!parsedParts.isEmpty()) { |
| 320 |
manageStringList.append(parsedParts.takeFirst()); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (!mKnowledgeBaseQueryStringListHash.contains(graphIRIString)) { |
| 325 |
mKnowledgeBaseList.append(graphIRIString); |
| 326 |
} |
| 327 |
addKnowledgeBaseOperation(new CSPARQLKnowledgeBaseSplittingOperationData(CSPARQLKnowledgeBaseSplittingOperationData::SPARQL_UPDATE_MANAGE, graphIRIString, manageStringList)); |
| 328 |
|
| 329 |
} |
| 330 |
} |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
QString CSPARQLKnowledgeBaseSplittingOperationParser::getGraphName(const QString& currentPartString, QStringList& parsedParts, QStringList& updateStringList, bool directGraphReference) { |
| 336 |
QString graphIRIString; |
| 337 |
QString currentPartStringUpper = currentPartString.toUpper(); |
| 338 |
if (directGraphReference || currentPartStringUpper == "FROM" || currentPartStringUpper == "TO" || currentPartStringUpper == "INTO" || currentPartStringUpper == "GRAPH") { |
| 339 |
if (directGraphReference || !parsedParts.isEmpty()) { |
| 340 |
QString graphString = currentPartString; |
| 341 |
QString graphStringUpper = graphString.toUpper(); |
| 342 |
if (!directGraphReference) { |
| 343 |
graphString = parsedParts.takeFirst(); |
| 344 |
graphStringUpper = graphString.toUpper(); |
| 345 |
updateStringList.append(graphString); |
| 346 |
} |
| 347 |
if (graphStringUpper == "DEFAULT") { |
| 348 |
} else { |
| 349 |
if (graphStringUpper == "GRAPH") { |
| 350 |
graphString = parsedParts.takeFirst(); |
| 351 |
updateStringList.append(graphString); |
| 352 |
} |
| 353 |
if (graphStringUpper == "NAMED") { |
| 354 |
if (!parsedParts.isEmpty()) { |
| 355 |
QString namedGraphString = parsedParts.takeFirst(); |
| 356 |
updateStringList.append(namedGraphString); |
| 357 |
graphIRIString = getIRI(namedGraphString); |
| 358 |
} |
| 359 |
} else { |
| 360 |
graphIRIString = getIRI(graphString); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
return graphIRIString; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
CSPARQLKnowledgeBaseSplittingOperationParser* CSPARQLKnowledgeBaseSplittingOperationParser::addKnowledgeBaseOperation(CSPARQLKnowledgeBaseSplittingOperationData* kbOperationData) { |
| 370 |
mKnowledgeBaseQueryStringListHash.insertMulti(kbOperationData->getKnowledgeBaseString(), kbOperationData); |
| 371 |
mDataContainer.append(kbOperationData); |
| 372 |
return this; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
QStringList CSPARQLKnowledgeBaseSplittingOperationParser::getKnowlegdeBaseList() { |
| 377 |
return mKnowledgeBaseList; |
| 378 |
} |
| 379 |
|
| 380 |
QList<CSPARQLKnowledgeBaseSplittingOperationData*> CSPARQLKnowledgeBaseSplittingOperationParser::getKnowlegdeBaseSPARQLOperationList(const QString& knowledgeBase) { |
| 381 |
return mKnowledgeBaseQueryStringListHash.values(knowledgeBase); |
| 382 |
} |
| 383 |
|
| 384 |
QList<CSPARQLKnowledgeBaseSplittingOperationData*> CSPARQLKnowledgeBaseSplittingOperationParser::getSPARQLOperationList() { |
| 385 |
return mDataContainer; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
}; // end namespace Parser |
| 390 |
|
| 391 |
}; // end namespace Konclude |