| 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 TAXNAMENTRY_H |
| 21 | #define TAXNAMENTRY_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "fpp_assert.h" |
| 26 | #include "tNamedEntry.h" |
| 27 | #include "globaldef.h" |
| 28 | |
| 29 | class TaxonomyVertex; |
| 30 | |
| 31 | class ClassifiableEntry : public TNamedEntry |
| 32 | { |
| 33 | public: // type definitions |
| 34 | /// type for the set of told subsumers |
| 35 | typedef std::vector<ClassifiableEntry*> linkSet; |
| 36 | /// told subsumers RW iterator |
| 37 | typedef linkSet::iterator iterator; |
| 38 | /// told subsumers RO iterator |
| 39 | typedef linkSet::const_iterator const_iterator; |
| 40 | |
| 41 | protected: // members |
| 42 | /// link to taxonomy entry for current entry |
| 43 | TaxonomyVertex* taxVertex = nullptr; |
| 44 | /// links to 'told subsumers' (entries that are direct super-entries for current) |
| 45 | linkSet toldSubsumers; |
| 46 | /// pointer to synonym (entry which contains whole information the same as current) |
| 47 | ClassifiableEntry* pSynonym = nullptr; |
| 48 | /// index as a vertex in the SubsumptionMap/model cache |
| 49 | unsigned int Index = 0; |
| 50 | |
| 51 | public: // interface |
| 52 | /// C'tor |
| 53 | explicit ClassifiableEntry ( const std::string& name ) : TNamedEntry(name) {} |
| 54 | /// no copy c'tor |
| 55 | ClassifiableEntry ( const ClassifiableEntry& ) = delete; |
| 56 | /// no assignment |
| 57 | ClassifiableEntry& operator = ( const ClassifiableEntry& ) = delete; |
| 58 | |
| 59 | // taxonomy entry access |
| 60 | |
| 61 | /// is current entry classified |
| 62 | bool isClassified ( void ) const { return ( taxVertex != nullptr ); } |
| 63 | /// set up given entry |
| 64 | void setTaxVertex ( TaxonomyVertex* vertex ) { taxVertex = vertex; } |
| 65 | /// get taxonomy vertex of the entry |
| 66 | TaxonomyVertex* getTaxVertex ( void ) const { return taxVertex; } |
| 67 | |
| 68 | // completely defined interface |
| 69 | |
| 70 | /// register a Completely Defined flag |
| 71 | FPP_ADD_FLAG(CompletelyDefined,0x2); |
| 72 | /// register a non-classifiable flag |
| 73 | FPP_ADD_FLAG(NonClassifiable,0x4); |
| 74 | |
| 75 | // told subsumers interface |
| 76 | |
| 77 | /// RW access to the told subsumers |
| 78 | linkSet& told ( void ) { return toldSubsumers; } |
| 79 | /// RO access to the told subsumers |
| 80 | const linkSet& told ( void ) const { return toldSubsumers; } |
| 81 | |
| 82 | /// check whether the entry has any TS |
| 83 | bool hasToldSubsumers ( void ) const { return !toldSubsumers.empty(); } |
| 84 | /// add told subsumer of entry (duplications possible) |
| 85 | void addParent ( ClassifiableEntry* parent ) { toldSubsumers.push_back (parent); } |
| 86 | /// add told subsumer if doesn't already recorded |
| 87 | void addParentIfNew ( ClassifiableEntry* parent ); |
| 88 | /// add all parents (with duplicates) from given container to current node |
| 89 | template <typename Container> |
| 90 | void addParents ( const Container& parents ) |
| 91 | { |
| 92 | for ( auto& parent: parents ) |
| 93 | addParentIfNew(parent); |
| 94 | } |
| 95 | |
| 96 | // index interface |
| 97 | |
| 98 | /// get the index value |
| 99 | unsigned int index ( void ) const { return Index; } |
| 100 | /// set the index value |
| 101 | void setIndex ( unsigned int ind ) { Index = ind; } |
| 102 | |
| 103 | // synonym interface |
| 104 | |
| 105 | /// check if current entry is a synonym |
| 106 | bool isSynonym ( void ) const { return (pSynonym != nullptr); } |
| 107 | /// get synonym of current entry |
| 108 | ClassifiableEntry* getSynonym ( void ) const { return pSynonym; } |
| 109 | /// make sure that synonym's representative is not a synonym itself |
| 110 | void canonicaliseSynonym ( void ); |
| 111 | /// add entry's synonym |
| 112 | void setSynonym ( ClassifiableEntry* syn ) |
| 113 | { |
| 114 | fpp_assert ( pSynonym == nullptr ); // do it only once |
| 115 | pSynonym = syn; |
| 116 | canonicaliseSynonym(); |
| 117 | } |
| 118 | |
| 119 | /// if two synonyms are in 'told' list, merge them |
| 120 | void removeSynonymsFromParents ( void ) |
| 121 | { |
| 122 | linkSet copy; |
| 123 | copy.swap(toldSubsumers); |
| 124 | addParents(copy); |
| 125 | } |
| 126 | }; // ClassifiableEntry |
| 127 | |
| 128 | /// general RW resolving synonym operator |
| 129 | template <typename T> |
| 130 | inline T* |
| 131 | resolveSynonym ( T* p ) |
| 132 | { |
| 133 | return !p ? nullptr : p->isSynonym() ? resolveSynonym(static_cast<T*>(p->getSynonym())) : p; |
| 134 | } |
| 135 | |
| 136 | /// general RO resolving synonym operator |
| 137 | template <typename T> |
| 138 | inline const T* |
| 139 | resolveSynonym ( const T* p ) |
| 140 | { |
| 141 | return !p ? nullptr : p->isSynonym() ? resolveSynonym(static_cast<const T*>(p->getSynonym())) : p; |
| 142 | } |
| 143 | |
| 144 | /// make sure that synonym's representative is not a synonym itself |
| 145 | inline void |
| 146 | ClassifiableEntry :: canonicaliseSynonym ( void ) |
| 147 | { |
| 148 | fpp_assert(isSynonym()); |
| 149 | pSynonym = resolveSynonym(pSynonym); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | inline void |
| 154 | ClassifiableEntry :: addParentIfNew ( ClassifiableEntry* parent ) |
| 155 | { |
| 156 | // resolve synonyms |
| 157 | parent = resolveSynonym(parent); |
| 158 | |
| 159 | // node can not be its own parent |
| 160 | if ( parent == this ) |
| 161 | return; |
| 162 | |
| 163 | // check if such entry already exists |
| 164 | for ( const auto& p: told() ) |
| 165 | if ( parent == p ) |
| 166 | return; |
| 167 | |
| 168 | addParent(parent); |
| 169 | } |
| 170 | |
| 171 | #endif // TAXNAMENTRY_H |