| 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 "COWL2RDFTurtleAssertionsSimpleParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
COWL2RDFTurtleAssertionsSimpleParser::COWL2RDFTurtleAssertionsSimpleParser(COntologyBuilder* ontoBuilder, CConcreteOntology* ontology) { |
| 31 |
mOntoBuilder = ontoBuilder; |
| 32 |
mOntology = ontology; |
| 33 |
|
| 34 |
mKeywordSet.insert("BASE"); |
| 35 |
mKeywordSet.insert("@BASE"); |
| 36 |
mKeywordSet.insert("PREFIX"); |
| 37 |
mKeywordSet.insert("@PREFIX"); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
COWL2RDFTurtleAssertionsSimpleParser::~COWL2RDFTurtleAssertionsSimpleParser() { |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
bool COWL2RDFTurtleAssertionsSimpleParser::isKeyword(const QString& part) { |
| 47 |
return mKeywordSet.contains(part.toUpper()); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
bool COWL2RDFTurtleAssertionsSimpleParser::parseOntology(const QString& fileString) { |
| 54 |
bool successfullParsed = true; |
| 55 |
|
| 56 |
// parse file |
| 57 |
QFile file(fileString); |
| 58 |
if (file.open(QIODevice::ReadOnly)) { |
| 59 |
successfullParsed = parseOntology(&file); |
| 60 |
} else { |
| 61 |
successfullParsed = false; |
| 62 |
LOG(ERROR, "::Konclude::Parser::OWL2RDFTurtleAssertionsSimpleParser", logTr("File '%1' not found or could not be opened.").arg(fileString), this); |
| 63 |
} |
| 64 |
|
| 65 |
return successfullParsed; |
| 66 |
} |
| 67 |
|
| 68 |
bool COWL2RDFTurtleAssertionsSimpleParser::parseOntology(QIODevice* ioDevice) { |
| 69 |
|
| 70 |
QStringList partStringList; |
| 71 |
while (!ioDevice->atEnd()) { |
| 72 |
QString line(ioDevice->readLine()); |
| 73 |
cint64 linePos = 0; |
| 74 |
while (linePos != line.length()) { |
| 75 |
QString partString = getNextPart(line, linePos); |
| 76 |
if (!partString.isEmpty()) { |
| 77 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 78 |
processParts(partStringList); |
| 79 |
partStringList.clear(); |
| 80 |
} |
| 81 |
partStringList.append(partString); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
if (!partStringList.isEmpty()) { |
| 86 |
processParts(partStringList); |
| 87 |
} |
| 88 |
|
| 89 |
ioDevice->close(); |
| 90 |
return !mParsingError; |
| 91 |
} |
| 92 |
|
| 93 |
QString COWL2RDFTurtleAssertionsSimpleParser::getErrorString() { |
| 94 |
return mLastParsingError; |
| 95 |
} |
| 96 |
|
| 97 |
bool COWL2RDFTurtleAssertionsSimpleParser::hasError() { |
| 98 |
return mParsingError; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
bool COWL2RDFTurtleAssertionsSimpleParser::parseUpdateText(const QString& filetext) { |
| 108 |
QStringList partStringList; |
| 109 |
QString line(filetext); |
| 110 |
cint64 linePos = 0; |
| 111 |
while (linePos != line.length()) { |
| 112 |
QString partString = getNextPart(line, linePos); |
| 113 |
if (!partString.isEmpty()) { |
| 114 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 115 |
processParts(partStringList); |
| 116 |
partStringList.clear(); |
| 117 |
} |
| 118 |
partStringList.append(partString); |
| 119 |
} |
| 120 |
} |
| 121 |
if (!partStringList.isEmpty()) { |
| 122 |
processParts(partStringList); |
| 123 |
} |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
bool COWL2RDFTurtleAssertionsSimpleParser::parseUpdateTextList(const QStringList& queryTextList) { |
| 129 |
QStringList partStringList; |
| 130 |
QStringList tmpQueryTextList = queryTextList; |
| 131 |
cint64 linePos = 0; |
| 132 |
while (!tmpQueryTextList.isEmpty()) { |
| 133 |
QString partString = tmpQueryTextList.takeFirst(); |
| 134 |
if (!partString.isEmpty()) { |
| 135 |
if (isKeyword(partString) && !partStringList.isEmpty()) { |
| 136 |
processParts(partStringList); |
| 137 |
partStringList.clear(); |
| 138 |
} |
| 139 |
partStringList.append(partString); |
| 140 |
} |
| 141 |
} |
| 142 |
if (!partStringList.isEmpty()) { |
| 143 |
processParts(partStringList); |
| 144 |
} |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
bool COWL2RDFTurtleAssertionsSimpleParser::processParts(QStringList& parsedParts) { |
| 152 |
while (parsedParts.size() > 0) { |
| 153 |
QString keyword = parsedParts.first().toUpper(); |
| 154 |
if (keyword == "BASE" || keyword == "@BASE") { |
| 155 |
parsedParts.removeFirst(); |
| 156 |
mBaseIRI = ""; |
| 157 |
if (parsedParts.size() >= 1) { |
| 158 |
mBaseIRI = getIRI(parsedParts.takeFirst()); |
| 159 |
} |
| 160 |
if (parsedParts.size() >= 1) { |
| 161 |
if (parsedParts.first() == ".") { |
| 162 |
parsedParts.takeFirst(); |
| 163 |
} |
| 164 |
} |
| 165 |
} else if (keyword == "PREFIX" || keyword == "@PREFIX") { |
| 166 |
parsedParts.removeFirst(); |
| 167 |
if (parsedParts.size() >= 2) { |
| 168 |
QString prefix = parsedParts.takeFirst(); |
| 169 |
while (prefix.endsWith(":")) { |
| 170 |
prefix = prefix.mid(0, prefix.length() - 1); |
| 171 |
} |
| 172 |
QString iri = getIRI(parsedParts.takeFirst()); |
| 173 |
mPrefixHash.insert(prefix, iri); |
| 174 |
} |
| 175 |
if (parsedParts.size() >= 1) { |
| 176 |
if (parsedParts.first() == ".") { |
| 177 |
parsedParts.takeFirst(); |
| 178 |
} |
| 179 |
} |
| 180 |
} else { |
| 181 |
|
| 182 |
QList<CRDFStringTriple>* subjectTripleHash = getRDFStringTriples(parsedParts); |
| 183 |
if (!mParsingError) { |
| 184 |
QList<CAxiomExpression*> axiomList = getTripleOWLAxioms(subjectTripleHash); |
| 185 |
delete subjectTripleHash; |
| 186 |
for (CAxiomExpression* axiom : axiomList) { |
| 187 |
mOntoBuilder->tellOntologyAxiom(axiom); |
| 188 |
} |
| 189 |
} |
| 190 |
parsedParts.clear(); |
| 191 |
|
| 192 |
} |
| 193 |
} |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
QString COWL2RDFTurtleAssertionsSimpleParser::getVariableName(const QString& variableName) { |
| 203 |
return variableName.mid(1); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
QList<CAxiomExpression*> COWL2RDFTurtleAssertionsSimpleParser::getTripleOWLAxioms(QList<CRDFStringTriple>* tripleStringList) { |
| 208 |
QList<CAxiomExpression*> axiomList; |
| 209 |
for (auto triple : *tripleStringList) { |
| 210 |
|
| 211 |
QString subjectString = triple.string[0]; |
| 212 |
CIndividualTermExpression* indiExpression = nullptr; |
| 213 |
if (subjectString.startsWith("_:")) { |
| 214 |
indiExpression = mOntoBuilder->getAnonymousIndividual(mOntoBuilder->getOntologyAnonymousIdentifier(mOntoBuilder->getOntologyName()), subjectString.mid(2)); |
| 215 |
} else { |
| 216 |
indiExpression = mOntoBuilder->getNamedIndividual(subjectString); |
| 217 |
} |
| 218 |
|
| 219 |
QString predicateString = triple.string[1]; |
| 220 |
QString objectString = triple.string[2]; |
| 221 |
|
| 222 |
if (predicateString == "a" || predicateString == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { |
| 223 |
CClassExpression* classExp = mOntoBuilder->getClass(objectString); |
| 224 |
CAxiomExpression* assExp = mOntoBuilder->getClassAssertion(indiExpression, classExp); |
| 225 |
axiomList.append(assExp); |
| 226 |
} else { |
| 227 |
if (!isDataProperty(predicateString, objectString)) { |
| 228 |
CIndividualTermExpression* otherIndiExpression = nullptr; |
| 229 |
if (objectString.startsWith("_:")) { |
| 230 |
otherIndiExpression = mOntoBuilder->getAnonymousIndividual(mOntoBuilder->getOntologyAnonymousIdentifier(mOntoBuilder->getOntologyName()), objectString.mid(2)); |
| 231 |
} else { |
| 232 |
otherIndiExpression = mOntoBuilder->getNamedIndividual(objectString); |
| 233 |
} |
| 234 |
CObjectPropertyExpression* propExp = mOntoBuilder->getObjectProberty(predicateString); |
| 235 |
CObjectPropertyAssertionExpression* propAss = mOntoBuilder->getObjectPropertyAssertion(indiExpression, propExp, otherIndiExpression); |
| 236 |
axiomList.append(propAss); |
| 237 |
} else { |
| 238 |
CDataLiteralTermExpression* dataRangeExpr = nullptr; |
| 239 |
QPair<QString, QString> literalDatatypeStringPair = extractDataLiteral(objectString); |
| 240 |
CDataLexicalValueExpression* literalValueExp = mOntoBuilder->getDataLexicalValue(literalDatatypeStringPair.first); |
| 241 |
CDatatypeExpression* datatypeExp = mOntoBuilder->getDatatype(literalDatatypeStringPair.second); |
| 242 |
dataRangeExpr = mOntoBuilder->getDataLiteral(literalValueExp,datatypeExp); |
| 243 |
CDataPropertyExpression* propExp = mOntoBuilder->getDataProberty(predicateString); |
| 244 |
CDataPropertyAssertionExpression* propAss = mOntoBuilder->getDataPropertyAssertion(indiExpression, propExp, dataRangeExpr); |
| 245 |
axiomList.append(propAss); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
return axiomList; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
bool COWL2RDFTurtleAssertionsSimpleParser::isDataProperty(const QString& propertyName, const QString& objectString) { |
| 254 |
if (objectString.startsWith("\"")) { |
| 255 |
return true; |
| 256 |
} |
| 257 |
return mOntology->getBuildData()->getDataPropertyEntityBuildHash()->contains(propertyName); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
}; // end namespace Parser |
| 264 |
|
| 265 |
}; // end namespace Konclude |