| 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 "CSubSuperClassesResultVisitCallbackQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CSubSuperClassesResultVisitCallbackQuery::CSubSuperClassesResultVisitCallbackQuery(CConcreteOntology* ontology, CConfigurationBase* configuration, CConcept* concept, const QString& className, bool subClasses, bool superClasses, bool direct, const QString& queryName) |
| 32 |
: CTaxonomyPremisingQuery(ontology,configuration) { |
| 33 |
mClassName = className; |
| 34 |
mConcept = concept; |
| 35 |
mQueryName = queryName; |
| 36 |
|
| 37 |
mSubClasses = subClasses; |
| 38 |
mSuperClasses = superClasses; |
| 39 |
|
| 40 |
QString subSuperString("Sub/Super"); |
| 41 |
if (mSubClasses && !mSuperClasses) { |
| 42 |
subSuperString = QString("Sub"); |
| 43 |
} else if (!mSubClasses && mSuperClasses) { |
| 44 |
subSuperString = QString("Super"); |
| 45 |
} |
| 46 |
if (mClassName.isEmpty()) { |
| 47 |
mQueryString = QString("Get %1 Classes").arg(subSuperString); |
| 48 |
} else { |
| 49 |
mQueryString = QString("Get %1 Classes for '%2'").arg(subSuperString).arg(mClassName); |
| 50 |
} |
| 51 |
mResult = nullptr; |
| 52 |
|
| 53 |
mTaxonomyCalcError = false; |
| 54 |
mQueryConstructError = false; |
| 55 |
mDirect = direct; |
| 56 |
mNode = nullptr; |
| 57 |
|
| 58 |
mCalcConfig = configuration; |
| 59 |
} |
| 60 |
|
| 61 |
CSubSuperClassesResultVisitCallbackQuery::~CSubSuperClassesResultVisitCallbackQuery() { |
| 62 |
if (mResult) { |
| 63 |
delete mResult; |
| 64 |
} |
| 65 |
delete mCalcConfig; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
CQueryResult* CSubSuperClassesResultVisitCallbackQuery::getQueryResult() { |
| 70 |
return mResult; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
CQueryResult *CSubSuperClassesResultVisitCallbackQuery::constructResult(CTaxonomy *taxonomy) { |
| 75 |
if (mResult) { |
| 76 |
delete mResult; |
| 77 |
mResult = 0; |
| 78 |
} |
| 79 |
mNode = taxonomy->getHierarchyNode(mConcept); |
| 80 |
if (mNode) { |
| 81 |
mResult = new CSucceedQueryResult(); |
| 82 |
} |
| 83 |
return mResult; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
bool CSubSuperClassesResultVisitCallbackQuery::callbackVisitingClasses(CSetOfEntityExpressionSetResultVisitingCallback* visitingCallback) { |
| 88 |
if (mNode) { |
| 89 |
QSet<CHierarchyNode*>* visitedHierNodeSet = nullptr; |
| 90 |
if (!mDirect) { |
| 91 |
visitedHierNodeSet = new QSet<CHierarchyNode*>(); |
| 92 |
} |
| 93 |
QList<CHierarchyNode*> visitHierNodetList; |
| 94 |
if (mSubClasses) { |
| 95 |
QSet<CHierarchyNode*>* childNodeSet = mNode->getChildNodeSet(); |
| 96 |
for (QSet<CHierarchyNode*>::const_iterator it = childNodeSet->constBegin(), itEnd = childNodeSet->constEnd(); it != itEnd; ++it) { |
| 97 |
CHierarchyNode* childNode(*it); |
| 98 |
visitHierNodetList.append(childNode); |
| 99 |
} |
| 100 |
} |
| 101 |
if (mSuperClasses) { |
| 102 |
QSet<CHierarchyNode*>* parentNodeSet = mNode->getParentNodeSet(); |
| 103 |
for (QSet<CHierarchyNode*>::const_iterator it = parentNodeSet->constBegin(), itEnd = parentNodeSet->constEnd(); it != itEnd; ++it) { |
| 104 |
CHierarchyNode* parentNode(*it); |
| 105 |
visitHierNodetList.append(parentNode); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
while (!visitHierNodetList.isEmpty()) { |
| 110 |
CHierarchyNode* nextNode = visitHierNodetList.takeFirst(); |
| 111 |
|
| 112 |
visitingCallback->startEntityExpressionSet(ontology); |
| 113 |
QList<CConcept*>* eqConList = nextNode->getEquivalentConceptList(); |
| 114 |
for (QList<CConcept*>::const_iterator it = eqConList->constBegin(), itEnd = eqConList->constEnd(); it != itEnd; ++it) { |
| 115 |
CConcept* concept(*it); |
| 116 |
visitingCallback->visitConceptAssociatedEntityExpression(concept,ontology); |
| 117 |
} |
| 118 |
visitingCallback->endEntityExpressionSet(ontology); |
| 119 |
|
| 120 |
if (!mDirect) { |
| 121 |
if (mSubClasses) { |
| 122 |
QSet<CHierarchyNode*>* tmpChildNodeSet = nextNode->getChildNodeSet(); |
| 123 |
for (QSet<CHierarchyNode*>::const_iterator it = tmpChildNodeSet->constBegin(), itEnd = tmpChildNodeSet->constEnd(); it != itEnd; ++it) { |
| 124 |
CHierarchyNode* nextChildNode(*it); |
| 125 |
if (!visitedHierNodeSet->contains(nextChildNode)) { |
| 126 |
visitedHierNodeSet->insert(nextChildNode); |
| 127 |
visitHierNodetList.append(nextChildNode); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
if (mSuperClasses) { |
| 132 |
QSet<CHierarchyNode*>* tmpParentNodeSet = nextNode->getParentNodeSet(); |
| 133 |
for (QSet<CHierarchyNode*>::const_iterator it = tmpParentNodeSet->constBegin(), itEnd = tmpParentNodeSet->constEnd(); it != itEnd; ++it) { |
| 134 |
CHierarchyNode* nextParentNode(*it); |
| 135 |
if (!visitedHierNodeSet->contains(nextParentNode)) { |
| 136 |
visitedHierNodeSet->insert(nextParentNode); |
| 137 |
visitHierNodetList.append(nextParentNode); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
delete visitedHierNodeSet; |
| 144 |
return true; |
| 145 |
} |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
QString CSubSuperClassesResultVisitCallbackQuery::getQueryName() { |
| 151 |
return mQueryName; |
| 152 |
} |
| 153 |
|
| 154 |
QString CSubSuperClassesResultVisitCallbackQuery::getQueryString() { |
| 155 |
return mQueryString; |
| 156 |
} |
| 157 |
|
| 158 |
bool CSubSuperClassesResultVisitCallbackQuery::hasAnswer() { |
| 159 |
return mResult != 0; |
| 160 |
} |
| 161 |
|
| 162 |
QString CSubSuperClassesResultVisitCallbackQuery::getAnswerString() { |
| 163 |
if (!mResult) { |
| 164 |
return QString("Unknown Classes"); |
| 165 |
} else { |
| 166 |
return mResult->getQueryResultString(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
bool CSubSuperClassesResultVisitCallbackQuery::hasError() { |
| 171 |
return mTaxonomyCalcError || mQueryConstructError || CQuery::hasError(); |
| 172 |
} |
| 173 |
|
| 174 |
bool CSubSuperClassesResultVisitCallbackQuery::isDirect() { |
| 175 |
return mDirect; |
| 176 |
} |
| 177 |
|
| 178 |
}; // end namespace Query |
| 179 |
|
| 180 |
}; // end namespace Reasoner |
| 181 |
|
| 182 |
}; // end namespace Konclude |