Debian Codegraph @

Source/Reasoner/Preprocess/CCommonDisjunctConceptExtractionPreProcess.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 "CCommonDisjunctConceptExtractionPreProcess.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Preprocess {
29
30
31 CCommonDisjunctConceptExtractionPreProcess::CCommonDisjunctConceptExtractionPreProcess() {
32 mOntology = nullptr;
33 mLastConceptId = 0;
34 }
35
36
37 CCommonDisjunctConceptExtractionPreProcess::~CCommonDisjunctConceptExtractionPreProcess() {
38 cDeleteAll(mSetContainer);
39 }
40
41
42 CConcreteOntology *CCommonDisjunctConceptExtractionPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) {
43 bool skipForELOntologies = CConfigDataReader::readConfigBoolean(context->getConfiguration(),"Konclude.Calculation.Preprocessing.CommonDisjunctConceptExtraction.SkipForELFragment",true);
44 bool nonELConstructsUsed = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getBuildConstructFlags()->isNonELConstructUsed();
45
46 mOntology = nullptr;
47 mLastConceptId = 0;
48
49 if (nonELConstructsUsed || !skipForELOntologies) {
50
51 mOntology = ontology;
52
53 CMBox *mBox = ontology->getDataBoxes()->getMBox();
54 CTBox *tBox = ontology->getDataBoxes()->getTBox();
55 CRBox *rBox = ontology->getDataBoxes()->getRBox();
56 CABox *aBox = ontology->getDataBoxes()->getABox();
57
58 mConceptVector = tBox->getConceptVector();
59
60 mTopConcept = mConceptVector->getLocalData(1);
61
62 mMemMan = ontology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager();
63
64
65 mMemMan = ontology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager();
66
67 mDisjunctionCommDisConHash.clear();
68
69 mStatExtractedCommDisCon = 0;
70 mStatExtractedConceptForDisjunctionCount = 0;
71
72 extractCommonDisjuncts();
73
74 LOG(INFO,"::Konclude::Reasoner::Preprocess::CommonDisjunctConceptExtractor",logTr("Extracted %1 concepts for %2 disjunctions.").arg(mStatExtractedCommDisCon).arg(mStatExtractedConceptForDisjunctionCount),this);
75 }
76 return ontology;
77 }
78
79
80 CCommonDisjunctConceptExtractionPreProcess* CCommonDisjunctConceptExtractionPreProcess::extractCommonDisjuncts() {
81 CMBox *mBox = mOntology->getDataBoxes()->getMBox();
82 CImplicationReplacementVector* repVector = mBox->getImplicationReplacementVector(true);
83 cint64 conCount = mConceptVector->getItemCount();
84 for (cint64 conIdx = mLastConceptId; conIdx < conCount; ++conIdx) {
85 CConcept* concept = mConceptVector->getData(conIdx);
86 if (concept) {
87 cint64 conOpCode = concept->getOperatorCode();
88 cint64 opCount = concept->getOperandCount();
89 if (concept->getOperandCount() >= 1 && (conOpCode == CCAND || conOpCode == CCEQ || conOpCode == CCOR)) {
90 bool negateConcept = conOpCode == CCAND || conOpCode == CCEQ;
91 QSet<TConNegPair> consideredDisConConceptSet;
92 QSet<TConNegPair> disConConceptSet;
93 getDisjunctConcepts(concept, negateConcept, &disConConceptSet, &consideredDisConConceptSet);
94 disConConceptSet.remove(TConNegPair(concept, negateConcept));
95 bool localRepData = false;
96 CReplacementData* prevReplData = repVector->getData(conIdx, &localRepData);
97 if (!disConConceptSet.isEmpty() || prevReplData) {
98 CReplacementData* replData = prevReplData;
99 if (!localRepData) {
100 replData = CObjectAllocator<CReplacementData>::allocateAndConstruct(mMemMan);
101 repVector->setData(conIdx, replData);
102 replData->initReplacementData(prevReplData);
103 }
104 ++mStatExtractedConceptForDisjunctionCount;
105 replData->clearCommonDisjunctConceptLinker();
106 for (QSet<TConNegPair>::const_iterator it = disConConceptSet.constBegin(), itEnd = disConConceptSet.constEnd(); it != itEnd; ++it) {
107 TConNegPair conNegPair(*it);
108 CConcept* commCon = conNegPair.first;
109 bool commNeg = conNegPair.second;
110 if (commCon != concept || negateConcept != commNeg) {
111 CXNegLinker<CConcept*>* commNegConLinker = CObjectAllocator< CXNegLinker<CConcept*> >::allocateAndConstruct(mMemMan);
112 commNegConLinker->initNegLinker(commCon, commNeg);
113 replData->addCommonDisjunctConceptLinker(commNegConLinker);
114 ++mStatExtractedCommDisCon;
115 }
116 }
117 }
118 }
119 }
120 }
121 mLastConceptId = conCount;
122 return this;
123 }
124
125 CConcreteOntology* CCommonDisjunctConceptExtractionPreProcess::continuePreprocessing() {
126 if (mOntology) {
127 extractCommonDisjuncts();
128 }
129 return mOntology;
130 }
131
132
133
134 bool CCommonDisjunctConceptExtractionPreProcess::getDisjunctConcepts(CConcept* concept, bool negated, QSet<TConNegPair>* collectConSet, QSet<TConNegPair>* consideredConSet) {
135 if (!consideredConSet->contains(TConNegPair(concept,negated))) {
136 consideredConSet->insert(TConNegPair(concept,negated));
137 collectConSet->insert(TConNegPair(concept,negated));
138 cint64 opCode = concept->getOperatorCode();
139 if (!negated && (opCode == CCSUB || opCode == CCEQ || opCode == CCAND || (concept->getOperandCount() == 1 && opCode == CCOR)) || negated && (opCode == CCOR || (concept->getOperandCount() == 1 && (opCode == CCAND || opCode == CCEQ)))) {
140 for (CSortedNegLinker<CConcept*>* opLinker = concept->getOperandList(); opLinker; opLinker = opLinker->getNext()) {
141 CConcept* opConcept = opLinker->getData();
142 bool opConNegation = opLinker->isNegated() ^ negated;
143 getDisjunctConcepts(opConcept,opConNegation,collectConSet,consideredConSet);
144 }
145 } else if (!negated && (opCode == CCOR) || negated && (opCode == CCAND || opCode == CCEQ)) {
146
147 QSet<TConNegPair>* firstDisjunctConSet = mDisjunctionCommDisConHash.value(TConNegPair(concept,negated));
148 if (!firstDisjunctConSet) {
149 firstDisjunctConSet = new QSet<TConNegPair>();
150 QSet<TConNegPair> nextDisjunctConSet;
151 CSortedNegLinker<CConcept*>* opLinker = concept->getOperandList();
152 if (opLinker) {
153 CConcept* firstDisjunctConcept = opLinker->getData();
154 bool firstDisConNegation = opLinker->isNegated() ^ negated;
155 opLinker = opLinker->getNext();
156 QSet<TConNegPair> consideredConSet1(*consideredConSet);
157 getDisjunctConcepts(firstDisjunctConcept,firstDisConNegation,firstDisjunctConSet,&consideredConSet1);
158 while (opLinker && !firstDisjunctConSet->isEmpty()) {
159 CConcept* nextDisjunctConcept = opLinker->getData();
160 bool nextDisConNegation = opLinker->isNegated() ^ negated;
161 nextDisjunctConSet.clear();
162 QSet<TConNegPair> consideredConSet2(*consideredConSet);
163 getDisjunctConcepts(nextDisjunctConcept,nextDisConNegation,&nextDisjunctConSet,&consideredConSet2);
164
165 QSet<TConNegPair>::iterator it = firstDisjunctConSet->begin();
166 while (it != firstDisjunctConSet->end()) {
167 TConNegPair conNegPair(*it);
168 if (!nextDisjunctConSet.contains(conNegPair)) {
169 it = firstDisjunctConSet->erase(it);
170 } else {
171 ++it;
172 }
173 }
174
175 opLinker = opLinker->getNext();
176 }
177 }
178
179 mSetContainer.append(firstDisjunctConSet);
180 mDisjunctionCommDisConHash.insert(TConNegPair(concept,negated),firstDisjunctConSet);
181 }
182
183 for (QSet<TConNegPair>::iterator it = firstDisjunctConSet->begin(); it != firstDisjunctConSet->end(); ++it) {
184 TConNegPair conNegPair(*it);
185 collectConSet->insert(conNegPair);
186 }
187 }
188 return true;
189 }
190 return false;
191 }
192
193
194 }; // end namespace Preprocess
195
196 }; // end namespace Reasoner
197
198 }; // end namespace Konclude