Debian Codegraph @

Source/Reasoner/Query/CSubSuperPropertiesResultVisitCallbackQuery.cpp

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