Debian Codegraph @

Source/Reasoner/Classifier/CPartialPruningTaxonomy.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 "CPartialPruningTaxonomy.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Classifier {
29
30
31
32 CPartialPruningTaxonomy::CPartialPruningTaxonomy(CConcept *topConcept, CConcept *bottomConcept) : CTaxonomy(new CPartialPruningHierarchyNode(topConcept),new CPartialPruningHierarchyNode(bottomConcept)) {
33 activeNodeSet.insert(bottomNode);
34 confPruneRelations = true;
35 confDerivateRelations = true;
36 statistics = 0;
37 testCompStatistics = 0;
38 }
39
40
41 CPartialPruningTaxonomy::~CPartialPruningTaxonomy() {
42 if (statistics) {
43 delete statistics;
44 }
45 }
46
47 CPartialPruningTaxonomy *CPartialPruningTaxonomy::createStatistics(CClassifierStatistics *nextClassificationStatistics) {
48 testCompStatistics = nextClassificationStatistics;
49 if (!statistics) {
50 statistics = new CClassifierStatistics(nextClassificationStatistics);
51 }
52 return this;
53 }
54
55 CPartialPruningTaxonomy *CPartialPruningTaxonomy::readConfig(CConfigurationBase *config) {
56 confPruneRelations = CConfigDataReader::readConfigBoolean(config,"Konclude.Calculation.Classification.PruneSubsumptionRelations",false);
57 confDerivateRelations = CConfigDataReader::readConfigBoolean(config,"Konclude.Calculation.Classification.DerivateSubsumptionRelations",false);
58 return this;
59 }
60
61
62 CTaxonomy *CPartialPruningTaxonomy::setConceptSatisfiable(CConcept *concept, bool satisfiable) {
63 if (satisfiable) {
64 CHierarchyNode *conNode = getHierarchyNode(concept);
65 if (conNode != topNode && !conNode->hasParentNode(topNode)) {
66 activeNodeSet.insert(conNode);
67 topNode->makeChild(conNode);
68 conNode->makeChild(bottomNode);
69 ((CPartialPruningHierarchyNode *)topNode)->repairChildNodes(statistics);
70 ((CPartialPruningHierarchyNode *)conNode)->repairParentNodes(statistics);
71 //((CPartialPruningHierarchyNode *)topNode)->repairChildsParentNodeReplacement(conNode,statistics);
72 //((CPartialPruningHierarchyNode *)conNode)->repairParentNodeInsertion(topNode,statistics);
73 conNode->addIncompatiblePredecessorNode(bottomNode);
74 bottomNode->addIncompatibleSuccessorNode(conNode);
75 if (statistics) {
76 // inc statistics to top concepts
77 statistics->incDerivatedTestedSubsumptionCount(topNode->getEquivalentConceptCount()*conNode->getEquivalentConceptCount());
78 // inc statistics to bottom concepts
79 statistics->incDerivatedTestedSubsumptionCount(bottomNode->getEquivalentConceptCount()*conNode->getEquivalentConceptCount());
80 statistics->incPrunedTestedSubsumptionCount(bottomNode->getEquivalentConceptCount()*conNode->getEquivalentConceptCount());
81 }
82 if (confDerivateRelations) {
83 ((CPartialPruningHierarchyNode *)conNode)->propagateTransitiveRelations(statistics);
84 }
85 } else if (conNode == topNode) {
86 activeNodeSet.insert(topNode);
87 topNode->addIncompatiblePredecessorNode(bottomNode);
88 bottomNode->addIncompatibleSuccessorNode(topNode);
89 if (statistics) {
90 statistics->incDerivatedTestedSubsumptionCount(bottomNode->getEquivalentConceptCount()*topNode->getEquivalentConceptCount());
91 statistics->incPrunedTestedSubsumptionCount(bottomNode->getEquivalentConceptCount()*topNode->getEquivalentConceptCount());
92 }
93 if (confDerivateRelations) {
94 ((CPartialPruningHierarchyNode *)conNode)->propagateTransitiveRelations(statistics);
95 }
96 }
97 } else {
98 CHierarchyNode *conNode = getHierarchyNode(concept,false);
99 activeNodeSet.remove(conNode);
100 if (conNode == topNode) {
101 topNode = bottomNode;
102 }
103 bottomNode->addEquivalentConcept(concept);
104 conNodeHash.insert(concept,bottomNode);
105 }
106 #ifdef DEBUGTESTTAXONOMYCONSITENCE
107 testTaxonomyConsistence();
108 #endif
109 return this;
110 }
111
112
113
114 CTaxonomy *CPartialPruningTaxonomy::setConceptSubsumption(CConcept *subsumerConcept, CConcept *subsumedConcept) {
115 CHierarchyNode *subsumedNode = getHierarchyNode(subsumedConcept);
116 CHierarchyNode *subsumerNode = getHierarchyNode(subsumerConcept);
117 if (subsumedNode != subsumerNode && !subsumedNode->hasSuccessorNode(subsumerNode)) {
118 bool makeEquivalent = subsumerNode->hasSuccessorNode(subsumedNode);
119 if (makeEquivalent) {
120 //taxString = getConceptHierarchyString();
121
122 ((CPartialPruningHierarchyNode *)subsumedNode)->mergeToParentNode(subsumerNode,statistics);
123 QList<CConcept *> *eqList = subsumedNode->getEquivalentConceptList();
124 foreach (CConcept *concept, *eqList) {
125 CHierarchyNode *prevNode = conNodeHash.value(concept);
126 if (prevNode != subsumedNode) {
127 setInactiveNode(prevNode);
128 conNodeHash.insert(concept,subsumedNode);
129 }
130 }
131
132 //QString taxString2 = getConceptHierarchyString();
133
134 #ifdef DEBUGTESTTAXONOMYCONSITENCE
135 testTaxonomyConsistence();
136 #endif
137 } else {
138 subsumedNode->makeChild(subsumerNode);
139 // for all common predecessors and successors repair quasi ordering relation
140 ((CPartialPruningHierarchyNode *)subsumedNode)->repairChildNodes(statistics);
141 ((CPartialPruningHierarchyNode *)subsumedNode)->repairParentNodes(statistics);
142 ((CPartialPruningHierarchyNode *)subsumerNode)->repairParentNodes(statistics);
143 ((CPartialPruningHierarchyNode *)subsumerNode)->repairChildNodes(statistics);
144 //((CPartialPruningHierarchyNode *)subsumedNode)->repairChildsParentNodeReplacement(subsumerNode,statistics);
145 //((CPartialPruningHierarchyNode *)subsumerNode)->repairParentNodeInsertion(subsumedNode,statistics);
146 }
147 if (confDerivateRelations) {
148 ((CPartialPruningHierarchyNode *)subsumerNode)->propagateTransitiveRelations(statistics);
149 ((CPartialPruningHierarchyNode *)subsumedNode)->propagateTransitiveRelations(statistics);
150 }
151 }
152 #ifdef DEBUGTESTTAXONOMYCONSITENCE
153 testTaxonomyConsistence();
154 #endif
155 return this;
156 }
157
158
159 CTaxonomy *CPartialPruningTaxonomy::setConceptNotSubsumption(CConcept *notSubsumerConcept, CConcept *notSubsumedConcept) {
160 CHierarchyNode *notSubsumedNode = getHierarchyNode(notSubsumedConcept);
161 CHierarchyNode *notSubsumerNode = getHierarchyNode(notSubsumerConcept);
162
163 if (statistics) {
164 statistics->incPrunedTestedSubsumptionCount(-notSubsumedNode->getEquivalentConceptCount()*notSubsumerNode->getEquivalentConceptCount());
165 }
166
167 if (confPruneRelations) {
168 ((CPartialPruningHierarchyNode *)notSubsumerNode)->pruneTransitiveRelations(notSubsumedNode,statistics);
169 }
170
171 if (confDerivateRelations) {
172 ((CPartialPruningHierarchyNode *)notSubsumerNode)->propagateTransitiveRelations(statistics);
173 ((CPartialPruningHierarchyNode *)notSubsumedNode)->propagateTransitiveRelations(statistics);
174 }
175
176 #ifdef DEBUGTESTTAXONOMYCONSITENCE
177 testTaxonomyConsistence();
178 #endif
179
180 return this;
181 }
182
183
184 CTaxonomy *CPartialPruningTaxonomy::setConceptDisjoints(CConcept *disjointConcept1, CConcept *disjointConcept2) {
185 CHierarchyNode *disjNode1 = getHierarchyNode(disjointConcept1);
186 CHierarchyNode *disjNode2 = getHierarchyNode(disjointConcept2);
187
188
189 if (!disjNode1->hasIncompatiblePredecessorNode(disjNode2)) {
190 if (confPruneRelations) {
191 ((CPartialPruningHierarchyNode *)disjNode1)->pruneTransitiveRelations(disjNode2,statistics);
192 }
193 if (confDerivateRelations) {
194 ((CPartialPruningHierarchyNode *)disjNode1)->propagateTransitiveRelations(statistics);
195 ((CPartialPruningHierarchyNode *)disjNode2)->propagateTransitiveRelations(statistics);
196 }
197
198 }
199 if (!disjNode2->hasIncompatiblePredecessorNode(disjNode1)) {
200 if (confPruneRelations) {
201 ((CPartialPruningHierarchyNode *)disjNode2)->pruneTransitiveRelations(disjNode1,statistics);
202 }
203 if (confDerivateRelations) {
204 ((CPartialPruningHierarchyNode *)disjNode2)->propagateTransitiveRelations(statistics);
205 ((CPartialPruningHierarchyNode *)disjNode1)->propagateTransitiveRelations(statistics);
206 }
207 }
208
209 disjNode2->makeDisjoint(disjNode1);
210 if (confDerivateRelations) {
211 ((CPartialPruningHierarchyNode *)disjNode2)->propagateTransitiveRelations(statistics);
212 ((CPartialPruningHierarchyNode *)disjNode1)->propagateTransitiveRelations(statistics);
213 }
214
215 #ifdef DEBUGTESTTAXONOMYCONSITENCE
216 testTaxonomyConsistence();
217 #endif
218
219 return this;
220 }
221
222
223
224 CHierarchyNode *CPartialPruningTaxonomy::createHierarchyNode(CConcept *concept) {
225 return new CPartialPruningHierarchyNode(concept);
226 }
227
228
229
230
231
232 void CPartialPruningTaxonomy::testTaxonomyConsistence() {
233 //#ifdef DEBUGTESTTAXONOMYCONSITENCE
234 // taxString = getConceptHierarchyString();
235 //#endif
236 //for (QList<CHierarchyNode *>::iterator it = nodeContainer.begin(); it != nodeContainer.end(); ++it) {
237 // CHierarchyNode* node = *it;
238 // if (node->getParentNodeSet()->isEmpty() && node->isActive() && node->getOneEquivalentConcept()->getConceptTag() != 0 && node->getOneEquivalentConcept()->getConceptTag() != 1) {
239 // bool bug = true;
240 // }
241 //}
242
243 //for (QHash<CConcept *,CHierarchyNode *>::iterator conIt = conNodeHash.begin(), conItEnd = conNodeHash.end(); conIt != conItEnd; ++conIt) {
244 // CConcept* con = conIt.key();
245 // cint count = 0;
246 // CHierarchyNode* firstNode = nullptr;
247 // for (QList<CHierarchyNode *>::iterator it = nodeContainer.begin(); it != nodeContainer.end(); ++it) {
248 // CHierarchyNode* node = *it;
249 // if (node->isActive() && node->getEquivalentConceptList()->count(con) > 0) {
250 // count += node->getEquivalentConceptList()->count(con);
251 // if (count > 1) {
252 // bool bug = true;
253 // }
254 // firstNode = node;
255 // }
256 // }
257 //}
258
259
260 //if (testCompStatistics) {
261 // testSubsumptionStatistics(testCompStatistics->getTestedSubsumptionCount());
262 //}
263
264 //for (QHash<CConcept *,CHierarchyNode *>::const_iterator it = conNodeHash.constBegin(), itEnd = conNodeHash.constEnd(); it != itEnd; ++it) {
265 // CHierarchyNode *node = *it;
266
267 //if (node->getEquivalentConceptsNodeString() == "#Existential" || node->getEquivalentConceptsNodeString() == "#EliminateExistential2") {
268 // foreach (CHierarchyNode *otherNode, *node->getIncompatiblePredecessorNodeSet()) {
269 // if (otherNode->getEquivalentConceptsNodeString() == "#Existential" || otherNode->getEquivalentConceptsNodeString() == "#EliminateExistential2") {
270 // bool bug = true;
271 // }
272 // }
273 // foreach (CHierarchyNode *otherNode, *node->getSuccessorNodeSet()) {
274 // if (otherNode->getEquivalentConceptsNodeString() == "#Existential" || otherNode->getEquivalentConceptsNodeString() == "#EliminateExistential2") {
275 // bool bug = true;
276 // }
277 // }
278 //}
279
280 // if (isActiveNode(node)) {
281 // bool consistence = true;
282 // foreach (CHierarchyNode *otherNode, node->getPredecessorNodeList()) {
283 // if (isActiveNode(otherNode) && node != bottomNode) {
284 // if (node->hasIncompatiblePredecessorNode(otherNode)) {
285 // consistence = false;
286 // }
287 // }
288 // }
289 // foreach (CHierarchyNode *otherNode, node->getSuccessorNodeList()) {
290 // if (otherNode != bottomNode && node->hasIncompatibleSuccessorNode(otherNode) && isActiveNode(otherNode)) {
291 // consistence = false;
292 // }
293 // }
294 // foreach (CHierarchyNode *otherNode, node->getChildNodeList()) {
295 // if (!otherNode->hasParentNode(node) && isActiveNode(otherNode)) {
296 // consistence = false;
297 // }
298 // if (!otherNode->hasPredecessorNode(node) && isActiveNode(otherNode)) {
299 // consistence = false;
300 // }
301 // }
302 // foreach (CHierarchyNode *otherNode, node->getParentNodeList()) {
303 // if (!otherNode->hasChildNode(node) && isActiveNode(otherNode)) {
304 // consistence = false;
305 // }
306 // if (!otherNode->hasSuccessorNode(node) && isActiveNode(otherNode)) {
307 // consistence = false;
308 // }
309 // }
310 // foreach (CHierarchyNode *otherNode, node->getPredecessorNodeList()) {
311 // if (!otherNode->hasSuccessorNode(node) && isActiveNode(otherNode)) {
312 // consistence = false;
313 // }
314 // }
315 // foreach (CHierarchyNode *otherNode, node->getSuccessorNodeList()) {
316 // if (!otherNode->hasPredecessorNode(node) && isActiveNode(otherNode)) {
317 // consistence = false;
318 // }
319 // }
320 // foreach (CHierarchyNode *otherNode, *node->getIncompatiblePredecessorNodeSet()) {
321 // if (!otherNode->hasIncompatibleSuccessorNode(node) && isActiveNode(otherNode) && node != bottomNode) {
322 // consistence = false;
323 // }
324 // }
325 // foreach (CHierarchyNode *otherNode, *node->getIncompatibleSuccessorNodeSet()) {
326 // if (!otherNode->hasIncompatiblePredecessorNode(node) && isActiveNode(otherNode)) {
327 // consistence = false;
328 // }
329 // }
330 // if (!consistence) {
331 // LOG(ERROR,"::Konclude::Taxonomy::PartialPruningTaxonomy",logTr("Taxonomy inconsistence detected."),this);
332
333 // }
334 // }
335 //}
336 }
337
338
339
340 void CPartialPruningTaxonomy::testSubsumptionStatistics(qint64 subsumCount) {
341 qint64 testSubSumCount = 0;
342
343 QSet<CHierarchyNode *> checkedSet;
344
345 for (QHash<CConcept *,CHierarchyNode *>::const_iterator it = conNodeHash.constBegin(), itEnd = conNodeHash.constEnd(); it != itEnd; ++it) {
346 CHierarchyNode *node = *it;
347 qint64 thisNodeSubSumCount = 0;
348 if (isActiveNode(node) && !checkedSet.contains(node)) {
349 checkedSet.insert(node);
350 if (node->getEquivalentConceptCount() > 1) {
351 thisNodeSubSumCount += node->getEquivalentConceptCount();
352 }
353 foreach (CHierarchyNode *otherNode, node->getPredecessorNodeList()) {
354 if (isActiveNode(otherNode)) {
355 thisNodeSubSumCount += node->getEquivalentConceptCount() * otherNode->getEquivalentConceptCount();
356 }
357 }
358 foreach (CHierarchyNode *otherNode, *node->getIncompatiblePredecessorNodeSet()) {
359 if (isActiveNode(otherNode)) {
360 thisNodeSubSumCount += node->getEquivalentConceptCount() * otherNode->getEquivalentConceptCount();
361 }
362 }
363 }
364 testSubSumCount += thisNodeSubSumCount;
365 }
366 if (subsumCount != testSubSumCount) {
367 bool bug = true;
368 }
369 }
370
371
372
373 CTaxonomy *CPartialPruningTaxonomy::completeUnknownAsNonSubsumptions() {
374 foreach (CHierarchyNode *updateNode, bottomNode->getParentNodeList()) {
375 QSet<CHierarchyNode *> *parentNodeSet = updateNode->getParentNodeSet();
376 for (QSet<CHierarchyNode *>::const_iterator it1 = parentNodeSet->constBegin(), it1End = parentNodeSet->constEnd(); it1 != it1End; ++it1) {
377 CHierarchyNode *parentNode(*it1);
378 if (!updateNode->hasIncompatibleSuccessorNode(parentNode)) {
379 setConceptNotSubsumption(parentNode->getOneEquivalentConcept(),updateNode->getOneEquivalentConcept());
380 }
381 QSet<CHierarchyNode *> *predNodeSet = updateNode->getPredecessorNodeSet();
382 for (QSet<CHierarchyNode *>::const_iterator it1 = predNodeSet->constBegin(), it1End = predNodeSet->constEnd(); it1 != it1End; ++it1) {
383 CHierarchyNode *predNode(*it1);
384 QSet<CHierarchyNode *> *predChildNodeSet = predNode->getChildNodeSet();
385 for (QSet<CHierarchyNode *>::const_iterator it2 = predChildNodeSet->constBegin(), it2End = predChildNodeSet->constEnd(); it2 != it2End; ++it2) {
386 CHierarchyNode *predChildNode(*it2);
387 for (QSet<CHierarchyNode *>::const_iterator it3 = predChildNodeSet->constBegin(), it3End = predChildNodeSet->constEnd(); it3 != it3End; ++it3) {
388 CHierarchyNode *otherPredChildNode(*it3);
389 if (predChildNode != otherPredChildNode) {
390 if (!predChildNode->hasIncompatiblePredecessorNode(otherPredChildNode)) {
391 setConceptNotSubsumption(predChildNode->getOneEquivalentConcept(),otherPredChildNode->getOneEquivalentConcept());
392 }
393 }
394 }
395 if (predChildNode != updateNode) {
396 if (!updateNode->hasPredecessorNode(predChildNode) && !updateNode->hasIncompatiblePredecessorNode(predChildNode)) {
397 setConceptNotSubsumption(updateNode->getOneEquivalentConcept(),predChildNode->getOneEquivalentConcept());
398 }
399 if (!predChildNode->hasIncompatibleSuccessorNode(predNode)) {
400 setConceptNotSubsumption(predNode->getOneEquivalentConcept(),predChildNode->getOneEquivalentConcept());
401 }
402 }
403 }
404 }
405 }
406 }
407 return this;
408 }
409
410
411
412 }; // end namespace Classifier
413
414 }; // end namespace Reasoner
415
416 }; // end namespace Konclude