| 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 | #ifndef DLCOMPLETIONTREE_H |
| 21 | #define DLCOMPLETIONTREE_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "globaldef.h" |
| 26 | #include "dlCompletionTreeArc.h" |
| 27 | #include "tSaveList.h" |
| 28 | #include "tRestorer.h" |
| 29 | #include "CGLabel.h" |
| 30 | #include "logging.h" |
| 31 | |
| 32 | class DLDag; |
| 33 | class DlCompletionGraph; |
| 34 | |
| 35 | // use the following to control logging information about saving/restoring nodes |
| 36 | #define RKG_CHECK_BACKJUMPING |
| 37 | |
| 38 | #if USE_LOGGING // don't gather statistics w/o logging |
| 39 | # define USE_BLOCKING_STATISTICS |
| 40 | #endif |
| 41 | |
| 42 | #ifdef USE_BLOCKING_STATISTICS |
| 43 | extern void printBlockingStat1 ( std::ostream& o ); |
| 44 | extern void clearBlockingStat1 ( void ); |
| 45 | # define printBlockingStat(O) printBlockingStat1(O) |
| 46 | # define clearBlockingStat() clearBlockingStat1() |
| 47 | #else |
| 48 | # define printBlockingStat(O) (void)NULL |
| 49 | # define clearBlockingStat() (void)NULL |
| 50 | #endif |
| 51 | |
| 52 | /// level of CTree's nominal node |
| 53 | typedef unsigned short CTNominalLevel; |
| 54 | /// default level for the Blockable node |
| 55 | const CTNominalLevel BlockableLevel = static_cast<CTNominalLevel>(-1); |
| 56 | |
| 57 | class DlCompletionTree |
| 58 | { |
| 59 | friend class DlCompletionGraph; |
| 60 | |
| 61 | protected: // internal classes |
| 62 | /// class for saving Completion Tree nodes state |
| 63 | class SaveState |
| 64 | { |
| 65 | public: // members |
| 66 | /// saving status of the label |
| 67 | CGLabel::SaveState lab; |
| 68 | /// curLevel of the Node structure |
| 69 | unsigned int curLevel = 0; |
| 70 | /// amount of neighbours |
| 71 | size_t nNeighbours = 0; |
| 72 | |
| 73 | public: // interface |
| 74 | /// get level of a saved node |
| 75 | unsigned int level ( void ) const { return curLevel; } |
| 76 | }; // SaveState |
| 77 | |
| 78 | /// restore blocked node |
| 79 | class UnBlock: public TRestorer |
| 80 | { |
| 81 | protected: |
| 82 | DlCompletionTree* p; |
| 83 | const DlCompletionTree* Blocker; |
| 84 | DepSet dep; |
| 85 | bool pBlocked, dBlocked; |
| 86 | public: |
| 87 | explicit UnBlock ( DlCompletionTree* q ) : p(q), Blocker(q->Blocker), dep(q->pDep), pBlocked(q->pBlocked), dBlocked(q->dBlocked) {} |
| 88 | void restore ( void ) override { p->Blocker = Blocker; p->pDep = dep; p->pBlocked = pBlocked; p->dBlocked = dBlocked; } |
| 89 | }; // UnBlock |
| 90 | |
| 91 | /// restore (un)cached node |
| 92 | class CacheRestorer: public TRestorer |
| 93 | { |
| 94 | protected: |
| 95 | DlCompletionTree* p; |
| 96 | bool cached; |
| 97 | public: |
| 98 | explicit CacheRestorer ( DlCompletionTree* q ) : p(q), cached(q->cached) {} |
| 99 | void restore ( void ) override { p->cached = cached; } |
| 100 | }; // CacheRestorer |
| 101 | |
| 102 | #ifdef RKG_IR_IN_NODE_LABEL |
| 103 | /// restore node after IR set change |
| 104 | class IRRestorer: public TRestorer |
| 105 | { |
| 106 | protected: |
| 107 | DlCompletionTree* p; |
| 108 | size_t n; |
| 109 | public: |
| 110 | explicit IRRestorer ( DlCompletionTree* q ) : p(q), n(q->IR.size()) {} |
| 111 | void restore ( void ) override { p->IR.resize(n); } |
| 112 | }; // IRRestorer |
| 113 | #endif |
| 114 | |
| 115 | public: // type interface |
| 116 | /// type for set of arcs |
| 117 | typedef std::vector<DlCompletionTreeArc*> ArcCollection; |
| 118 | |
| 119 | /// iterator on edges |
| 120 | typedef ArcCollection::iterator edge_iterator; |
| 121 | /// const iterator on edges |
| 122 | typedef ArcCollection::const_iterator const_edge_iterator; |
| 123 | |
| 124 | /// RO iterator on label |
| 125 | typedef CGLabel::const_iterator const_label_iterator; |
| 126 | |
| 127 | #ifdef RKG_IR_IN_NODE_LABEL |
| 128 | /// type for inequality relation information |
| 129 | typedef std::vector<ConceptWDep> IRInfo; |
| 130 | #endif |
| 131 | |
| 132 | protected: // members |
| 133 | /// label of a node |
| 134 | CGLabel Label; |
| 135 | #ifdef RKG_IR_IN_NODE_LABEL |
| 136 | /// inequality relation information respecting current node |
| 137 | IRInfo IR; |
| 138 | #endif |
| 139 | /// Neighbours information |
| 140 | ArcCollection Neighbour; |
| 141 | /// pointer to last saved node |
| 142 | TSaveList<SaveState> saves; |
| 143 | /// ID of node (used in print) |
| 144 | unsigned int id = 0; |
| 145 | /// concept that init the newly created node |
| 146 | BipolarPointer Init = bpINVALID; |
| 147 | |
| 148 | /// blocker of a node |
| 149 | const DlCompletionTree* Blocker = nullptr; |
| 150 | /// dep-set for Purge op |
| 151 | DepSet pDep; |
| 152 | |
| 153 | // save state information |
| 154 | unsigned int curLevel = 0; // current level |
| 155 | /// level of a nominal node; 0 means blockable one |
| 156 | CTNominalLevel nominalLevel; |
| 157 | |
| 158 | /// is given node a data node |
| 159 | bool flagDataNode : 1; |
| 160 | /// flag if node is Cached |
| 161 | bool cached : 1; |
| 162 | /// flag whether node is permanently/temporarily blocked |
| 163 | bool pBlocked : 1; |
| 164 | /// flag whether node is directly/indirectly blocked |
| 165 | bool dBlocked : 1; |
| 166 | /** Whether node is affected by change of some potential blocker. |
| 167 | This flag may be viewed as a cache for a 'blocked' status |
| 168 | */ |
| 169 | bool affected : 1; |
| 170 | |
| 171 | protected: // methods |
| 172 | |
| 173 | //---------------------------------------------- |
| 174 | // blocking support methods |
| 175 | //---------------------------------------------- |
| 176 | |
| 177 | // sub-methods for optimal blocking |
| 178 | |
| 179 | /// check B1 and B2 from optimal blocking for given blocker candidate |
| 180 | bool isCommonlyBlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const; |
| 181 | /// check B3 and B4 from optimal blocking for given blocker candidate |
| 182 | bool isABlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const; |
| 183 | /// check B5 and B6 from optimal blocking for given blocker candidate |
| 184 | bool isCBlockedBy ( const DLDag& dag, const DlCompletionTree* p ) const; |
| 185 | |
| 186 | // checking the blocking conditions for optimized blocking |
| 187 | |
| 188 | /// check if B1 holds for a given vertex (p is a candidate for blocker) |
| 189 | bool B1 ( const DlCompletionTree* p ) const; |
| 190 | /// check if B2 holds for (AS C) with transitions RST from A[0] using a simple automaton A for S |
| 191 | bool B2Simple ( const RAStateTransitions& RST, BipolarPointer C ) const; |
| 192 | /// check if B2 holds for C=(AS{n} X) with transitions RST from A[n] using a complex automaton A for S |
| 193 | bool B2Complex ( const RAStateTransitions& RST, BipolarPointer C ) const; |
| 194 | /// check if B2 holds for given DL vertex with C=V |
| 195 | bool B2 ( const DLVertex& v, BipolarPointer C ) const |
| 196 | { |
| 197 | # ifdef ENABLE_CHECKING |
| 198 | fpp_assert ( hasParent() ); // safety |
| 199 | # endif |
| 200 | const RAStateTransitions& RST = v.getRole()->getAutomaton()[v.getState()]; |
| 201 | if ( v.getRole()->isSimple() ) |
| 202 | return B2Simple ( RST, v.getC() ); |
| 203 | else |
| 204 | { |
| 205 | if ( RST.empty() ) // no possible applications |
| 206 | return true; |
| 207 | // pointer to current forall |
| 208 | BipolarPointer bp = C - (BipolarPointer)v.getState(); |
| 209 | if ( RST.isSingleton() ) |
| 210 | return B2Simple ( RST, bp + (BipolarPointer)RST.getTransitionEnd() ); |
| 211 | return B2Complex ( RST, bp ); |
| 212 | } |
| 213 | } |
| 214 | /// check if B3 holds for (<= n S.C)\in w' (p is a candidate for blocker) |
| 215 | bool B3 ( const DlCompletionTree* p, unsigned int n, const TRole* S, BipolarPointer C ) const; |
| 216 | /// check if B4 holds for (>= m T.E)\in w' (p is a candidate for blocker) |
| 217 | bool B4 ( const DlCompletionTree* p, unsigned int m, const TRole* T, BipolarPointer E ) const; |
| 218 | /// check if B5 holds for (<= n T.E)\in w' |
| 219 | bool B5 ( const TRole* T, BipolarPointer E ) const; |
| 220 | /// check if B6 holds for (>= m U.F)\in v |
| 221 | bool B6 ( const TRole* U, BipolarPointer F ) const; |
| 222 | |
| 223 | //---------------------------------------------- |
| 224 | // re-building blocking hierarchy |
| 225 | //---------------------------------------------- |
| 226 | |
| 227 | /// check whether a node can block another one with init concept C |
| 228 | bool canBlockInit ( BipolarPointer C ) const { return C == bpTOP || label().contains(C); } |
| 229 | /// check if all parent arcs are blocked |
| 230 | bool isParentArcIBlocked ( void ) const |
| 231 | { |
| 232 | for ( const DlCompletionTreeArc* edge : *this ) |
| 233 | if ( edge->isPredEdge() && !edge->isIBlocked() ) |
| 234 | return false; |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | //---------------------------------------------- |
| 239 | // Transitive SOME support interface |
| 240 | //---------------------------------------------- |
| 241 | |
| 242 | /// check if SOME rule is applicable for transitive R |
| 243 | const DlCompletionTree* isTSomeApplicable ( const TRole* R, BipolarPointer C ) const; |
| 244 | /// check if SOME rule is applicable for non-transitive R |
| 245 | const DlCompletionTree* isNSomeApplicable ( const TRole* R, BipolarPointer C ) const; |
| 246 | /// check if transitive R-successor labelled with C |
| 247 | const DlCompletionTree* isTSuccLabelled ( const TRole* R, BipolarPointer C ) const; |
| 248 | /// check if transitive R-predecessor labelled with C; skip FROM node |
| 249 | const DlCompletionTree* isTPredLabelled ( const TRole* R, BipolarPointer C, const DlCompletionTree* from ) const; |
| 250 | |
| 251 | //---------------------------------------------- |
| 252 | // inequality relation methods |
| 253 | //---------------------------------------------- |
| 254 | |
| 255 | /// check if the current node is in IR wrt C; if so, write the clash-set to DEP |
| 256 | bool inIRwithC ( const ConceptWDep& C, DepSet& dep ) const; |
| 257 | |
| 258 | //---------------------------------------------- |
| 259 | // saving/restoring |
| 260 | //---------------------------------------------- |
| 261 | |
| 262 | /// get current save-level |
| 263 | unsigned int getCurLevel ( void ) const { return curLevel; } |
| 264 | /// save current state to given SS |
| 265 | void save ( SaveState* nss ) const; |
| 266 | /// restore state from given SS; delete SS after |
| 267 | void restore ( SaveState* nss ); |
| 268 | |
| 269 | //---------------------------------------------- |
| 270 | // logging/output |
| 271 | //---------------------------------------------- |
| 272 | |
| 273 | /// log saving/restoring node |
| 274 | void logSRNode ( const char* action ATTR_UNUSED ) const |
| 275 | { |
| 276 | # if defined(RKG_CHECK_BACKJUMPING) |
| 277 | if ( LLM.isWritable(llSRInfo) ) |
| 278 | LL << " " << action << "(" << id << "[" << Neighbour.size() << "]," << getCurLevel() << ")"; |
| 279 | # undef RKG_CHECK_BACKJUMPING // it is the only user |
| 280 | # endif |
| 281 | } |
| 282 | /// get letter corresponding to the blocking mode |
| 283 | const char* getBlockingStatusName ( void ) const { return isPBlocked() ? "p" : isDBlocked() ? "d" : isIBlocked() ? "i" : "u"; } |
| 284 | /// log node status (d-,i-,p-blocked or cached |
| 285 | void logNodeBStatus ( std::ostream& o ) const |
| 286 | { |
| 287 | // blocking status information |
| 288 | if ( Blocker ) |
| 289 | o << getBlockingStatusName() << Blocker->getId(); |
| 290 | if ( isCached() ) |
| 291 | o << "c"; |
| 292 | } |
| 293 | /// log if node became p-blocked |
| 294 | void logNodeBlocked ( void ) const |
| 295 | { |
| 296 | if ( LLM.isWritable(llGTA) ) |
| 297 | { |
| 298 | LL << " " << getBlockingStatusName() << "b(" << id; |
| 299 | if ( Blocker ) |
| 300 | LL << "," << Blocker->id; |
| 301 | LL << ")"; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | public: // methods |
| 306 | /// init newly created node with starting LEVEL |
| 307 | void init ( unsigned int level ); |
| 308 | /// c'tor: create an empty node |
| 309 | explicit DlCompletionTree ( unsigned int newId ) : id(newId) {} |
| 310 | /// no copy c'tor |
| 311 | DlCompletionTree ( const DlCompletionTree& ) = delete; |
| 312 | /// no assignment |
| 313 | DlCompletionTree& operator = ( const DlCompletionTree& ) = delete; |
| 314 | /// d'tor: delete node |
| 315 | ~DlCompletionTree() { saves.clear(); } |
| 316 | |
| 317 | /// add given arc P as a neighbour |
| 318 | void addNeighbour ( DlCompletionTreeArc* p ) { Neighbour.push_back(p); } |
| 319 | |
| 320 | /// get Node's id |
| 321 | unsigned int getId ( void ) const { return id; } |
| 322 | /// check if the node is cached (IE need not to be expanded) |
| 323 | bool isCached ( void ) const { return cached; } |
| 324 | /// set cached status of given node |
| 325 | TRestorer* setCached ( bool val ) |
| 326 | { |
| 327 | if ( cached == val ) |
| 328 | return nullptr; |
| 329 | TRestorer* ret = new CacheRestorer(this); |
| 330 | cached = val; |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | // data node methods |
| 335 | |
| 336 | bool isDataNode ( void ) const { return flagDataNode; } |
| 337 | void setDataNode ( void ) { flagDataNode = true; } |
| 338 | |
| 339 | // nominal node methods |
| 340 | |
| 341 | bool isBlockableNode ( void ) const { return nominalLevel == BlockableLevel; } |
| 342 | bool isNominalNode ( void ) const { return nominalLevel != BlockableLevel; } |
| 343 | bool isNominalNode ( CTNominalLevel level ) const { return nominalLevel == level; } |
| 344 | void setNominalLevel ( void ) { nominalLevel = 0; } |
| 345 | void setNominalLevel ( CTNominalLevel newLevel ) { nominalLevel = newLevel; } |
| 346 | CTNominalLevel getNominalLevel ( void ) const { return nominalLevel; } |
| 347 | |
| 348 | /// compare 2 CT nodes wrt their nominal level/status |
| 349 | bool operator < ( const DlCompletionTree& node ) const |
| 350 | { |
| 351 | return ( getNominalLevel() < node.getNominalLevel() ) || |
| 352 | ( ( getNominalLevel() == node.getNominalLevel() ) && |
| 353 | ( getId() < node.getId() ) ); |
| 354 | } |
| 355 | |
| 356 | /// adds concept P to a label, defined by TAG; update blocked status if necessary |
| 357 | void addConcept ( const ConceptWDep& p, bool isComplex ) { Label.getLabel(isComplex).add(p); } |
| 358 | /// set the Init concept |
| 359 | void setInit ( BipolarPointer p ) { Init = p; } |
| 360 | BipolarPointer getInit() { return Init; } |
| 361 | |
| 362 | //---------------------------------------------- |
| 363 | // children/parent access interface |
| 364 | //---------------------------------------------- |
| 365 | |
| 366 | // neighbour iterators |
| 367 | const_edge_iterator begin ( void ) const { return Neighbour.begin(); } |
| 368 | const_edge_iterator end ( void ) const { return Neighbour.end(); } |
| 369 | edge_iterator begin ( void ) { return Neighbour.begin(); } |
| 370 | edge_iterator end ( void ) { return Neighbour.end(); } |
| 371 | |
| 372 | /// return true if node is a non-root; works for reflexive roles |
| 373 | bool hasParent ( void ) const |
| 374 | { |
| 375 | if ( Neighbour.empty() ) |
| 376 | return false; |
| 377 | return (*begin())->isPredEdge(); |
| 378 | } |
| 379 | |
| 380 | //---------------------------------------------- |
| 381 | // Transitive SOME support interface |
| 382 | //---------------------------------------------- |
| 383 | |
| 384 | /// check if SOME rule is applicable; includes transitive SOME support |
| 385 | const DlCompletionTree* isSomeApplicable ( const TRole* R, BipolarPointer C ) const |
| 386 | { return R->isTransitive() ? isTSomeApplicable(R,C) : isNSomeApplicable(R,C); } |
| 387 | |
| 388 | //---------------------------------------------- |
| 389 | // Label access interface |
| 390 | //---------------------------------------------- |
| 391 | |
| 392 | /// RO access to a label |
| 393 | const CGLabel& label ( void ) const { return Label; } |
| 394 | /// RW access to a label |
| 395 | CGLabel& label ( void ) { return Label; } |
| 396 | |
| 397 | // label iterators |
| 398 | |
| 399 | /// begin() iterator for a label with simple concepts |
| 400 | const_label_iterator beginl_sc ( void ) const { return Label.begin_sc(); } |
| 401 | /// end() iterator for a label with simple concepts |
| 402 | const_label_iterator endl_sc ( void ) const { return Label.end_sc(); } |
| 403 | /// begin() iterator for a label with complex concepts |
| 404 | const_label_iterator beginl_cc ( void ) const { return Label.begin_cc(); } |
| 405 | /// end() iterator for a label with complex concepts |
| 406 | const_label_iterator endl_cc ( void ) const { return Label.end_cc(); } |
| 407 | |
| 408 | /// check whether node's label contains P |
| 409 | bool isLabelledBy ( BipolarPointer p ) const { return Label.contains(p); } |
| 410 | |
| 411 | //---------------------------------------------- |
| 412 | // blocking interface |
| 413 | //---------------------------------------------- |
| 414 | |
| 415 | // Blocked-By methods for different logics |
| 416 | |
| 417 | /// check blocking condition for SH logic |
| 418 | bool isBlockedBy_SH ( const DlCompletionTree* p ) const { return B1(p); } |
| 419 | /// check blocking condition for SHI logic |
| 420 | bool isBlockedBy_SHI ( const DLDag& dag, const DlCompletionTree* p ) const { return isCommonlyBlockedBy ( dag, p ); } |
| 421 | /// check blocking condition for SHIQ logic using optimised blocking |
| 422 | bool isBlockedBy_SHIQ ( const DLDag& dag, const DlCompletionTree* p ) const |
| 423 | { return isCommonlyBlockedBy ( dag, p ) && ( isCBlockedBy ( dag, p ) || isABlockedBy ( dag, p ) ); } |
| 424 | |
| 425 | // WARNING!! works only for blockable nodes |
| 426 | // every non-root node will have first upcoming edge pointed to a parent |
| 427 | |
| 428 | /// return RO pointer to the parent node; WARNING: correct only for nodes with hasParent()==TRUE |
| 429 | const DlCompletionTree* getParentNode ( void ) const { return (*begin())->getArcEnd(); } |
| 430 | /// return RW pointer to the parent node; WARNING: correct only for nodes with hasParent()==TRUE |
| 431 | DlCompletionTree* getParentNode ( void ) { return (*begin())->getArcEnd(); } |
| 432 | |
| 433 | //---------------------------------------------- |
| 434 | // managing AFFECTED flag |
| 435 | //---------------------------------------------- |
| 436 | |
| 437 | /// check whether node is affected by blocking-related changes |
| 438 | bool isAffected ( void ) const { return affected; } |
| 439 | /// set node (and all subnodes) affected |
| 440 | void setAffected ( void ) |
| 441 | { |
| 442 | // don't mark already affected, nominal or p-blocked nodes |
| 443 | if ( isAffected() || isNominalNode() || isPBlocked() ) |
| 444 | return; |
| 445 | affected = true; |
| 446 | for ( const_edge_iterator q = begin(), q_end = end(); q < q_end; ++q ) |
| 447 | if ( (*q)->isSuccEdge() ) |
| 448 | (*q)->getArcEnd()->setAffected(); |
| 449 | } |
| 450 | /// clear affected flag |
| 451 | void clearAffected ( void ) { affected = false; } |
| 452 | |
| 453 | // just returns calculated values |
| 454 | |
| 455 | /// check if node is directly blocked |
| 456 | bool isDBlocked ( void ) const { return Blocker != nullptr && !pBlocked && dBlocked; } |
| 457 | /// check if node is indirectly blocked |
| 458 | bool isIBlocked ( void ) const { return Blocker != nullptr && !pBlocked && !dBlocked; } |
| 459 | /// check if node is purged (and so indirectly blocked) |
| 460 | bool isPBlocked ( void ) const { return Blocker != nullptr && pBlocked && !dBlocked; } |
| 461 | /// check if node is blocked (d/i) |
| 462 | bool isBlocked ( void ) const { return Blocker != nullptr && !pBlocked; } |
| 463 | /// check the legality of the direct block |
| 464 | bool isIllegallyDBlocked ( void ) const { return isDBlocked() && Blocker->isBlocked(); } |
| 465 | |
| 466 | /// get access to the blocker |
| 467 | const DlCompletionTree* getBlocker ( void ) const { return Blocker; } |
| 468 | /// get RW node to which current one was merged |
| 469 | DlCompletionTree* resolvePBlocker ( void ) |
| 470 | { |
| 471 | if ( unlikely(isPBlocked()) ) |
| 472 | return const_cast<DlCompletionTree*>(Blocker)->resolvePBlocker(); |
| 473 | else |
| 474 | return this; |
| 475 | } |
| 476 | /// get RO node to which current one was merged |
| 477 | const DlCompletionTree* resolvePBlocker ( void ) const |
| 478 | { |
| 479 | if ( unlikely(isPBlocked()) ) |
| 480 | return Blocker->resolvePBlocker(); |
| 481 | else |
| 482 | return this; |
| 483 | } |
| 484 | /// get node to which current one was merged; fills DEP from pDep's |
| 485 | DlCompletionTree* resolvePBlocker ( DepSet& dep ) |
| 486 | { |
| 487 | if ( likely(!isPBlocked()) ) |
| 488 | return this; |
| 489 | dep += pDep; |
| 490 | return const_cast<DlCompletionTree*>(Blocker)->resolvePBlocker(dep); |
| 491 | } |
| 492 | /// get purge dep-set of a given node |
| 493 | const DepSet& getPurgeDep ( void ) const { return pDep; } |
| 494 | /// check whether a node can block node P according to it's Init value |
| 495 | bool canBlockInit ( const DlCompletionTree* p ) const { return canBlockInit(p->Init); } |
| 496 | |
| 497 | /// check whether the loop between a DBlocked NODE and it's parent blocked contains C |
| 498 | bool isLoopLabelled ( BipolarPointer c ) |
| 499 | { |
| 500 | fpp_assert ( isDBlocked() ); |
| 501 | if ( Blocker->isLabelledBy(c) ) |
| 502 | return true; |
| 503 | int n = 1; // Blocker is the 1st node in the loop |
| 504 | for ( DlCompletionTree* p = getParentNode(); p->hasParent() && p != Blocker; p = p->getParentNode() ) |
| 505 | if ( p->isLabelledBy(c) ) |
| 506 | return true; |
| 507 | else |
| 508 | ++n; |
| 509 | if ( LLM.isWritable(llGTA) ) |
| 510 | LL << " loop(" << n << ")"; |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | //---------------------------------------------- |
| 515 | // re-building blocking hierarchy |
| 516 | //---------------------------------------------- |
| 517 | |
| 518 | /// set node blocked |
| 519 | TRestorer* setBlocked ( const DlCompletionTree* blocker, bool permanently, bool directly ) |
| 520 | { |
| 521 | TRestorer* ret = new UnBlock(this); |
| 522 | Blocker = blocker; |
| 523 | pBlocked = permanently; |
| 524 | dBlocked = directly; |
| 525 | logNodeBlocked(); |
| 526 | return ret; |
| 527 | } |
| 528 | /// mark node d-blocked |
| 529 | TRestorer* setDBlocked ( const DlCompletionTree* blocker ) { return setBlocked ( blocker, false, true ); } |
| 530 | /// mark node i-blocked |
| 531 | TRestorer* setIBlocked ( const DlCompletionTree* blocker ) { return setBlocked ( blocker, false, false ); } |
| 532 | /// mark node unblocked |
| 533 | TRestorer* setUBlocked ( void ) { return setBlocked ( nullptr, true, true ); } |
| 534 | /// mark node purged |
| 535 | TRestorer* setPBlocked ( const DlCompletionTree* blocker, const DepSet& dep ) |
| 536 | { |
| 537 | TRestorer* ret = new UnBlock(this); |
| 538 | Blocker = blocker; |
| 539 | if ( isNominalNode() ) |
| 540 | pDep = dep; |
| 541 | pBlocked = true; |
| 542 | dBlocked = false; |
| 543 | logNodeBlocked(); |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | //---------------------------------------------- |
| 548 | // checking edge labeling |
| 549 | //---------------------------------------------- |
| 550 | |
| 551 | /// check if edge to NODE is labeled by R; return NULL if does not |
| 552 | DlCompletionTreeArc* getEdgeLabelled ( const TRole* R, const DlCompletionTree* node ) const |
| 553 | { |
| 554 | for ( const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 555 | if ( (*p)->getArcEnd() == node && (*p)->isNeighbour(R) ) |
| 556 | return *p; |
| 557 | return nullptr; |
| 558 | } |
| 559 | /// check if parent arc is labeled by R; works only for blockable nodes |
| 560 | bool isParentArcLabelled ( const TRole* R ) const { return getEdgeLabelled ( R, getParentNode() ) != nullptr; } |
| 561 | |
| 562 | //---------------------------------------------- |
| 563 | // inequality relation interface |
| 564 | //---------------------------------------------- |
| 565 | |
| 566 | #ifdef RKG_IR_IN_NODE_LABEL |
| 567 | /// init IR with given entry and dep-set; @return true if IR already has this label |
| 568 | bool initIR ( BipolarPointer level, const DepSet& ds ) |
| 569 | { |
| 570 | ConceptWDep C(level,ds); |
| 571 | DepSet dummy; // we don't need a clash-set here |
| 572 | if ( inIRwithC ( C, dummy ) ) |
| 573 | return true; |
| 574 | IR.push_back(std::move(C)); |
| 575 | return false; |
| 576 | } |
| 577 | /// check if the current node is in IR with NODE; if so, write the clash-set to DEP |
| 578 | bool nonMergeable ( const DlCompletionTree* node, DepSet& dep ) const; |
| 579 | /// update IR of the current node with IR from NODE and additional dep-set; @return restorer |
| 580 | TRestorer* updateIR ( const DlCompletionTree* node, const DepSet& toAdd ); |
| 581 | #endif |
| 582 | |
| 583 | //---------------------------------------------- |
| 584 | // saving/restoring |
| 585 | //---------------------------------------------- |
| 586 | |
| 587 | /// check if node needs to be saved |
| 588 | bool needSave ( unsigned int newLevel ) const { return getCurLevel() < newLevel; } |
| 589 | /// save node using internal stack |
| 590 | void save ( unsigned int level ) |
| 591 | { |
| 592 | save(saves.push()); |
| 593 | curLevel = level; |
| 594 | } |
| 595 | /// restore node from the topmost entry |
| 596 | void restore ( void ) |
| 597 | { |
| 598 | fpp_assert ( !saves.empty() ); |
| 599 | restore (saves.pop()); |
| 600 | } |
| 601 | /// check if node needs to be restored |
| 602 | bool needRestore ( unsigned int restLevel ) const { return getCurLevel() > restLevel; } |
| 603 | /// restore node to given level |
| 604 | void restore ( unsigned int level ) { restore(saves.pop(level)); } |
| 605 | |
| 606 | // output |
| 607 | |
| 608 | /// log node information (number, i/d blockers, cached) |
| 609 | void logNode ( void ) const { LL << getId(); logNodeBStatus(LL); } |
| 610 | /// print body of the node (without neighbours) |
| 611 | void PrintBody ( std::ostream& o ) const; |
| 612 | }; // DlCompletionTree |
| 613 | |
| 614 | /* |
| 615 | * Implementation |
| 616 | */ |
| 617 | |
| 618 | inline void DlCompletionTree :: init ( unsigned int level ) |
| 619 | { |
| 620 | flagDataNode = false; |
| 621 | nominalLevel = BlockableLevel; |
| 622 | curLevel = level; |
| 623 | cached = false; |
| 624 | affected = true; // every (newly created) node can be blocked |
| 625 | dBlocked = true; |
| 626 | pBlocked = true; // unused flag combination |
| 627 | |
| 628 | Label.init(); |
| 629 | Init = bpTOP; |
| 630 | |
| 631 | // node was used -- clear all previous content |
| 632 | saves.clear(); |
| 633 | #ifdef RKG_IR_IN_NODE_LABEL |
| 634 | IR.clear(); |
| 635 | #endif |
| 636 | Neighbour.clear(); |
| 637 | Blocker = nullptr; |
| 638 | pDep.clear(); |
| 639 | } |
| 640 | |
| 641 | #endif // DLCOMPLETIONTREE_H |