| 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 "CKRSSOntologyParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CKRSSOntologyParser::CKRSSOntologyParser(COntologyBuilder* ontoBuilder) |
| 31 |
: CLogIdentifier("::Konclude::Parser::KRSSOntologyParser",this),recordRouter(0,this) { |
| 32 |
|
| 33 |
mOntoBuilder = ontoBuilder; |
| 34 |
LOG(INFO,getLogDomain(),logTr("Created KRSS Parser."),this); |
| 35 |
|
| 36 |
mDefName = QString("KRSSTestConcept"); |
| 37 |
mDefNumber = 1; |
| 38 |
|
| 39 |
#ifdef KONCLUDE_PARSER_USEPARSEJUMPFUNCTIONHASH |
| 40 |
initializeParseFunctionJumpHash(); |
| 41 |
#endif |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
CKRSSOntologyParser::CKRSSOntologyParser(COntologyBuilder* ontoBuilder, const QString& defNameString, CCommandRecordRouter *commandRecordRouter) |
| 46 |
: CLogIdentifier("::Konclude::Parser::KRSSOntologyParser",this),recordRouter(*commandRecordRouter,this) { |
| 47 |
|
| 48 |
mOntoBuilder = ontoBuilder; |
| 49 |
LOG(INFO,getLogDomain(),logTr("Created KRSS Parser."),this); |
| 50 |
|
| 51 |
mDefName = defNameString; |
| 52 |
if (mDefName.isEmpty()) { |
| 53 |
mDefName = "KRSSTestConcept"; |
| 54 |
} |
| 55 |
mDefNumber = 1; |
| 56 |
|
| 57 |
#ifdef KONCLUDE_PARSER_USEPARSEJUMPFUNCTIONHASH |
| 58 |
initializeParseFunctionJumpHash(); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
CKRSSOntologyParser::~CKRSSOntologyParser() { |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
bool CKRSSOntologyParser::parseOntologyFile(const QString& filename) { |
| 70 |
LOG(INFO,getLogDomain(),logTr("Loading KRSS File: '%1'.").arg(filename),this); |
| 71 |
bool successfullParsed = true; |
| 72 |
|
| 73 |
|
| 74 |
// parse file |
| 75 |
QFile file(filename); |
| 76 |
if (file.open(QIODevice::ReadOnly)) { |
| 77 |
while (!file.atEnd()) { |
| 78 |
QByteArray krssLine = file.readLine(); |
| 79 |
parseKRSSLine(krssLine); |
| 80 |
} |
| 81 |
|
| 82 |
file.close(); |
| 83 |
} else { |
| 84 |
successfullParsed = false; |
| 85 |
LOG(ERROR,getLogDomain(),logTr("File '%1' not found or couldn't be opened.").arg(filename),this); |
| 86 |
} |
| 87 |
|
| 88 |
return successfullParsed; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
bool CKRSSOntologyParser::parseOntologyText(const QString& text) { |
| 93 |
bool successfullParsed = false; |
| 94 |
QStringList list(text.split("\n")); |
| 95 |
cint64 listCount = list.count(); |
| 96 |
for (cint64 i = 0; i < listCount; ++i) { |
| 97 |
QString string = list.at(i); |
| 98 |
parseKRSSLine(string); |
| 99 |
} |
| 100 |
return successfullParsed; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
CBuildExpression* CKRSSOntologyParser::parseKRSSLine(const QString& line) { |
| 106 |
CBuildExpression* buildExpression = nullptr; |
| 107 |
if (line.startsWith("(")) { |
| 108 |
cint64 index = 0; |
| 109 |
buildExpression = parseKRSSExpression(line,index); |
| 110 |
QString nextDefNameString = QString("%1%2").arg(mDefName).arg(mDefNumber++); |
| 111 |
CBuildExpression* classExp = mOntoBuilder->getClass(nextDefNameString); |
| 112 |
parseDeclarationNode(classExp); |
| 113 |
CAxiomExpression* subClassExp = mOntoBuilder->getSubClassOf(classExp,buildExpression); |
| 114 |
mOntoBuilder->tellOntologyAxiom(subClassExp); |
| 115 |
} |
| 116 |
return buildExpression; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
CBuildExpression* CKRSSOntologyParser::parseKRSSExpression(const QString& line, cint64& index) { |
| 121 |
CBuildExpression* buildExpression = nullptr; |
| 122 |
cint64 beginIndex = index; |
| 123 |
cint64 clamInc = 0; |
| 124 |
while (index < line.size() && line[(uint)index] == QChar('(')) { |
| 125 |
++clamInc; |
| 126 |
++index; |
| 127 |
} |
| 128 |
QString ident = parseIdentifierNode(line,index); |
| 129 |
|
| 130 |
ParseFunction function = mParseFunctionJumpHash.value(ident); |
| 131 |
if (function) { |
| 132 |
buildExpression = (this->*function)(line,index); |
| 133 |
} else { |
| 134 |
buildExpression = mOntoBuilder->getClass(ident); |
| 135 |
parseDeclarationNode(buildExpression); |
| 136 |
} |
| 137 |
|
| 138 |
index += clamInc; |
| 139 |
return buildExpression; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
bool CKRSSOntologyParser::initializeParseFunctionJumpHash() { |
| 145 |
|
| 146 |
mParseFunctionJumpHash.insert("AND",&CKRSSOntologyParser::parseObjectIntersectionOfNode); |
| 147 |
mParseFunctionJumpHash.insert("OR",&CKRSSOntologyParser::parseObjectUnionOfNode); |
| 148 |
mParseFunctionJumpHash.insert("SOME",&CKRSSOntologyParser::parseObjectSomeValuesFromNode); |
| 149 |
mParseFunctionJumpHash.insert("ALL",&CKRSSOntologyParser::parseObjectAllValuesFromNode); |
| 150 |
mParseFunctionJumpHash.insert("NOT",&CKRSSOntologyParser::parseObjectComplementOfNode); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
CDeclarationAxiomExpression* CKRSSOntologyParser::parseDeclarationNode(CBuildExpression* expression) { |
| 157 |
QList<CBuildExpression *> builds; |
| 158 |
builds.append(expression); |
| 159 |
CDeclarationAxiomExpression* declarationExpression = mOntoBuilder->getDeclaration(builds); |
| 160 |
mOntoBuilder->tellOntologyAxiom(declarationExpression); |
| 161 |
return declarationExpression; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
QString CKRSSOntologyParser::parseIdentifierNode(const QString& text, cint64& index) { |
| 166 |
QString entitiy; |
| 167 |
cint64 endIdentL = text.indexOf(" ",index); |
| 168 |
cint64 endIdentC = text.indexOf(")",index); |
| 169 |
|
| 170 |
cint64 endIdent = 0; |
| 171 |
if (endIdentL == -1) { |
| 172 |
endIdent = endIdentC; |
| 173 |
} else if (endIdentC == -1) { |
| 174 |
endIdent = endIdentL; |
| 175 |
} else { |
| 176 |
endIdent = qMin(endIdentL,endIdentC); |
| 177 |
} |
| 178 |
QString ident = text.mid(index,endIdent-index); |
| 179 |
index = endIdent; |
| 180 |
if (endIdent == endIdentL) { |
| 181 |
++index; |
| 182 |
} |
| 183 |
return ident; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool CKRSSOntologyParser::checkIdentifierNode(const QString& text, const QString& expectedString, cint64& index) { |
| 189 |
QString entitiy; |
| 190 |
cint64 clOff = 0; |
| 191 |
if (text[(uint)index] == QChar('(')) { |
| 192 |
++clOff; |
| 193 |
} |
| 194 |
cint64 endIdentL = text.indexOf(" ",index+clOff); |
| 195 |
cint64 endIdentC = text.indexOf(")",index+clOff); |
| 196 |
|
| 197 |
cint64 endIdent = 0; |
| 198 |
if (endIdentL == -1) { |
| 199 |
endIdent = endIdentC; |
| 200 |
} else if (endIdentC == -1) { |
| 201 |
endIdent = endIdentL; |
| 202 |
} else { |
| 203 |
endIdent = qMin(endIdentL,endIdentC); |
| 204 |
} |
| 205 |
QString ident = text.mid(index+clOff,endIdent-index-clOff); |
| 206 |
bool expected = ident == expectedString; |
| 207 |
if (expected) { |
| 208 |
index = endIdent; |
| 209 |
if (endIdent == endIdentL) { |
| 210 |
++index; |
| 211 |
} |
| 212 |
} |
| 213 |
return expected; |
| 214 |
} |
| 215 |
|
| 216 |
CObjectPropertyExpression *CKRSSOntologyParser::parseObjectPropertyNode(const QString& text, cint64& index) { |
| 217 |
CObjectPropertyExpression* propExpression = nullptr; |
| 218 |
QString ident = parseIdentifierNode(text,index); |
| 219 |
propExpression = mOntoBuilder->getObjectProberty(ident); |
| 220 |
parseDeclarationNode(propExpression); |
| 221 |
return propExpression; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
CClassTermExpression *CKRSSOntologyParser::parseClassNode(const QString& text, cint64& index) { |
| 226 |
CClassTermExpression* classExpression = nullptr; |
| 227 |
QString ident = parseIdentifierNode(text,index); |
| 228 |
if (ident == "*BOTTOM*") { |
| 229 |
classExpression = mOntoBuilder->getBottomClass(); |
| 230 |
} else if (ident == "*TOP*") { |
| 231 |
classExpression = mOntoBuilder->getTopClass(); |
| 232 |
} else { |
| 233 |
classExpression = mOntoBuilder->getClass(ident); |
| 234 |
parseDeclarationNode(classExpression); |
| 235 |
} |
| 236 |
return classExpression; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
QList<CBuildExpression *> CKRSSOntologyParser::parseClassChildNodes(const QString& text, cint64& index) { |
| 241 |
QList<CBuildExpression *> builds; |
| 242 |
while (index < text.size() && text[(uint)index] != QChar(')')) { |
| 243 |
if (text[(uint)index] == QChar(' ')) { |
| 244 |
++index; |
| 245 |
} else { |
| 246 |
CBuildExpression *expression = parseKRSSExpression(text,index); |
| 247 |
if (expression) { |
| 248 |
builds.append(expression); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
return builds; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
QList<CBuildExpression *> CKRSSOntologyParser::parseSpecialClassChildNodes(const QString& text, const QString& identicalChild, cint64& index) { |
| 257 |
QList<CBuildExpression *> builds; |
| 258 |
cint64 identDepth = 0; |
| 259 |
while (index < text.size() && (identDepth > 0 || text[(uint)index] != QChar(')'))) { |
| 260 |
if (text[(uint)index] == QChar(' ')) { |
| 261 |
++index; |
| 262 |
} else if (text[(uint)index] == QChar(')')) { |
| 263 |
--identDepth; |
| 264 |
++index; |
| 265 |
} else { |
| 266 |
if (checkIdentifierNode(text,identicalChild,index)) { |
| 267 |
++identDepth; |
| 268 |
} else { |
| 269 |
CBuildExpression *expression = parseKRSSExpression(text,index); |
| 270 |
if (expression) { |
| 271 |
builds.append(expression); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
return builds; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
QList<CBuildExpression* > CKRSSOntologyParser::parsePropertyClassChildNodes(const QString& text, cint64& index) { |
| 281 |
QList<CBuildExpression *> builds; |
| 282 |
CBuildExpression* properyExp = parseObjectPropertyNode(text,index); |
| 283 |
CBuildExpression* classExp = parseKRSSExpression(text,index); |
| 284 |
if (properyExp) { |
| 285 |
builds.append(properyExp); |
| 286 |
} |
| 287 |
if (classExp) { |
| 288 |
builds.append(classExp); |
| 289 |
} |
| 290 |
return builds; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
CBuildExpression *CKRSSOntologyParser::parseObjectIntersectionOfNode(const QString& text, cint64& index) { |
| 295 |
QList<CBuildExpression *> builds = parseSpecialClassChildNodes(text,"AND",index); |
| 296 |
CObjectIntersectionOfExpression *exp = mOntoBuilder->getObjectIntersectionOf(builds); |
| 297 |
return exp; |
| 298 |
} |
| 299 |
|
| 300 |
CBuildExpression *CKRSSOntologyParser::parseObjectUnionOfNode(const QString& text, cint64& index) { |
| 301 |
QList<CBuildExpression *> builds = parseSpecialClassChildNodes(text,"OR",index); |
| 302 |
CObjectUnionOfExpression *exp = mOntoBuilder->getObjectUnionOf(builds); |
| 303 |
return exp; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
CBuildExpression *CKRSSOntologyParser::parseObjectSomeValuesFromNode(const QString& text, cint64& index) { |
| 308 |
QList<CBuildExpression *> builds = parsePropertyClassChildNodes(text,index); |
| 309 |
CObjectSomeValuesFromExpression *exp = mOntoBuilder->getObjectSomeValuesFrom(builds); |
| 310 |
return exp; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
CBuildExpression *CKRSSOntologyParser::parseObjectAllValuesFromNode(const QString& text, cint64& index) { |
| 315 |
QList<CBuildExpression *> builds = parsePropertyClassChildNodes(text,index); |
| 316 |
CObjectAllValuesFromExpression *exp = mOntoBuilder->getObjectAllValuesFrom(builds); |
| 317 |
return exp; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
CBuildExpression *CKRSSOntologyParser::parseObjectComplementOfNode(const QString& text, cint64& index) { |
| 322 |
QList<CBuildExpression *> builds = parseClassChildNodes(text,index); |
| 323 |
CObjectComplementOfExpression *exp = mOntoBuilder->getObjectComplementOf(builds); |
| 324 |
return exp; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
CBuildExpression* CKRSSOntologyParser::jumpFunctionParseObjectIntersectionOfNode(const QString& text, cint64& index) { |
| 333 |
return parseObjectIntersectionOfNode(text,index); |
| 334 |
} |
| 335 |
|
| 336 |
CBuildExpression* CKRSSOntologyParser::jumpFunctionParseObjectUnionOfNode(const QString& text, cint64& index) { |
| 337 |
return parseObjectUnionOfNode(text,index); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
CBuildExpression* CKRSSOntologyParser::jumpFunctionParseObjectSomeValuesFromNode(const QString& text, cint64& index) { |
| 342 |
return parseObjectSomeValuesFromNode(text,index); |
| 343 |
} |
| 344 |
|
| 345 |
CBuildExpression* CKRSSOntologyParser::jumpFunctionParseObjectAllValuesFromNode(const QString& text, cint64& index) { |
| 346 |
return parseObjectAllValuesFromNode(text,index); |
| 347 |
} |
| 348 |
|
| 349 |
CBuildExpression* CKRSSOntologyParser::jumpFunctionParseObjectComplementOfNode(const QString& text, cint64& index) { |
| 350 |
return parseObjectComplementOfNode(text,index); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
}; // end namespace Parser |
| 357 |
|
| 358 |
}; // end namespace Konclude |