| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2011-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 TSIGNATURE_H |
| 21 | #define TSIGNATURE_H |
| 22 | |
| 23 | #include <set> |
| 24 | #include <algorithm> |
| 25 | #include <iterator> |
| 26 | |
| 27 | #include "tDLExpression.h" |
| 28 | |
| 29 | /// class to hold the signature of a module |
| 30 | class TSignature |
| 31 | { |
| 32 | public: // types |
| 33 | /// set of entities as a base underlying type os a signature |
| 34 | typedef std::set<const TNamedEntity*> BaseType; |
| 35 | /// RO iterator over a set of entities |
| 36 | typedef BaseType::const_iterator iterator; |
| 37 | |
| 38 | protected: // members |
| 39 | /// set to keep all the elements in signature |
| 40 | BaseType Set; |
| 41 | /// true if concept TOP-locality; false if concept BOTTOM-locality |
| 42 | bool topCLocality = false; |
| 43 | /// true if role TOP-locality; false if role BOTTOM-locality |
| 44 | bool topRLocality = false; |
| 45 | |
| 46 | protected: // methods |
| 47 | /// @return true if *THIS \subseteq SIG (\subset if IMPROPER = false ) |
| 48 | bool subset ( const TSignature& sig, bool improper ) const |
| 49 | { |
| 50 | iterator p = Set.begin(), p_end = Set.end(), q = sig.Set.begin(), q_end = sig.Set.end(); |
| 51 | bool proper = false; |
| 52 | while ( p != p_end && q != q_end ) |
| 53 | { |
| 54 | if ( *p < *q ) // something in THIS doesn't exist in SIG |
| 55 | return false; |
| 56 | if ( *q < *p ) |
| 57 | ++q, proper = true; |
| 58 | else |
| 59 | ++p, ++q; |
| 60 | } |
| 61 | if ( p != p_end ) // extra stuff in THIS |
| 62 | return false; |
| 63 | // here THIS is empty |
| 64 | if ( q != q_end ) // there are extra elements in SIG |
| 65 | return improper; |
| 66 | // both are done; the answer depends on flags |
| 67 | return improper ? true : proper; |
| 68 | } |
| 69 | |
| 70 | public: // interface |
| 71 | // add names to signature |
| 72 | |
| 73 | /// add pointer to named object to signature |
| 74 | void add ( const TNamedEntity* p ) { Set.insert(p); } |
| 75 | /// add set of named entities to signature |
| 76 | void add ( const BaseType& aSet ) { Set.insert ( aSet.begin(), aSet.end() ); } |
| 77 | /// add another signature to a given one |
| 78 | void add ( const TSignature& Sig ) { add(Sig.Set); } |
| 79 | /// remove given element from a signature |
| 80 | void remove ( const TNamedEntity* p ) { Set.erase(p); } |
| 81 | /// set new locality polarity |
| 82 | void setLocality ( bool topC, bool topR ) { topCLocality = topC; topRLocality = topR; } |
| 83 | /// set new locality polarity |
| 84 | void setLocality ( bool top ) { setLocality ( top, top ); } |
| 85 | |
| 86 | // comparison |
| 87 | |
| 88 | /// check whether 2 signatures are the same |
| 89 | bool operator == ( const TSignature& sig ) const { return Set == sig.Set; } |
| 90 | /// check whether 2 signatures are different |
| 91 | bool operator != ( const TSignature& sig ) const { return Set != sig.Set; } |
| 92 | /// @return true if *THIS \subset SIG |
| 93 | bool operator < ( const TSignature& sig ) const { return subset ( sig, /*improper=*/false ); } |
| 94 | /// @return true if *THIS \subseteq SIG |
| 95 | bool operator <= ( const TSignature& sig ) const { return subset ( sig, /*improper=*/true ); } |
| 96 | /// @return true if SIG \subset *THIS |
| 97 | bool operator > ( const TSignature& sig ) const { return sig.subset ( *this, /*improper=*/false ); } |
| 98 | /// @return true if SIG \subseteq *THIS |
| 99 | bool operator >= ( const TSignature& sig ) const { return sig.subset ( *this, /*improper=*/true ); } |
| 100 | /// @return true iff signature contains given element |
| 101 | bool contains ( const TNamedEntity* p ) const { return Set.count(p) > 0; } |
| 102 | /// @return true iff signature contains given element |
| 103 | bool contains ( const TDLExpression* p ) const |
| 104 | { |
| 105 | if ( const TNamedEntity* e = dynamic_cast<const TNamedEntity*>(p) ) |
| 106 | return contains(e); |
| 107 | if ( const TDLObjectRoleInverse* inv = dynamic_cast<const TDLObjectRoleInverse*>(p) ) |
| 108 | return contains(inv->getOR()); |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | /// @return size of the signature |
| 113 | size_t size ( void ) const { return Set.size(); } |
| 114 | /// clear the signature |
| 115 | void clear ( void ) { Set.clear(); } |
| 116 | |
| 117 | /// RO access to the elements of signature |
| 118 | iterator begin ( void ) const { return Set.begin(); } |
| 119 | /// RO access to the elements of signature |
| 120 | iterator end ( void ) const { return Set.end(); } |
| 121 | |
| 122 | /// @return true iff concepts are treated as TOPs |
| 123 | bool topCLocal ( void ) const { return topCLocality; } |
| 124 | /// @return true iff concepts are treated as BOTTOMs |
| 125 | bool botCLocal ( void ) const { return !topCLocality; } |
| 126 | /// @return true iff roles are treated as TOPs |
| 127 | bool topRLocal ( void ) const { return topRLocality; } |
| 128 | /// @return true iff roles are treated as BOTTOMs |
| 129 | bool botRLocal ( void ) const { return !topRLocality; } |
| 130 | }; // TSignature |
| 131 | |
| 132 | inline TSignature::BaseType |
| 133 | intersect ( const TSignature& s1, const TSignature& s2 ) |
| 134 | { |
| 135 | TSignature::BaseType ret; |
| 136 | set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(ret, ret.begin())); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | #endif |