| 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 | #ifndef DLCONCEPTTAXONOMY_H |
| 21 | #define DLCONCEPTTAXONOMY_H |
| 22 | |
| 23 | #include "TaxonomyCreator.h" |
| 24 | #include "dlTBox.h" |
| 25 | #include "tProgressMonitor.h" |
| 26 | |
| 27 | /// Taxonomy of named DL concepts (and mapped individuals) |
| 28 | class DLConceptTaxonomy: public TaxonomyCreator |
| 29 | { |
| 30 | protected: // types |
| 31 | typedef std::vector<TaxonomyVertex*> TaxVertexVec; |
| 32 | /// all the derived subsumers of a class (came from the model) |
| 33 | class DerivedSubsumers: public KnownSubsumers |
| 34 | { |
| 35 | protected: // typedefs |
| 36 | /// set of the subsumers |
| 37 | typedef TaxonomyCreator::SubsumerSet SubsumerSet; |
| 38 | /// SS RW iterator |
| 39 | typedef SubsumerSet::iterator ss_iterator; |
| 40 | |
| 41 | protected: // members |
| 42 | /// set of sure- and possible subsumers |
| 43 | SubsumerSet Sure, Possible; |
| 44 | |
| 45 | public: // interface |
| 46 | /// c'tor: copy given sets |
| 47 | DerivedSubsumers ( const SubsumerSet& sure, const SubsumerSet& possible ) |
| 48 | : KnownSubsumers() |
| 49 | , Sure(sure) |
| 50 | , Possible(possible) |
| 51 | {} |
| 52 | |
| 53 | // iterators |
| 54 | |
| 55 | /// begin of the Sure subsumers interval |
| 56 | ss_iterator s_begin ( void ) override { return Sure.begin(); } |
| 57 | /// end of the Sure subsumers interval |
| 58 | ss_iterator s_end ( void ) override { return Sure.end(); } |
| 59 | /// begin of the Possible subsumers interval |
| 60 | ss_iterator p_begin ( void ) override { return Possible.begin(); } |
| 61 | /// end of the Possible subsumers interval |
| 62 | ss_iterator p_end ( void ) override { return Possible.end(); } |
| 63 | }; // DerivedSubsumers |
| 64 | |
| 65 | protected: // members |
| 66 | /// host tBox |
| 67 | TBox& tBox; |
| 68 | /// incremental sets M+ and M- |
| 69 | std::set<const TNamedEntity*> MPlus, MMinus; |
| 70 | /// set of possible parents |
| 71 | std::set<TaxonomyVertex*> candidates; |
| 72 | /// whether look into it |
| 73 | bool useCandidates = false; |
| 74 | /// common descendants of all parents of currently classified concept |
| 75 | TaxVertexVec Common; |
| 76 | /// number of processed common parents |
| 77 | unsigned int nCommon = 0; |
| 78 | |
| 79 | // statistic counters |
| 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 | /// number of positive cached subsumptions |
| 89 | unsigned long nCachedPositive = 0; |
| 90 | /// number of negative cached subsumptions |
| 91 | unsigned long nCachedNegative = 0; |
| 92 | /// number of non-subsumptions detected by a sorted reasoning |
| 93 | unsigned long nSortedNegative = 0; |
| 94 | /// number of non-subsumptions because of module reasons |
| 95 | unsigned long nModuleNegative = 0; |
| 96 | |
| 97 | /// indicator of taxonomy creation progress |
| 98 | TProgressMonitor* pTaxProgress = nullptr; |
| 99 | |
| 100 | // flags |
| 101 | |
| 102 | /// flag to use Bottom-Up search |
| 103 | bool flagNeedBottomUp = false; |
| 104 | |
| 105 | protected: // methods |
| 106 | //----------------------------------------------------------------- |
| 107 | //-- General support for DL concept classification |
| 108 | //----------------------------------------------------------------- |
| 109 | |
| 110 | /// get access to curEntry as a TConcept |
| 111 | const TConcept* curConcept ( void ) const { return static_cast<const TConcept*>(curEntry); } |
| 112 | /// tests subsumption (via tBox) and gather statistics. Use cache and other optimisations. |
| 113 | bool testSub ( const TConcept* p, const TConcept* q ); |
| 114 | /// test subsumption via TBox explicitly |
| 115 | bool testSubTBox ( const TConcept* p, const TConcept* q ) |
| 116 | { |
| 117 | bool res = tBox.isSubHolds ( p, q ); |
| 118 | |
| 119 | // update statistic |
| 120 | ++nTries; |
| 121 | |
| 122 | if ( res ) |
| 123 | ++nPositives; |
| 124 | else |
| 125 | ++nNegatives; |
| 126 | |
| 127 | return res; |
| 128 | } |
| 129 | |
| 130 | // interface from BAADER paper |
| 131 | |
| 132 | /// SEARCH procedure from Baader et al paper |
| 133 | void searchBaader ( TaxonomyVertex* cur ); |
| 134 | /// ENHANCED_SUBS procedure from Baader et al paper |
| 135 | bool enhancedSubs1 ( TaxonomyVertex* cur ); |
| 136 | /// short-cut from ENHANCED_SUBS |
| 137 | bool enhancedSubs2 ( TaxonomyVertex* cur ) |
| 138 | { |
| 139 | // if bottom-up search and CUR is not a successor of checking entity -- return false |
| 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 | // wrapper for the ENHANCED_SUBS |
| 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 | /// explicitly test appropriate subsumption relation |
| 157 | bool testSubsumption ( TaxonomyVertex* cur ); |
| 158 | /// test whether a node could be a super-node of CUR |
| 159 | bool possibleSub ( TaxonomyVertex* v ) const |
| 160 | { |
| 161 | const TConcept* C = static_cast<const TConcept*>(v->getPrimer()); |
| 162 | // non-prim concepts are candidates |
| 163 | if ( !C->isPrimitive() ) |
| 164 | return true; |
| 165 | // all others should be in the possible sups list |
| 166 | return ksStack.top()->isPossibleSub(C); |
| 167 | } |
| 168 | /// @return true if non-subsumption is due to ENTITY is not in the \bot-module |
| 169 | bool isNotInModule ( const TNamedEntity* entity ) const; |
| 170 | /// reclassify node |
| 171 | void reclassify ( TaxonomyVertex* node, const TSignature* s ); |
| 172 | |
| 173 | /// propagate common value from NODE to all its descendants; save visited nodes |
| 174 | void propagateOneCommon ( TaxonomyVertex* node ); |
| 175 | /// mark as COMMON all vertexes that are sub-concepts of every parent of CURRENT |
| 176 | bool propagateUp ( void ); |
| 177 | /// clear all COMMON information |
| 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 | /// check if concept is unsat; add it as a synonym of BOTTOM if necessary |
| 185 | bool isUnsatisfiable ( void ); |
| 186 | /// fill candidates |
| 187 | void fillCandidates ( TaxonomyVertex* cur ); |
| 188 | |
| 189 | //----------------------------------------------------------------- |
| 190 | //-- Tunable methods (depending on taxonomy type) |
| 191 | //----------------------------------------------------------------- |
| 192 | |
| 193 | /// prepare told subsumers for given entry if necessary |
| 194 | KnownSubsumers* buildKnownSubsumers ( ClassifiableEntry* p ) override; |
| 195 | /// prepare signature for given entry |
| 196 | const TSignature* buildSignature ( ClassifiableEntry* p ) override; |
| 197 | /// check if no classification needed (synonym, orphan, unsatisfiable) |
| 198 | bool immediatelyClassified ( void ) override; |
| 199 | /// check if no BU classification is required as C=TOP |
| 200 | bool isEqualToTop ( void ); |
| 201 | |
| 202 | /// check if it is possible to skip TD phase |
| 203 | bool needTopDown ( void ) const override |
| 204 | { return !(useCompletelyDefined && curEntry->isCompletelyDefined ()); } |
| 205 | /// explicitly run TD phase |
| 206 | void runTopDown ( void ) override { searchBaader(pTax->getTopVertex()); } |
| 207 | /// check if it is possible to skip BU phase |
| 208 | bool needBottomUp ( void ) const override |
| 209 | { |
| 210 | // we DON'T need bottom-up phase for primitive concepts during CD-like reasoning |
| 211 | // if no GCIs are in the TBox (C [= T, T [= X or Y, X [= D, Y [= D) or (T [= {o}) |
| 212 | // or no reflexive roles w/RnD present (Refl(R), Range(R)=D) |
| 213 | return flagNeedBottomUp || !useCompletelyDefined || curConcept()->isNonPrimitive(); |
| 214 | } |
| 215 | /// explicitly run BU phase |
| 216 | void runBottomUp ( void ) override |
| 217 | { |
| 218 | if ( propagateUp() ) // Common is set up here |
| 219 | return; |
| 220 | if ( isEqualToTop() ) // nothing to do |
| 221 | return; |
| 222 | if ( pTax->queryMode() ) // after classification -- bottom set up already |
| 223 | searchBaader(pTax->getBottomVertex()); |
| 224 | else // during classification -- have to find leaf nodes |
| 225 | for ( TaxVertexVec::iterator p = Common.begin(), p_end = Common.end(); p < p_end; ++p ) |
| 226 | if ( (*p)->noNeighbours(/*upDirection=*/false) ) |
| 227 | searchBaader(*p); |
| 228 | } |
| 229 | |
| 230 | /// actions that to be done BEFORE entry will be classified |
| 231 | void preClassificationActions ( void ) override |
| 232 | { |
| 233 | ++nConcepts; |
| 234 | if ( pTaxProgress != nullptr ) |
| 235 | pTaxProgress->nextClass(); |
| 236 | } |
| 237 | /// @return true iff curEntry is classified as a synonym |
| 238 | bool classifySynonym ( void ) override; |
| 239 | |
| 240 | /// check if it is necessary to log taxonomy action |
| 241 | bool needLogging ( void ) const override { return true; } |
| 242 | |
| 243 | public: // interface |
| 244 | /// the only c'tor |
| 245 | DLConceptTaxonomy ( Taxonomy* tax, TBox& kb ) |
| 246 | : TaxonomyCreator(tax) |
| 247 | , tBox(kb) |
| 248 | {} |
| 249 | /// no copy c'tor |
| 250 | DLConceptTaxonomy ( const DLConceptTaxonomy& ) = delete; |
| 251 | /// no assignment |
| 252 | DLConceptTaxonomy& operator = ( const DLConceptTaxonomy& ) = delete; |
| 253 | |
| 254 | /// set bottom-up flag |
| 255 | void setBottomUp ( const TKBFlags& GCIs ) { flagNeedBottomUp = (GCIs.isGCI() || (GCIs.isReflexive() && GCIs.isRnD())); } |
| 256 | /// reclassify taxonomy wrt changed sets |
| 257 | void reclassify ( const std::set<const TNamedEntity*>& MPlus, const std::set<const TNamedEntity*>& MMinus ); |
| 258 | /// set progress indicator |
| 259 | void setProgressIndicator ( TProgressMonitor* pMon ) { pTaxProgress = pMon; } |
| 260 | /// output taxonomy to a stream |
| 261 | void print ( std::ostream& o ) const override; |
| 262 | }; // DLConceptTaxonomy |
| 263 | |
| 264 | // |
| 265 | // DLConceptTaxonomy implementation |
| 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; // true CD concepts can not be unsat |
| 286 | |
| 287 | // after SAT testing plan would be implemented |
| 288 | tBox.initCache(const_cast<TConcept*>(curConcept())); |
| 289 | |
| 290 | return isUnsatisfiable(); |
| 291 | } |
| 292 | |
| 293 | //----------------------------------------------------------------------------- |
| 294 | //-- implementation of taxonomy-related parts of TBox |
| 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)); // make sure that the possible synonym is already classified |
| 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; // FIXME!! check whether it is classified/realised |
| 318 | } |
| 319 | |
| 320 | #endif // DLCONCEPTTAXONOMY_H |