Debian Codegraph @

Source/Reasoner/Query/CWritePropertySubsumptionsHierarchyQuery.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 "CWritePropertySubsumptionsHierarchyQuery.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Query {
29
30
31 CWritePropertySubsumptionsHierarchyQuery::CWritePropertySubsumptionsHierarchyQuery(CConcreteOntology *premisingClassificationOntology, CConfigurationBase *configuration, const QString& outputFileString, bool dataProperties, const QString &superPropertyName, const QString &subPropertyHierarchyQueryName)
32 : CClassificationPremisingQuery(premisingClassificationOntology,configuration) {
33 mPropertyName = superPropertyName;
34 mQueryName = subPropertyHierarchyQueryName;
35 if (mPropertyName.isEmpty()) {
36 mQueryString = QString("Get Subproperty Hierarchy");
37 } else {
38 mQueryString = QString("Get Subproperty Hierarchy of property '%1'").arg(mPropertyName);
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 mBottomPropertyNameString = QString("http://www.w3.org/2002/07/owl#Nothing");
48 mTopPropertyNameString = QString("http://www.w3.org/2002/07/owl#Thing");
49 mOutputFileNameString = outputFileString;
50 mQueryAnswered = false;
51 mDataProperties = dataProperties;
52
53 if (mDataProperties) {
54 mRequiresDataRoleClassification = true;
55 } else {
56 mRequiresObjectRoleClassification = true;
57 }
58 }
59
60 CQueryResult *CWritePropertySubsumptionsHierarchyQuery::getQueryResult() {
61 return nullptr;
62 }
63
64 CWriteQuery::WRITEQUERYTYPE CWritePropertySubsumptionsHierarchyQuery::getWriteQueryType() {
65 if (mDataProperties) {
66 return CWriteQuery::WRITESUBDATAPROPERTYHIERARCHY;
67 } else {
68 return CWriteQuery::WRITESUBOBJECTPROPERTYHIERARCHY;
69 }
70 }
71
72
73 CQuery* CWritePropertySubsumptionsHierarchyQuery::addQueryError(CQueryError* queryError) {
74 if (CQueryInconsitentOntologyError::hasInconsistentOntologyError(queryError)) {
75 // write inconsistency
76 if (!writeInconsistentHierarchyResult()) {
77 addQueryError(new CQueryUnspecifiedStringError(QString("Could not write property hierarchy to file '%1'.").arg(mOutputFileNameString)));
78 }
79 }
80 CQuery::addQueryError(queryError);
81 return this;
82 }
83
84
85 bool CWritePropertySubsumptionsHierarchyQuery::writeInconsistentHierarchyResult() {
86 if (startWritingOutput()) {
87
88 writeOntologyStart();
89
90 CRole* topRole = mOntology->getRBox()->getTopObjectRole();
91 CRole* bottomRole = mOntology->getRBox()->getBottomObjectRole();
92 if (mDataProperties) {
93 topRole = mOntology->getRBox()->getTopDataRole();
94 bottomRole = mOntology->getRBox()->getBottomDataRole();
95 }
96
97 CBOXSET<CRole*>* activePropertyRoleSet = mOntology->getRBox()->getActivePropertyRoleSet();
98
99
100 if (mWriteDeclarations) {
101 for (CBOXSET<CRole*>::const_iterator it = activePropertyRoleSet->constBegin(), itEnd = activePropertyRoleSet->constEnd(); it != itEnd; ++it) {
102 CRole* role(*it);
103 QString propertyName = CIRIName::getRecentIRIName(role->getPropertyNameLinker());
104 if (mUseAbbreviatedIRIs) {
105 propertyName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(role->getPropertyNameLinker());
106 }
107 writeDeclaration(propertyName);
108 }
109 }
110
111 QStringList incPropertyNameList;
112 for (CBOXSET<CRole*>::const_iterator it = activePropertyRoleSet->constBegin(), itEnd = activePropertyRoleSet->constEnd(); it != itEnd; ++it) {
113 CRole* role(*it);
114 QString propertyName = CIRIName::getRecentIRIName(role->getPropertyNameLinker());
115 if (mUseAbbreviatedIRIs) {
116 propertyName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(role->getPropertyNameLinker());
117 }
118 incPropertyNameList.append(propertyName);
119 }
120 writeEquivalenceRelations(incPropertyNameList);
121
122 writeOntologyEnd();
123
124 return endWritingOutput();
125 }
126 return false;
127 }
128
129
130 CQueryResult *CWritePropertySubsumptionsHierarchyQuery::constructResult(CClassification* classification) {
131 mQueryAnswered = true;
132 CPropertyRoleClassification* roleClassification = classification->getObjectPropertyRoleClassification();
133 if (mDataProperties) {
134 roleClassification = classification->getDataPropertyRoleClassification();
135 }
136 if (!roleClassification || !writeSubPropertyHierarchyResult(roleClassification->getRolePropertiesHierarchy())) {
137 addQueryError(new CQueryUnspecifiedStringError(QString("Could not write class hierarchy to file '%1'.").arg(mOutputFileNameString)));
138 }
139 return nullptr;
140 }
141
142 bool CWritePropertySubsumptionsHierarchyQuery::writeSubPropertyHierarchyResult(CRolePropertiesHierarchy *hierarchy) {
143 if (startWritingOutput()) {
144 writeOntologyStart();
145
146 CRolePropertiesHierarchyNode* topNode = hierarchy->getTopHierarchyNode();
147 CRolePropertiesHierarchyNode* bottomNode = hierarchy->getBottomHierarchyNode();
148 CRolePropertiesHierarchyNode* basicNode = topNode;
149 if (!mPropertyName.isEmpty()) {
150 CMAPPINGHASH<CStringRefStringHasher,CRole*>* propertyNameConceptMappingHash = mOntology->getStringMapping()->getPropertyNameRoleMappingHash(false);
151 CRole* role = propertyNameConceptMappingHash->value(mPropertyName);
152 basicNode = hierarchy->getHierarchyNode(role);
153 }
154 writeDeclarations(bottomNode);
155 writeBottomEquivalences(bottomNode);
156 QSet<CRolePropertiesHierarchyNode *> processNodeSet;
157 processNodeSet.insert(bottomNode);
158 if (basicNode) {
159 QList<CRolePropertiesHierarchyNode *> processNodeList;
160 writeDeclarations(basicNode);
161 writeTopEquivalences(basicNode);
162 processNodeSet.insert(basicNode);
163 processNodeList.append(basicNode);
164 while (!processNodeList.isEmpty()) {
165 CRolePropertiesHierarchyNode *procNode = processNodeList.takeFirst();
166 QSet<CRolePropertiesHierarchyNode *>* childNodeSet(procNode->getChildNodeSet());
167 foreach (CRolePropertiesHierarchyNode *childNode, *childNodeSet) {
168
169 if (!processNodeSet.contains(childNode)) {
170 writeDeclarations(childNode);
171 writeEquivalences(childNode);
172 processNodeSet.insert(childNode);
173 processNodeList.append(childNode);
174 }
175
176 if (childNode != bottomNode) {
177 writeSubPropertyRelations(procNode,childNode);
178 }
179 }
180 }
181 }
182 writeOntologyEnd();
183 return endWritingOutput();
184 }
185 return false;
186 }
187
188 void CWritePropertySubsumptionsHierarchyQuery::writeBottomEquivalences(CRolePropertiesHierarchyNode* node) {
189 if (node->getEquivalentElementCount() > 1) {
190 writeEquivalenceRelations(node);
191 }
192 }
193
194 void CWritePropertySubsumptionsHierarchyQuery::writeTopEquivalences(CRolePropertiesHierarchyNode* node) {
195 if (node->getEquivalentElementCount() > 1) {
196 writeEquivalenceRelations(node);
197 }
198 }
199
200
201
202
203 void CWritePropertySubsumptionsHierarchyQuery::writeEquivalenceRelations(CRolePropertiesHierarchyNode* node) {
204 const QStringList& propertyNameList(node->getEquivalentRoleStringList(mUseAbbreviatedIRIs));
205 writeEquivalenceRelations(propertyNameList);
206 }
207
208
209
210 void CWritePropertySubsumptionsHierarchyQuery::writeEquivalences(CRolePropertiesHierarchyNode* node) {
211 if (node->getEquivalentElementCount() > 1) {
212 writeEquivalenceRelations(node);
213 }
214 }
215
216
217 void CWritePropertySubsumptionsHierarchyQuery::writeDeclarations(CRolePropertiesHierarchyNode* node) {
218 if (mWriteDeclarations) {
219 const QStringList& propertyNameList(node->getEquivalentRoleStringList(mUseAbbreviatedIRIs));
220 foreach (const QString& propertyName, propertyNameList) {
221 writeDeclaration(propertyName);
222 }
223 }
224 }
225
226
227
228
229 void CWritePropertySubsumptionsHierarchyQuery::writeSubPropertyRelations(CRolePropertiesHierarchyNode* superNode, CRolePropertiesHierarchyNode* subNode) {
230 const QStringList& superPropertyNameList(superNode->getEquivalentRoleStringList(mUseAbbreviatedIRIs));
231 const QStringList& subPropertyNameList(subNode->getEquivalentRoleStringList(mUseAbbreviatedIRIs));
232
233 writeSubPropertyRelation(subPropertyNameList.first(),superPropertyNameList.first());
234 }
235
236
237
238
239 QString CWritePropertySubsumptionsHierarchyQuery::getQueryName() {
240 return mQueryName;
241 }
242
243 QString CWritePropertySubsumptionsHierarchyQuery::getQueryString() {
244 return mQueryString;
245 }
246
247 bool CWritePropertySubsumptionsHierarchyQuery::hasAnswer() {
248 return mQueryAnswered;
249 }
250
251 QString CWritePropertySubsumptionsHierarchyQuery::getAnswerString() {
252 return QString("Subproperty Hierarchy written to file '%1'").arg(mOutputFileNameString);
253 }
254
255
256 bool CWritePropertySubsumptionsHierarchyQuery::hasError() {
257 return mTaxCalcError || mQueryConstructError || CQuery::hasError();
258 }
259
260 }; // end namespace Query
261
262 }; // end namespace Reasoner
263
264 }; // end namespace Konclude