| 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 TAXONOMY_H |
| 21 | #define TAXONOMY_H |
| 22 | |
| 23 | // taxonomy graph for DL |
| 24 | |
| 25 | #include "taxVertex.h" |
| 26 | #include "WalkerInterface.h" |
| 27 | |
| 28 | class SaveLoadManager; |
| 29 | |
| 30 | class Taxonomy |
| 31 | { |
| 32 | protected: // typedefs |
| 33 | /// type for a vector of TaxVertex |
| 34 | typedef std::vector<TaxonomyVertex*> TaxVertexVec; |
| 35 | |
| 36 | protected: // members |
| 37 | /// array of taxonomy vertices |
| 38 | TaxVertexVec Graph; |
| 39 | /// vertex with parent Top and child Bot, represents the fresh entity |
| 40 | TaxonomyVertex FreshNode; |
| 41 | |
| 42 | /// labeller for marking nodes as checked |
| 43 | TLabeller visitedLabel; |
| 44 | /// aux vertex to be included to taxonomy |
| 45 | TaxonomyVertex* Current; |
| 46 | |
| 47 | /// behaviour flag: if true, insert temporary vertex into taxonomy |
| 48 | bool willInsertIntoTaxonomy = true; |
| 49 | |
| 50 | public: // classification interface |
| 51 | |
| 52 | //----------------------------------------------------------------- |
| 53 | //-- General classification support |
| 54 | //----------------------------------------------------------------- |
| 55 | |
| 56 | /// @return true if current entry is a synonym of an already classified one |
| 57 | bool processSynonym ( void ); |
| 58 | /// add current entry to a synonym SYN |
| 59 | void addCurrentToSynonym ( TaxonomyVertex* syn ); |
| 60 | /// remove node from the taxonomy; assume no references to the node |
| 61 | void removeNode ( TaxonomyVertex* node ) { node->setInUse(false); } |
| 62 | /// insert current node either directly or as a synonym |
| 63 | void finishCurrentNode ( void ); |
| 64 | |
| 65 | /// @return true if taxonomy works in a query mode (no need to insert query vertex) |
| 66 | bool queryMode ( void ) const { return !willInsertIntoTaxonomy; } |
| 67 | |
| 68 | /// set node NODE as checked within taxonomy |
| 69 | void setVisited ( TaxonomyVertex* node ) const { node->setChecked(visitedLabel); } |
| 70 | /// check whether NODE is checked within taxonomy |
| 71 | bool isVisited ( TaxonomyVertex* node ) const { return node->isChecked(visitedLabel); } |
| 72 | /// clear the CHECKED label from all the taxonomy vertex |
| 73 | void clearVisited ( void ) { visitedLabel.newLabel(); } |
| 74 | |
| 75 | /// call this method after taxonomy is built |
| 76 | void finalise ( void ); |
| 77 | /// unlink the bottom from the taxonomy |
| 78 | void deFinalise ( void ); |
| 79 | |
| 80 | protected: // methods |
| 81 | /// apply ACTOR to subgraph starting from NODE as defined by flags |
| 82 | template<bool onlyDirect, bool upDirection, class Actor> |
| 83 | void getRelativesInfoRec ( TaxonomyVertex* node, Actor& actor ) |
| 84 | { |
| 85 | // recursive applicability checking |
| 86 | if ( isVisited(node) ) |
| 87 | return; |
| 88 | |
| 89 | // label node as visited |
| 90 | setVisited(node); |
| 91 | |
| 92 | // if current node processed OK and there is no need to continue -- exit |
| 93 | // if node is NOT processed for some reasons -- go to another level |
| 94 | if ( actor.apply(*node) && onlyDirect ) |
| 95 | return; |
| 96 | |
| 97 | // apply method to the proper neighbours with proper parameters |
| 98 | for ( TaxonomyVertex::iterator p = node->begin(upDirection), p_end = node->end(upDirection); p != p_end; ++p ) |
| 99 | getRelativesInfoRec<onlyDirect, upDirection> ( *p, actor ); |
| 100 | } |
| 101 | |
| 102 | public: // interface |
| 103 | /// init c'tor |
| 104 | Taxonomy ( const ClassifiableEntry* pTop, const ClassifiableEntry* pBottom ) |
| 105 | : Current(new TaxonomyVertex()) |
| 106 | { |
| 107 | Graph.push_back (new TaxonomyVertex(pBottom)); // bottom |
| 108 | Graph.push_back (new TaxonomyVertex(pTop)); // top |
| 109 | // set up fresh node |
| 110 | FreshNode.addNeighbour ( /*upDirection=*/true, getTopVertex() ); |
| 111 | FreshNode.addNeighbour ( /*upDirection=*/false, getBottomVertex() ); |
| 112 | } |
| 113 | /// no copy c'tor |
| 114 | Taxonomy ( const Taxonomy& ) = delete; |
| 115 | /// no assignment |
| 116 | Taxonomy& operator = ( const Taxonomy& ) = delete; |
| 117 | /// d'tor |
| 118 | ~Taxonomy(); |
| 119 | |
| 120 | //------------------------------------------------------------------------------ |
| 121 | //-- Access to taxonomy entries |
| 122 | //------------------------------------------------------------------------------ |
| 123 | |
| 124 | /// special access to TOP of taxonomy |
| 125 | TaxonomyVertex* getTopVertex ( void ) const { return *(Graph.begin()+1); } |
| 126 | /// special access to BOTTOM of taxonomy |
| 127 | TaxonomyVertex* getBottomVertex ( void ) const { return *Graph.begin(); } |
| 128 | /// get node for fresh entity E |
| 129 | TaxonomyVertex* getFreshVertex ( const ClassifiableEntry* e ) { FreshNode.setSample(e,false); return &FreshNode; } |
| 130 | /// get RW access to current |
| 131 | TaxonomyVertex* getCurrent ( void ) { return Current; } |
| 132 | /// get RO access to current |
| 133 | const TaxonomyVertex* getCurrent ( void ) const { return Current; } |
| 134 | /// set current to a given node |
| 135 | void setCurrent ( TaxonomyVertex* cur ) { Current = cur; } |
| 136 | |
| 137 | /// apply ACTOR to subgraph starting from NODE as defined by flags; |
| 138 | template<bool needCurrent, bool onlyDirect, bool upDirection, class Actor> |
| 139 | void getRelativesInfo ( TaxonomyVertex* node, Actor& actor ) |
| 140 | { |
| 141 | // if current node processed OK and there is no need to continue -- exit |
| 142 | // this is the helper to the case like getDomain(): |
| 143 | // if there is a named concept that represent's a domain -- that's what we need |
| 144 | if ( needCurrent ) |
| 145 | if ( actor.apply(*node) && onlyDirect ) |
| 146 | return; |
| 147 | |
| 148 | for ( TaxonomyVertex::iterator p = node->begin(upDirection), p_end = node->end(upDirection); p != p_end; ++p ) |
| 149 | getRelativesInfoRec<onlyDirect, upDirection> ( *p, actor ); |
| 150 | |
| 151 | clearVisited(); |
| 152 | } |
| 153 | |
| 154 | // taxonomy info access |
| 155 | |
| 156 | /// print taxonomy info to a stream |
| 157 | void print ( std::ostream& o ) const; |
| 158 | |
| 159 | // save/load interface; implementation is in SaveLoad.cpp |
| 160 | |
| 161 | /// save entry |
| 162 | void Save ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded ) const; |
| 163 | /// load entry |
| 164 | void Load ( SaveLoadManager& m ); |
| 165 | }; // Taxonomy |
| 166 | |
| 167 | #endif // TAXONOMY_H |