| 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 TAXONOMYCREATOR_H |
| 21 | #define TAXONOMYCREATOR_H |
| 22 | |
| 23 | // taxonomy creator for DL |
| 24 | |
| 25 | #include "Taxonomy.h" |
| 26 | #include "SearchableStack.h" |
| 27 | #include "tSignature.h" |
| 28 | |
| 29 | class TaxonomyCreator |
| 30 | { |
| 31 | protected: // internal typedefs |
| 32 | /// set of the subsumers |
| 33 | typedef ClassifiableEntry::linkSet SubsumerSet; |
| 34 | /// SS RW iterator |
| 35 | typedef SubsumerSet::iterator ss_iterator; |
| 36 | /// abstract class to represent the known subsumers of a concept |
| 37 | class KnownSubsumers |
| 38 | { |
| 39 | public: // interface |
| 40 | /// empty d'tor |
| 41 | virtual ~KnownSubsumers() = default; |
| 42 | |
| 43 | // iterators |
| 44 | |
| 45 | /// begin of the Sure subsumers interval |
| 46 | virtual ss_iterator s_begin ( void ) = 0; |
| 47 | /// end of the Sure subsumers interval |
| 48 | virtual ss_iterator s_end ( void ) = 0; |
| 49 | /// begin of the Possible subsumers interval |
| 50 | virtual ss_iterator p_begin ( void ) = 0; |
| 51 | /// end of the Possible subsumers interval |
| 52 | virtual ss_iterator p_end ( void ) = 0; |
| 53 | |
| 54 | // flags |
| 55 | |
| 56 | /// whether there are no sure subsumers |
| 57 | bool s_empty ( void ) { return s_begin() == s_end(); } |
| 58 | /// whether there are no possible subsumers |
| 59 | bool p_empty ( void ) { return p_begin() == p_end(); } |
| 60 | /// @return true iff CE is the possible subsumer |
| 61 | virtual bool isPossibleSub ( const ClassifiableEntry* ) const { return true; } |
| 62 | }; // KnownSubsumers |
| 63 | |
| 64 | /// class to represent the TS's |
| 65 | class ToldSubsumers: public KnownSubsumers |
| 66 | { |
| 67 | protected: // members |
| 68 | /// two iterators for the TS of a concept |
| 69 | ss_iterator beg, end; |
| 70 | |
| 71 | public: // interface |
| 72 | /// c'tor |
| 73 | ToldSubsumers ( ss_iterator b, ss_iterator e ) : beg(b), end(e) {} |
| 74 | |
| 75 | // iterators |
| 76 | |
| 77 | /// begin of the Sure subsumers interval |
| 78 | ss_iterator s_begin ( void ) override { return beg; } |
| 79 | /// end of the Sure subsumers interval |
| 80 | ss_iterator s_end ( void ) override { return end; } |
| 81 | /// begin of the Possible subsumers interval |
| 82 | ss_iterator p_begin ( void ) override { return end; } |
| 83 | /// end of the Possible subsumers interval |
| 84 | ss_iterator p_end ( void ) override { return end; } |
| 85 | }; // ToldSubsumers |
| 86 | |
| 87 | protected: // members |
| 88 | /// Taxonomy to be build |
| 89 | Taxonomy* pTax; |
| 90 | /// aux array to keep synonyms found during TS classification |
| 91 | std::vector<ClassifiableEntry*> Syns; |
| 92 | |
| 93 | /// labeller for marking nodes with a label wrt classification |
| 94 | TLabeller valueLabel; |
| 95 | /// pointer to currently classified entry |
| 96 | const ClassifiableEntry* curEntry= nullptr; |
| 97 | |
| 98 | /// number of tested entries |
| 99 | unsigned int nEntries = 0; |
| 100 | /// number of completely-defined entries |
| 101 | unsigned long nCDEntries = 0; |
| 102 | |
| 103 | /// session flag: shows the direction of the search |
| 104 | bool upDirection = false; |
| 105 | /// optimisation flag: if entry is completely defined by it's told subsumers, no other classification required |
| 106 | bool useCompletelyDefined = false; |
| 107 | |
| 108 | /// stack for Taxonomy creation |
| 109 | SearchableStack <ClassifiableEntry*> waitStack; |
| 110 | /// told subsumers corresponding to a given entry |
| 111 | SearchableStack <KnownSubsumers*> ksStack; |
| 112 | /// signature of a \bot-module corresponding to a given entry |
| 113 | SearchableStack <const TSignature*> sigStack; |
| 114 | |
| 115 | protected: // methods |
| 116 | /// initialise aux entry with given concept p |
| 117 | void setCurrentEntry ( const ClassifiableEntry* p ) |
| 118 | { |
| 119 | pTax->getCurrent()->clear(); |
| 120 | pTax->getCurrent()->setSample(p); |
| 121 | curEntry = p; |
| 122 | } |
| 123 | |
| 124 | //----------------------------------------------------------------- |
| 125 | //-- General classification support |
| 126 | //----------------------------------------------------------------- |
| 127 | |
| 128 | /// return 1 if current entry is classified as a synonym of already classified one |
| 129 | virtual bool classifySynonym ( void ) { return pTax->processSynonym(); } |
| 130 | |
| 131 | /// set up Told Subsumers for the current entry |
| 132 | void setToldSubsumers ( void ); |
| 133 | /// add non-redundant candidates for the current entry |
| 134 | void setNonRedundantCandidates ( void ); |
| 135 | |
| 136 | //----------------------------------------------------------------- |
| 137 | //-- Tunable methods (depending on taxonomy type) |
| 138 | //----------------------------------------------------------------- |
| 139 | |
| 140 | /// check if no classification needed (synonym, orphan, unsatisfiable) |
| 141 | virtual bool immediatelyClassified ( void ) { return classifySynonym(); } |
| 142 | /// setup TD phase (ie, identify/set parent candidates) |
| 143 | void setupTopDown ( void ) |
| 144 | { |
| 145 | setToldSubsumers(); |
| 146 | if ( !needTopDown() ) |
| 147 | { |
| 148 | ++nCDEntries; |
| 149 | setNonRedundantCandidates(); |
| 150 | } |
| 151 | } |
| 152 | /// check if it is possible to skip TD phase |
| 153 | virtual bool needTopDown ( void ) const { return false; } |
| 154 | /// explicitly run TD phase |
| 155 | virtual void runTopDown ( void ) {} |
| 156 | /// check if it is possible to skip BU phase |
| 157 | virtual bool needBottomUp ( void ) const { return false; } |
| 158 | /// explicitly run BU phase |
| 159 | virtual void runBottomUp ( void ) {} |
| 160 | |
| 161 | /// actions that to be done BEFORE entry will be classified |
| 162 | virtual void preClassificationActions ( void ) {} |
| 163 | |
| 164 | //----------------------------------------------------------------- |
| 165 | //-- General classification methods |
| 166 | //----------------------------------------------------------------- |
| 167 | |
| 168 | /// Common pre- and post-action to setup 2-phase algorithm |
| 169 | void performClassification ( void ); |
| 170 | /// fills parents and children of Current using tunable general approach |
| 171 | void generalTwoPhaseClassification ( void ); |
| 172 | /// @return true if V is a direct parent of current wrt labels |
| 173 | bool isDirectParent ( TaxonomyVertex* v ) const; |
| 174 | /// add PARENT as a parent if it exists and is direct parent |
| 175 | void addPossibleParent ( TaxonomyVertex* parent ) |
| 176 | { |
| 177 | if ( parent && isDirectParent(parent) ) |
| 178 | pTax->getCurrent()->addNeighbour ( /*upDirection=*/true, parent ); |
| 179 | } |
| 180 | |
| 181 | //----------------------------------------------------------------- |
| 182 | //-- DFS-based classification |
| 183 | //----------------------------------------------------------------- |
| 184 | |
| 185 | /// @return true if a NODE has been valued during current classification pass |
| 186 | bool isValued ( TaxonomyVertex* node ) const { return node->isValued(valueLabel); } |
| 187 | /// get the subsumption value of a NODE wrt currently classified one |
| 188 | bool getValue ( TaxonomyVertex* node ) const { return node->getValue(); } |
| 189 | /// set the classification value of a NODE to VALUE |
| 190 | bool setValue ( TaxonomyVertex* node, bool value ) const { return node->setValued ( value, valueLabel ); } |
| 191 | |
| 192 | /// prepare known subsumers for given entry if necessary |
| 193 | virtual KnownSubsumers* buildKnownSubsumers ( ClassifiableEntry* p ) |
| 194 | { return new ToldSubsumers(p->told().begin(), p->told().end()); } |
| 195 | /// prepare signature for given entry |
| 196 | virtual const TSignature* buildSignature ( ClassifiableEntry* ) { return nullptr; } |
| 197 | /// add top entry together with its known subsumers |
| 198 | void addTop ( ClassifiableEntry* p ) |
| 199 | { |
| 200 | waitStack.push(p); |
| 201 | ksStack.push(buildKnownSubsumers(p)); |
| 202 | sigStack.push(buildSignature(p)); |
| 203 | } |
| 204 | /// remove top entry |
| 205 | void removeTop ( void ) |
| 206 | { |
| 207 | waitStack.pop(); |
| 208 | delete ksStack.top(); |
| 209 | ksStack.pop(); |
| 210 | sigStack.pop(); |
| 211 | } |
| 212 | /// ensure that all TS of the top entry are classified. @return the reason of cycle or NULL. |
| 213 | ClassifiableEntry* prepareTS ( ClassifiableEntry* cur ); |
| 214 | /// classify top entry of the stack |
| 215 | void classifyTop ( void ) |
| 216 | { |
| 217 | fpp_assert ( !waitStack.empty() ); // sanity check |
| 218 | setCurrentEntry(waitStack.top()); |
| 219 | performClassification(); |
| 220 | removeTop(); |
| 221 | } |
| 222 | /// propagate the TRUE value of the KS subsumption up the hierarchy |
| 223 | void propagateTrueUp ( TaxonomyVertex* node ); |
| 224 | /// propagate the FALSE value of the KS subsumption down the hierarchy |
| 225 | void propagateFalseDown ( TaxonomyVertex* node ); |
| 226 | /// propagate constant VALUE into an appropriate direction |
| 227 | bool setAndPropagate ( TaxonomyVertex* node, bool value ) |
| 228 | { |
| 229 | if ( value ) |
| 230 | propagateTrueUp(node); |
| 231 | else |
| 232 | propagateFalseDown(node); |
| 233 | return value; |
| 234 | } |
| 235 | |
| 236 | ss_iterator told_begin ( void ) { return ksStack.top()->s_begin(); } |
| 237 | ss_iterator told_end ( void ) { return ksStack.top()->s_end(); } |
| 238 | |
| 239 | /// check if it is necessary to log taxonomy action |
| 240 | virtual bool needLogging ( void ) const { return false; } |
| 241 | |
| 242 | public: // interface |
| 243 | /// init c'tor |
| 244 | explicit TaxonomyCreator ( Taxonomy* tax ) : pTax(tax) {} |
| 245 | /// no copy c'tor |
| 246 | TaxonomyCreator ( const TaxonomyCreator& ) = delete; |
| 247 | /// no assignment |
| 248 | TaxonomyCreator& operator = ( const TaxonomyCreator& ) = delete; |
| 249 | /// d'tor |
| 250 | virtual ~TaxonomyCreator() = default; |
| 251 | |
| 252 | //------------------------------------------------------------------------------ |
| 253 | //-- classification interface |
| 254 | //------------------------------------------------------------------------------ |
| 255 | |
| 256 | /// classify given entry: general method is by DFS |
| 257 | void classifyEntry ( ClassifiableEntry* p ) |
| 258 | { |
| 259 | fpp_assert ( waitStack.empty() ); // sanity check |
| 260 | |
| 261 | // don't classify artificial concepts |
| 262 | if ( p->isNonClassifiable() ) |
| 263 | return; |
| 264 | prepareTS(p); |
| 265 | } |
| 266 | /// clear all labels from Taxonomy vertices |
| 267 | void clearLabels ( void ) { pTax->clearVisited(); valueLabel.newLabel(); } |
| 268 | |
| 269 | // flags interface |
| 270 | |
| 271 | /// set Completely Defined flag |
| 272 | void setCompletelyDefined ( bool use ) { useCompletelyDefined = use; } |
| 273 | |
| 274 | // taxonomy info access |
| 275 | |
| 276 | /// print taxonomy info to a stream |
| 277 | virtual void print ( std::ostream& o ) const; |
| 278 | }; // TaxonomyCreator |
| 279 | |
| 280 | #endif // TAXONOMYCREATOR_H |