File: CClassAssertionQuery.cpp

package info (click to toggle)
konclude 0.6.2~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,976 kB
  • ctags: 43,332
  • sloc: cpp: 250,898; xml: 54,573; makefile: 29; ansic: 3; sh: 3
file content (224 lines) | stat: -rw-r--r-- 8,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 *		Copyright (C) 2013, 2014, 2015 by the Konclude Developer Team.
 *
 *		This file is part of the reasoning system Konclude.
 *		For details and support, see <http://konclude.com/>.
 *
 *		Konclude is free software: you can redistribute it and/or modify it under
 *		the terms of version 2.1 of the GNU Lesser General Public License (LGPL2.1)
 *		as published by the Free Software Foundation.
 *
 *		You should have received a copy of the GNU Lesser General Public License
 *		along with Konclude. If not, see <http://www.gnu.org/licenses/>.
 *
 *		Konclude is distributed in the hope that it will be useful,
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more
 *		details, see GNU Lesser General Public License.
 *
 */

#include "CClassAssertionQuery.h"


#include "Reasoner/Kernel/Task/CConsistenceTaskData.h"
#include "Reasoner/Kernel/Task/CSatisfiableCalculationTask.h"


namespace Konclude {

	namespace Reasoner {

		using namespace Kernel::Task;

		namespace Query {


			CClassAssertionQuery::CClassAssertionQuery(CConcreteOntology* ontology, CConfigurationBase* configuration, const QSet<CIndividual*>& individualSet, const QString& queryName)
					: CConsistencePremisingQuery(ontology,configuration) {
				mQueryName = queryName;
				if (mQueryName.isEmpty()) {
					mQueryName = QString("Get Concept Assertions");
				}
				mResult = nullptr;

				mUseAbbreviatedIRIs = CConfigDataReader::readConfigBoolean(configuration,"Konclude.OWLlink.AbbreviatedIRIs",false);

				mConsistencyCalcError = false;
				mQueryConstructError = false;
				mDeterministicAssertions = false;
				mNondeterministicAssertions = false;

				mIndividualSet = individualSet;

				mCalcConfig = configuration;
			}

			CClassAssertionQuery::~CClassAssertionQuery() {
				if (mResult) {
					delete mResult;
				}
				delete mCalcConfig;
			}


			CQueryResult* CClassAssertionQuery::getQueryResult() {
				return mResult;
			}


			CQueryResult *CClassAssertionQuery::constructResult(CConsistence* consistence) {
				return constructAssertionResult(consistence);
			}


