| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef DLCONCEPTTAXONOMY_H |
| 21 |
#define DLCONCEPTTAXONOMY_H |
| 22 |
|
| 23 |
#include "TaxonomyCreator.h" |
| 24 |
#include "dlTBox.h" |
| 25 |
#include "tProgressMonitor.h" |
| 26 |
|
| 27 |
|
| 28 |
class DLConceptTaxonomy: public TaxonomyCreator |
| 29 |
{ |
| 30 |
protected: |
| 31 |
typedef std::vector<TaxonomyVertex*> TaxVertexVec; |
| 32 |
|
| 33 |
class DerivedSubsumers: public KnownSubsumers |
| 34 |
{ |
| 35 |
protected: |
| 36 |
|
| 37 |
typedef TaxonomyCreator::SubsumerSet SubsumerSet; |
| 38 |
|
| 39 |
typedef SubsumerSet::iterator ss_iterator; |
| 40 |
|
| 41 |
protected: |
| 42 |
|
| 43 |
SubsumerSet Sure, Possible; |
| 44 |
|
| 45 |
public: |
| 46 |
|
| 47 |
DerivedSubsumers ( const SubsumerSet& sure, const SubsumerSet& possible ) |
| 48 |
: KnownSubsumers() |
| 49 |
, Sure(sure) |
| 50 |
, Possible(possible) |
| 51 |
{} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
ss_iterator s_begin ( void ) override { return Sure.begin(); } |
| 57 |
|
| 58 |
ss_iterator s_end ( void ) override { return Sure.end(); } |
| 59 |
|
| 60 |
ss_iterator p_begin ( void ) override { return Possible.begin(); } |
| 61 |
|
| 62 |
ss_iterator p_end ( void ) override { return Possible.end(); } |
| 63 |
}; |
| 64 |
|
| 65 |
protected: |
| 66 |
|
| 67 |
TBox& tBox; |
| 68 |
|
| 69 |
std::set<const TNamedEntity*> MPlus, MMinus; |
| 70 |
|
| 71 |
std::set<TaxonomyVertex*> candidates; |
| 72 |
|
| 73 |
bool useCandidates = false; |
| 74 |
|
| 75 |
TaxVertexVec Common; |
| 76 |
|
| 77 |
unsigned int nCommon = 0; |
| 78 |
|
| 79 |
|
| 80 |
unsigned long nConcepts = 0; |
| 81 |
unsigned long nTries = 0; |
| 82 |
unsigned long nPositives = 0; |
| 83 |
unsigned long nNegatives = 0; |
| 84 |
unsigned long nSearchCalls = 0; |
| 85 |
unsigned long nSubCalls = 0; |
| 86 |
unsigned long nNonTrivialSubCalls = 0; |
| 87 |
|
| 88 |
|
| 89 |
unsigned long nCachedPositive = 0; |
| 90 |
|
| 91 |
unsigned long nCachedNegative = 0; |
| 92 |
|
| 93 |
unsigned long nSortedNegative = 0; |
| 94 |
|
| 95 |
unsigned long nModuleNegative = 0; |
| 96 |
|
| 97 |
|
| 98 |
TProgressMonitor* pTaxProgress = nullptr; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
bool flagNeedBottomUp = false; |
| 104 |
|
| 105 |
protected: |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
const TConcept* curConcept ( void ) const { return static_cast<const TConcept*>(curEntry); } |
| 112 |
|
| 113 |
bool testSub ( const TConcept* p, const TConcept* q ); |
| 114 |
|
| 115 |
bool testSubTBox ( const TConcept* p, const TConcept* q ) |
| 116 |
{ |
| 117 |
bool res = tBox.isSubHolds ( p, q ); |
| 118 |
|
| 119 |
|
| 120 |
++nTries; |
| 121 |
|
| 122 |
if ( res ) |
| 123 |
++nPositives; |
| 124 |
else |
| 125 |
++nNegatives; |
| 126 |
|
| 127 |
return res; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
void searchBaader ( TaxonomyVertex* cur ); |
| 134 |
|
| 135 |
bool enhancedSubs1 ( TaxonomyVertex* cur ); |
| 136 |
|
| 137 |
bool enhancedSubs2 ( TaxonomyVertex* cur ) |
| 138 |
{ |
| 139 |
|
| 140 |
if ( unlikely(upDirection && !cur->isCommon()) ) |
| 141 |
return false; |
| 142 |
if ( unlikely ( useCandidates && candidates.find(cur) == candidates.end() ) ) |
| 143 |
return false; |
| 144 |
return enhancedSubs1(cur); |
| 145 |
} |
| 146 |
|
| 147 |
inline bool enhancedSubs ( TaxonomyVertex* cur ) |
| 148 |
{ |
| 149 |
++nSubCalls; |
| 150 |
|
| 151 |
if ( isValued(cur) ) |
| 152 |
return getValue(cur); |
| 153 |
else |
| 154 |
return setValue ( cur, enhancedSubs2(cur) ); |
| 155 |
} |
| 156 |
|
| 157 |
bool testSubsumption ( TaxonomyVertex* cur ); |
| 158 |
|
| 159 |
bool possibleSub ( TaxonomyVertex* v ) const |
| 160 |
{ |
| 161 |
const TConcept* C = static_cast<const TConcept*>(v->getPrimer()); |
| 162 |
|
| 163 |
if ( !C->isPrimitive() ) |
| 164 |
return true; |
| 165 |
|
| 166 |
return ksStack.top()->isPossibleSub(C); |
| 167 |
} |
| 168 |
|
| 169 |
bool isNotInModule ( const TNamedEntity* entity ) const; |
| 170 |
|
| 171 |
void reclassify ( TaxonomyVertex* node, const TSignature* s ); |
| 172 |
|
| 173 |
|
| 174 |
void propagateOneCommon ( TaxonomyVertex* node ); |
| 175 |
|
| 176 |
bool propagateUp ( void ); |
| 177 |
|
| 178 |
void clearCommon ( void ) |
| 179 |
{ |
| 180 |
for ( TaxVertexVec::iterator p = Common.begin(), p_end = Common.end(); p < p_end; ++p ) |
| 181 |
(*p)->clearCommon(); |
| 182 |
Common.clear(); |
| 183 |
} |
| 184 |
|
| 185 |
bool isUnsatisfiable ( void ); |
| 186 |
|
| 187 |
void fillCandidates ( TaxonomyVertex* cur ); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
KnownSubsumers* buildKnownSubsumers ( ClassifiableEntry* p ) override; |
| 195 |
|
| 196 |
const TSignature* buildSignature ( ClassifiableEntry* p ) override; |
| 197 |
|
| 198 |
bool immediatelyClassified ( void ) override; |
| 199 |
|
| 200 |
bool isEqualToTop ( void ); |
| 201 |
|
| 202 |
|
| 203 |
bool needTopDown ( void ) const override |
| 204 |
{ return !(useCompletelyDefined && curEntry->isCompletelyDefined ()); } |
| 205 |
|
| 206 |
void runTopDown ( void ) override { searchBaader(pTax->getTopVertex()); } |
| 207 |
|
| 208 |
bool needBottomUp ( void ) const override |
| 209 |
{ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
return flagNeedBottomUp || !useCompletelyDefined || curConcept()->isNonPrimitive(); |
| 214 |
} |
| 215 |
|
| 216 |
void runBottomUp ( void ) override |
| 217 |
{ |
| 218 |
if ( propagateUp() ) |
| 219 |
return; |
| 220 |
if ( isEqualToTop() ) |
| 221 |
return; |
| 222 |
if ( pTax->queryMode() ) |
| 223 |
searchBaader(pTax->getBottomVertex()); |
| 224 |
else |
| 225 |
for ( TaxVertexVec::iterator p = Common.begin(), p_end = Common.end(); p < p_end; ++p ) |
| 226 |
if ( (*p)->noNeighbours(false) ) |
| 227 |
searchBaader(*p); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
void preClassificationActions ( void ) override |
| 232 |
{ |
| 233 |
++nConcepts; |
| 234 |
if ( pTaxProgress != nullptr ) |
| 235 |
pTaxProgress->nextClass(); |
| 236 |
} |
| 237 |
|
| 238 |
bool classifySynonym ( void ) override; |
| 239 |
|
| 240 |
|
| 241 |
bool needLogging ( void ) const override { return true; } |
| 242 |
|
| 243 |
public: |
| 244 |
|
| 245 |
DLConceptTaxonomy ( Taxonomy* tax, TBox& kb ) |
| 246 |
: TaxonomyCreator(tax) |
| 247 |
, tBox(kb) |
| 248 |
{} |
| 249 |
|
| 250 |
DLConceptTaxonomy ( const DLConceptTaxonomy& ) = delete; |
| 251 |
|
| 252 |
DLConceptTaxonomy& operator = ( const DLConceptTaxonomy& ) = delete; |
| 253 |
|
| 254 |
|
| 255 |
void setBottomUp ( const TKBFlags& GCIs ) { flagNeedBottomUp = (GCIs.isGCI() || (GCIs.isReflexive() && GCIs.isRnD())); } |
| 256 |
|
| 257 |
void reclassify ( const std::set<const TNamedEntity*>& MPlus, const std::set<const TNamedEntity*>& MMinus ); |
| 258 |
|
| 259 |
void setProgressIndicator ( TProgressMonitor* pMon ) { pTaxProgress = pMon; } |
| 260 |
|
| 261 |
void print ( std::ostream& o ) const override; |
| 262 |
}; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
inline bool DLConceptTaxonomy :: isUnsatisfiable ( void ) |
| 269 |
{ |
| 270 |
const TConcept* p = curConcept(); |
| 271 |
|
| 272 |
if ( tBox.isSatisfiable(p) ) |
| 273 |
return false; |
| 274 |
|
| 275 |
pTax->addCurrentToSynonym(pTax->getBottomVertex()); |
| 276 |
return true; |
| 277 |
} |
| 278 |
|
| 279 |
inline bool DLConceptTaxonomy :: immediatelyClassified ( void ) |
| 280 |
{ |
| 281 |
if ( classifySynonym() ) |
| 282 |
return true; |
| 283 |
|
| 284 |
if ( curConcept()->getClassTag() == cttTrueCompletelyDefined ) |
| 285 |
return false; |
| 286 |
|
| 287 |
|
| 288 |
tBox.initCache(const_cast<TConcept*>(curConcept())); |
| 289 |
|
| 290 |
return isUnsatisfiable(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
inline void |
| 298 |
TBox :: initTaxonomy ( void ) |
| 299 |
{ |
| 300 |
pTax = new Taxonomy ( pTop, pBottom ); |
| 301 |
pTaxCreator = new DLConceptTaxonomy ( pTax, *this ); |
| 302 |
} |
| 303 |
|
| 304 |
inline void |
| 305 |
TBox :: classifyEntry ( TConcept* entry ) |
| 306 |
{ |
| 307 |
if ( unlikely(isBlockedInd(entry)) ) |
| 308 |
classifyEntry(getBlockingInd(entry)); |
| 309 |
if ( !entry->isClassified() ) |
| 310 |
pTaxCreator->classifyEntry(entry); |
| 311 |
} |
| 312 |
|
| 313 |
inline void |
| 314 |
TBox :: reclassify ( const std::set<const TNamedEntity*>& MPlus, const std::set<const TNamedEntity*>& MMinus ) |
| 315 |
{ |
| 316 |
pTaxCreator->reclassify ( MPlus, MMinus ); |
| 317 |
Status = kbRealised; |
| 318 |
} |
| 319 |
|
| 320 |
#endif |