| 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 "CWriteOREFunctionalClassSubsumptionsHierarchyQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CWriteOREFunctionalClassSubsumptionsHierarchyQuery::CWriteOREFunctionalClassSubsumptionsHierarchyQuery(CConcreteOntology *premisingTaxonomyOntology, CConfigurationBase *configuration, const QString& outputFileString, const QString &superClassName, const QString &subClassHierarchyQueryName) |
| 32 |
: CTaxonomyPremisingQuery(premisingTaxonomyOntology,configuration) { |
| 33 |
mClassName = superClassName; |
| 34 |
mQueryName = subClassHierarchyQueryName; |
| 35 |
if (mClassName.isEmpty()) { |
| 36 |
mQueryString = QString("Get Subclass Hierarchy"); |
| 37 |
} else { |
| 38 |
mQueryString = QString("Get Subclass Hierarchy of class '%1'").arg(mClassName); |
| 39 |
} |
| 40 |
|
| 41 |
mUseAbbreviatedIRIs = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.AbbreviatedIRIs",false); |
| 42 |
mWriteDeclarations = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.WriteDeclarations",false); |
| 43 |
mWriteDirectEquivalences = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.WriteDirectEquivalences",false); |
| 44 |
|
| 45 |
mWriteSimpleTopImplications = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.WriteReducedTopSubclasses",false); |
| 46 |
mWriteSimpleBottomImplications = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.WriteReducedBottomSubclasses",true); |
| 47 |
mWriteSimpleInconsistency = CConfigDataReader::readConfigBoolean(configuration,"Konclude.ORE.ClassHierarchyResult.WriteReducedInconsistency",true); |
| 48 |
|
| 49 |
mTaxCalcError = false; |
| 50 |
mQueryConstructError = false; |
| 51 |
|
| 52 |
mBottomClassNameString = QString("http://www.w3.org/2002/07/owl#Nothing"); |
| 53 |
mTopClassNameString = QString("http://www.w3.org/2002/07/owl#Thing"); |
| 54 |
mOutputFileNameString = outputFileString; |
| 55 |
mQueryAnswered = false; |
| 56 |
} |
| 57 |
|
| 58 |
CQueryResult *CWriteOREFunctionalClassSubsumptionsHierarchyQuery::getQueryResult() { |
| 59 |
return nullptr; |
| 60 |
} |
| 61 |
|
| 62 |
CQuery* CWriteOREFunctionalClassSubsumptionsHierarchyQuery::addQueryError(CQueryError* queryError) { |
| 63 |
if (CQueryInconsitentOntologyError::hasInconsistentOntologyError(queryError)) { |
| 64 |
// write inconsistency |
| 65 |
if (!writeInconsistentHierarchyResult()) { |
| 66 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write class hierarchy to file '%1'.").arg(mOutputFileNameString))); |
| 67 |
} |
| 68 |
} |
| 69 |
CQuery::addQueryError(queryError); |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::forcedPathCreated(const QString& filePath) { |
| 74 |
QString path = filePath; |
| 75 |
if (path.contains("/") || path.contains("\\")) { |
| 76 |
int lastSlash = path.lastIndexOf("/"); |
| 77 |
int lastBackSlash = path.lastIndexOf("\\"); |
| 78 |
int lastSeparator = qMax(lastBackSlash,lastSlash); |
| 79 |
path = path.mid(0,lastSeparator); |
| 80 |
if (!path.isEmpty()) { |
| 81 |
QDir dir; |
| 82 |
dir.mkpath(path); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
bool CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeInconsistentHierarchyResult() { |
| 88 |
forcedPathCreated(mOutputFileNameString); |
| 89 |
QFile outputFile(mOutputFileNameString); |
| 90 |
if (outputFile.open(QIODevice::WriteOnly)) { |
| 91 |
writeOntologyStart(&outputFile); |
| 92 |
|
| 93 |
CConcept* mTopConcept = ontology->getTBox()->getTopConcept(); |
| 94 |
CConcept* mBottomConcept = ontology->getTBox()->getTopConcept(); |
| 95 |
|
| 96 |
CBOXSET<CConcept*>* activeClassConceptSet = ontology->getTBox()->getActiveClassConceptSet(); |
| 97 |
|
| 98 |
if (mWriteSimpleInconsistency) { |
| 99 |
writeSubClassRelation(mTopClassNameString,mBottomClassNameString,&outputFile); |
| 100 |
} else { |
| 101 |
if (mWriteDeclarations) { |
| 102 |
for (CBOXSET<CConcept*>::const_iterator it = activeClassConceptSet->constBegin(), itEnd = activeClassConceptSet->constEnd(); it != itEnd; ++it) { |
| 103 |
CConcept* concept(*it); |
| 104 |
QString className = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 105 |
if (mUseAbbreviatedIRIs) { |
| 106 |
className = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 107 |
} |
| 108 |
writeDeclaration(className,&outputFile); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (mWriteDirectEquivalences) { |
| 113 |
QStringList incClassNameList; |
| 114 |
for (CBOXSET<CConcept*>::const_iterator it = activeClassConceptSet->constBegin(), itEnd = activeClassConceptSet->constEnd(); it != itEnd; ++it) { |
| 115 |
CConcept* concept(*it); |
| 116 |
QString className = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 117 |
if (mUseAbbreviatedIRIs) { |
| 118 |
className = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 119 |
} |
| 120 |
incClassNameList.append(className); |
| 121 |
} |
| 122 |
writeEquivalenceRelations(incClassNameList,&outputFile); |
| 123 |
} else { |
| 124 |
for (CBOXSET<CConcept*>::const_iterator it1 = activeClassConceptSet->constBegin(), it1End = activeClassConceptSet->constEnd(); it1 != it1End; ++it1) { |
| 125 |
CConcept* concept1(*it1); |
| 126 |
if (concept1 != mBottomConcept) { |
| 127 |
for (CBOXSET<CConcept*>::const_iterator it2 = activeClassConceptSet->constBegin(), it2End = activeClassConceptSet->constEnd(); it2 != it1End; ++it2) { |
| 128 |
CConcept* concept2(*it2); |
| 129 |
if (concept1 != concept2 && concept2 != mTopConcept) { |
| 130 |
QString className1 = CIRIName::getRecentIRIName(concept1->getClassNameLinker()); |
| 131 |
QString className2 = CIRIName::getRecentIRIName(concept2->getClassNameLinker()); |
| 132 |
if (mUseAbbreviatedIRIs) { |
| 133 |
className1 = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept1->getClassNameLinker()); |
| 134 |
className2 = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept2->getClassNameLinker()); |
| 135 |
} |
| 136 |
writeSubClassRelation(className1,className2,&outputFile); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
writeOntologyEnd(&outputFile); |
| 145 |
outputFile.close(); |
| 146 |
return true; |
| 147 |
} |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
CQueryResult *CWriteOREFunctionalClassSubsumptionsHierarchyQuery::constructResult(CTaxonomy *taxonomy) { |
| 153 |
mQueryAnswered = true; |
| 154 |
if (!writeSubClassHierarchyResult(taxonomy)) { |
| 155 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write class hierarchy to file '%1'.").arg(mOutputFileNameString))); |
| 156 |
} |
| 157 |
return nullptr; |
| 158 |
} |
| 159 |
|
| 160 |
bool CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeSubClassHierarchyResult(CTaxonomy *taxonomy) { |
| 161 |
forcedPathCreated(mOutputFileNameString); |
| 162 |
QFile outputFile(mOutputFileNameString); |
| 163 |
if (outputFile.open(QIODevice::WriteOnly)) { |
| 164 |
|
| 165 |
writeOntologyStart(&outputFile); |
| 166 |
|
| 167 |
CHierarchyNode* topNode = taxonomy->getTopHierarchyNode(); |
| 168 |
CHierarchyNode* bottomNode = taxonomy->getBottomHierarchyNode(); |
| 169 |
CHierarchyNode* basicNode = topNode; |
| 170 |
if (!mClassName.isEmpty()) { |
| 171 |
basicNode = taxonomy->getHierarchyNode(ontology->getConcept(mClassName)); |
| 172 |
} |
| 173 |
writeDeclarations(bottomNode,&outputFile); |
| 174 |
writeBottomEquivalences(bottomNode,&outputFile); |
| 175 |
QSet<CHierarchyNode *> processNodeSet; |
| 176 |
processNodeSet.insert(bottomNode); |
| 177 |
if (basicNode) { |
| 178 |
QList<CHierarchyNode *> processNodeList; |
| 179 |
writeDeclarations(basicNode,&outputFile); |
| 180 |
writeTopEquivalences(basicNode,&outputFile); |
| 181 |
processNodeSet.insert(basicNode); |
| 182 |
processNodeList.append(basicNode); |
| 183 |
while (!processNodeList.isEmpty()) { |
| 184 |
CHierarchyNode *procNode = processNodeList.takeFirst(); |
| 185 |
QSet<CHierarchyNode *>* childNodeSet(procNode->getChildNodeSet()); |
| 186 |
foreach (CHierarchyNode *childNode, *childNodeSet) { |
| 187 |
|
| 188 |
if (childNode != bottomNode) { |
| 189 |
if (!processNodeSet.contains(childNode)) { |
| 190 |
writeEquivalences(childNode,&outputFile); |
| 191 |
writeDeclarations(childNode,&outputFile); |
| 192 |
processNodeSet.insert(childNode); |
| 193 |
processNodeList.append(childNode); |
| 194 |
} |
| 195 |
|
| 196 |
writeSubClassRelations(procNode,childNode,&outputFile); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
writeOntologyEnd(&outputFile); |
| 202 |
outputFile.close(); |
| 203 |
return true; |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeBottomEquivalences(CHierarchyNode* node, QFile* outputFile) { |
| 209 |
if (node->getEquivalentConceptCount() > 1) { |
| 210 |
if (mWriteDirectEquivalences) { |
| 211 |
writeEquivalenceRelations(node,outputFile); |
| 212 |
} else { |
| 213 |
if (mWriteSimpleBottomImplications) { |
| 214 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 215 |
foreach (const QString& className1, classNameList) { |
| 216 |
if (className1 != mBottomClassNameString) { |
| 217 |
writeSubClassRelation(className1,mBottomClassNameString,outputFile); |
| 218 |
} |
| 219 |
} |
| 220 |
} else { |
| 221 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 222 |
foreach (const QString& className1, classNameList) { |
| 223 |
foreach (const QString& className2, classNameList) { |
| 224 |
if (className1 != className2 && className1 != mBottomClassNameString) { |
| 225 |
writeSubClassRelation(className1,className2,outputFile); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeTopEquivalences(CHierarchyNode* node, QFile* outputFile) { |
| 235 |
if (node->getEquivalentConceptCount() > 1) { |
| 236 |
if (mWriteDirectEquivalences) { |
| 237 |
writeEquivalenceRelations(node,outputFile); |
| 238 |
} else { |
| 239 |
if (mWriteSimpleTopImplications) { |
| 240 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 241 |
foreach (const QString& className1, classNameList) { |
| 242 |
if (className1 != mTopClassNameString) { |
| 243 |
writeSubClassRelation(mTopClassNameString,className1,outputFile); |
| 244 |
} |
| 245 |
} |
| 246 |
} else { |
| 247 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 248 |
foreach (const QString& className1, classNameList) { |
| 249 |
foreach (const QString& className2, classNameList) { |
| 250 |
if (className1 != className2 && className2 != mTopClassNameString) { |
| 251 |
writeSubClassRelation(className1,className2,outputFile); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeEquivalenceRelations(const QStringList& classNameList, QFile* outputFile) { |
| 262 |
QString outputClassNameList; |
| 263 |
foreach (const QString& className, classNameList) { |
| 264 |
if (!outputClassNameList.isEmpty()) { |
| 265 |
outputClassNameList += QString(" "); |
| 266 |
} |
| 267 |
outputClassNameList += QString("<%1>").arg(className); |
| 268 |
} |
| 269 |
QString writeString = QString("EquivalentClasses(%1)\n").arg(outputClassNameList); |
| 270 |
outputFile->write(writeString.toUtf8()); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeEquivalenceRelations(CHierarchyNode* node, QFile* outputFile) { |
| 275 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 276 |
writeEquivalenceRelations(classNameList,outputFile); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeEquivalences(CHierarchyNode* node, QFile* outputFile) { |
| 282 |
if (node->getEquivalentConceptCount() > 1) { |
| 283 |
if (mWriteDirectEquivalences) { |
| 284 |
writeEquivalenceRelations(node,outputFile); |
| 285 |
} else { |
| 286 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 287 |
foreach (const QString& className1, classNameList) { |
| 288 |
foreach (const QString& className2, classNameList) { |
| 289 |
if (className1 != className2) { |
| 290 |
writeSubClassRelation(className1,className2,outputFile); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeDeclarations(CHierarchyNode* node, QFile* outputFile) { |
| 300 |
if (mWriteDeclarations) { |
| 301 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 302 |
foreach (const QString& className, classNameList) { |
| 303 |
writeDeclaration(className,outputFile); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeDeclaration(const QString& className, QFile* outputFile) { |
| 310 |
QString writeString = QString("Declaration(Class(<%1>))\n").arg(className); |
| 311 |
outputFile->write(writeString.toUtf8()); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeOntologyStart(QFile* outputFile) { |
| 316 |
outputFile->write(QString("Prefix(:=<http://www.w3.org/2002/07/owl#>)\n").toUtf8()); |
| 317 |
outputFile->write(QString("Prefix(owl:=<http://www.w3.org/2002/07/owl#>)\n").toUtf8()); |
| 318 |
outputFile->write(QString("Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)\n").toUtf8()); |
| 319 |
outputFile->write(QString("Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)\n").toUtf8()); |
| 320 |
outputFile->write(QString("Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)\n").toUtf8()); |
| 321 |
outputFile->write(QString("Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)\n").toUtf8()); |
| 322 |
|
| 323 |
outputFile->write(QString("\nOntology(\n").toUtf8()); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeOntologyEnd(QFile* outputFile) { |
| 328 |
outputFile->write(QString(")").toUtf8()); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeSubClassRelations(CHierarchyNode* superNode, CHierarchyNode* subNode, QFile* outputFile) { |
| 333 |
const QStringList& superClassNameList(superNode->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 334 |
const QStringList& subClassNameList(subNode->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 335 |
|
| 336 |
foreach (const QString& subClassName, subClassNameList) { |
| 337 |
foreach (const QString& superClassName, superClassNameList) { |
| 338 |
writeSubClassRelation(subClassName,superClassName,outputFile); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
void CWriteOREFunctionalClassSubsumptionsHierarchyQuery::writeSubClassRelation(const QString& subClassName, const QString& superClassName, QFile* outputFile) { |
| 346 |
QString writeString = QString("SubClassOf(<%1> <%2>)\n").arg(subClassName,superClassName); |
| 347 |
outputFile->write(writeString.toUtf8()); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
QString CWriteOREFunctionalClassSubsumptionsHierarchyQuery::getQueryName() { |
| 353 |
return mQueryName; |
| 354 |
} |
| 355 |
|
| 356 |
QString CWriteOREFunctionalClassSubsumptionsHierarchyQuery::getQueryString() { |
| 357 |
return mQueryString; |
| 358 |
} |
| 359 |
|
| 360 |
bool CWriteOREFunctionalClassSubsumptionsHierarchyQuery::hasAnswer() { |
| 361 |
return mQueryAnswered; |
| 362 |
} |
| 363 |
|
| 364 |
QString CWriteOREFunctionalClassSubsumptionsHierarchyQuery::getAnswerString() { |
| 365 |
return QString("Subclass Hierarchy written to file '%1'").arg(mOutputFileNameString); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
bool CWriteOREFunctionalClassSubsumptionsHierarchyQuery::hasError() { |
| 370 |
return mTaxCalcError || mQueryConstructError || CQuery::hasError(); |
| 371 |
} |
| 372 |
|
| 373 |
}; // end namespace Query |
| 374 |
|
| 375 |
}; // end namespace Reasoner |
| 376 |
|
| 377 |
}; // end namespace Konclude |