| 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 "CWriteClassSubsumptionsHierarchyQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CWriteClassSubsumptionsHierarchyQuery::CWriteClassSubsumptionsHierarchyQuery(CConcreteOntology *premisingTaxonomyOntology, CConfigurationBase *configuration, const QString& outputName, 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.CLI.Output.AbbreviatedIRIs",false); |
| 42 |
mWriteDeclarations = CConfigDataReader::readConfigBoolean(configuration,"Konclude.CLI.Output.WriteDeclarations",false); |
| 43 |
|
| 44 |
mTaxCalcError = false; |
| 45 |
mQueryConstructError = false; |
| 46 |
|
| 47 |
mOutputFileNameString = outputName; |
| 48 |
|
| 49 |
mBottomClassNameString = QString("http://www.w3.org/2002/07/owl#Nothing"); |
| 50 |
mTopClassNameString = QString("http://www.w3.org/2002/07/owl#Thing"); |
| 51 |
mQueryAnswered = false; |
| 52 |
} |
| 53 |
|
| 54 |
CQueryResult *CWriteClassSubsumptionsHierarchyQuery::getQueryResult() { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
|
| 58 |
CQuery* CWriteClassSubsumptionsHierarchyQuery::addQueryError(CQueryError* queryError) { |
| 59 |
if (CQueryInconsitentOntologyError::hasInconsistentOntologyError(queryError)) { |
| 60 |
// write inconsistency |
| 61 |
if (!writeInconsistentHierarchyResult()) { |
| 62 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write class hierarchy to file '%1'.").arg(mOutputFileNameString))); |
| 63 |
} |
| 64 |
} |
| 65 |
CQuery::addQueryError(queryError); |
| 66 |
return this; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
bool CWriteClassSubsumptionsHierarchyQuery::writeInconsistentHierarchyResult() { |
| 71 |
if (startWritingOutput()) { |
| 72 |
|
| 73 |
writeOntologyStart(); |
| 74 |
|
| 75 |
CConcept* mTopConcept = ontology->getTBox()->getTopConcept(); |
| 76 |
CConcept* mBottomConcept = ontology->getTBox()->getTopConcept(); |
| 77 |
|
| 78 |
CBOXSET<CConcept*>* activeClassConceptSet = ontology->getTBox()->getActiveClassConceptSet(); |
| 79 |
|
| 80 |
|
| 81 |
if (mWriteDeclarations) { |
| 82 |
for (CBOXSET<CConcept*>::const_iterator it = activeClassConceptSet->constBegin(), itEnd = activeClassConceptSet->constEnd(); it != itEnd; ++it) { |
| 83 |
CConcept* concept(*it); |
| 84 |
QString className = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 85 |
if (mUseAbbreviatedIRIs) { |
| 86 |
className = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 87 |
} |
| 88 |
writeDeclaration(className); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
QStringList incClassNameList; |
| 93 |
for (CBOXSET<CConcept*>::const_iterator it = activeClassConceptSet->constBegin(), itEnd = activeClassConceptSet->constEnd(); it != itEnd; ++it) { |
| 94 |
CConcept* concept(*it); |
| 95 |
QString className = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 96 |
if (mUseAbbreviatedIRIs) { |
| 97 |
className = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 98 |
} |
| 99 |
incClassNameList.append(className); |
| 100 |
} |
| 101 |
writeEquivalenceRelations(incClassNameList); |
| 102 |
|
| 103 |
writeOntologyEnd(); |
| 104 |
|
| 105 |
return endWritingOutput(); |
| 106 |
} |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
CQueryResult *CWriteClassSubsumptionsHierarchyQuery::constructResult(CTaxonomy *taxonomy) { |
| 112 |
mQueryAnswered = true; |
| 113 |
if (!writeSubClassHierarchyResult(taxonomy)) { |
| 114 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write class hierarchy to file '%1'.").arg(mOutputFileNameString))); |
| 115 |
} |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
|
| 119 |
bool CWriteClassSubsumptionsHierarchyQuery::writeSubClassHierarchyResult(CTaxonomy *taxonomy) { |
| 120 |
if (startWritingOutput()) { |
| 121 |
writeOntologyStart(); |
| 122 |
|
| 123 |
CHierarchyNode* topNode = taxonomy->getTopHierarchyNode(); |
| 124 |
CHierarchyNode* bottomNode = taxonomy->getBottomHierarchyNode(); |
| 125 |
CHierarchyNode* basicNode = topNode; |
| 126 |
if (!mClassName.isEmpty()) { |
| 127 |
basicNode = taxonomy->getHierarchyNode(ontology->getConcept(mClassName)); |
| 128 |
} |
| 129 |
writeDeclarations(bottomNode); |
| 130 |
writeBottomEquivalences(bottomNode); |
| 131 |
QSet<CHierarchyNode *> processNodeSet; |
| 132 |
processNodeSet.insert(bottomNode); |
| 133 |
if (basicNode) { |
| 134 |
QList<CHierarchyNode *> processNodeList; |
| 135 |
writeDeclarations(basicNode); |
| 136 |
writeTopEquivalences(basicNode); |
| 137 |
processNodeSet.insert(basicNode); |
| 138 |
processNodeList.append(basicNode); |
| 139 |
while (!processNodeList.isEmpty()) { |
| 140 |
CHierarchyNode *procNode = processNodeList.takeFirst(); |
| 141 |
QSet<CHierarchyNode *>* childNodeSet(procNode->getChildNodeSet()); |
| 142 |
foreach (CHierarchyNode *childNode, *childNodeSet) { |
| 143 |
|
| 144 |
if (!processNodeSet.contains(childNode)) { |
| 145 |
writeDeclarations(childNode); |
| 146 |
writeEquivalences(childNode); |
| 147 |
processNodeSet.insert(childNode); |
| 148 |
processNodeList.append(childNode); |
| 149 |
} |
| 150 |
|
| 151 |
if (childNode != bottomNode) { |
| 152 |
writeSubClassRelations(procNode,childNode); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
writeOntologyEnd(); |
| 158 |
return endWritingOutput(); |
| 159 |
} |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
void CWriteClassSubsumptionsHierarchyQuery::writeBottomEquivalences(CHierarchyNode* node) { |
| 164 |
if (node->getEquivalentConceptCount() > 1) { |
| 165 |
writeEquivalenceRelations(node); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void CWriteClassSubsumptionsHierarchyQuery::writeTopEquivalences(CHierarchyNode* node) { |
| 170 |
if (node->getEquivalentConceptCount() > 1) { |
| 171 |
writeEquivalenceRelations(node); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void CWriteClassSubsumptionsHierarchyQuery::writeEquivalenceRelations(CHierarchyNode* node) { |
| 179 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 180 |
writeEquivalenceRelations(classNameList); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
void CWriteClassSubsumptionsHierarchyQuery::writeEquivalences(CHierarchyNode* node) { |
| 186 |
if (node->getEquivalentConceptCount() > 1) { |
| 187 |
writeEquivalenceRelations(node); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
void CWriteClassSubsumptionsHierarchyQuery::writeDeclarations(CHierarchyNode* node) { |
| 193 |
if (mWriteDeclarations) { |
| 194 |
const QStringList& classNameList(node->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 195 |
foreach (const QString& className, classNameList) { |
| 196 |
writeDeclaration(className); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
void CWriteClassSubsumptionsHierarchyQuery::writeSubClassRelations(CHierarchyNode* superNode, CHierarchyNode* subNode) { |
| 205 |
const QStringList& superClassNameList(superNode->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 206 |
const QStringList& subClassNameList(subNode->getEquivalentConceptStringList(mUseAbbreviatedIRIs)); |
| 207 |
|
| 208 |
writeSubClassRelation(subClassNameList.first(),superClassNameList.first()); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
CWriteQuery::WRITEQUERYTYPE CWriteClassSubsumptionsHierarchyQuery::getWriteQueryType() { |
| 214 |
return CWriteQuery::WRITESUBCLASSHIERARCHY; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
QString CWriteClassSubsumptionsHierarchyQuery::getQueryName() { |
| 220 |
return mQueryName; |
| 221 |
} |
| 222 |
|
| 223 |
QString CWriteClassSubsumptionsHierarchyQuery::getQueryString() { |
| 224 |
return mQueryString; |
| 225 |
} |
| 226 |
|
| 227 |
bool CWriteClassSubsumptionsHierarchyQuery::hasAnswer() { |
| 228 |
return mQueryAnswered; |
| 229 |
} |
| 230 |
|
| 231 |
QString CWriteClassSubsumptionsHierarchyQuery::getAnswerString() { |
| 232 |
return QString("Subclass Hierarchy written to file '%1'").arg(mOutputFileNameString); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
bool CWriteClassSubsumptionsHierarchyQuery::hasError() { |
| 237 |
return mTaxCalcError || mQueryConstructError || CQuery::hasError(); |
| 238 |
} |
| 239 |
|
| 240 |
}; // end namespace Query |
| 241 |
|
| 242 |
}; // end namespace Reasoner |
| 243 |
|
| 244 |
}; // end namespace Konclude |