| 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 "tConcept.h"cxx . . $ `<file>/Kernel/tConcept.h`/ |
| 21 | #include "tRole.h"cxx . . $ `<file>/Kernel/tRole.h`/ |
| 22 | |
| 23 | void TConceptcxx . . $ TConcept# :: clearcxx . . $ TConcept#clear(49f6e7a06ebc5aa8). ( void ) |
| 24 | { |
| 25 | // TNamedEntry clean |
| 26 | setIdcxx . . $ TNamedEntry#setId(d4f767463ce0a6b3).(0); |
| 27 | // ClassifiableEntry clean |
| 28 | taxVertexcxx . . $ ClassifiableEntry#taxVertex. = nullptr; |
| 29 | toldSubsumerscxx . . $ ClassifiableEntry#toldSubsumers..clearcxx . . $ std/vector#clear(49f6e7a06ebc5aa8).(); |
| 30 | setCompletelyDefinedcxx . . $ ClassifiableEntry#setCompletelyDefined(b45ddcbc8423892c).(false); |
| 31 | pSynonymcxx . . $ ClassifiableEntry#pSynonym. = nullptr; |
| 32 | // TConcept clean |
| 33 | removeDescriptioncxx . . $ TConcept#removeDescription(49f6e7a06ebc5aa8).(); |
| 34 | setPrimitivecxx . . $ TConcept#setPrimitive(49f6e7a06ebc5aa8).(); |
| 35 | pNamecxx . . $ TConcept#pName. = pBodycxx . . $ TConcept#pBody. = bpINVALIDcxx . . $ bpINVALID.; |
| 36 | } |
| 37 | |
| 38 | void TConceptcxx . . $ TConcept# :: addDesccxx . . $ TConcept#addDesc(edf821a2fc5509f9). ( DLTreecxx . . $ DLTree#* Desc ) |
| 39 | { |
| 40 | fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`! (!isNonPrimitive()); // safety check |
| 41 | |
| 42 | // FIXME!! check about reverse order |
| 43 | Descriptioncxx . . $ TConcept#Description. = createSNFAndcxx . . $ createSNFAnd(f2877343b7b69ea7). ( Desc, Descriptioncxx . . $ TConcept#Description. ); |
| 44 | } |
| 45 | |
| 46 | /// calculate value of classification TAG based on told subsumers. WARNING: no TS cycles allowed |
| 47 | CTTagcxx . . $ CTTag# TConceptcxx . . $ TConcept# :: determineClassTagcxx . . $ TConcept#determineClassTag(9839f985a220c015). ( void ) |
| 48 | { |
| 49 | // for synonyms -- set tag as a primer's one |
| 50 | if ( isSynonymcxx . . $ ClassifiableEntry#isSynonym(50ce9a9e25b4a850).() ) |
| 51 | return resolveSynonymcxx . . $ resolveSynonym(98ea26de6545f0ca).(this)->getClassTagcxx . . $ TConcept#getClassTag(9839f985a220c015).(); |
| 52 | |
| 53 | // check if it is non-primitive |
| 54 | if ( isNonPrimitivecxx . . $ TConcept#isNonPrimitive(50ce9a9e25b4a850).() ) |
| 55 | return cttNonPrimitivecxx . . $ cttNonPrimitive.; |
| 56 | |
| 57 | // no told subsumers |
| 58 | if ( !hasToldSubsumerscxx . . $ ClassifiableEntry#hasToldSubsumers(50ce9a9e25b4a850).() ) |
| 59 | return cttOrphancxx . . $ cttOrphan.; |
| 60 | |
| 61 | // now need to check all the told subsumers |
| 62 | bool hasLCD = false; |
| 63 | bool hasOther = false; |
| 64 | bool hasNP = false; |
| 65 | |
| 66 | for ( auto& p: toldcxx . . $ ClassifiableEntry#told(e7230ad44187fae2).() ) |
| 67 | switch ( static_cast<TConceptcxx . . $ TConcept#*>(p)->getClassTagcxx . . $ TConcept#getClassTag(9839f985a220c015).() ) |
| 68 | { |
| 69 | case cttTrueCompletelyDefinedcxx . . $ cttTrueCompletelyDefined.: |
| 70 | break; |
| 71 | case cttOrphancxx . . $ cttOrphan.: |
| 72 | case cttLikeCompletelyDefinedcxx . . $ cttLikeCompletelyDefined.: |
| 73 | hasLCD = true; |
| 74 | break; |
| 75 | case cttRegularcxx . . $ cttRegular.: |
| 76 | hasOther = true; |
| 77 | break; |
| 78 | case cttHasNonPrimitiveTScxx . . $ cttHasNonPrimitiveTS.: |
| 79 | case cttNonPrimitivecxx . . $ cttNonPrimitive.: |
| 80 | hasNP = true; |
| 81 | break; |
| 82 | default: |
| 83 | fpp_unreachablecxx . . $ `Kernel/fpp_assert.h:49:9`!(); |
| 84 | } |
| 85 | |
| 86 | // there are non-primitive TS |
| 87 | if ( hasNP ) |
| 88 | return cttHasNonPrimitiveTScxx . . $ cttHasNonPrimitiveTS.; |
| 89 | |
| 90 | // has something different from CD-like ones (and not CD) |
| 91 | if ( hasOther || !isCompletelyDefinedcxx . . $ ClassifiableEntry#isCompletelyDefined(50ce9a9e25b4a850).() ) |
| 92 | return cttRegularcxx . . $ cttRegular.; |
| 93 | |
| 94 | // no more 'other' concepts here, and the CD-like structure |
| 95 | if ( hasLCD ) |
| 96 | return cttLikeCompletelyDefinedcxx . . $ cttLikeCompletelyDefined.; |
| 97 | |
| 98 | return cttTrueCompletelyDefinedcxx . . $ cttTrueCompletelyDefined.; |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | TConceptcxx . . $ TConcept# :: hasSelfInDesccxx . . $ TConcept#hasSelfInDesc(4f9ee8c35ac4b1e6). ( const DLTreecxx . . $ DLTree#* t ) const |
| 103 | { |
| 104 | if ( t == nullptr ) |
| 105 | return false; |
| 106 | |
| 107 | switch ( t->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() ) |
| 108 | { |
| 109 | case NAMEcxx . . $ `Kernel/grammar.h:61:9`!: // if ID contains synonym of P |
| 110 | { |
| 111 | const ClassifiableEntrycxx . . $ ClassifiableEntry#* name = static_cast<const ClassifiableEntrycxx . . $ ClassifiableEntry#*>(t->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getNEcxx . . $ TLexeme#getNE(28c03b9fd3c689f8).()); |
| 112 | return resolveSynonymcxx . . $ resolveSynonym(bd9f52c6ae4be57a).(name) == this; |
| 113 | } |
| 114 | case ANDcxx . . $ AND.: |
| 115 | return hasSelfInDesccxx . . $ TConcept#hasSelfInDesc(4f9ee8c35ac4b1e6).(t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()) || hasSelfInDesccxx . . $ TConcept#hasSelfInDesc(4f9ee8c35ac4b1e6).(t->Rightcxx . . $ TsTTree#Right(f4b2f47551fb31d7).()); |
| 116 | case NOTcxx . . $ NOT.: // a [= (not a) -> a [= BOTTOM; a [= (a or b) -> a [= TOP |
| 117 | switch ( t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()->Elementcxx . . $ TsTTree#Element(19b2b6f17af048b).().getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() ) |
| 118 | { |
| 119 | case ANDcxx . . $ AND.: |
| 120 | case NAMEcxx . . $ `Kernel/grammar.h:61:9`!: |
| 121 | return hasSelfInDesccxx . . $ TConcept#hasSelfInDesc(4f9ee8c35ac4b1e6).(t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()); |
| 122 | default: |
| 123 | return false; |
| 124 | } |
| 125 | default: |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | DLTreecxx . . $ DLTree#* |
| 131 | TConceptcxx . . $ TConcept# :: replaceSelfWithConstcxx . . $ TConcept#replaceSelfWithConst(8f188fbd9d5632d6). ( const DLTreecxx . . $ DLTree#* t ) const |
| 132 | { |
| 133 | if ( t == nullptr ) |
| 134 | return nullptr; |
| 135 | |
| 136 | switch ( t->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() ) |
| 137 | { |
| 138 | case NAMEcxx . . $ `Kernel/grammar.h:61:9`!: // if ID contains synonym of P |
| 139 | { |
| 140 | const ClassifiableEntrycxx . . $ ClassifiableEntry#* name = static_cast<const ClassifiableEntrycxx . . $ ClassifiableEntry#*>(t->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getNEcxx . . $ TLexeme#getNE(28c03b9fd3c689f8).()); |
| 141 | if ( resolveSynonymcxx . . $ resolveSynonym(bd9f52c6ae4be57a).(name) == this ) |
| 142 | return createTopcxx . . $ createTop(aa22cc08b69ae2ef).(); |
| 143 | else |
| 144 | break; |
| 145 | } |
| 146 | case ANDcxx . . $ AND.: // a [= (and a b) -> a [= b |
| 147 | return createSNFAndcxx . . $ createSNFAnd(f2877343b7b69ea7). ( replaceSelfWithConstcxx . . $ TConcept#replaceSelfWithConst(8f188fbd9d5632d6).(t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()), replaceSelfWithConstcxx . . $ TConcept#replaceSelfWithConst(8f188fbd9d5632d6).(t->Rightcxx . . $ TsTTree#Right(f4b2f47551fb31d7).()) ); |
| 148 | case NOTcxx . . $ NOT.: // a [= (not a) -> a [= BOTTOM; a [= (a or b) -> a [= TOP |
| 149 | switch ( t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()->Elementcxx . . $ TsTTree#Element(19b2b6f17af048b).().getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() ) |
| 150 | { |
| 151 | case ANDcxx . . $ AND.: |
| 152 | case NAMEcxx . . $ `Kernel/grammar.h:61:9`!: |
| 153 | return createSNFNotcxx . . $ createSNFNot(1bf715d485c1268).(replaceSelfWithConstcxx . . $ TConcept#replaceSelfWithConst(8f188fbd9d5632d6).(t->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).())); |
| 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | return clonecxx . . $ clone(ec41a33c52a5614b).(t); |
| 161 | } |
| 162 | |
| 163 | /// init told subsumers of the concept by given DESCription; @return TRUE iff concept is CD |
| 164 | bool TConceptcxx . . $ TConcept# :: initToldSubsumerscxx . . $ TConcept#initToldSubsumers(791c1b764e5b10e1). ( const DLTreecxx . . $ DLTree#* desc, RoleSSetcxx . . $ TConcept#RoleSSet#& RolesProcessed ) |
| 165 | { |
| 166 | // no description => nothing to do (and yes, it is told) |
| 167 | if ( desc == nullptr ) |
| 168 | return true; |
| 169 | |
| 170 | switch ( desc->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() ) |
| 171 | { |
| 172 | case TOPcxx . . $ TOP.: // the 1st node |
| 173 | return true; |
| 174 | case NAMEcxx . . $ `Kernel/grammar.h:61:9`!: // it is a concept ID |
| 175 | return addToldSubsumercxx . . $ TConcept#addToldSubsumer(3a0d410f9ff34d05).(static_cast<TConceptcxx . . $ TConcept#*>(desc->Elementcxx . . $ TsTTree#Element(ea465e50d690edfe).().getNEcxx . . $ TLexeme#getNE(28c03b9fd3c689f8).())); |
| 176 | case ANDcxx . . $ AND.: // add TS from BOTH parts of AND |
| 177 | return initToldSubsumerscxx . . $ TConcept#initToldSubsumers(791c1b764e5b10e1). ( desc->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).(), RolesProcessed ) & initToldSubsumerscxx . . $ TConcept#initToldSubsumers(791c1b764e5b10e1). ( desc->Rightcxx . . $ TsTTree#Right(f4b2f47551fb31d7).(), RolesProcessed ); |
| 178 | case NOTcxx . . $ NOT.: // Domains from \ER.C and (>= n R.C) are told concepts |
| 179 | { |
| 180 | const TLexemecxx . . $ TLexeme#& cur = desc->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()->Elementcxx . . $ TsTTree#Element(19b2b6f17af048b).(); |
| 181 | |
| 182 | if ( cur.getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() == FORALLcxx . . $ FORALL. || cur.getTokencxx . . $ TLexeme#getToken(d71a1020745f3953).() == LEcxx . . $ LE. ) |
| 183 | SearchTSbyRoleAndSuperscxx . . $ TConcept#SearchTSbyRoleAndSupers(d7566cf11d0aec48). ( resolveRolecxx . . $ resolveRole(c256d257f6e22050).(desc->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()), RolesProcessed ); |
| 184 | |
| 185 | return false; |
| 186 | } |
| 187 | case SELFcxx . . $ SELF.: // Domains and Range from participating role |
| 188 | { |
| 189 | const TRolecxx . . $ TRole#* R = resolveRolecxx . . $ resolveRole(c256d257f6e22050).(desc->Leftcxx . . $ TsTTree#Left(f4b2f47551fb31d7).()); |
| 190 | SearchTSbyRoleAndSuperscxx . . $ TConcept#SearchTSbyRoleAndSupers(d7566cf11d0aec48). ( R, RolesProcessed ); |
| 191 | SearchTSbyRoleAndSuperscxx . . $ TConcept#SearchTSbyRoleAndSupers(d7566cf11d0aec48). ( R->inversecxx . . $ TRole#inverse(69f26110a94b259f).(), RolesProcessed ); |
| 192 | return false; |
| 193 | } |
| 194 | default: // not told one |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void TConceptcxx . . $ TConcept# :: SearchTSbyRolecxx . . $ TConcept#SearchTSbyRole(d7566cf11d0aec48). ( const TRolecxx . . $ TRole#* R, RoleSSetcxx . . $ TConcept#RoleSSet#& RolesProcessed ) |
| 200 | { |
| 201 | const DLTreecxx . . $ DLTree#* Domain = R->getTDomaincxx . . $ TRole#getTDomain(7ba0ba2d2b26f6a8).(); |
| 202 | if ( Domain == nullptr || isConstcxx . . $ isConst(937bda690ff1bb73).(Domain) ) |
| 203 | return; |
| 204 | |
| 205 | // don't process the same role twice |
| 206 | if ( RolesProcessed.findcxx . . $ std/set#find(f8922f46a681e6cc).(R) !=cxx . . $ std/`operator!=`(7e1308a0738ea758). RolesProcessed.endcxx . . $ std/set#end(a1617ab40ea76896).() ) |
| 207 | return; |
| 208 | |
| 209 | // mark role as processed; usually it's the only role, so set hint as a begin() |
| 210 | RolesProcessed.insertcxx . . $ std/set#insert(b0ba4ad043089306). ( RolesProcessed.begincxx . . $ std/set#begin(a1617ab40ea76896).(), R ); |
| 211 | |
| 212 | // init TS by the domain of role |
| 213 | initToldSubsumerscxx . . $ TConcept#initToldSubsumers(791c1b764e5b10e1). ( Domain, RolesProcessed ); // don't bother about result |
| 214 | } |
| 215 | |
| 216 | void TConceptcxx . . $ TConcept# :: SearchTSbyRoleAndSuperscxx . . $ TConcept#SearchTSbyRoleAndSupers(d7566cf11d0aec48). ( const TRolecxx . . $ TRole#* r, RoleSSetcxx . . $ TConcept#RoleSSet#& RolesProcessed ) |
| 217 | { |
| 218 | SearchTSbyRolecxx . . $ TConcept#SearchTSbyRole(d7566cf11d0aec48). ( r, RolesProcessed ); |
| 219 | |
| 220 | // do the same for all super-roles if necessary |
| 221 | // FIXME!! need to do the same for DomSupers (like SoR [= R) |
| 222 | for ( const auto& sup: r->ancestorscxx . . $ TRole#ancestors(5c1fd3e4e1fb59f2).() ) |
| 223 | SearchTSbyRolecxx . . $ TConcept#SearchTSbyRole(d7566cf11d0aec48). ( sup, RolesProcessed ); |
| 224 | } |
| 225 | |
| 226 | unsigned int TConceptcxx . . $ TConcept# :: calculateTSDepthcxx . . $ TConcept#calculateTSDepth(db52760e79bf9054). ( void ) |
| 227 | { |
| 228 | if ( tsDepthcxx . . $ TConcept#tsDepth. > 0 ) |
| 229 | return tsDepthcxx . . $ TConcept#tsDepth.; |
| 230 | |
| 231 | unsigned int max = 0; |
| 232 | |
| 233 | for ( auto& p: toldcxx . . $ ClassifiableEntry#told(e7230ad44187fae2).() ) |
| 234 | { |
| 235 | unsigned int cur = static_cast<TConceptcxx . . $ TConcept#*>(p)->calculateTSDepthcxx . . $ TConcept#calculateTSDepth(db52760e79bf9054).(); |
| 236 | if ( max < cur ) |
| 237 | max = cur; |
| 238 | } |
| 239 | |
| 240 | return (tsDepthcxx . . $ TConcept#tsDepth. = max+1); |
| 241 | } |