| 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 "dlDag.h" |
| 21 | #include "dlCompletionGraph.h" |
| 22 | #include "Reasoner.h" |
| 23 | #include "logging.h" |
| 24 | |
| 25 | #ifdef USE_BLOCKING_STATISTICS |
| 26 | // statistic for calling blocking |
| 27 | unsigned long tries[6], fails[6], nSucc, failedRule; |
| 28 | |
| 29 | void printBlockingStat1 ( std::ostream& o ) |
| 30 | { |
| 31 | if ( *tries == 0 ) // nothing to inform |
| 32 | return; |
| 33 | |
| 34 | // else -- print precise statistics |
| 35 | o << "\nThere were made " << *tries << " blocking tests of which " |
| 36 | << nSucc << " successful.\nBlocking rules failure statistic:"; |
| 37 | for ( int i = 0; i < 6; ++i ) |
| 38 | { |
| 39 | if ( i != 0 ) |
| 40 | o << ","; |
| 41 | o << " " << fails[i] << "/" << tries[i]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void clearBlockingStat1 ( void ) |
| 46 | { |
| 47 | for ( int i = 5; i >= 0; --i ) |
| 48 | tries[i] = fails[i] = 0; |
| 49 | nSucc = failedRule = 0; |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | // universal Blocked-By method |
| 54 | bool |
| 55 | DlCompletionGraph :: isBlockedBy ( const DlCompletionTree* node, const DlCompletionTree* blocker ) const |
| 56 | { |
| 57 | fpp_assert ( !node->isNominalNode() ); |
| 58 | fpp_assert ( !blocker->isNominalNode() ); |
| 59 | |
| 60 | // blocked node can't be blocked itself |
| 61 | if ( blocker->isBlocked() ) |
| 62 | return false; |
| 63 | |
| 64 | // easy check: Init is not in the label if a blocker |
| 65 | if ( !blocker->canBlockInit(node) ) |
| 66 | return false; |
| 67 | |
| 68 | bool ret; |
| 69 | if ( sessionHasInverseRoles ) // subset blocking |
| 70 | { |
| 71 | const DLDag& dag = pReasoner->getDAG(); |
| 72 | if ( sessionHasNumberRestrictions ) // I+F -- optimised blocking |
| 73 | ret = node->isBlockedBy_SHIQ ( dag, blocker ); |
| 74 | else // just I -- equality blocking |
| 75 | ret = node->isBlockedBy_SHI ( dag, blocker ); |
| 76 | } |
| 77 | else |
| 78 | ret = node->isBlockedBy_SH(blocker); |
| 79 | |
| 80 | #ifdef USE_BLOCKING_STATISTICS |
| 81 | if ( ret ) |
| 82 | ++nSucc; |
| 83 | else |
| 84 | if ( LLM.isWritable(llGTA) ) |
| 85 | LL << " fb" << failedRule << "(" << node->getId() << "," << blocker->getId() << ")"; |
| 86 | #endif |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | // Blocked-by implementation |
| 92 | |
| 93 | bool DlCompletionTree :: isCommonlyBlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const |
| 94 | { |
| 95 | // common B1: |
| 96 | if ( !B1(p) ) |
| 97 | return false; |
| 98 | |
| 99 | for ( const_label_iterator q = p->beginl_cc(), q_end = p->endl_cc(); q < q_end; ++q ) |
| 100 | { |
| 101 | BipolarPointer bp = q->bp(); |
| 102 | const DLVertex& v = dag[bp]; |
| 103 | |
| 104 | if ( v.Type() == dtForall && isPositive(bp) ) |
| 105 | { // (all S C) \in L(w') |
| 106 | if ( !B2 ( v, bp ) ) |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool DlCompletionTree :: isABlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const |
| 115 | { |
| 116 | // current = w; p = w'; parent = v |
| 117 | #ifdef ENABLE_CHECKING |
| 118 | fpp_assert ( hasParent() ); // there exists v |
| 119 | #endif |
| 120 | |
| 121 | // B3,B4 |
| 122 | for ( const_label_iterator q = p->beginl_cc(), q_end = p->endl_cc(); q < q_end; ++q ) |
| 123 | { |
| 124 | BipolarPointer bp = q->bp(); |
| 125 | const DLVertex& v = dag[bp]; |
| 126 | |
| 127 | if ( v.Type() == dtForall && isNegative(bp) ) |
| 128 | { // (some T E) \in L(w') |
| 129 | if ( !B4 ( p, 1, v.getRole(), inverse(v.getC()) ) ) |
| 130 | return false; |
| 131 | } |
| 132 | else if ( v.Type() == dtLE ) |
| 133 | { |
| 134 | if ( isPositive(bp) ) |
| 135 | { // (<= n S C) \in L(w') |
| 136 | if ( !B3 ( p, v.getNumberLE(), v.getRole(), v.getC() ) ) |
| 137 | return false; |
| 138 | } |
| 139 | else |
| 140 | { // (>= m T E) \in L(w') |
| 141 | if ( !B4 ( p, v.getNumberGE(), v.getRole(), v.getC() ) ) |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // all other is OK -- done; |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool DlCompletionTree :: isCBlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const |
| 152 | { |
| 153 | // current = w; p = w'; parent = v |
| 154 | #ifdef ENABLE_CHECKING |
| 155 | fpp_assert ( hasParent() ); // there exists v |
| 156 | #endif |
| 157 | |
| 158 | // B5 |
| 159 | const_label_iterator q, q_end; |
| 160 | for ( q = p->beginl_cc(), q_end = p->endl_cc(); q < q_end; ++q ) |
| 161 | { |
| 162 | BipolarPointer bp = q->bp(); |
| 163 | const DLVertex& v = dag[bp]; |
| 164 | |
| 165 | if ( v.Type() == dtLE && isPositive(bp) ) |
| 166 | { // (<= n T E) \in L(w') |
| 167 | if ( !B5 ( v.getRole(), v.getC() ) ) |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // B6 |
| 173 | const DlCompletionTree* par = getParentNode(); |
| 174 | for ( q = par->beginl_cc(), q_end = par->endl_cc(); q < q_end; ++q ) |
| 175 | { |
| 176 | BipolarPointer bp = q->bp(); |
| 177 | const DLVertex& v = dag[bp]; |
| 178 | |
| 179 | if ( v.Type() == dtLE && isNegative(bp) ) |
| 180 | { // (>= m U F) \in L(v) |
| 181 | if ( !B6 ( v.getRole(), v.getC() ) ) |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | //---------------------------------------------------------------------- |
| 190 | //-- B1 to B6 conditions implementation |
| 191 | //-- WARNING!! 19-06-2005. All blockable nodes has the only parent |
| 192 | //-- (with probably several links to it). So we should check all of them |
| 193 | //---------------------------------------------------------------------- |
| 194 | |
| 195 | #ifdef USE_BLOCKING_STATISTICS |
| 196 | # define TRY_B(i) ++tries[i-1] |
| 197 | # define FAIL_B(i) failedRule = i, ++fails[i-1] |
| 198 | #else |
| 199 | # define TRY_B(i) (void)NULL |
| 200 | # define FAIL_B(i) (void)NULL |
| 201 | #endif |
| 202 | |
| 203 | /// check if B1 holds for a given vertex (p is a candidate for blocker) |
| 204 | bool DlCompletionTree :: B1 ( const DlCompletionTree* p ) const |
| 205 | { |
| 206 | TRY_B(1); |
| 207 | |
| 208 | if ( Label <= p->Label ) |
| 209 | return true; |
| 210 | |
| 211 | FAIL_B(1); |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | bool |
| 216 | DlCompletionTree :: B2Simple ( const RAStateTransitions& RST, BipolarPointer C ) const |
| 217 | { |
| 218 | const DlCompletionTree* parent = getParentNode(); |
| 219 | const CGLabel& parLab = parent->label(); |
| 220 | TRY_B(2); |
| 221 | |
| 222 | for ( const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 223 | if ( !(*p)->isIBlocked() && (*p)->getArcEnd() == parent && RST.recognise((*p)->getRole()) ) |
| 224 | { |
| 225 | if ( !parLab.contains(C) ) |
| 226 | { |
| 227 | FAIL_B(2); |
| 228 | return false; |
| 229 | } |
| 230 | else |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool |
| 238 | DlCompletionTree :: B2Complex ( const RAStateTransitions& RST, BipolarPointer C ) const |
| 239 | { |
| 240 | const DlCompletionTree* parent = getParentNode(); |
| 241 | const CGLabel& parLab = parent->label(); |
| 242 | TRY_B(2); |
| 243 | |
| 244 | for ( const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 245 | { |
| 246 | if ( (*p)->isIBlocked() || (*p)->getArcEnd() != parent ) |
| 247 | continue; |
| 248 | const TRole* R = (*p)->getRole(); |
| 249 | if ( !RST.recognise(R) ) |
| 250 | continue; |
| 251 | |
| 252 | for ( const auto& trans: RST ) |
| 253 | if ( trans.applicable(R) ) |
| 254 | { |
| 255 | BipolarPointer bpFinal = C + (BipolarPointer) trans.final(); |
| 256 | if ( !parLab.containsCC(bpFinal) ) |
| 257 | { |
| 258 | FAIL_B(2); |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | /// check if B3 holds for (<= n S.C)\in w' (p is a candidate for blocker) |
| 268 | bool DlCompletionTree :: B3 ( const DlCompletionTree* p, unsigned int n, const TRole* S, BipolarPointer C ) const |
| 269 | { |
| 270 | #ifdef ENABLE_CHECKING |
| 271 | fpp_assert ( hasParent() ); // safety check |
| 272 | #endif |
| 273 | TRY_B(3); |
| 274 | |
| 275 | bool ret; |
| 276 | // if (<= n S C) \in L(w') then |
| 277 | // a) w is an inv(S)-succ of v or |
| 278 | if ( !isParentArcLabelled(S) ) |
| 279 | ret = true; |
| 280 | // b) w is an inv(S) succ of v and ~C\in L(v) or |
| 281 | else if ( getParentNode()->isLabelledBy(inverse(C)) ) |
| 282 | ret = true; |
| 283 | // c) w is an inv(S) succ of v and C\in L(v)... |
| 284 | else if ( !getParentNode()->isLabelledBy(C) ) |
| 285 | ret = false; |
| 286 | else |
| 287 | { // ...and <=n-1 S-succ. z with C\in L(z) |
| 288 | unsigned int m = 0; |
| 289 | for ( const_edge_iterator q = p->begin(), q_end = p->end(); q < q_end; ++q ) |
| 290 | if ( (*q)->isSuccEdge() && (*q)->isNeighbour(S) && (*q)->getArcEnd()->isLabelledBy(C) ) |
| 291 | ++m; |
| 292 | |
| 293 | ret = ( m < n ); |
| 294 | } |
| 295 | |
| 296 | if ( !ret ) |
| 297 | FAIL_B(3); |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | /// check if B4 holds for (>= m T.E)\in w' (p is a candidate for blocker) |
| 303 | bool DlCompletionTree :: B4 ( const DlCompletionTree* p, unsigned int m, const TRole* T, BipolarPointer E ) const |
| 304 | { |
| 305 | #ifdef ENABLE_CHECKING |
| 306 | fpp_assert ( hasParent() ); // safety check |
| 307 | #endif |
| 308 | TRY_B(4); |
| 309 | |
| 310 | // if (>= m T E) \in L(w') then |
| 311 | // b) w is an inv(T) succ of v and E\in L(v) and m == 1 or |
| 312 | if ( isParentArcLabelled(T) && m == 1 && getParentNode()->isLabelledBy(E) ) |
| 313 | return true; |
| 314 | |
| 315 | // a) w' has at least m T-succ z with E\in L(z) |
| 316 | // check all sons |
| 317 | unsigned int n = 0; |
| 318 | for ( const_edge_iterator q = p->begin(), q_end = p->end(); q < q_end; ++q ) |
| 319 | if ( (*q)->isSuccEdge() && (*q)->isNeighbour(T) && (*q)->getArcEnd()->isLabelledBy(E) ) |
| 320 | if ( ++n >= m ) // check if node has enough successors |
| 321 | return true; |
| 322 | |
| 323 | // rule check fails |
| 324 | FAIL_B(4); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /// check if B5 holds for (<= n T.E)\in w' |
| 329 | bool DlCompletionTree :: B5 ( const TRole* T, BipolarPointer E ) const |
| 330 | { |
| 331 | #ifdef ENABLE_CHECKING |
| 332 | fpp_assert ( hasParent() ); // safety check |
| 333 | #endif |
| 334 | TRY_B(5); |
| 335 | |
| 336 | // if (<= n T E) \in L(w'), then |
| 337 | // either w is not an inv(T)-successor of v... |
| 338 | if ( !isParentArcLabelled(T) ) |
| 339 | return true; |
| 340 | // or ~E \in L(v) |
| 341 | if ( getParentNode()->isLabelledBy ( inverse(E) ) ) |
| 342 | return true; |
| 343 | |
| 344 | FAIL_B(5); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | /// check if B6 holds for (>= m U.F)\in v |
| 349 | bool DlCompletionTree :: B6 ( const TRole* U, BipolarPointer F ) const |
| 350 | { |
| 351 | #ifdef ENABLE_CHECKING |
| 352 | fpp_assert ( hasParent() ); // safety check |
| 353 | #endif |
| 354 | TRY_B(6); |
| 355 | |
| 356 | // if (>= m U F) \in L(v), and |
| 357 | // w is U-successor of v... |
| 358 | if ( !isParentArcLabelled(U->inverse()) ) |
| 359 | return true; |
| 360 | // then ~F\in L(w) |
| 361 | if ( isLabelledBy ( inverse(F) ) ) |
| 362 | return true; |
| 363 | |
| 364 | FAIL_B(6); |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | #undef TRY_B |
| 369 | #undef FAIL_B |
| 370 | |
| 371 | //---------------------------------------------------------------------- |
| 372 | //-- changing blocked status |
| 373 | //---------------------------------------------------------------------- |
| 374 | |
| 375 | void DlCompletionGraph :: detectBlockedStatus ( DlCompletionTree* node ) |
| 376 | { |
| 377 | DlCompletionTree* p = node; |
| 378 | bool wasBlocked = node->isBlocked(); |
| 379 | bool wasDBlocked = node->isDBlocked(); |
| 380 | |
| 381 | // if we are here, then node *need* to be checked |
| 382 | // so this is to prevent from going out of the loop |
| 383 | node->setAffected(); |
| 384 | |
| 385 | while ( p->hasParent() && p->isBlockableNode() && p->isAffected() ) |
| 386 | { |
| 387 | findDBlocker(p); |
| 388 | if ( p->isBlocked() ) |
| 389 | return; |
| 390 | p = p->getParentNode(); |
| 391 | } |
| 392 | p->clearAffected(); |
| 393 | if ( wasBlocked && !node->isBlocked() ) |
| 394 | unblockNode ( node, wasDBlocked ); |
| 395 | } |
| 396 | |
| 397 | void DlCompletionGraph :: unblockNode ( DlCompletionTree* node, bool wasDBlocked ) |
| 398 | { |
| 399 | if ( node->isPBlocked() || !node->isBlockableNode() ) |
| 400 | return; |
| 401 | if ( !wasDBlocked ) // if it was DBlocked -- findDBlocker() made it |
| 402 | saveRareCond(node->setUBlocked()); |
| 403 | pReasoner->repeatUnblockedNode(node,wasDBlocked); |
| 404 | unblockNodeChildren(node); |
| 405 | } |
| 406 | |
| 407 | void DlCompletionGraph :: findDAncestorBlocker ( DlCompletionTree* node ) |
| 408 | { |
| 409 | const DlCompletionTree* p = node; |
| 410 | |
| 411 | if ( RKG_USE_FAIRNESS && nSkipBeforeBlock ) |
| 412 | for ( int n = nSkipBeforeBlock-1; n >= 0 && p->hasParent() && p->isBlockableNode(); --n ) |
| 413 | p = p->getParentNode(); |
| 414 | |
| 415 | while ( p->hasParent() ) |
| 416 | { |
| 417 | p = p->getParentNode(); |
| 418 | |
| 419 | if ( !p->isBlockableNode() ) |
| 420 | return; |
| 421 | |
| 422 | if ( isBlockedBy ( node, p ) ) |
| 423 | { |
| 424 | setNodeDBlocked ( node, p ); |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void DlCompletionGraph :: findDAnywhereBlocker ( DlCompletionTree* node ) |
| 431 | { |
| 432 | for ( const_iterator q = begin(), q_end = end(); q < q_end && *q != node; ++q ) |
| 433 | { |
| 434 | const DlCompletionTree* p = *q; |
| 435 | |
| 436 | // node was merge to the one with the larger ID or is cached or blocked itself |
| 437 | if ( p->isBlocked() || p->isPBlocked() || p->isNominalNode() || p->isCached() ) |
| 438 | continue; |
| 439 | |
| 440 | if ( isBlockedBy ( node, p ) ) |
| 441 | { |
| 442 | setNodeDBlocked ( node, p ); |
| 443 | return; |
| 444 | } |
| 445 | } |
| 446 | } |