| 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 ROLEMASTER_H |
| 21 | #define ROLEMASTER_H |
| 22 | |
| 23 | #include "globaldef.h" |
| 24 | #include "tNameSet.h" |
| 25 | #include "eFPPCantRegName.h" |
| 26 | #include "tRole.h" |
| 27 | #include "Taxonomy.h" |
| 28 | |
| 29 | class RoleMaster |
| 30 | { |
| 31 | public: // types |
| 32 | /// vector of roles |
| 33 | typedef TRole::TRoleVec TRoleVec; |
| 34 | /// RW access to roles |
| 35 | typedef TRoleVec::iterator iterator; |
| 36 | /// RO access to roles |
| 37 | typedef TRoleVec::const_iterator const_iterator; |
| 38 | |
| 39 | protected: // members |
| 40 | /// number of the last registered role |
| 41 | int newRoleId = 1; |
| 42 | |
| 43 | /// all registered roles |
| 44 | TRoleVec Roles; |
| 45 | /// internal empty role (bottom in the taxonomy) |
| 46 | TRole emptyRole; |
| 47 | /// internal universal role (top in the taxonomy) |
| 48 | TRole universalRole; |
| 49 | |
| 50 | /// roles nameset |
| 51 | TNameSet<TRole> roleNS; |
| 52 | /// Taxonomy of roles |
| 53 | Taxonomy* pTax = nullptr; |
| 54 | |
| 55 | /// two halves of disjoint roles axioms |
| 56 | TRoleVec DJRolesA, DJRolesB; |
| 57 | |
| 58 | /// flag whether to create data roles or not |
| 59 | bool DataRoles; |
| 60 | |
| 61 | /// flag if it is possible to introduce new names |
| 62 | bool useUndefinedNames = true; |
| 63 | |
| 64 | private: // methods |
| 65 | /// constant defining first user role in the RBox |
| 66 | static unsigned int firstRoleIndex ( void ) { return 2; } |
| 67 | |
| 68 | protected: // methods |
| 69 | /// register TRole and it's inverse in RoleBox |
| 70 | void registerRole ( TRole* r ); |
| 71 | /// @return true if P is a role that is registered in the RM |
| 72 | bool isRegisteredRole ( const TNamedEntry* p ) const |
| 73 | { |
| 74 | const TRole* R = dynamic_cast<const TRole*>(p); |
| 75 | if ( R == nullptr ) |
| 76 | return false; |
| 77 | unsigned int ind = R->getIndex(); |
| 78 | return ( ind >= firstRoleIndex() && |
| 79 | ind < Roles.size() && |
| 80 | Roles[ind] == p ); |
| 81 | } |
| 82 | |
| 83 | /// add parent for the input role; both roles are not synonyms |
| 84 | void addRoleParentProper ( TRole* role, TRole* parent ) const; |
| 85 | |
| 86 | /// get number of roles |
| 87 | size_t size ( void ) const { return Roles.size()/2-1; } |
| 88 | |
| 89 | public: // interface |
| 90 | /// the only c'tor |
| 91 | RoleMaster ( bool dataRoles, const std::string& TopRoleName, const std::string& BotRoleName ); |
| 92 | /// no copy c'tor |
| 93 | RoleMaster ( const RoleMaster& ) = delete; |
| 94 | /// no assignment |
| 95 | RoleMaster& operator = ( const RoleMaster& ) = delete; |
| 96 | /// d'tor (delete taxonomy) |
| 97 | ~RoleMaster() { delete pTax; } |
| 98 | |
| 99 | /// create role entry with given name |
| 100 | TRole* ensureRoleName ( const std::string& name ); |
| 101 | |
| 102 | /// add parent for the input role or role composition; delete ROLE afterwards |
| 103 | void addRoleParent ( DLTree* role, TRole* parent ) const; |
| 104 | /// add parent for the input role |
| 105 | void addRoleParent ( TRole* role, TRole* parent ) const { addRoleParentProper ( resolveSynonym(role), resolveSynonym(parent) ); } |
| 106 | /// add synonym to existing role |
| 107 | void addRoleSynonym ( TRole* role, TRole* syn ) const |
| 108 | { |
| 109 | // no synonyms |
| 110 | // role = resolveSynonym(role); |
| 111 | // syn = resolveSynonym(syn); |
| 112 | // FIXME!! 1st call can make one of them a synonym of a const |
| 113 | addRoleParentProper ( resolveSynonym(role), resolveSynonym(syn) ); |
| 114 | addRoleParentProper ( resolveSynonym(syn), resolveSynonym(role) ); |
| 115 | } |
| 116 | |
| 117 | /// register a pair of disjoint roles |
| 118 | void addDisjointRoles ( TRole* R, TRole* S ) |
| 119 | { |
| 120 | // object- and data roles are always disjoint |
| 121 | if ( R->isDataRole() != S->isDataRole() ) |
| 122 | return; |
| 123 | DJRolesA.push_back(R); |
| 124 | DJRolesB.push_back(S); |
| 125 | } |
| 126 | |
| 127 | /// create taxonomy of roles (using the Parent data) |
| 128 | void initAncDesc ( void ); |
| 129 | |
| 130 | /// change the undefined names usage policy |
| 131 | void setUndefinedNames ( bool val ) { useUndefinedNames = val; } |
| 132 | |
| 133 | // access to roles |
| 134 | |
| 135 | /// @return pointer to a TOP role |
| 136 | TRole* getTopRole ( void ) { return &universalRole; } |
| 137 | /// @return const pointer to a TOP role |
| 138 | const TRole* getTopRole ( void ) const { return &universalRole; } |
| 139 | /// @return pointer to a BOTTOM role |
| 140 | TRole* getBotRole ( void ) { return &emptyRole; } |
| 141 | /// @return const pointer to a BOTTOM role |
| 142 | const TRole* getBotRole ( void ) const { return &emptyRole; } |
| 143 | /// RW pointer to the first user-defined role |
| 144 | iterator begin ( void ) { return Roles.begin()+firstRoleIndex(); } |
| 145 | /// RW pointer after the last user-defined role |
| 146 | iterator end ( void ) { return Roles.end(); } |
| 147 | /// RO pointer to the first user-defined role |
| 148 | const_iterator begin ( void ) const { return Roles.begin()+firstRoleIndex(); } |
| 149 | /// RO pointer after the last user-defined role |
| 150 | const_iterator end ( void ) const { return Roles.end(); } |
| 151 | |
| 152 | /// get access to the taxonomy |
| 153 | Taxonomy* getTaxonomy ( void ) const { return pTax; } |
| 154 | |
| 155 | /// @return true iff there is a reflexive role |
| 156 | bool hasReflexiveRoles ( void ) const; |
| 157 | /// put all reflexive roles to a RR array |
| 158 | void fillReflexiveRoles ( TRoleVec& RR ) const; |
| 159 | |
| 160 | // output interface |
| 161 | |
| 162 | void Print ( std::ostream& o, const char* type ) const |
| 163 | { |
| 164 | if ( size() == 0 ) |
| 165 | return; |
| 166 | o << type << " Roles (" << size() << "):\n"; |
| 167 | emptyRole.Print(o); |
| 168 | for ( const TRole* role : *this ) |
| 169 | role->Print(o); |
| 170 | } |
| 171 | }; // RoleMaster |
| 172 | |
| 173 | inline bool |
| 174 | RoleMaster :: hasReflexiveRoles ( void ) const |
| 175 | { |
| 176 | for ( const_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 177 | if ( (*p)->isReflexive() ) |
| 178 | return true; |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | inline void |
| 184 | RoleMaster :: fillReflexiveRoles ( TRoleVec& RR ) const |
| 185 | { |
| 186 | RR.clear(); |
| 187 | for ( const_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 188 | if ( !(*p)->isSynonym() && (*p)->isReflexive() ) |
| 189 | RR.push_back(*p); |
| 190 | } |
| 191 | |
| 192 | #endif |