			bool CClassAssertionQuery::addIndividualResultData(cint64 indiID, CIndividualProcessNodeVector* indiNodeVec) {
				CBOXSET<CConcept*>* eqNonCanSet = ontology->getTBox()->getEquivalentConceptNonCandidateSet(false);
				bool indiConAssertionAdded = false;
				CIndividualProcessNode* baseIndiNode = indiNodeVec->getData(indiID);
				CIndividualProcessNode* indiNode = baseIndiNode;
				if (indiNode && indiNode->getNominalIndividual()) {
					cint64 mergedIndiID = indiID;
					bool nonDeterministicMerged = false;
					while (mergedIndiID != indiNode->getMergedIntoIndividualNodeID()) {
						mergedIndiID = indiNode->getMergedIntoIndividualNodeID();
						CDependencyTrackPoint* mergedDepTrackPoint = indiNode->getMergedDependencyTrackPoint();
						if (!mergedDepTrackPoint || mergedDepTrackPoint->getBranchingTag() > 0) {
							nonDeterministicMerged = true;
						}
						indiNode = indiNodeVec->getData(mergedIndiID);
					}

					CReapplyConceptLabelSet* conLabelSet = indiNode->getReapplyConceptLabelSet(false);
					if (conLabelSet) {
						bool allNonDeterministic = nonDeterministicMerged;
						QString indiName;
						if (mUseAbbreviatedIRIs) {
							indiName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(baseIndiNode->getNominalIndividual()->getIndividualNameLinker());
						} else {
							indiName = CIRIName::getRecentIRIName(baseIndiNode->getNominalIndividual()->getIndividualNameLinker());
						}

						for (CConceptDescriptor* conDesIt = conLabelSet->getAddingSortedConceptDescriptionLinker(); conDesIt; conDesIt = conDesIt->getNext()) {
							CDependencyTrackPoint* depTrackPoint = conDesIt->getDependencyTrackPoint();
							bool conNegation = conDesIt->isNegated();
							CConcept* concept = conDesIt->getConcept();
							if (mDeterministicAssertions && depTrackPoint && depTrackPoint->getBranchingTag() <= 0 && !allNonDeterministic || mNondeterministicAssertions && (allNonDeterministic || !depTrackPoint || depTrackPoint->getBranchingTag() > 0)) {
								if (!conNegation && concept && concept->hasClassName()) {

									QString conceptName;
									if (mUseAbbreviatedIRIs) {
										conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(concept->getClassNameLinker());
									} else {
										conceptName = CIRIName::getRecentIRIName(concept->getClassNameLinker());
									}
									mResult->addIndividualConceptAssertion(indiName,conceptName);
									indiConAssertionAdded = true;

								}
							}
							if (mNondeterministicAssertions && concept->getOperatorCode() == CCEQCAND) {
								CConcept* eqConcept = concept->getOperandList()->getData();
								bool negation = false;
								if (!conLabelSet->containsConcept(eqConcept,&negation)) {

									QString conceptName;
									if (mUseAbbreviatedIRIs) {
										conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(eqConcept->getClassNameLinker());
									} else {
										conceptName = CIRIName::getRecentIRIName(eqConcept->getClassNameLinker());
									}
									mResult->addIndividualConceptAssertion(indiName,conceptName);
									indiConAssertionAdded = true;
								}
							}
						}

						if (eqNonCanSet) {
							for (CBOXSET<CConcept*>::const_iterator it = eqNonCanSet->constBegin(), itEnd = eqNonCanSet->constEnd(); it != itEnd; ++it) {
								CConcept* eqConcept(*it);
								bool negation = false;
								if (!conLabelSet->containsConcept(eqConcept,&negation)) {

									QString conceptName;
									if (mUseAbbreviatedIRIs) {
										conceptName = CAbbreviatedIRIName::getRecentAbbreviatedPrefixWithAbbreviatedIRIName(eqConcept->getClassNameLinker());
									} else {
										conceptName = CIRIName::getRecentIRIName(eqConcept->getClassNameLinker());
									}
									mResult->addIndividualConceptAssertion(indiName,conceptName);
									indiConAssertionAdded = true;
								}
							}
						}
					}
				}
				return indiConAssertionAdded;
			}


			CIndividualClassAssertionsResult *CClassAssertionQuery::constructAssertionResult(CConsistence* consistence) {
				if (mResult) {
					delete mResult;
					mResult = 0;
				}
				if (consistence) {
					mResult = new CIndividualClassAssertionsResult();
					CConsistenceTaskData* consTaskData = (CConsistenceTaskData*)consistence->getConsistenceModelData();
					if (consTaskData) {
						CSatisfiableCalculationTask* detTask = consTaskData->getDeterministicSatisfiableTask();
						CSatisfiableCalculationTask* nonDetTask = consTaskData->getCompletionGraphCachedSatisfiableTask();
						if (detTask != nonDetTask) {
							CProcessingDataBox* dataBox = nonDetTask->getProcessingDataBox();
							if (dataBox) {
								CIndividualProcessNodeVector* indiNodeVec = dataBox->getIndividualProcessNodeVector();
								cint64 indiNodeCount = indiNodeVec->getItemCount();
								if (mIndividualSet.isEmpty()) {
									for (cint64 indiID = 0; indiID < indiNodeCount; ++indiID) {
										addIndividualResultData(indiID,indiNodeVec);
									}
								} else {
									for (QSet<CIndividual*>::const_iterator it = mIndividualSet.constBegin(), itEnd = mIndividualSet.constEnd(); it  != itEnd; ++it) {
										CIndividual* individual(*it);
										cint64 indiID = individual->getIndividualID();
										addIndividualResultData(indiID,indiNodeVec);
									}
								}
							}
						}
					}
				}
				return mResult;
			}


			QString CClassAssertionQuery::getQueryName() {
				return mQueryName;
			}

			QString CClassAssertionQuery::getQueryString() {
				return mQueryString;
			}

			bool CClassAssertionQuery::hasAnswer() {
				return mResult != 0;
			}

			QString CClassAssertionQuery::getAnswerString() {
				if (!mResult) {
					return QString("Unknown Concept Assertions");
				} else {
					return mResult->getQueryResultString();
				}
			}

			bool CClassAssertionQuery::hasError() {
				return mConsistencyCalcError || mQueryConstructError || CQuery::hasError();
			}


		}; // end namespace Query

	}; // end namespace Reasoner

}; // end namespace Konclude