Debian Codegraph @

Kernel/TaxonomyCreator.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*******************************************************\
21 |* Implementation of taxonomy building for the FaCT++ *|
22 \*******************************************************/
23
24 #include "TaxonomyCreator.h"cxx . . $ `<file>/Kernel/TaxonomyCreator.h`/
25 #include "logging.h"cxx . . $ `<file>/Kernel/logging.h`/
26
27 void TaxonomyCreatorcxx . . $ TaxonomyCreator# :: printcxx . . $ TaxonomyCreator#print(490063bd7e6d8d5d). ( stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& o ) const
28 {
29 o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Taxonomy consists of " <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). nEntriescxx . . $ TaxonomyCreator#nEntries. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " entries\n";
30 o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " of which " <<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed). nCDEntriescxx . . $ TaxonomyCreator#nCDEntries. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " are completely defined\n\n";
31 pTaxcxx . . $ TaxonomyCreator#pTax.->printcxx . . $ Taxonomy#print(490063bd7e6d8d5d).(o);
32 }
33
34 void
35 TaxonomyCreatorcxx . . $ TaxonomyCreator# :: performClassificationcxx . . $ TaxonomyCreator#performClassification(49f6e7a06ebc5aa8). ( void )
36 {
37 // do something before classification (tunable)
38 preClassificationActionscxx . . $ TaxonomyCreator#preClassificationActions(49f6e7a06ebc5aa8).();
39
40 ++nEntriescxx . . $ TaxonomyCreator#nEntries.;
41
42 if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llStartCfyEntrycxx . . $ llStartCfyEntry.) && needLoggingcxx . . $ TaxonomyCreator#needLogging(50ce9a9e25b4a850).() )
43 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n\nTAX: start classifying entry " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). curEntrycxx . . $ TaxonomyCreator#curEntry.->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).();
44
45 // if no classification needed -- nothing to do
46 if ( immediatelyClassifiedcxx . . $ TaxonomyCreator#immediatelyClassified(c9857a73d68e59ba).() )
47 return;
48
49 // perform main classification
50 generalTwoPhaseClassificationcxx . . $ TaxonomyCreator#generalTwoPhaseClassification(49f6e7a06ebc5aa8).();
51
52 // create new vertex
53 pTaxcxx . . $ TaxonomyCreator#pTax.->finishCurrentNodecxx . . $ Taxonomy#finishCurrentNode(49f6e7a06ebc5aa8).();
54
55 // clear all labels
56 clearLabelscxx . . $ TaxonomyCreator#clearLabels(49f6e7a06ebc5aa8).();
57 }
58
59 void TaxonomyCreatorcxx . . $ TaxonomyCreator# :: generalTwoPhaseClassificationcxx . . $ TaxonomyCreator#generalTwoPhaseClassification(49f6e7a06ebc5aa8). ( void )
60 {
61 // Top-Down phase
62
63 // setup TD phase (ie, identify parent candidates)
64 setupTopDowncxx . . $ TaxonomyCreator#setupTopDown(49f6e7a06ebc5aa8).();
65
66 // run TD phase if necessary (ie, entry is completely defined)
67 if ( needTopDowncxx . . $ TaxonomyCreator#needTopDown(50ce9a9e25b4a850).() )
68 {
69 setValuecxx . . $ TaxonomyCreator#setValue(2266beb367975663). ( pTaxcxx . . $ TaxonomyCreator#pTax.->getTopVertexcxx . . $ Taxonomy#getTopVertex(16f9ca5fa66f7d36).(), true ); // C [= TOP == true
70 setValuecxx . . $ TaxonomyCreator#setValue(2266beb367975663). ( pTaxcxx . . $ TaxonomyCreator#pTax.->getBottomVertexcxx . . $ Taxonomy#getBottomVertex(16f9ca5fa66f7d36).(), false ); // C [= BOT == false (caught by UNSAT)
71 upDirectioncxx . . $ TaxonomyCreator#upDirection. = false;
72 runTopDowncxx . . $ TaxonomyCreator#runTopDown(49f6e7a06ebc5aa8).();
73 }
74
75 clearLabelscxx . . $ TaxonomyCreator#clearLabels(49f6e7a06ebc5aa8).();
76
77 // Bottom-Up phase
78
79 // run BU if necessary
80 if ( needBottomUpcxx . . $ TaxonomyCreator#needBottomUp(50ce9a9e25b4a850).() )
81 {
82 setValuecxx . . $ TaxonomyCreator#setValue(2266beb367975663). ( pTaxcxx . . $ TaxonomyCreator#pTax.->getBottomVertexcxx . . $ Taxonomy#getBottomVertex(16f9ca5fa66f7d36).(), true ); // BOT [= C == true
83 upDirectioncxx . . $ TaxonomyCreator#upDirection. = true;
84 runBottomUpcxx . . $ TaxonomyCreator#runBottomUp(49f6e7a06ebc5aa8).();
85 }
86
87 clearLabelscxx . . $ TaxonomyCreator#clearLabels(49f6e7a06ebc5aa8).();
88 }
89
90 bool
91 TaxonomyCreatorcxx . . $ TaxonomyCreator# :: isDirectParentcxx . . $ TaxonomyCreator#isDirectParent(5273acd5b7b596a5). ( TaxonomyVertexcxx . . $ TaxonomyVertex#* v ) const
92 {
93 for ( TaxonomyVertexcxx . . $ TaxonomyVertex#::const_iteratorcxx . . $ TaxonomyVertex#const_iterator# q = vcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ea0f1a22ba5f21a0).->begincxx . . $ TaxonomyVertex#begin(98265602f6db0361).(/*upDirection=*/false), q_end = vcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ea0f1a22ba5f21a0).->endcxx . . $ TaxonomyVertex#end(98265602f6db0361).(/*upDirection=*/false); q !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). q_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).q )
94 if ( isValuedcxx . . $ TaxonomyCreator#isValued(5273acd5b7b596a5).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q) && getValuecxx . . $ TaxonomyCreator#getValue(5273acd5b7b596a5).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q) )
95 return false;
96 return true;
97 }
98
99 void TaxonomyCreatorcxx . . $ TaxonomyCreator# :: setNonRedundantCandidatescxx . . $ TaxonomyCreator#setNonRedundantCandidates(49f6e7a06ebc5aa8). ( void )
100 {
101 if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llCDConceptcxx . . $ llCDConcept.) && needLoggingcxx . . $ TaxonomyCreator#needLogging(50ce9a9e25b4a850).() )
102 {
103 if ( !curEntrycxx . . $ TaxonomyCreator#curEntry.->hasToldSubsumerscxx . . $ ClassifiableEntry#hasToldSubsumers(50ce9a9e25b4a850).() )
104 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nTAX: TOP";
105 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " completely defines concept " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). curEntrycxx . . $ TaxonomyCreator#curEntry.->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).();
106 }
107
108 // test if some "told subsumer" is not an immediate TS (ie, not a border element)
109 for ( ss_iteratorcxx . . $ TaxonomyCreator#ss_iterator# p = told_begincxx . . $ TaxonomyCreator#told_begin(eb69919d41dfeca3).(), p_end = told_endcxx . . $ TaxonomyCreator#told_end(eb69919d41dfeca3).(); p <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). p_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p )
110 addPossibleParentcxx . . $ TaxonomyCreator#addPossibleParent(ea5ea6ad7488bb8a).((*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->getTaxVertexcxx . . $ ClassifiableEntry#getTaxVertex(16f9ca5fa66f7d36).());
111 }
112
113 void TaxonomyCreatorcxx . . $ TaxonomyCreator# :: setToldSubsumerscxx . . $ TaxonomyCreator#setToldSubsumers(49f6e7a06ebc5aa8). ( void )
114 {
115 if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llTSListcxx . . $ llTSList.) && needLoggingcxx . . $ TaxonomyCreator#needLogging(50ce9a9e25b4a850).() && !ksStackcxx . . $ TaxonomyCreator#ksStack..topcxx . . $ SearchableStack#top(ce3ba953f324383c).()->s_emptycxx . . $ TaxonomyCreator#KnownSubsumers#s_empty(c9857a73d68e59ba).() )
116 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nTAX: told subsumers";
117
118 for ( ss_iteratorcxx . . $ TaxonomyCreator#ss_iterator# p = told_begincxx . . $ TaxonomyCreator#told_begin(eb69919d41dfeca3).(), p_end = told_endcxx . . $ TaxonomyCreator#told_end(eb69919d41dfeca3).(); p <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). p_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p )
119 {
120 if ( !(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->isClassifiedcxx . . $ ClassifiableEntry#isClassified(50ce9a9e25b4a850).() ) // non-primitive/non-classifiable concept
121 continue; // safety check
122
123 if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llTSListcxx . . $ llTSList.) && needLoggingcxx . . $ TaxonomyCreator#needLogging(50ce9a9e25b4a850).() )
124 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " '" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "'";
125
126 propagateTrueUpcxx . . $ TaxonomyCreator#propagateTrueUp(ea5ea6ad7488bb8a).((*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->getTaxVertexcxx . . $ ClassifiableEntry#getTaxVertex(16f9ca5fa66f7d36).());
127 }
128
129 if ( !ksStackcxx . . $ TaxonomyCreator#ksStack..topcxx . . $ SearchableStack#top(ce3ba953f324383c).()->p_emptycxx . . $ TaxonomyCreator#KnownSubsumers#p_empty(c9857a73d68e59ba).() && LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llTSListcxx . . $ llTSList.) && needLoggingcxx . . $ TaxonomyCreator#needLogging(50ce9a9e25b4a850).() )
130 {
131 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " and possibly ";
132
133 for ( ss_iteratorcxx . . $ TaxonomyCreator#ss_iterator# q = ksStackcxx . . $ TaxonomyCreator#ksStack..topcxx . . $ SearchableStack#top(ce3ba953f324383c).()->p_begincxx . . $ TaxonomyCreator#KnownSubsumers#p_begin(eb69919d41dfeca3).(), q_end = ksStackcxx . . $ TaxonomyCreator#ksStack..topcxx . . $ SearchableStack#top(ce3ba953f324383c).()->p_endcxx . . $ TaxonomyCreator#KnownSubsumers#p_end(eb69919d41dfeca3).(); q <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). q_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).q )
134 LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " '" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q)->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "'";
135 }
136 }
137
138 void
139 TaxonomyCreatorcxx . . $ TaxonomyCreator# :: propagateTrueUpcxx . . $ TaxonomyCreator#propagateTrueUp(ea5ea6ad7488bb8a). ( TaxonomyVertexcxx . . $ TaxonomyVertex#* node )
140 {
141 // if taxonomy class already checked -- do nothing
142 if ( isValuedcxx . . $ TaxonomyCreator#isValued(5273acd5b7b596a5).(node) )
143 {
144 fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`! ( getValue(node) );
145 return;
146 }
147
148 // otherwise -- value it...
149 setValuecxx . . $ TaxonomyCreator#setValue(2266beb367975663). ( node, true );
150
151 // ... and value all parents
152 for ( TaxonomyVertexcxx . . $ TaxonomyVertex#::iteratorcxx . . $ TaxonomyVertex#iterator# p = node->begincxx . . $ TaxonomyVertex#begin(98265602f6db0361).(/*upDirection=*/true), p_end = node->endcxx . . $ TaxonomyVertex#end(98265602f6db0361).(/*upDirection=*/true); p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). p_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p )
153 propagateTrueUpcxx . . $ TaxonomyCreator#propagateTrueUp(ea5ea6ad7488bb8a).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p);
154 }
155
156 /// propagate the FALSE value of the KS subsumption down the hierarchy
157 void
158 TaxonomyCreatorcxx . . $ TaxonomyCreator# :: propagateFalseDowncxx . . $ TaxonomyCreator#propagateFalseDown(ea5ea6ad7488bb8a). ( TaxonomyVertexcxx . . $ TaxonomyVertex#* node )
159 {
160 // if taxonomy class already checked -- do nothing
161 if ( isValuedcxx . . $ TaxonomyCreator#isValued(5273acd5b7b596a5).(node) )
162 {
163 fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`! ( getValue(node) == false );
164 return;
165 }
166
167 // otherwise -- value it...
168 setValuecxx . . $ TaxonomyCreator#setValue(2266beb367975663). ( node, false );
169
170 // ... and value all children
171 for ( TaxonomyVertexcxx . . $ TaxonomyVertex#::iteratorcxx . . $ TaxonomyVertex#iterator# p = node->begincxx . . $ TaxonomyVertex#begin(98265602f6db0361).(/*upDirection=*/false), p_end = node->endcxx . . $ TaxonomyVertex#end(98265602f6db0361).(/*upDirection=*/false); p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). p_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p )
172 propagateFalseDowncxx . . $ TaxonomyCreator#propagateFalseDown(ea5ea6ad7488bb8a).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p);
173 }
174
175 //-----------------------------------------------------------------
176 //-- DFS-based classification methods
177 //-----------------------------------------------------------------
178
179 ClassifiableEntrycxx . . $ ClassifiableEntry#*
180 TaxonomyCreatorcxx . . $ TaxonomyCreator# :: prepareTScxx . . $ TaxonomyCreator#prepareTS(b3dd06e634335b24). ( ClassifiableEntrycxx . . $ ClassifiableEntry#* cur )
181 {
182 // we just found that TS forms a cycle -- return stop-marker
183 if ( waitStackcxx . . $ TaxonomyCreator#waitStack..containscxx . . $ SearchableStack#contains(8877ebe272b9620d).(cur) )
184 return cur;
185
186 // starting from the topmost entry
187 addTopcxx . . $ TaxonomyCreator#addTop(866fbb357e35bef0).(cur);
188 // true iff CUR is a reason of the cycle
189 bool cycleFound = false;
190 // for all the told subsumers...
191 for ( ss_iteratorcxx . . $ TaxonomyCreator#ss_iterator# p = told_begincxx . . $ TaxonomyCreator#told_begin(eb69919d41dfeca3).(), p_end = told_endcxx . . $ TaxonomyCreator#told_end(eb69919d41dfeca3).(); p <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). p_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p )
192 if ( !(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->isClassifiedcxx . . $ ClassifiableEntry#isClassified(50ce9a9e25b4a850).() ) // need to classify it first
193 {
194 if ( unlikelycxx . . $ `Kernel/globaldef.h:29:10`!((*p)->isNonClassifiable()) )
195 continue;
196 // prepare TS for *p
197 ClassifiableEntrycxx . . $ ClassifiableEntry#* v = prepareTScxx . . $ TaxonomyCreator#prepareTS(b3dd06e634335b24).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p);
198 // if NULL is returned -- just continue
199 if ( v == nullptr )
200 continue;
201 if ( v == cur ) // current cycle is finished, all saved in Syns
202 {
203 // after classification of CUR we need to mark all the Syns as synonyms
204 cycleFound = true;
205 // continue to prepare its classification
206 continue;
207 }
208 else
209 {
210 // arbitrary vertex in a cycle: save in synonyms of a root cause
211 Synscxx . . $ TaxonomyCreator#Syns..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(cur);
212 // don't need to classify it
213 removeTopcxx . . $ TaxonomyCreator#removeTop(49f6e7a06ebc5aa8).();
214 // return the cycle cause
215 return v;
216 }
217 }
218 // all TS are ready here -- let's classify!
219 classifyTopcxx . . $ TaxonomyCreator#classifyTop(49f6e7a06ebc5aa8).();
220 // now if CUR is the reason of cycle mark all SYNs as synonyms
221 if ( cycleFound )
222 {
223 TaxonomyVertexcxx . . $ TaxonomyVertex#* syn = cur->getTaxVertexcxx . . $ ClassifiableEntry#getTaxVertex(16f9ca5fa66f7d36).();
224 for ( const ClassifiableEntrycxx . . $ ClassifiableEntry#* entry : Synscxx . . $ TaxonomyCreator#Syns. )
225 syn->addSynonymcxx . . $ TaxonomyVertex#addSynonym(25e3a48069a92e6e).(entry);
226 Synscxx . . $ TaxonomyCreator#Syns..clearcxx . . $ std/vector#clear(49f6e7a06ebc5aa8).();
227 }
228 // here the cycle is done
229 return nullptr;
230 }
231