| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2005-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 <cmath> |
| 21 | #include <iostream> |
| 22 | |
| 23 | #include "dlTBox.h" |
| 24 | |
| 25 | void TBox :: setRelevant ( BipolarPointer p ) |
| 26 | { |
| 27 | fpp_assert ( isValid(p) ); |
| 28 | |
| 29 | if ( p == bpTOP || p == bpBOTTOM ) |
| 30 | return; |
| 31 | |
| 32 | const DLVertex& v = DLHeap[p]; |
| 33 | bool pos = isPositive(p); |
| 34 | |
| 35 | ++nRelevantBCalls; |
| 36 | collectLogicFeature(v,pos); |
| 37 | |
| 38 | switch ( v.Type() ) |
| 39 | { |
| 40 | case dtDataType: // types and values are not relevant |
| 41 | case dtDataValue: |
| 42 | case dtDataExpr: |
| 43 | case dtNN: // not appear in any expression => not relevant |
| 44 | break; |
| 45 | |
| 46 | case dtPConcept: // negated primitive entries -- does nothing |
| 47 | case dtPSingleton: |
| 48 | // if ( !pos ) |
| 49 | // break; |
| 50 | |
| 51 | // fall through |
| 52 | case dtNConcept: // named concepts |
| 53 | case dtNSingleton: |
| 54 | setRelevant(const_cast<TConcept*>(static_cast<const TConcept*>(v.getConcept()))); |
| 55 | break; |
| 56 | |
| 57 | case dtForall: |
| 58 | case dtLE: |
| 59 | setRelevant(const_cast<TRole*>(v.getRole())); |
| 60 | setRelevant (v.getC()); |
| 61 | break; |
| 62 | |
| 63 | case dtProj: // no need to set (inverse) roles as it doesn't really matter |
| 64 | case dtChoose: |
| 65 | setRelevant(v.getC()); |
| 66 | break; |
| 67 | |
| 68 | case dtIrr: |
| 69 | setRelevant(const_cast<TRole*>(v.getRole())); |
| 70 | break; |
| 71 | |
| 72 | case dtAnd: |
| 73 | for ( DLVertex::const_iterator q = v.begin(); q != v.end(); ++q ) |
| 74 | setRelevant(*q); |
| 75 | break; |
| 76 | |
| 77 | default: |
| 78 | std::cerr << "Error setting relevant vertex of type " << v.getTagName() << "(" << v.Type () << ")"; |
| 79 | fpp_unreachable(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void TBox :: setRelevant1 ( TConcept* p ) |
| 84 | { |
| 85 | ++nRelevantCCalls; |
| 86 | p->setRelevant(relevance); |
| 87 | collectLogicFeature(p); |
| 88 | setRelevant (p->pBody); |
| 89 | } |
| 90 | |
| 91 | void TBox :: setRelevant1 ( TRole* p ) |
| 92 | { |
| 93 | p->setRelevant(relevance); |
| 94 | collectLogicFeature(p); |
| 95 | |
| 96 | // Range and Domain are also relevant |
| 97 | setRelevant ( p->getBPDomain() ); |
| 98 | setRelevant ( p->getBPRange() ); |
| 99 | |
| 100 | // all super-roles are also relevant |
| 101 | for ( auto& sup: p->ancestors() ) |
| 102 | setRelevant(sup); |
| 103 | } |
| 104 | |
| 105 | void TBox :: gatherRelevanceInfo ( void ) |
| 106 | { |
| 107 | nRelevantCCalls = 0; |
| 108 | nRelevantBCalls = 0; |
| 109 | |
| 110 | // gather GCIs features |
| 111 | curFeature = &GCIFeatures; |
| 112 | markGCIsRelevant(); |
| 113 | clearRelevanceInfo(); |
| 114 | KBFeatures |= GCIFeatures; |
| 115 | |
| 116 | // fills in nominal cloud relevance info |
| 117 | NCFeatures = GCIFeatures; |
| 118 | |
| 119 | // set up relevance info |
| 120 | for ( i_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 121 | { |
| 122 | setConceptRelevant(*pi); |
| 123 | NCFeatures |= (*pi)->posFeatures; |
| 124 | } |
| 125 | |
| 126 | // correct NC inverse role information |
| 127 | if ( NCFeatures.hasSomeAll() && !RelatedI.empty() ) |
| 128 | NCFeatures.setInverseRoles(); |
| 129 | |
| 130 | for ( c_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 131 | setConceptRelevant(*pc); |
| 132 | auto cSize = ( c_end() - c_begin() ) + ( i_end() - i_begin() ); |
| 133 | size_t bSize = DLHeap.size()-2; |
| 134 | |
| 135 | curFeature = nullptr; |
| 136 | |
| 137 | float cRatio, bRatio = 0, logCSize = 1, logBSize = 1, sqCSize = 1, sqBSize = 1; |
| 138 | if ( cSize > 10 ) |
| 139 | { |
| 140 | cRatio = ((float)nRelevantCCalls)/cSize; |
| 141 | sqCSize = sqrtf((float)cSize); |
| 142 | if ( cSize > 1 ) |
| 143 | logCSize = logf((float)cSize); |
| 144 | } |
| 145 | if ( bSize > 20 ) |
| 146 | { |
| 147 | bRatio = ((float)nRelevantBCalls)/bSize; |
| 148 | sqBSize = sqrtf((float)bSize); |
| 149 | if ( bSize > 1 ) |
| 150 | logBSize = logf((float)bSize); |
| 151 | } |
| 152 | |
| 153 | if (0) // relevance stat |
| 154 | { |
| 155 | if ( LLM.isWritable(llAlways) && cSize > 10 ) |
| 156 | LL << "There were made " << nRelevantCCalls << " relevance C calls for " |
| 157 | << cSize << " concepts\nRC ratio=" << cRatio << ", ratio/logSize=" |
| 158 | << cRatio/logCSize << ", ratio/sqSize=" << cRatio/sqCSize << ", ratio/size=" |
| 159 | << cRatio/cSize<< "\n"; |
| 160 | if ( LLM.isWritable(llAlways) && bSize > 20 ) |
| 161 | LL << "There were made " << nRelevantBCalls << " relevance B calls for " |
| 162 | << bSize << " nodes\nRB ratio=" << bRatio << ", ratio/logSize=" |
| 163 | << bRatio/logBSize << ", ratio/sqSize=" << bRatio/sqBSize << ", ratio/size=" |
| 164 | << bRatio/bSize << "\n"; |
| 165 | } |
| 166 | |
| 167 | // set up GALEN-like flag; based on r/n^{3/2}, add r/n^2<1 |
| 168 | isLikeGALEN = (bRatio > sqBSize*20) && (bRatio < bSize); |
| 169 | |
| 170 | // switch off sorted reasoning iff top role appears |
| 171 | if ( KBFeatures.hasTopRole() ) |
| 172 | useSortedReasoning = false; |
| 173 | } |