| 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 | /*******************************************************\ |
| 21 | |* Implementation of taxonomy vertex class *| |
| 22 | \*******************************************************/ |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <ostream> |
| 26 | |
| 27 | #include "taxVertex.h" |
| 28 | #include "logging.h" |
| 29 | |
| 30 | // removes given pointer from neigh. |
| 31 | bool TaxonomyVertex :: removeLink ( bool upDirection, TaxonomyVertex* p ) |
| 32 | { |
| 33 | // for all neighbours of current vertex... |
| 34 | auto& neighbours = neigh(upDirection); |
| 35 | auto exists = std::find(neighbours.begin(), neighbours.end(), p); |
| 36 | // Vertex P is not a neighbour -- nothing to do |
| 37 | if ( exists == neighbours.end() ) |
| 38 | return false; |
| 39 | // replace P with the last entry |
| 40 | *exists = neighbours.back(); |
| 41 | // remove last entry (by resizing) |
| 42 | neighbours.pop_back(); |
| 43 | // there is at most one link to a node, so return now |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | void TaxonomyVertex :: incorporate ( void ) |
| 48 | { |
| 49 | // setup links |
| 50 | auto& subs = neigh(/*upDirection=*/false); |
| 51 | auto& supers = neigh(/*upDirection=*/true); |
| 52 | |
| 53 | // correct links on sub concepts... |
| 54 | for ( auto& sub: subs ) |
| 55 | { |
| 56 | // remove all links between subs and supers, if exists |
| 57 | for ( auto& super: supers ) |
| 58 | if ( sub->removeLink ( /*upDirection=*/true, super ) ) |
| 59 | super->removeLink ( /*upDirection=*/false, sub ); |
| 60 | |
| 61 | // add new link between sub-class and current |
| 62 | sub->removeLink ( /*upDirection=*/true, this ); // safe in general case (link doesn't exists), crucial for incremental |
| 63 | sub->addNeighbour ( /*upDirection=*/true, this ); |
| 64 | } |
| 65 | |
| 66 | // add new links between current and supers |
| 67 | for ( auto& super: supers ) |
| 68 | super->addNeighbour ( /*upDirection=*/false, this ); |
| 69 | |
| 70 | CHECK_LL_RETURN(llTaxInsert); |
| 71 | |
| 72 | LL << "\nTAX:inserting '" << getPrimer()->getName() << "' with up ="; |
| 73 | printNeighbours ( LL, /*upDirection=*/true ); |
| 74 | LL << " and down ="; |
| 75 | printNeighbours ( LL, /*upDirection=*/false ); |
| 76 | } |
| 77 | |
| 78 | /// remove one half of a given node from a graph |
| 79 | void |
| 80 | TaxonomyVertex :: removeLinks ( bool upDirection ) |
| 81 | { |
| 82 | for ( auto& vertex: neigh(upDirection) ) |
| 83 | vertex->removeLink ( !upDirection, this ); |
| 84 | |
| 85 | clearLinks(upDirection); |
| 86 | } |
| 87 | |
| 88 | void TaxonomyVertex :: printSynonyms ( std::ostream& o ) const |
| 89 | { |
| 90 | fpp_assert ( sample != nullptr ); |
| 91 | |
| 92 | if ( likely(Synonyms.empty()) ) |
| 93 | o << '"' << getPrimer()->getName() << '"'; |
| 94 | else |
| 95 | { |
| 96 | o << "(\"" << getPrimer()->getName(); |
| 97 | for ( const auto& synonym: synonyms() ) |
| 98 | o << "\"=\"" << synonym->getName(); |
| 99 | o << "\")"; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void TaxonomyVertex :: printNeighbours ( std::ostream& o, bool upDirection ) const |
| 104 | { |
| 105 | // write number of elements |
| 106 | o << " {" << neigh(upDirection).size() << ":"; |
| 107 | |
| 108 | TVSet sorted ( begin(upDirection), end(upDirection) ); |
| 109 | for ( auto vertex: sorted) |
| 110 | o << " \"" << vertex->getPrimer()->getName() << '"'; |
| 111 | |
| 112 | o << "}"; |
| 113 | } |
| 114 | |
| 115 | /// print taxonomy vertex in format <equals parents children> |
| 116 | void |
| 117 | TaxonomyVertex :: print ( std::ostream& o ) const |
| 118 | { |
| 119 | printSynonyms(o); |
| 120 | printNeighbours ( o, true ); |
| 121 | printNeighbours ( o, false ); |
| 122 | o << "\n"; |
| 123 | } |