| 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 building for the FaCT++ *| |
| 22 | \*******************************************************/ |
| 23 | |
| 24 | #include "Taxonomy.h" |
| 25 | #include "logging.h" |
| 26 | |
| 27 | /********************************************************\ |
| 28 | |* Implementation of class Taxonomy *| |
| 29 | \********************************************************/ |
| 30 | Taxonomy :: ~Taxonomy() |
| 31 | { |
| 32 | delete Current; |
| 33 | for ( TaxVertexVec::iterator p = Graph.begin(), p_end = Graph.end(); p < p_end; ++p ) |
| 34 | delete *p; |
| 35 | } |
| 36 | |
| 37 | void Taxonomy :: print ( std::ostream& o ) const |
| 38 | { |
| 39 | o << "All entries are in format:\n\"entry\" {n: parent_1 ... parent_n} {m: child_1 child_m}\n\n"; |
| 40 | |
| 41 | TVSet sorted(Graph.begin()+2, Graph.end()); |
| 42 | |
| 43 | getTopVertex()->print(o); |
| 44 | for ( const TaxonomyVertex* vertex : sorted ) |
| 45 | if ( likely(vertex->isInUse()) ) |
| 46 | vertex->print(o); |
| 47 | getBottomVertex()->print(o); |
| 48 | } |
| 49 | |
| 50 | //--------------------------------------------------- |
| 51 | // classification part |
| 52 | //--------------------------------------------------- |
| 53 | void |
| 54 | Taxonomy :: addCurrentToSynonym ( TaxonomyVertex* syn ) |
| 55 | { |
| 56 | const ClassifiableEntry* curEntry = Current->getPrimer(); |
| 57 | if ( queryMode() ) // no need to insert; just mark SYN as a host to curEntry |
| 58 | syn->setVertexAsHost(curEntry); |
| 59 | else |
| 60 | { |
| 61 | syn->addSynonym(curEntry); |
| 62 | |
| 63 | if ( LLM.isWritable(llTaxInsert) ) |
| 64 | LL << "\nTAX:set " << curEntry->getName() << " equal " << syn->getPrimer()->getName(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// insert current node either directly or as a synonym |
| 69 | void |
| 70 | Taxonomy :: finishCurrentNode ( void ) |
| 71 | { |
| 72 | TaxonomyVertex* syn = Current->getSynonymNode(); |
| 73 | if ( syn ) |
| 74 | addCurrentToSynonym(syn); |
| 75 | else if ( !queryMode() ) // insert node into taxonomy |
| 76 | { |
| 77 | Current->incorporate(); |
| 78 | Graph.push_back(Current); |
| 79 | // we used the Current so need to create a new one |
| 80 | Current = new TaxonomyVertex(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | bool Taxonomy :: processSynonym ( void ) |
| 85 | { |
| 86 | const ClassifiableEntry* curEntry = Current->getPrimer(); |
| 87 | const ClassifiableEntry* syn = resolveSynonym(curEntry); |
| 88 | |
| 89 | if ( syn == curEntry ) |
| 90 | return false; // not a synonym |
| 91 | |
| 92 | // update synonym vertex: |
| 93 | fpp_assert ( syn->getTaxVertex() != nullptr ); |
| 94 | addCurrentToSynonym(syn->getTaxVertex()); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /// call this method after taxonomy is built |
| 100 | void |
| 101 | Taxonomy :: finalise ( void ) |
| 102 | { // create links from leaf concepts to bottom |
| 103 | const bool upDirection = false; |
| 104 | for ( TaxVertexVec::iterator p = Graph.begin()+1, p_end = Graph.end(); p < p_end; ++p ) |
| 105 | if ( likely((*p)->isInUse()) && (*p)->noNeighbours(upDirection) ) |
| 106 | { |
| 107 | (*p)->addNeighbour ( upDirection, getBottomVertex() ); |
| 108 | getBottomVertex()->addNeighbour ( !upDirection, *p ); |
| 109 | } |
| 110 | willInsertIntoTaxonomy = false; // after finalisation one shouldn't add new entries to taxonomy |
| 111 | } |
| 112 | |
| 113 | /// unlink the bottom from the taxonomy |
| 114 | void |
| 115 | Taxonomy :: deFinalise ( void ) |
| 116 | { |
| 117 | const bool upDirection = true; |
| 118 | TaxonomyVertex* bot = getBottomVertex(); |
| 119 | for ( TaxonomyVertex::iterator p = bot->begin(upDirection), p_end = bot->end(upDirection); p != p_end; ++p ) |
| 120 | (*p)->removeLink ( !upDirection, bot ); |
| 121 | bot->clearLinks(upDirection); |
| 122 | willInsertIntoTaxonomy = true; // it's possible again to add entries |
| 123 | } |