| 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 "CSubSuperPropertiesQuery.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Query { |
| 29 |
|
| 30 |
|
| 31 |
CSubSuperPropertiesQuery::CSubSuperPropertiesQuery(CConcreteOntology* ontology, CConfigurationBase* configuration, bool direct, CRole* role, const QString& propertyName, bool dataProperty, bool subQuery, bool superQuery, const QString& queryName) |
| 32 |
: CObjectDataPropertyClassificationPremisingQuery(ontology,configuration,dataProperty) { |
| 33 |
|
| 34 |
mCalcConfig = nullptr; |
| 35 |
initSubSuperPropertiesQuery(ontology,configuration,direct,role,propertyName,subQuery,superQuery,queryName); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
CSubSuperPropertiesQuery* CSubSuperPropertiesQuery::initSubSuperPropertiesQuery(CConcreteOntology* ontology, CConfigurationBase* configuration, bool direct, CRole* role, const QString& propertyName, bool subQuery, bool superQuery, const QString& queryName = QString("UnnamedQuery")) { |
| 40 |
mPropertyName = propertyName; |
| 41 |
mRole = role; |
| 42 |
mQueryName = queryName; |
| 43 |
if (mQueryName.isEmpty()) { |
| 44 |
QString subSuperString = QString("Sub/Super"); |
| 45 |
if (superQuery && !subQuery) { |
| 46 |
subSuperString = QString("Super"); |
| 47 |
} else if (!superQuery && subQuery) { |
| 48 |
subSuperString = QString("Sub"); |
| 49 |
} |
| 50 |
if (mPropertyName.isEmpty()) { |
| 51 |
mQueryString = QString("Get %1 Properties").arg(subSuperString); |
| 52 |
} else { |
| 53 |
mQueryString = QString("Get %1 Properties for role '%2'").arg(subSuperString).arg(mPropertyName); |
| 54 |
} |
| 55 |
} |
| 56 |
mResult = nullptr; |
| 57 |
|
| 58 |
mUseAbbreviatedIRIs = CConfigDataReader::readConfigBoolean(configuration,"Konclude.OWLlink.AbbreviatedIRIs",false); |
| 59 |
|
| 60 |
mClassificationCalcError = false; |
| 61 |
mQueryConstructError = false; |
| 62 |
|
| 63 |
mDirectQuery = direct; |
| 64 |
mSuperPropertiesQuery = superQuery; |
| 65 |
mSubPropertiesQuery = subQuery; |
| 66 |
|
| 67 |
return this; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
CSubSuperPropertiesQuery::~CSubSuperPropertiesQuery() { |
| 72 |
if (mResult) { |
| 73 |
delete mResult; |
| 74 |
} |
| 75 |
delete mCalcConfig; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
bool CSubSuperPropertiesQuery::isSubPropertyQuery() { |
| 80 |
return mSubPropertiesQuery; |
| 81 |
} |
| 82 |
|
| 83 |
bool CSubSuperPropertiesQuery::isSuperPropertyQuery() { |
| 84 |
return mSuperPropertiesQuery; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
CQueryResult* CSubSuperPropertiesQuery::getQueryResult() { |
| 92 |
return mResult; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
CQueryResult *CSubSuperPropertiesQuery::constructResult(CClassification* classification) { |
| 97 |
CPropertyRoleClassification* roleClassification = nullptr; |
| 98 |
if (isDataPropertyQuery()) { |
| 99 |
roleClassification = classification->getDataPropertyRoleClassification(); |
| 100 |
} else { |
| 101 |
roleClassification = classification->getObjectPropertyRoleClassification(); |
| 102 |
} |
| 103 |
if (roleClassification) { |
| 104 |
return constructPropertySynsetsResult(roleClassification->getRolePropertiesHierarchy()); |
| 105 |
} |
| 106 |
return nullptr; |
| 107 |
} |
| 108 |
|
| 109 |
CPropertySynsetsResult *CSubSuperPropertiesQuery::constructPropertySynsetsResult(CRolePropertiesHierarchy *hierarchy) { |
| 110 |
if (mResult) { |
| 111 |
delete mResult; |
| 112 |
mResult = 0; |
| 113 |
} |
| 114 |
if (hierarchy) { |
| 115 |
CRolePropertiesHierarchyNode* node = hierarchy->getHierarchyNode(mRole); |
| 116 |
if (node) { |
| 117 |
mResult = new CPropertySynsetsResult(mDataPropertiesRolesQuery); |
| 118 |
QSet<CRolePropertiesHierarchyNode*>* visitedHierNodeSet = nullptr; |
| 119 |
if (!mDirectQuery) { |
| 120 |
visitedHierNodeSet = new QSet<CRolePropertiesHierarchyNode*>(); |
| 121 |
} |
| 122 |
QList<CRolePropertiesHierarchyNode*> visitHierNodetList; |
| 123 |
if (mSubPropertiesQuery) { |
| 124 |
QSet<CRolePropertiesHierarchyNode*>* childNodeSet = node->getChildNodeSet(); |
| 125 |
for (QSet<CRolePropertiesHierarchyNode*>::const_iterator it = childNodeSet->constBegin(), itEnd = childNodeSet->constEnd(); it != itEnd; ++it) { |
| 126 |
CRolePropertiesHierarchyNode* childNode(*it); |
| 127 |
visitHierNodetList.append(childNode); |
| 128 |
} |
| 129 |
} |
| 130 |
if (mSuperPropertiesQuery) { |
| 131 |
QSet<CRolePropertiesHierarchyNode*>* parentNodeSet = node->getParentNodeSet(); |
| 132 |
for (QSet<CRolePropertiesHierarchyNode*>::const_iterator it = parentNodeSet->constBegin(), itEnd = parentNodeSet->constEnd(); it != itEnd; ++it) { |
| 133 |
CRolePropertiesHierarchyNode* parentNode(*it); |
| 134 |
visitHierNodetList.append(parentNode); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
while (!visitHierNodetList.isEmpty()) { |
| 139 |
CRolePropertiesHierarchyNode* nextNode = visitHierNodetList.takeFirst(); |
| 140 |
CPropertySynsetResult* nextSubPropertySynset = new CPropertySynsetResult(nextNode->getEquivalentRoleStringList(mUseAbbreviatedIRIs)); |
| 141 |
mResult->addPropertySynset(nextSubPropertySynset); |
| 142 |
if (!mDirectQuery) { |
| 143 |
if (mSubPropertiesQuery) { |
| 144 |
QSet<CRolePropertiesHierarchyNode*>* tmpChildNodeSet = nextNode->getChildNodeSet(); |
| 145 |
for (QSet<CRolePropertiesHierarchyNode*>::const_iterator it = tmpChildNodeSet->constBegin(), itEnd = tmpChildNodeSet->constEnd(); it != itEnd; ++it) { |
| 146 |
CRolePropertiesHierarchyNode* nextChildNode(*it); |
| 147 |
if (!visitedHierNodeSet->contains(nextChildNode)) { |
| 148 |
visitedHierNodeSet->insert(nextChildNode); |
| 149 |
visitHierNodetList.append(nextChildNode); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
if (mSuperPropertiesQuery) { |
| 154 |
QSet<CRolePropertiesHierarchyNode*>* tmpParentNodeSet = nextNode->getParentNodeSet(); |
| 155 |
for (QSet<CRolePropertiesHierarchyNode*>::const_iterator it = tmpParentNodeSet->constBegin(), itEnd = tmpParentNodeSet->constEnd(); it != itEnd; ++it) { |
| 156 |
CRolePropertiesHierarchyNode* nextParentNode(*it); |
| 157 |
if (!visitedHierNodeSet->contains(nextParentNode)) { |
| 158 |
visitedHierNodeSet->insert(nextParentNode); |
| 159 |
visitHierNodetList.append(nextParentNode); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
delete visitedHierNodeSet; |
| 166 |
|
| 167 |
} |
| 168 |
} |
| 169 |
return mResult; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
QString CSubSuperPropertiesQuery::getQueryName() { |
| 174 |
return mQueryName; |
| 175 |
} |
| 176 |
|
| 177 |
QString CSubSuperPropertiesQuery::getQueryString() { |
| 178 |
return mQueryString; |
| 179 |
} |
| 180 |
|
| 181 |
bool CSubSuperPropertiesQuery::hasAnswer() { |
| 182 |
return mResult != 0; |
| 183 |
} |
| 184 |
|
| 185 |
QString CSubSuperPropertiesQuery::getAnswerString() { |
| 186 |
if (!mResult) { |
| 187 |
return QString("Unknown Properties"); |
| 188 |
} else { |
| 189 |
return mResult->getQueryResultString(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
bool CSubSuperPropertiesQuery::hasError() { |
| 194 |
return mClassificationCalcError || mQueryConstructError || CQuery::hasError(); |
| 195 |
} |
| 196 |
|
| 197 |
bool CSubSuperPropertiesQuery::isDirect() { |
| 198 |
return mDirectQuery; |
| 199 |
} |
| 200 |
|
| 201 |
}; // end namespace Query |
| 202 |
|
| 203 |
}; // end namespace Reasoner |
| 204 |
|
| 205 |
}; // end namespace Konclude |