| 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 TAXVERTEX_H |
| 21 | #define TAXVERTEX_H |
| 22 | |
| 23 | #include <set> |
| 24 | #include <vector> |
| 25 | #include <iosfwd> |
| 26 | #include <cstring> |
| 27 | |
| 28 | #include "taxNamEntry.h" |
| 29 | #include "tLabeller.h" |
| 30 | |
| 31 | class SaveLoadManager; |
| 32 | |
| 33 | class TaxonomyVertex |
| 34 | { |
| 35 | protected: // typedefs |
| 36 | /// vertex vector for links to parents and children |
| 37 | typedef std::vector<TaxonomyVertex*> TaxVertexLink; |
| 38 | /// vector of entries for synonyms |
| 39 | typedef std::vector<const ClassifiableEntry*> EqualNames; |
| 40 | |
| 41 | public: // typedefs |
| 42 | // accessors type |
| 43 | |
| 44 | /// RW iterator for the neighbours |
| 45 | typedef TaxVertexLink::iterator iterator; |
| 46 | /// RO iterator for the neighbours |
| 47 | typedef TaxVertexLink::const_iterator const_iterator; |
| 48 | |
| 49 | private: // members |
| 50 | /// immediate parents and children |
| 51 | TaxVertexLink Links[2]; |
| 52 | |
| 53 | protected: // members |
| 54 | /// entry corresponding to current tax vertex |
| 55 | const ClassifiableEntry* sample = nullptr; |
| 56 | /// synonyms of the sample entry |
| 57 | EqualNames Synonyms; |
| 58 | |
| 59 | // labels for different purposes. all for 2 directions: top-down and bottom-up search |
| 60 | |
| 61 | /// flag if given vertex was checked; connected with checkLab |
| 62 | TLabeller::LabelType theChecked; |
| 63 | /// flag if given vertex has value; connected with valuedLab |
| 64 | TLabeller::LabelType theValued; |
| 65 | /// number of common parents of a node |
| 66 | unsigned int common = 0; |
| 67 | /// satisfiability value of a valued vertex |
| 68 | bool checkValue = false; |
| 69 | /// flag to check whether the vertex is in use |
| 70 | bool inUse = true; |
| 71 | |
| 72 | protected: // methods |
| 73 | /// indirect RW access to Links |
| 74 | TaxVertexLink& neigh ( bool upDirection ) { return Links[!upDirection]; } |
| 75 | /// indirect RO access to Links |
| 76 | const TaxVertexLink& neigh ( bool upDirection ) const { return Links[!upDirection]; } |
| 77 | |
| 78 | /// print entry name and its synonyms (if any) |
| 79 | void printSynonyms ( std::ostream& o ) const; |
| 80 | /// print neighbours of a vertex in given direction |
| 81 | void printNeighbours ( std::ostream& o, bool upDirection ) const; |
| 82 | |
| 83 | public: // flags interface |
| 84 | |
| 85 | // checked part |
| 86 | bool isChecked ( const TLabeller& checkLab ) const { return checkLab.isLabelled(theChecked); } |
| 87 | void setChecked ( const TLabeller& checkLab ) { checkLab.set(theChecked); } |
| 88 | |
| 89 | // value part |
| 90 | bool isValued ( const TLabeller& valueLab ) const { return valueLab.isLabelled(theValued); } |
| 91 | bool getValue ( void ) const { return checkValue; } |
| 92 | bool setValued ( bool val, const TLabeller& valueLab ) |
| 93 | { |
| 94 | valueLab.set(theValued); |
| 95 | checkValue = val; |
| 96 | return val; |
| 97 | } |
| 98 | |
| 99 | // common part |
| 100 | bool isCommon ( void ) const { return common != 0; } |
| 101 | void setCommon ( void ) { ++common; } |
| 102 | void clearCommon ( void ) { common = 0; } |
| 103 | /// keep COMMON flag iff both flags are set; @return true if it is the case |
| 104 | bool correctCommon ( unsigned int n ) |
| 105 | { |
| 106 | if ( common == n ) |
| 107 | return true; |
| 108 | clearCommon(); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /// put initial values on the flags |
| 113 | void initFlags ( void ) |
| 114 | { |
| 115 | TLabeller::clear(theChecked); |
| 116 | TLabeller::clear(theValued); |
| 117 | clearCommon(); |
| 118 | } |
| 119 | |
| 120 | // get info about taxonomy structure |
| 121 | |
| 122 | const EqualNames& synonyms ( void ) const { return Synonyms; } |
| 123 | |
| 124 | /// mark vertex as the one corresponding to a given ENTRY |
| 125 | void setVertexAsHost ( const ClassifiableEntry* entry ) { const_cast<ClassifiableEntry*>(entry)->setTaxVertex(this); } |
| 126 | /// set sample to ENTRY |
| 127 | void setSample ( const ClassifiableEntry* entry, bool linkBack = true ) |
| 128 | { |
| 129 | sample = entry; |
| 130 | if ( likely(linkBack) ) |
| 131 | setVertexAsHost(entry); |
| 132 | } |
| 133 | |
| 134 | public: |
| 135 | /// empty c'tor |
| 136 | TaxonomyVertex() { initFlags(); } |
| 137 | /// init c'tor; use it only for Top/Bot initialisations |
| 138 | explicit TaxonomyVertex ( const ClassifiableEntry* p ) : TaxonomyVertex() |
| 139 | { setSample(p); } |
| 140 | /// no copy |
| 141 | TaxonomyVertex ( const TaxonomyVertex& ) = delete; |
| 142 | /// no assignment |
| 143 | TaxonomyVertex& operator = ( const TaxonomyVertex& ) = delete; |
| 144 | |
| 145 | /// add P as a synonym to current vertex |
| 146 | void addSynonym ( const ClassifiableEntry* p ) |
| 147 | { |
| 148 | Synonyms.push_back(p); |
| 149 | setVertexAsHost(p); |
| 150 | } |
| 151 | /// clears the vertex |
| 152 | void clear ( void ) |
| 153 | { |
| 154 | Links[0].clear(); |
| 155 | Links[1].clear(); |
| 156 | sample = nullptr; |
| 157 | initFlags(); |
| 158 | } |
| 159 | /// get RO access to the primer |
| 160 | const ClassifiableEntry* getPrimer ( void ) const { return sample; } |
| 161 | |
| 162 | /// add link in given direction to vertex |
| 163 | void addNeighbour ( bool upDirection, TaxonomyVertex* p ) { neigh(upDirection).push_back(p); } |
| 164 | /// check if vertex has no neighbours in given direction |
| 165 | bool noNeighbours ( bool upDirection ) const { return neigh(upDirection).empty(); } |
| 166 | |
| 167 | // iterator access to parents/children |
| 168 | |
| 169 | iterator begin ( bool upDirection ) { return neigh(upDirection).begin(); } |
| 170 | iterator end ( bool upDirection ) { return neigh(upDirection).end(); } |
| 171 | |
| 172 | const_iterator begin ( bool upDirection ) const { return neigh(upDirection).begin(); } |
| 173 | const_iterator end ( bool upDirection ) const { return neigh(upDirection).end(); } |
| 174 | |
| 175 | /** Adds vertex to existing graph. For every Up, Down such that (Up->Down) |
| 176 | creates couple of links (Up->this), (this->Down). Don't work with synonyms!!! |
| 177 | */ |
| 178 | void incorporate ( void ); |
| 179 | /// @return v if node represents a synonym (v=Up[i]==Down[j]); @return NULL otherwise |
| 180 | TaxonomyVertex* getSynonymNode ( void ) |
| 181 | { |
| 182 | // try to find Vertex such that Vertex\in Up and Vertex\in Down |
| 183 | for ( auto& upV: neigh(true) ) |
| 184 | for ( const auto& downV: neigh(false) ) |
| 185 | if ( upV == downV ) // found such vertex |
| 186 | return upV; |
| 187 | return nullptr; |
| 188 | } |
| 189 | /// Remove link to P from neighbours (given by flag). @return true if such link was removed |
| 190 | bool removeLink ( bool upDirection, TaxonomyVertex* p ); |
| 191 | /// clear all links in a given direction |
| 192 | void clearLinks ( bool upDirection ) { neigh(upDirection).clear(); } |
| 193 | /// remove one half of a given node from a graph |
| 194 | void removeLinks ( bool upDirection ); |
| 195 | /// remove given node from a graph |
| 196 | void remove ( void ) { removeLinks(true); removeLinks(false); setInUse(false); } |
| 197 | |
| 198 | // usage methods |
| 199 | |
| 200 | /// @return true iff the node is in use |
| 201 | bool isInUse ( void ) const { return inUse; } |
| 202 | /// set the inUse value of the node |
| 203 | void setInUse ( bool value ) { inUse = value; } |
| 204 | |
| 205 | // output methods |
| 206 | |
| 207 | /// print taxonomy vertex in format <equals parents children> |
| 208 | void print ( std::ostream& o ) const; |
| 209 | |
| 210 | // save/load interface; implementation is in SaveLoad.cpp |
| 211 | |
| 212 | /// save label of the entry |
| 213 | void SaveLabel ( SaveLoadManager& m ) const; |
| 214 | /// load label of the entry |
| 215 | void LoadLabel ( SaveLoadManager& m ); |
| 216 | /// save neighbours of the entry |
| 217 | void SaveNeighbours ( SaveLoadManager& m ) const; |
| 218 | /// load neighbours of the entry |
| 219 | void LoadNeighbours ( SaveLoadManager& m ); |
| 220 | }; // TaxonomyVertex |
| 221 | |
| 222 | /// structure to sort tax vertices |
| 223 | struct TaxVertexLess |
| 224 | { |
| 225 | bool operator()(const TaxonomyVertex* s1, const TaxonomyVertex* s2) const |
| 226 | { return strcmp(s1->getPrimer()->getName(), s2->getPrimer()->getName()) < 0; } |
| 227 | }; |
| 228 | |
| 229 | /// sorted vertices set |
| 230 | typedef std::set<const TaxonomyVertex*, TaxVertexLess> TVSet; |
| 231 | |
| 232 | #endif // TAXVERTEX_H |