| 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 "CWriteIndividualFlattenedTypesQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CWriteIndividualFlattenedTypesQuery::CWriteIndividualFlattenedTypesQuery(CConcreteOntology* ontology, CConfigurationBase *configuration, const QString& outputFileString, const QString& individualNameString, const QString &subClassHierarchyQueryName) |
| 32 |
: CRealizationPremisingQuery(ontology,configuration) { |
| 33 |
mQueryName = subClassHierarchyQueryName; |
| 34 |
mIndividualNameString = individualNameString; |
| 35 |
if (mIndividualNameString.isEmpty()) { |
| 36 |
mQueryString = QString("Write Individual Types"); |
| 37 |
} else { |
| 38 |
mQueryString = QString("Write Individual Types for '%1'").arg(mIndividualNameString); |
| 39 |
} |
| 40 |
|
| 41 |
mUseAbbreviatedIRIs = CConfigDataReader::readConfigBoolean(configuration,"Konclude.CLI.Output.AbbreviatedIRIs",false); |
| 42 |
mWriteDeclarations = CConfigDataReader::readConfigBoolean(configuration,"Konclude.CLI.Output.WriteDeclarations",false); |
| 43 |
mWriteOnlyDirectTypes = CConfigDataReader::readConfigBoolean(configuration,"Konclude.CLI.Output.WriteOnlyDirectTypes",false); |
| 44 |
mWriteSubClassOfInconsistency = CConfigDataReader::readConfigBoolean(configuration, "Konclude.CLI.Output.WriteReducedInconsistency", false); |
| 45 |
mWriteAnonymousIndividuals = CConfigDataReader::readConfigBoolean(configuration, "Konclude.CLI.Output.WriteAnonymousIndividualResults", false); |
| 46 |
|
| 47 |
mRealizationCalcError = false; |
| 48 |
mQueryConstructError = false; |
| 49 |
|
| 50 |
mBottomClassNameString = QString("http://www.w3.org/2002/07/owl#Nothing"); |
| 51 |
mTopClassNameString = QString("http://www.w3.org/2002/07/owl#Thing"); |
| 52 |
mOutputFileNameString = outputFileString; |
| 53 |
mQueryAnswered = false; |
| 54 |
|
| 55 |
mRequiresSameIndividualRealisation = true; |
| 56 |
mRequiresConceptRealisation = true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
CQueryResult *CWriteIndividualFlattenedTypesQuery::getQueryResult() { |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
CQuery* CWriteIndividualFlattenedTypesQuery::addQueryError(CQueryError* queryError) { |
| 66 |
if (CQueryInconsitentOntologyError::hasInconsistentOntologyError(queryError)) { |
| 67 |
// write inconsistency |
| 68 |
if (!writeInconsistentIndividualTypes()) { |
| 69 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write individual types to file '%1'.").arg(mOutputFileNameString))); |
| 70 |
} |
| 71 |
} |
| 72 |
CQuery::addQueryError(queryError); |
| 73 |
return this; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
bool CWriteIndividualFlattenedTypesQuery::visitIndividuals(function<bool(const CIndividualReference& indiRef)> visitFunc) { |
| 78 |
CBOXSET<CIndividual*>* activeIndiSet = mOntology->getABox()->getActiveIndividualSet(); |
| 79 |
CIndividualVector* indiVec = mOntology->getABox()->getIndividualVector(false); |
| 80 |
cint64 indiCount = 0; |
| 81 |
if (indiVec) { |
| 82 |
indiCount = indiVec->getItemCount(); |
| 83 |
} |
| 84 |
|
| 85 |
bool visited = false; |
| 86 |
bool continueVisiting = true; |
| 87 |
cint64 maxTriplesIndexedIndiId = 0; |
| 88 |
cint64 maxABoxIndiId = 0; |
| 89 |
COntologyTriplesAssertionsAccessor* triplesAccessor = mOntology->getOntologyTriplesData()->getTripleAssertionAccessor(); |
| 90 |
if (triplesAccessor) { |
| 91 |
maxTriplesIndexedIndiId = mOntology->getOntologyTriplesData()->getTripleAssertionAccessor()->getMaxIndexedIndividualId(); |
| 92 |
} |
| 93 |
if (indiVec) { |
| 94 |
cint64 indiCount = indiVec->getItemCount(); |
| 95 |
for (cint64 idx = 0; idx < indiCount && continueVisiting; ++idx) { |
| 96 |
CIndividual* indi = indiVec->getData(idx); |
| 97 |
if (indi && activeIndiSet->contains(indi)) { |
| 98 |
visited = true; |
| 99 |
continueVisiting = visitFunc(CIndividualReference(indi)); |
| 100 |
} else if (idx <= maxTriplesIndexedIndiId) { |
| 101 |
visited = true; |
| 102 |
continueVisiting = visitFunc(CIndividualReference(idx)); |
| 103 |
} |
| 104 |
maxABoxIndiId = qMax(idx, maxABoxIndiId); |
| 105 |
} |
| 106 |
} |
| 107 |
return visited; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
bool CWriteIndividualFlattenedTypesQuery::writeInconsistentIndividualTypes() { |
| 112 |
if (startWritingOutput()) { |
| 113 |
writeOntologyStart(); |
| 114 |
|
| 115 |
CConcept* bottomConcept = mOntology->getTBox()->getBottomConcept(); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
QString bottomClassName = CIRIName::getRecentIRIName(bottomConcept->getClassNameLinker()); |
| 120 |
if (mUseAbbreviatedIRIs) { |
| 121 |
bottomClassName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(bottomConcept->getClassNameLinker()); |
| 122 |
} |
| 123 |
|
| 124 |
if (mWriteDeclarations) { |
| 125 |
writeClassDeclaration(bottomClassName); |
| 126 |
} |
| 127 |
|
| 128 |
if (mWriteSubClassOfInconsistency) { |
| 129 |
|
| 130 |
CConcept* topConcept = mOntology->getTBox()->getTopConcept(); |
| 131 |
QString topClassName = CIRIName::getRecentIRIName(topConcept->getClassNameLinker()); |
| 132 |
if (mUseAbbreviatedIRIs) { |
| 133 |
topClassName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(topConcept->getClassNameLinker()); |
| 134 |
} |
| 135 |
if (mWriteDeclarations) { |
| 136 |
writeClassDeclaration(topClassName); |
| 137 |
} |
| 138 |
writeSubClassRelation(topClassName,bottomClassName); |
| 139 |
|
| 140 |
} else { |
| 141 |
if (mIndividualNameString.isEmpty()) { |
| 142 |
QStringList incClassNameList; |
| 143 |
|
| 144 |
visitIndividuals([&](const CIndividualReference& indiRef)->bool { |
| 145 |
bool anonymous = mOntology->getIndividualNameResolver()->isAnonymous(indiRef); |
| 146 |
if (mWriteAnonymousIndividuals || !anonymous) { |
| 147 |
QString individualName = mOntology->getIndividualNameResolver()->getIndividualName(indiRef, mUseAbbreviatedIRIs); |
| 148 |
if (mWriteDeclarations) { |
| 149 |
writeIndividualDeclaration(individualName, anonymous); |
| 150 |
} |
| 151 |
writeIndividualType(individualName, anonymous, bottomClassName); |
| 152 |
} |
| 153 |
return true; |
| 154 |
}); |
| 155 |
|
| 156 |
} else { |
| 157 |
if (mWriteDeclarations) { |
| 158 |
writeNamedIndividualDeclaration(mIndividualNameString); |
| 159 |
} |
| 160 |
writeIndividualType(mIndividualNameString, false, bottomClassName); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
writeOntologyEnd(); |
| 165 |
|
| 166 |
|
| 167 |
return endWritingOutput(); |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
bool CWriteIndividualFlattenedTypesQuery::visitConcept(CConcept* concept, CConceptRealization* conRealization) { |
| 176 |
QString conceptString; |
| 177 |
if (mUseAbbreviatedIRIs) { |
| 178 |
conceptString = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker()); |
| 179 |
} |
| 180 |
if (conceptString.isEmpty()) { |
| 181 |
conceptString = CIRIName::getRecentIRIName(concept->getClassNameLinker()); |
| 182 |
} |
| 183 |
if (!conceptString.isEmpty()) { |
| 184 |
if (mWriteDeclarations && !mDeclaratedConceptSet.contains(concept)) { |
| 185 |
mDeclaratedConceptSet.insert(concept); |
| 186 |
writeClassDeclaration(conceptString); |
| 187 |
} |
| 188 |
writeIndividualType(mCurrentIndividualName, mCurrentIndividualAnonymous, conceptString); |
| 189 |
} |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
CQueryResult *CWriteIndividualFlattenedTypesQuery::constructResult(CRealization* realization) { |
| 198 |
mQueryAnswered = true; |
| 199 |
if (!writeIndividualTypesResult(realization)) { |
| 200 |
addQueryError(new CQueryUnspecifiedStringError(QString("Could not write individual types to file '%1'.").arg(mOutputFileNameString))); |
| 201 |
} |
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
bool CWriteIndividualFlattenedTypesQuery::writeIndividualTypesResult(CRealization* realization) { |
| 213 |
if (startWritingOutput()) { |
| 214 |
|
| 215 |
CConceptRealization* conRealization = realization->getConceptRealization(); |
| 216 |
|
| 217 |
writeOntologyStart(); |
| 218 |
|
| 219 |
|
| 220 |
visitIndividuals([&](const CIndividualReference& indiRef)->bool { |
| 221 |
bool anonymous = mOntology->getIndividualNameResolver()->isAnonymous(indiRef); |
| 222 |
if (mWriteAnonymousIndividuals || !anonymous) { |
| 223 |
mCurrentIndividualName = mOntology->getIndividualNameResolver()->getIndividualName(indiRef, mUseAbbreviatedIRIs); |
| 224 |
mCurrentIndividualAnonymous = anonymous; |
| 225 |
if (mWriteDeclarations) { |
| 226 |
writeIndividualDeclaration(mCurrentIndividualName, anonymous); |
| 227 |
} |
| 228 |
conRealization->visitTypes(indiRef, mWriteOnlyDirectTypes, this); |
| 229 |
} |
| 230 |
return true; |
| 231 |
}); |
| 232 |
|
| 233 |
|
| 234 |
writeOntologyEnd(); |
| 235 |
|
| 236 |
return endWritingOutput(); |
| 237 |
} |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
CWriteQuery::WRITEQUERYTYPE CWriteIndividualFlattenedTypesQuery::getWriteQueryType() { |
| 245 |
return CWriteQuery::WRITEINDIVIDUALFLATTENEDTYPES; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
QString CWriteIndividualFlattenedTypesQuery::getQueryName() { |
| 250 |
return mQueryName; |
| 251 |
} |
| 252 |
|
| 253 |
QString CWriteIndividualFlattenedTypesQuery::getQueryString() { |
| 254 |
return mQueryString; |
| 255 |
} |
| 256 |
|
| 257 |
bool CWriteIndividualFlattenedTypesQuery::hasAnswer() { |
| 258 |
return mQueryAnswered; |
| 259 |
} |
| 260 |
|
| 261 |
QString CWriteIndividualFlattenedTypesQuery::getAnswerString() { |
| 262 |
if (mIndividualNameString.isEmpty()) { |
| 263 |
return QString("Individual Types written to file '%1'").arg(mOutputFileNameString); |
| 264 |
mQueryString = QString("Write Individual Types"); |
| 265 |
} else { |
| 266 |
return QString("Individual Types for '%1' written to file '%2'").arg(mIndividualNameString).arg(mOutputFileNameString); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
bool CWriteIndividualFlattenedTypesQuery::hasError() { |
| 272 |
return mRealizationCalcError || mQueryConstructError || CQuery::hasError(); |
| 273 |
} |
| 274 |
|
| 275 |
}; // end namespace Query |
| 276 |
|
| 277 |
}; // end namespace Reasoner |
| 278 |
|
| 279 |
}; // end namespace Konclude |