Debian Codegraph @

FaCT++.Python/lib/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"
25 #include "logging.h"
26
27 void TaxonomyCreator :: print ( std::ostream& o ) const
28 {
29 o << "Taxonomy consists of " << nEntries << " entries\n";
30 o << " of which " << nCDEntries << " are completely defined\n\n";
31 pTax->print(o);
32 }
33
34 void
35 TaxonomyCreator :: performClassification ( void )
36 {
37 // do something before classification (tunable)
38 preClassificationActions();
39
40 ++nEntries;
41
42 if ( LLM.isWritable(llStartCfyEntry) && needLogging() )
43 LL << "\n\nTAX: start classifying entry " << curEntry->getName();
44
45 // if no classification needed -- nothing to do
46 if ( immediatelyClassified() )
47 return;
48
49 // perform main classification
50 generalTwoPhaseClassification();
51
52 // create new vertex
53 pTax->finishCurrentNode();
54
55 // clear all labels
56 clearLabels();
57 }
58
59 void TaxonomyCreator :: generalTwoPhaseClassification ( void )
60 {
61 // Top-Down phase
62
63 // setup TD phase (ie, identify parent candidates)
64 setupTopDown();
65
66 // run TD phase if necessary (ie, entry is completely defined)
67 if ( needTopDown() )
68 {
69 setValue ( pTax->getTopVertex(), true ); // C [= TOP == true
70 setValue ( pTax->getBottomVertex(), false ); // C [= BOT == false (caught by UNSAT)
71 upDirection = false;
72 runTopDown();
73 }
74
75 clearLabels();
76
77 // Bottom-Up phase
78
79 // run BU if necessary
80 if ( needBottomUp() )
81 {
82 setValue ( pTax->getBottomVertex(), true ); // BOT [= C == true
83 upDirection = true;
84 runBottomUp();
85 }
86
87 clearLabels();
88 }
89
90 bool
91 TaxonomyCreator :: isDirectParent ( TaxonomyVertex* v ) const
92 {
93 for ( TaxonomyVertex::const_iterator q = v->begin(/*upDirection=*/false), q_end = v->end(/*upDirection=*/false); q != q_end; ++q )
94 if ( isValued(*q) && getValue(*q) )
95 return false;
96 return true;
97 }
98
99 void TaxonomyCreator :: setNonRedundantCandidates ( void )
100 {
101 if ( LLM.isWritable(llCDConcept) && needLogging() )
102 {
103 if ( !curEntry->hasToldSubsumers() )
104 LL << "\nTAX: TOP";
105 LL << " completely defines concept " << curEntry->getName();
106 }
107
108 // test if some "told subsumer" is not an immediate TS (ie, not a border element)
109 for ( ss_iterator p = told_begin(), p_end = told_end(); p < p_end; ++p )
110 addPossibleParent((*p)->getTaxVertex());
111 }
112
113 void TaxonomyCreator :: setToldSubsumers ( void )
114 {
115 if ( LLM.isWritable(llTSList) && needLogging() && !ksStack.top()->s_empty() )
116 LL << "\nTAX: told subsumers";
117
118 for ( ss_iterator p = told_begin(), p_end = told_end(); p < p_end; ++p )
119 {
120 if ( !(*p)->isClassified() ) // non-primitive/non-classifiable concept
121 continue; // safety check
122
123 if ( LLM.isWritable(llTSList) && needLogging() )
124 LL << " '" << (*p)->getName() << "'";
125
126 propagateTrueUp((*p)->getTaxVertex());
127 }
128
129 if ( !ksStack.top()->p_empty() && LLM.isWritable(llTSList) && needLogging() )
130 {
131 LL << " and possibly ";
132
133 for ( ss_iterator q = ksStack.top()->p_begin(), q_end = ksStack.top()->p_end(); q < q_end; ++q )
134 LL << " '" << (*q)->getName() << "'";
135 }
136 }
137
138 void
139 TaxonomyCreator :: propagateTrueUp ( TaxonomyVertex* node )
140 {
141 // if taxonomy class already checked -- do nothing
142 if ( isValued(node) )
143 {
144 fpp_assert ( getValue(node) );
145 return;
146 }
147
148 // otherwise -- value it...
149 setValue ( node, true );
150
151 // ... and value all parents
152 for ( TaxonomyVertex::iterator p = node->begin(/*upDirection=*/true), p_end = node->end(/*upDirection=*/true); p != p_end; ++p )
153 propagateTrueUp(*p);
154 }
155
156 /// propagate the FALSE value of the KS subsumption down the hierarchy
157 void
158 TaxonomyCreator :: propagateFalseDown ( TaxonomyVertex* node )
159 {
160 // if taxonomy class already checked -- do nothing
161 if ( isValued(node) )
162 {
163 fpp_assert ( getValue(node) == false );
164 return;
165 }
166
167 // otherwise -- value it...
168 setValue ( node, false );
169
170 // ... and value all children
171 for ( TaxonomyVertex::iterator p = node->begin(/*upDirection=*/false), p_end = node->end(/*upDirection=*/false); p != p_end; ++p )
172 propagateFalseDown(*p);
173 }
174
175 //-----------------------------------------------------------------
176 //-- DFS-based classification methods
177 //-----------------------------------------------------------------
178
179 ClassifiableEntry*
180 TaxonomyCreator :: prepareTS ( ClassifiableEntry* cur )
181 {
182 // we just found that TS forms a cycle -- return stop-marker
183 if ( waitStack.contains(cur) )
184 return cur;
185
186 // starting from the topmost entry
187 addTop(cur);
188 // true iff CUR is a reason of the cycle
189 bool cycleFound = false;
190 // for all the told subsumers...
191 for ( ss_iterator p = told_begin(), p_end = told_end(); p < p_end; ++p )
192 if ( !(*p)->isClassified() ) // need to classify it first
193 {
194 if ( unlikely((*p)->isNonClassifiable()) )
195 continue;
196 // prepare TS for *p
197 ClassifiableEntry* v = prepareTS(*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 Syns.push_back(cur);
212 // don't need to classify it
213 removeTop();
214 // return the cycle cause
215 return v;
216 }
217 }
218 // all TS are ready here -- let's classify!
219 classifyTop();
220 // now if CUR is the reason of cycle mark all SYNs as synonyms
221 if ( cycleFound )
222 {
223 TaxonomyVertex* syn = cur->getTaxVertex();
224 for ( const ClassifiableEntry* entry : Syns )
225 syn->addSynonym(entry);
226 Syns.clear();
227 }
228 // here the cycle is done
229 return nullptr;
230 }
231