| 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 <ostream> |
| 21 | |
| 22 | #include "modelCacheIan.h" |
| 23 | |
| 24 | /// clear the cache |
| 25 | void |
| 26 | modelCacheIan :: clear ( void ) |
| 27 | { |
| 28 | posDConcepts.clear(); |
| 29 | posNConcepts.clear(); |
| 30 | negDConcepts.clear(); |
| 31 | negNConcepts.clear(); |
| 32 | #ifdef RKG_USE_SIMPLE_RULES |
| 33 | extraDConcepts.clear(); |
| 34 | extraNConcepts.clear(); |
| 35 | #endif |
| 36 | existsRoles.clear(); |
| 37 | forallRoles.clear(); |
| 38 | funcRoles.clear(); |
| 39 | curState = csValid; |
| 40 | } |
| 41 | |
| 42 | void modelCacheIan :: processConcept ( const DLVertex& cur, bool pos, bool det ) |
| 43 | { |
| 44 | switch ( cur.Type() ) |
| 45 | { |
| 46 | case dtTop: // sanity checks |
| 47 | case dtDataType: // data entries can not be cached |
| 48 | case dtDataValue: |
| 49 | case dtDataExpr: |
| 50 | fpp_unreachable(); |
| 51 | |
| 52 | case dtNConcept: // add concepts to Concepts |
| 53 | case dtPConcept: |
| 54 | case dtNSingleton: |
| 55 | case dtPSingleton: |
| 56 | (det ? getDConcepts(pos) : getNConcepts(pos)).insert(static_cast<const ClassifiableEntry*>(cur.getConcept())->index()); |
| 57 | break; |
| 58 | |
| 59 | case dtIrr: // for \neg \ER.Self: add R to AR-set |
| 60 | case dtForall: // add AR.C roles to forallRoles |
| 61 | case dtLE: // for <= n R: add R to forallRoles |
| 62 | if ( unlikely ( cur.getRole()->isTop() ) ) // force clash to every other edge |
| 63 | (pos ? forallRoles : existsRoles).completeSet(); |
| 64 | else if ( pos ) // no need to deal with existentials here: they would be created through edges |
| 65 | { |
| 66 | if ( cur.getRole()->isSimple() ) |
| 67 | forallRoles.insert(cur.getRole()->index()); |
| 68 | else |
| 69 | processAutomaton(cur); |
| 70 | } |
| 71 | break; |
| 72 | |
| 73 | default: // all other -- nothing to do |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | modelCacheIan :: processAutomaton ( const DLVertex& cur ) |
| 80 | { |
| 81 | const RAStateTransitions& RST = cur.getRole()->getAutomaton()[cur.getState()]; |
| 82 | |
| 83 | // for every transition starting from a given state, |
| 84 | // add the role that is accepted by a transition |
| 85 | for ( const auto& trans: RST ) |
| 86 | for ( const auto& R: trans ) |
| 87 | forallRoles.insert(R->index()); |
| 88 | } |
| 89 | |
| 90 | modelCacheState modelCacheIan :: canMerge ( const modelCacheInterface* cache ) const |
| 91 | { |
| 92 | if ( hasNominalClash(cache) ) // fail to merge due to nominal presence |
| 93 | return csFailed; |
| 94 | |
| 95 | // check if something goes wrong |
| 96 | if ( cache->getState () != csValid || getState () != csValid ) |
| 97 | return mergeStatus ( cache->getState (), getState () ); |
| 98 | |
| 99 | // here both models are valid; |
| 100 | |
| 101 | if ( auto cacheIan = dynamic_cast<const modelCacheIan*>(cache) ) |
| 102 | return isMergeableIan(cacheIan); |
| 103 | if ( auto cacheSingleton = dynamic_cast<const modelCacheSingleton*>(cache) ) |
| 104 | return isMergeableSingleton(cacheSingleton->getValue()); |
| 105 | if ( dynamic_cast<const modelCacheConst*>(cache) ) |
| 106 | return csValid; // as we checked for the invalid |
| 107 | // something unexpected |
| 108 | return csUnknown; |
| 109 | } |
| 110 | |
| 111 | modelCacheState |
| 112 | modelCacheIan :: isMergeableSingleton ( BipolarPointer bp ) const |
| 113 | { |
| 114 | fpp_assert ( isValid(bp) ); |
| 115 | auto pos = isPositive(bp); |
| 116 | auto Singleton = getValue(bp); |
| 117 | |
| 118 | // deterministic clash |
| 119 | if ( getDConcepts(!pos).contains(Singleton) ) |
| 120 | return csInvalid; |
| 121 | // non-det clash |
| 122 | else if ( getNConcepts(!pos).contains(Singleton) ) |
| 123 | return csFailed; |
| 124 | |
| 125 | return csValid; |
| 126 | } |
| 127 | |
| 128 | modelCacheState modelCacheIan :: isMergeableIan ( const modelCacheIan* cache ) const |
| 129 | { |
| 130 | if ( posDConcepts.intersects(cache->negDConcepts) |
| 131 | || cache->posDConcepts.intersects(negDConcepts) |
| 132 | # ifdef RKG_USE_SIMPLE_RULES |
| 133 | || getExtra(/*det=*/true).intersects(cache->getExtra(/*det=*/true)) |
| 134 | # endif |
| 135 | ) |
| 136 | return csInvalid; |
| 137 | else if ( posDConcepts.intersects(cache->negNConcepts) |
| 138 | || posNConcepts.intersects(cache->negDConcepts) |
| 139 | || posNConcepts.intersects(cache->negNConcepts) |
| 140 | || cache->posDConcepts.intersects(negNConcepts) |
| 141 | || cache->posNConcepts.intersects(negDConcepts) |
| 142 | || cache->posNConcepts.intersects(negNConcepts) |
| 143 | # ifdef RKG_USE_SIMPLE_RULES |
| 144 | || getExtra(/*det=*/true).intersects(cache->getExtra(/*det=*/false)) |
| 145 | || getExtra(/*det=*/false).intersects(cache->getExtra(/*det=*/true)) |
| 146 | || getExtra(/*det=*/false).intersects(cache->getExtra(/*det=*/false)) |
| 147 | # endif |
| 148 | || existsRoles.intersects(cache->forallRoles) |
| 149 | || cache->existsRoles.intersects(forallRoles) |
| 150 | || funcRoles.intersects(cache->funcRoles) ) |
| 151 | return csFailed; |
| 152 | else // could be merged |
| 153 | return csValid; |
| 154 | } |
| 155 | |
| 156 | modelCacheState modelCacheIan :: merge ( const modelCacheInterface* cache ) |
| 157 | { |
| 158 | fpp_assert ( cache != nullptr ); |
| 159 | |
| 160 | // check for nominal clash |
| 161 | if ( hasNominalClash(cache) ) |
| 162 | { |
| 163 | curState = csFailed; |
| 164 | return getState(); |
| 165 | } |
| 166 | |
| 167 | if ( auto cacheIan = dynamic_cast<const modelCacheIan*>(cache) ) |
| 168 | mergeIan(cacheIan); |
| 169 | else if ( auto cacheSingleton = dynamic_cast<const modelCacheSingleton*>(cache) ) |
| 170 | mergeSingleton(cacheSingleton->getValue()); |
| 171 | else if ( dynamic_cast<const modelCacheConst*>(cache) ) |
| 172 | curState = mergeStatus ( getState(), cache->getState() ); // adds TOP/BOTTOM |
| 173 | else |
| 174 | fpp_unreachable(); |
| 175 | |
| 176 | updateNominalStatus(cache); |
| 177 | return getState(); |
| 178 | } |
| 179 | |
| 180 | /// actual merge with a singleton cache |
| 181 | void |
| 182 | modelCacheIan :: mergeSingleton ( BipolarPointer bp ) |
| 183 | { |
| 184 | modelCacheState newState = isMergeableSingleton(bp); |
| 185 | |
| 186 | if ( newState != csValid ) // some clash occurred: adjust state |
| 187 | curState = mergeStatus ( getState(), newState ); |
| 188 | else // add singleton; no need to change state here |
| 189 | getDConcepts(isPositive(bp)).insert(getValue(bp)); |
| 190 | } |
| 191 | |
| 192 | /// actual merge with an Ian's cache |
| 193 | void |
| 194 | modelCacheIan :: mergeIan ( const modelCacheIan* cache ) |
| 195 | { |
| 196 | // setup curState |
| 197 | curState = isMergeableIan(cache); |
| 198 | |
| 199 | // merge all sets: |
| 200 | posDConcepts |= cache->posDConcepts; |
| 201 | posNConcepts |= cache->posNConcepts; |
| 202 | negDConcepts |= cache->negDConcepts; |
| 203 | negNConcepts |= cache->negNConcepts; |
| 204 | #ifdef RKG_USE_SIMPLE_RULES |
| 205 | extraDConcepts |= cache->extraDConcepts; |
| 206 | extraNConcepts |= cache->extraNConcepts; |
| 207 | #endif |
| 208 | existsRoles |= cache->existsRoles; |
| 209 | forallRoles |= cache->forallRoles; |
| 210 | funcRoles |= cache->funcRoles; |
| 211 | } |
| 212 | |
| 213 | // logging |
| 214 | void |
| 215 | modelCacheIan :: logCacheEntryImpl ( void ) const |
| 216 | { |
| 217 | LL << "\nIan cache: posDConcepts = "; |
| 218 | posDConcepts.print(LL); |
| 219 | LL << ", posNConcepts = "; |
| 220 | posNConcepts.print(LL); |
| 221 | LL << ", negDConcepts = "; |
| 222 | negDConcepts.print(LL); |
| 223 | LL << ", negNConcepts = "; |
| 224 | negNConcepts.print(LL); |
| 225 | #ifdef RKG_USE_SIMPLE_RULES |
| 226 | LL << ", extraDConcepts = "; |
| 227 | extraDConcepts.print(LL); |
| 228 | LL << ", extraNConcepts = "; |
| 229 | extraNConcepts.print(LL); |
| 230 | #endif |
| 231 | LL << ", existsRoles = "; |
| 232 | existsRoles.print(LL); |
| 233 | LL << ", forallRoles = "; |
| 234 | forallRoles.print(LL); |
| 235 | LL << ", funcRoles = "; |
| 236 | funcRoles.print(LL); |
| 237 | } |