| 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 "CSPARQLSimpleManagementParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CSPARQLSimpleManagementParser::CSPARQLSimpleManagementParser(CCommandDelegater *commandDelegater, const QString& kbName, CCommand* command) { |
| 31 |
mKeywordSet.insert("LOAD"); |
| 32 |
mKeywordSet.insert("CREATE"); |
| 33 |
mKeywordSet.insert("DROP"); |
| 34 |
mKeywordSet.insert("PREPARE"); |
| 35 |
mKeywordSet.insert("CONNECT"); |
| 36 |
|
| 37 |
mCommandDelegater = commandDelegater; |
| 38 |
mCommand = command; |
| 39 |
mKBName = kbName; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
CSPARQLSimpleManagementParser::~CSPARQLSimpleManagementParser() { |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
bool CSPARQLSimpleManagementParser::parseUpdateFile(const QString& filename) { |
| 49 |
bool successfullParsed = true; |
| 50 |
|
| 51 |
// parse file |
| 52 |
QFile file(filename); |
| 53 |
if (file.open(QIODevice::ReadOnly)) { |
| 54 |
|
| 55 |
QStringList partStringList; |
| 56 |
while (!file.atEnd()) { |
| 57 |
QString line(file.readLine()); |
| 58 |
cint64 linePos = 0; |
| 59 |
while (linePos != line.length()) { |
| 60 |
QString partString = getNextPart(line, linePos); |
| 61 |
if (!partString.isEmpty()) { |
| 62 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 63 |
processParts(partStringList); |
| 64 |
partStringList.clear(); |
| 65 |
} |
| 66 |
partStringList.append(partString); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
if (!partStringList.isEmpty()) { |
| 71 |
processParts(partStringList); |
| 72 |
} |
| 73 |
|
| 74 |
file.close(); |
| 75 |
|
| 76 |
} else { |
| 77 |
successfullParsed = false; |
| 78 |
LOG(ERROR,"::Konclude::Parser::SPARQLSimpleManagementParser",logTr("File '%1' not found or couldn't be opened.").arg(filename),this); |
| 79 |
} |
| 80 |
|
| 81 |
return successfullParsed; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
bool CSPARQLSimpleManagementParser::parseUpdateText(const QString& filetext) { |
| 86 |
QStringList partStringList; |
| 87 |
QString line(filetext); |
| 88 |
cint64 linePos = 0; |
| 89 |
while (linePos != line.length()) { |
| 90 |
QString partString = getNextPart(line, linePos); |
| 91 |
if (!partString.isEmpty()) { |
| 92 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 93 |
processParts(partStringList); |
| 94 |
partStringList.clear(); |
| 95 |
} |
| 96 |
partStringList.append(partString); |
| 97 |
} |
| 98 |
} |
| 99 |
if (!partStringList.isEmpty()) { |
| 100 |
processParts(partStringList); |
| 101 |
} |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
bool CSPARQLSimpleManagementParser::parseUpdateTextList(const QStringList& queryTextList) { |
| 107 |
QStringList partStringList; |
| 108 |
QStringList tmpQueryTextList = queryTextList; |
| 109 |
cint64 linePos = 0; |
| 110 |
while (!tmpQueryTextList.isEmpty()) { |
| 111 |
QString partString = tmpQueryTextList.takeFirst(); |
| 112 |
if (!partString.isEmpty()) { |
| 113 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 114 |
processParts(partStringList); |
| 115 |
partStringList.clear(); |
| 116 |
} |
| 117 |
partStringList.append(partString); |
| 118 |
} |
| 119 |
} |
| 120 |
if (!partStringList.isEmpty()) { |
| 121 |
processParts(partStringList); |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
bool CSPARQLSimpleManagementParser::processParts(QStringList& parsedParts) { |
| 130 |
if (parsedParts.size() > 0) { |
| 131 |
QString keyword = parsedParts.takeFirst().toUpper(); |
| 132 |
if (keyword == "BASE" || keyword == "@BASE") { |
| 133 |
mBaseIRI = ""; |
| 134 |
if (parsedParts.size() >= 1) { |
| 135 |
mBaseIRI = getIRI(parsedParts.takeFirst()); |
| 136 |
} |
| 137 |
} else if (keyword == "PREFIX" || keyword == "@PREFIX") { |
| 138 |
if (parsedParts.size() >= 2) { |
| 139 |
QString prefix = parsedParts.takeFirst(); |
| 140 |
while (prefix.endsWith(":")) { |
| 141 |
prefix = prefix.mid(0, prefix.length() - 1); |
| 142 |
} |
| 143 |
QString iri = getIRI(parsedParts.takeFirst()); |
| 144 |
mPrefixHash.insert(prefix, iri); |
| 145 |
} |
| 146 |
} else if (keyword == "LOAD") { |
| 147 |
|
| 148 |
bool silent = false; |
| 149 |
QString addressIRIString; |
| 150 |
QString graphIRIString; |
| 151 |
while (!parsedParts.isEmpty() && addressIRIString.isEmpty()) { |
| 152 |
QString partString = parsedParts.takeFirst(); |
| 153 |
|
| 154 |
QString upperPartString = partString.toUpper(); |
| 155 |
if (upperPartString != "SILENT") { |
| 156 |
if (addressIRIString.isEmpty()) { |
| 157 |
addressIRIString = getIRI(partString); |
| 158 |
} else { |
| 159 |
graphIRIString = getGraphName(partString, parsedParts, false); |
| 160 |
} |
| 161 |
} else { |
| 162 |
silent = true; |
| 163 |
} |
| 164 |
} |
| 165 |
if (graphIRIString.isEmpty()) { |
| 166 |
graphIRIString = mKBName; |
| 167 |
} |
| 168 |
if (!addressIRIString.isEmpty()) { |
| 169 |
CLoadKnowledgeBaseOWLAutoOntologyCommand* loadKBOntComm = new CLoadKnowledgeBaseOWLAutoOntologyCommand(graphIRIString, QStringList(addressIRIString)); |
| 170 |
if (mCommand) { |
| 171 |
mCommand->makeToSubCommand(loadKBOntComm); |
| 172 |
} |
| 173 |
mCommandDelegater->delegateCommand(loadKBOntComm); |
| 174 |
|
| 175 |
} |
| 176 |
} else if (keyword == "CREATE" || keyword == "DROP") { |
| 177 |
|
| 178 |
bool silent = false; |
| 179 |
QString graphIRIString; |
| 180 |
if (!parsedParts.isEmpty()) { |
| 181 |
QString partString = parsedParts.takeFirst(); |
| 182 |
|
| 183 |
QString upperPartString = partString.toUpper(); |
| 184 |
if (upperPartString == "SILENT") { |
| 185 |
silent = true; |
| 186 |
} else { |
| 187 |
graphIRIString = getGraphName(partString, parsedParts, false); |
| 188 |
} |
| 189 |
} |
| 190 |
if (graphIRIString.isEmpty()) { |
| 191 |
graphIRIString = mKBName; |
| 192 |
} |
| 193 |
if (keyword == "CREATE") { |
| 194 |
CCreateKnowledgeBaseCommand* loadKBOntComm = new CCreateKnowledgeBaseCommand(graphIRIString); |
| 195 |
if (mCommand) { |
| 196 |
mCommand->makeToSubCommand(loadKBOntComm); |
| 197 |
} |
| 198 |
mCommandDelegater->delegateCommand(loadKBOntComm); |
| 199 |
} else if (keyword == "DROP") { |
| 200 |
CReleaseKnowledgeBaseCommand* loadKBOntComm = new CReleaseKnowledgeBaseCommand(graphIRIString); |
| 201 |
if (mCommand) { |
| 202 |
mCommand->makeToSubCommand(loadKBOntComm); |
| 203 |
} |
| 204 |
mCommandDelegater->delegateCommand(loadKBOntComm); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} else if (keyword == "PREPARE") { |
| 210 |
|
| 211 |
bool querying = false; |
| 212 |
QString graphIRIString; |
| 213 |
if (!parsedParts.isEmpty()) { |
| 214 |
QString partString = parsedParts.takeFirst(); |
| 215 |
|
| 216 |
QString upperPartString = partString.toUpper(); |
| 217 |
if (upperPartString == "QUERYING") { |
| 218 |
querying = true; |
| 219 |
} else { |
| 220 |
graphIRIString = getGraphName(partString, parsedParts, false); |
| 221 |
} |
| 222 |
} |
| 223 |
if (graphIRIString.isEmpty()) { |
| 224 |
graphIRIString = mKBName; |
| 225 |
} |
| 226 |
|
| 227 |
CGetCurrentKnowledgeBaseRevisionCommand* getCurrentKBOntComm = new CGetCurrentKnowledgeBaseRevisionCommand(graphIRIString); |
| 228 |
if (mCommand) { |
| 229 |
mCommand->makeToSubCommand(getCurrentKBOntComm); |
| 230 |
} |
| 231 |
|
| 232 |
CPrepareKnowledgeBaseCommand* prepKBC = new CPrepareKnowledgeBaseCommand(getCurrentKBOntComm); |
| 233 |
prepKBC->addCommandPrecondition(new CCommandProcessedPrecondition(getCurrentKBOntComm)); |
| 234 |
if (mCommand) { |
| 235 |
mCommand->makeToSubCommand(prepKBC); |
| 236 |
} |
| 237 |
COntologyProcessingStepVector* ontProStepVec = COntologyProcessingStepVector::getProcessingStepVectorInstance(); |
| 238 |
prepKBC->addRequirement(new COntologyProcessingStepRequirement(ontProStepVec->getProcessingStep(COntologyProcessingStep::OPSINITREALIZE), COntologyProcessingStatus::PSCOMPLETELYYPROCESSED, 0, COntologyProcessingStatus::PSSUCESSFULL, 0)); |
| 239 |
prepKBC->addRequirement(new COntologyProcessingStepRequirement(ontProStepVec->getProcessingStep(COntologyProcessingStep::OPSANSWERCOMPLEXQUERY), COntologyProcessingStatus::PSCOMPLETELYYPROCESSED, 0, COntologyProcessingStatus::PSSUCESSFULL, 0)); |
| 240 |
|
| 241 |
|
| 242 |
mCommandDelegater->delegateCommand(getCurrentKBOntComm); |
| 243 |
mCommandDelegater->delegateCommand(prepKBC); |
| 244 |
|
| 245 |
} else if (keyword == "CONNECT") { |
| 246 |
|
| 247 |
bool silent = false; |
| 248 |
QString graphIRIString; |
| 249 |
QString triplesDBString; |
| 250 |
QString dsnConfigString; |
| 251 |
|
| 252 |
if (!parsedParts.isEmpty()) { |
| 253 |
triplesDBString = getGraphName(parsedParts.takeFirst(), parsedParts, true); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
while (!parsedParts.isEmpty() && (dsnConfigString.isEmpty())) { |
| 258 |
QString partString = parsedParts.takeFirst(); |
| 259 |
QString upperPartString = partString.toUpper(); |
| 260 |
|
| 261 |
if (upperPartString == "WITH") { |
| 262 |
if (!partString.isEmpty()) { |
| 263 |
dsnConfigString = parsedParts.takeFirst(); |
| 264 |
} |
| 265 |
} else { |
| 266 |
graphIRIString = getGraphName(partString, parsedParts, false); |
| 267 |
} |
| 268 |
|
| 269 |
if (!parsedParts.isEmpty()) { |
| 270 |
dsnConfigString = parsedParts.takeFirst(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if (dsnConfigString.startsWith("\"")) { |
| 276 |
dsnConfigString = dsnConfigString.mid(1); |
| 277 |
if (dsnConfigString.endsWith("\"")) { |
| 278 |
dsnConfigString = dsnConfigString.mid(0, dsnConfigString.length() - 1); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if (graphIRIString.isEmpty()) { |
| 283 |
graphIRIString = mKBName; |
| 284 |
} |
| 285 |
if (!dsnConfigString.isEmpty() && !triplesDBString.isEmpty()) { |
| 286 |
CConnectKnowledgeBaseExternalTriplesDSNCommand* connectExTrDSNComm = new CConnectKnowledgeBaseExternalTriplesDSNCommand(graphIRIString, triplesDBString, dsnConfigString); |
| 287 |
if (mCommand) { |
| 288 |
mCommand->makeToSubCommand(connectExTrDSNComm); |
| 289 |
} |
| 290 |
mCommandDelegater->delegateCommand(connectExTrDSNComm); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
} |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
QString CSPARQLSimpleManagementParser::getGraphName(const QString& currentPartString, QStringList& parsedParts, bool directGraphReference) { |
| 302 |
QString graphIRIString; |
| 303 |
QString currentPartStringUpper = currentPartString.toUpper(); |
| 304 |
if (directGraphReference || currentPartStringUpper == "FROM" || currentPartStringUpper == "TO" || currentPartStringUpper == "INTO" || currentPartStringUpper == "GRAPH") { |
| 305 |
if (directGraphReference || !parsedParts.isEmpty()) { |
| 306 |
QString graphString = currentPartString; |
| 307 |
QString graphStringUpper = graphString.toUpper(); |
| 308 |
if (!directGraphReference) { |
| 309 |
graphString = parsedParts.takeFirst(); |
| 310 |
graphStringUpper = graphString.toUpper(); |
| 311 |
} |
| 312 |
if (graphStringUpper == "DEFAULT") { |
| 313 |
} |
| 314 |
else { |
| 315 |
if (graphStringUpper == "GRAPH") { |
| 316 |
graphString = parsedParts.takeFirst(); |
| 317 |
} |
| 318 |
if (graphStringUpper == "NAMED") { |
| 319 |
if (!parsedParts.isEmpty()) { |
| 320 |
QString namedGraphString = parsedParts.takeFirst(); |
| 321 |
graphIRIString = getIRI(namedGraphString); |
| 322 |
} |
| 323 |
} else { |
| 324 |
graphIRIString = getIRI(graphString); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
return graphIRIString; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
}; // end namespace Parser |
| 335 |
|
| 336 |
}; // end namespace Konclude |