| 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 | #include "globaldef.h" |
| 21 | |
| 22 | #ifdef RKG_USE_SORTED_REASONING |
| 23 | |
| 24 | #include "RoleMaster.h" |
| 25 | #include "dlDag.h" |
| 26 | #include "logging.h" |
| 27 | |
| 28 | /// merge label of given role and all its super-roles |
| 29 | void TRole :: mergeSupersDomain ( void ) |
| 30 | { |
| 31 | for ( const auto& sup: ancestors() ) |
| 32 | domLabel.merge(sup->domLabel); |
| 33 | |
| 34 | // for reflexive role -- merge domain and range labels |
| 35 | if ( isReflexive() ) |
| 36 | domLabel.merge(getRangeLabel()); |
| 37 | |
| 38 | // for R1*R2*...*Rn [= R, merge dom(R) with dom(R1) and ran(R) with ran(Rn) |
| 39 | for ( std::vector<TRoleVec>::iterator q = subCompositions.begin(), q_end = subCompositions.end(); q != q_end; ++q ) |
| 40 | if ( !q->empty() ) |
| 41 | { |
| 42 | domLabel.merge((*q->begin())->domLabel); |
| 43 | getRangeLabel().merge(q->back()->getRangeLabel()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// merge sorts for a given role |
| 48 | void DLDag :: mergeSorts ( TRole* R ) |
| 49 | { |
| 50 | // associate role domain labels |
| 51 | R->mergeSupersDomain(); |
| 52 | merge ( R->getDomainLabel(), R->getBPDomain() ); |
| 53 | // also associate functional nodes (if any) |
| 54 | for ( const auto& topf: R->topfuncs() ) |
| 55 | merge ( R->getDomainLabel(), topf->getFunctional() ); |
| 56 | } |
| 57 | |
| 58 | /// merge sorts for a given vertex |
| 59 | void DLDag :: mergeSorts ( DLVertex& v ) |
| 60 | { |
| 61 | switch ( v.Type() ) |
| 62 | { |
| 63 | case dtLE: // set R&D for role |
| 64 | case dtForall: |
| 65 | v.merge(const_cast<TRole*>(v.getRole())->getDomainLabel()); // domain(role)=cur |
| 66 | merge ( const_cast<TRole*>(v.getRole())->getRangeLabel(), v.getC() ); |
| 67 | break; |
| 68 | case dtProj: // projection: equate R&D of R and ProjR, and D(R) with C |
| 69 | v.merge(const_cast<TRole*>(v.getRole())->getDomainLabel()); |
| 70 | v.merge(const_cast<TRole*>(v.getProjRole())->getDomainLabel()); |
| 71 | merge ( const_cast<TRole*>(v.getRole())->getDomainLabel(), v.getC() ); |
| 72 | const_cast<TRole*>(v.getRole())->getRangeLabel().merge(const_cast<TRole*>(v.getProjRole())->getRangeLabel()); |
| 73 | break; |
| 74 | case dtIrr: // equate R&D for role |
| 75 | v.merge(const_cast<TRole*>(v.getRole())->getDomainLabel()); |
| 76 | v.merge(const_cast<TRole*>(v.getRole())->getRangeLabel()); |
| 77 | break; |
| 78 | case dtAnd: |
| 79 | for ( DLVertex::const_iterator q = v.begin(), q_end = v.end(); q < q_end; ++q ) |
| 80 | merge ( v.getSort(), *q ); |
| 81 | break; |
| 82 | case dtNSingleton: |
| 83 | case dtPSingleton: |
| 84 | case dtPConcept: |
| 85 | case dtNConcept: // merge with description |
| 86 | case dtChoose: |
| 87 | merge ( v.getSort(), v.getC() ); |
| 88 | break; |
| 89 | |
| 90 | case dtDataType: // nothing to do |
| 91 | case dtDataValue: |
| 92 | case dtDataExpr: |
| 93 | case dtNN: |
| 94 | break; |
| 95 | case dtTop: |
| 96 | default: |
| 97 | fpp_unreachable(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// build the sort system for given TBox |
| 102 | void DLDag :: determineSorts ( RoleMaster& ORM, RoleMaster& DRM ) |
| 103 | { |
| 104 | sortArraySize = Heap.size(); |
| 105 | |
| 106 | RoleMaster::iterator p, p_end; |
| 107 | HeapType::iterator i, i_end = Heap.end(); |
| 108 | |
| 109 | // init roles R&D sorts |
| 110 | for ( p = ORM.begin(), p_end = ORM.end(); p < p_end; ++p ) |
| 111 | if ( !(*p)->isSynonym() ) |
| 112 | mergeSorts(*p); |
| 113 | for ( p = DRM.begin(), p_end = DRM.end(); p < p_end; ++p ) |
| 114 | if ( !(*p)->isSynonym() ) |
| 115 | mergeSorts(*p); |
| 116 | |
| 117 | for ( i = Heap.begin()+2; i < i_end; ++i ) |
| 118 | mergeSorts(**i); |
| 119 | |
| 120 | unsigned int sum = 0; |
| 121 | for ( i = Heap.begin()+2; i < i_end; ++i ) |
| 122 | { |
| 123 | mergeableLabel& lab = (*i)->getSort(); |
| 124 | lab.resolve(); |
| 125 | if ( lab.isSample() ) |
| 126 | ++sum; |
| 127 | } |
| 128 | |
| 129 | for ( p = ORM.begin(), p_end = ORM.end(); p < p_end; ++p ) |
| 130 | if ( !(*p)->isSynonym() ) |
| 131 | { |
| 132 | mergeableLabel& lab = const_cast<TRole*>(*p)->getDomainLabel(); |
| 133 | lab.resolve(); |
| 134 | if ( lab.isSample() ) |
| 135 | ++sum; |
| 136 | } |
| 137 | for ( p = DRM.begin(), p_end = DRM.end(); p < p_end; ++p ) |
| 138 | if ( !(*p)->isSynonym() ) |
| 139 | { |
| 140 | mergeableLabel& lab = const_cast<TRole*>(*p)->getDomainLabel(); |
| 141 | lab.resolve(); |
| 142 | if ( lab.isSample() ) |
| 143 | ++sum; |
| 144 | } |
| 145 | |
| 146 | CHECK_LL_RETURN(llAlways); |
| 147 | // we added a temp concept here; don't count it |
| 148 | if ( sum > 0 ) |
| 149 | sum--; |
| 150 | |
| 151 | LL << "\nThere are "; |
| 152 | if ( sum > 1 ) |
| 153 | LL << sum; |
| 154 | else |
| 155 | LL << "no"; |
| 156 | LL << " different sorts in TBox\n"; |
| 157 | } |
| 158 | |
| 159 | #endif // RKG_USE_SORTED_REASONING |