| 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 REASONER_H |
| 21 | #define REASONER_H |
| 22 | |
| 23 | #include "globaldef.h" |
| 24 | #include "tBranchingContext.h" |
| 25 | #include "dlCompletionGraph.h" |
| 26 | #include "dlTBox.h" |
| 27 | #include "dlDag.h" |
| 28 | #include "modelCacheIan.h" |
| 29 | #include "tSaveStack.h" |
| 30 | #include "procTimer.h" |
| 31 | #include "DataReasoning.h" |
| 32 | #include "ToDoList.h" |
| 33 | #include "tFastSet.h" |
| 34 | |
| 35 | #if USE_LOGGING // don't gather statistics w/o logging |
| 36 | # define USE_REASONING_STATISTICS |
| 37 | #endif |
| 38 | |
| 39 | #ifdef USE_REASONING_STATISTICS |
| 40 | /// class for gathering statistic both for session and totally |
| 41 | class AccumulatedStatistic |
| 42 | { |
| 43 | protected: // static members |
| 44 | /// all members are linked together |
| 45 | static AccumulatedStatistic* root; |
| 46 | |
| 47 | public: // static methods |
| 48 | /// accumulate all registered statistic elements |
| 49 | static void accumulateAll ( void ) |
| 50 | { |
| 51 | for ( AccumulatedStatistic* cur = root; cur; cur = cur->next ) |
| 52 | cur->accumulate(); |
| 53 | } |
| 54 | |
| 55 | protected: // members |
| 56 | /// accumulated statistic |
| 57 | unsigned int total = 0; |
| 58 | /// current session statistic |
| 59 | unsigned int local = 0; |
| 60 | /// link to the next element |
| 61 | AccumulatedStatistic* next; |
| 62 | |
| 63 | public: // interface |
| 64 | /// c'tor: link itself to the list |
| 65 | AccumulatedStatistic ( void ) : next(root) { root = this; } |
| 66 | /// no copy c'tor |
| 67 | AccumulatedStatistic ( const AccumulatedStatistic& ) = delete; |
| 68 | /// no assignment |
| 69 | AccumulatedStatistic& operator = ( const AccumulatedStatistic& ) = delete; |
| 70 | /// d'tor: remove link from the list |
| 71 | ~AccumulatedStatistic() |
| 72 | { |
| 73 | // all statistic elements are deleted in the inverted creation order |
| 74 | if ( root == this ) |
| 75 | { |
| 76 | root = next; |
| 77 | return; |
| 78 | } |
| 79 | // we are here if it's multi-environment and arbitrary deletion order was chosen |
| 80 | AccumulatedStatistic *prev = root, *cur = root->next; |
| 81 | // find a pointer to current node |
| 82 | while ( cur && cur != this ) |
| 83 | prev = cur, cur = cur->next; |
| 84 | if ( cur == this ) |
| 85 | prev->next = next; |
| 86 | } |
| 87 | |
| 88 | // access to the elements |
| 89 | |
| 90 | /// get (RO) value of the element |
| 91 | unsigned int get ( bool needLocal ) const { return needLocal ? local : total; } |
| 92 | |
| 93 | // general statistic operators |
| 94 | |
| 95 | /// increment local value |
| 96 | void inc ( void ) |
| 97 | { |
| 98 | ++local; |
| 99 | } |
| 100 | /// set local value to particular N |
| 101 | void set ( unsigned int n ) |
| 102 | { |
| 103 | local = n; |
| 104 | } |
| 105 | /// add local value to a global one |
| 106 | void accumulate ( void ) { total += local; local = 0; } |
| 107 | |
| 108 | // output |
| 109 | |
| 110 | void Print ( std::ostream& o, bool needLocal, const char* prefix, const char* suffix ) const |
| 111 | { |
| 112 | if ( get(needLocal) > 0 ) |
| 113 | o << prefix << get(needLocal) << suffix; |
| 114 | } |
| 115 | }; // AccumulatedStatistic |
| 116 | #endif |
| 117 | |
| 118 | class DlSatTester |
| 119 | { |
| 120 | protected: // type definition |
| 121 | /// TODO table type |
| 122 | typedef ToDoList ToDoTableType; |
| 123 | /// vector of edges |
| 124 | typedef std::vector<DlCompletionTreeArc*> EdgeVector; |
| 125 | /// vector of nodes |
| 126 | typedef std::vector<DlCompletionTree*> NodeVector; |
| 127 | /// RO label iterator |
| 128 | typedef DlCompletionTree::const_label_iterator const_label_iterator; |
| 129 | /// set to keep BPs (during cascaded cache creation) |
| 130 | typedef std::set<BipolarPointer> BPSet; |
| 131 | |
| 132 | protected: // constants |
| 133 | /// possible flags of re-checking ALL-like expressions in new nodes |
| 134 | const unsigned int redoForall = 1; |
| 135 | const unsigned int redoFunc = 2; |
| 136 | const unsigned int redoAtMost = 4; |
| 137 | const unsigned int redoIrr = 8; |
| 138 | // Do NOT forget to update when new constant is added |
| 139 | const unsigned int redoEverything = redoForall | redoFunc | redoAtMost | redoIrr; |
| 140 | |
| 141 | protected: // classes |
| 142 | /// stack to keep BContext |
| 143 | class BCStack: public TSaveStack<BranchingContext> |
| 144 | { |
| 145 | protected: // members |
| 146 | /// pool for OR contexts |
| 147 | DeletelessAllocator<BCOr> PoolOr; |
| 148 | /// pool for NN contexts |
| 149 | DeletelessAllocator<BCNN> PoolNN; |
| 150 | /// pool for LE contexts |
| 151 | DeletelessAllocator<BCLE<DlCompletionTreeArc> > PoolLE; |
| 152 | /// pool for Top-LE contexts |
| 153 | DeletelessAllocator<BCLE<DlCompletionTree> > PoolTopLE; |
| 154 | /// pool for Choose contexts |
| 155 | DeletelessAllocator<BCChoose> PoolCh; |
| 156 | /// single entry for the barrier (good for nominal reasoner) |
| 157 | BCBarrier* bcBarrier; |
| 158 | |
| 159 | protected: // methods |
| 160 | /// specialise new method as the one doing nothing |
| 161 | BranchingContext* createNew ( void ) override { return nullptr; } |
| 162 | /// push method to use |
| 163 | BranchingContext* push ( BranchingContext* p ) |
| 164 | { |
| 165 | p->init(); |
| 166 | TSaveStack<BranchingContext>::push(); |
| 167 | this->Base[this->last-1] = p; |
| 168 | return p; |
| 169 | } |
| 170 | |
| 171 | public: // interface |
| 172 | /// empty c'tor |
| 173 | BCStack ( void ) : bcBarrier(new BCBarrier) {} |
| 174 | /// empty d'tor |
| 175 | ~BCStack() override |
| 176 | { |
| 177 | // all the members will be deleted in the resp. pools |
| 178 | for ( iterator p = this->Base.begin(), p_end = this->Base.end(); p < p_end; ++p ) |
| 179 | *p = nullptr; |
| 180 | delete bcBarrier; |
| 181 | } |
| 182 | |
| 183 | // push methods |
| 184 | |
| 185 | /// get BC for Or-rule |
| 186 | BranchingContext* pushOr ( void ) { return push(PoolOr.get()); } |
| 187 | /// get BC for NN-rule |
| 188 | BranchingContext* pushNN ( void ) { return push(PoolNN.get()); } |
| 189 | /// get BC for LE-rule |
| 190 | BranchingContext* pushLE ( void ) { return push(PoolLE.get()); } |
| 191 | /// get BC for TopLE-rule |
| 192 | BranchingContext* pushTopLE ( void ) { return push(PoolTopLE.get()); } |
| 193 | /// get BC for Choose-rule |
| 194 | BranchingContext* pushCh ( void ) { return push(PoolCh.get()); } |
| 195 | /// get BC for the barrier |
| 196 | BranchingContext* pushBarrier ( void ) { return push(bcBarrier); } |
| 197 | |
| 198 | /// clear all the pools |
| 199 | void clearPools ( void ) |
| 200 | { |
| 201 | PoolOr.clear(); |
| 202 | PoolNN.clear(); |
| 203 | PoolLE.clear(); |
| 204 | PoolTopLE.clear(); |
| 205 | PoolCh.clear(); |
| 206 | } |
| 207 | /// clear the stack and pools |
| 208 | void clear ( void ) override |
| 209 | { |
| 210 | clearPools(); |
| 211 | TSaveStack<BranchingContext>::clear(); |
| 212 | } |
| 213 | }; // BCStack |
| 214 | |
| 215 | protected: // members |
| 216 | /// host TBox |
| 217 | TBox& tBox; |
| 218 | /// link to dag from TBox |
| 219 | DLDag& DLHeap; |
| 220 | /// all the reflexive roles |
| 221 | RoleMaster::TRoleVec ReflexiveRoles; |
| 222 | |
| 223 | /// manager for all the dep-sets corresponding to a graph here |
| 224 | TDepSetManager Manager; |
| 225 | /// Completion Graph of tested concept(s) |
| 226 | DlCompletionGraph CGraph; |
| 227 | /// TODO list |
| 228 | ToDoTableType TODO; |
| 229 | /// reasoning subsystem for the datatypes |
| 230 | DataTypeReasoner DTReasoner; |
| 231 | /// Used sets for pos- and neg- entries |
| 232 | TFastSet<unsigned int> pUsed, nUsed; |
| 233 | /// cache for testing whether it's possible to non-expand newly created node |
| 234 | modelCacheIan newNodeCache; |
| 235 | /// auxiliary cache that is built from the edges of newly created node |
| 236 | modelCacheIan newNodeEdges; |
| 237 | |
| 238 | /// GCI-related KB flags |
| 239 | const TKBFlags& GCIs; |
| 240 | |
| 241 | /// record nodes that were processed during Cascaded Cache construction |
| 242 | std::set<BipolarPointer> inProcess; |
| 243 | |
| 244 | /// timer for the SAT tests (ie, cache creation) |
| 245 | TsProcTimer satTimer; |
| 246 | /// timer for the SUB tests (ie, general subsumption) |
| 247 | TsProcTimer subTimer; |
| 248 | /// timer for a single test; use it as a timeout checker |
| 249 | TsProcTimer testTimer; |
| 250 | |
| 251 | // save/restore option |
| 252 | |
| 253 | /// stack for the local reasoner's state |
| 254 | BCStack Stack; |
| 255 | /// context from the restored branching rule |
| 256 | BranchingContext* bContext = nullptr; |
| 257 | /// index of last non-det situation |
| 258 | unsigned int tryLevel = InitBranchingLevelValue; |
| 259 | /// shift in order to determine the 1st non-det application |
| 260 | unsigned int nonDetShift = 0; |
| 261 | |
| 262 | // statistic elements |
| 263 | |
| 264 | #ifdef USE_REASONING_STATISTICS |
| 265 | AccumulatedStatistic |
| 266 | nTacticCalls, |
| 267 | nUseless, |
| 268 | |
| 269 | nIdCalls, |
| 270 | nSingletonCalls, |
| 271 | nOrCalls, |
| 272 | nOrBrCalls, |
| 273 | nAndCalls, |
| 274 | nSomeCalls, |
| 275 | nAllCalls, |
| 276 | nFuncCalls, |
| 277 | nLeCalls, |
| 278 | nGeCalls, |
| 279 | |
| 280 | nNNCalls, |
| 281 | nMergeCalls, |
| 282 | |
| 283 | nAutoEmptyLookups, |
| 284 | nAutoTransLookups, |
| 285 | |
| 286 | nSRuleAdd, |
| 287 | nSRuleFire, |
| 288 | |
| 289 | nStateSaves, |
| 290 | nStateRestores, |
| 291 | nNodeSaves, |
| 292 | nNodeRestores, |
| 293 | |
| 294 | nLookups, |
| 295 | |
| 296 | nFairnessViolations, |
| 297 | |
| 298 | // reasoning cache |
| 299 | nCacheTry, |
| 300 | nCacheFailedNoCache, |
| 301 | nCacheFailedShallow, |
| 302 | nCacheFailed, |
| 303 | nCachedSat, |
| 304 | nCachedUnsat; |
| 305 | #endif |
| 306 | |
| 307 | // current values |
| 308 | |
| 309 | /// currently processed CTree node |
| 310 | DlCompletionTree* curNode = nullptr; |
| 311 | /// currently processed Concept |
| 312 | ConceptWDep curConcept; |
| 313 | /// GCIs local to session |
| 314 | std::vector<BipolarPointer> SessionGCIs; |
| 315 | |
| 316 | /// size of the DAG with some extra space |
| 317 | size_t dagSize = 0; |
| 318 | |
| 319 | /// temporary array used in OR operation |
| 320 | BCOr::OrIndex OrConceptsToTest; |
| 321 | /// temporary array used in <= operations |
| 322 | EdgeVector EdgesToMerge; |
| 323 | /// nodes to merge in the TopRole-LE rules |
| 324 | NodeVector NodesToMerge; |
| 325 | /// contains clash set if clash is encountered in a node label |
| 326 | DepSet clashSet; |
| 327 | |
| 328 | // session status flags: |
| 329 | |
| 330 | /// true if nominal-related expansion rule was fired during reasoning |
| 331 | bool encounterNominal = false; |
| 332 | /// flag to show if it is necessary to produce DT reasoning immediately |
| 333 | bool checkDataNode = true; |
| 334 | |
| 335 | protected: // methods |
| 336 | |
| 337 | /// increment statistic counter |
| 338 | #ifdef USE_REASONING_STATISTICS |
| 339 | # define incStat(stat) stat.inc() |
| 340 | #else |
| 341 | # define incStat(stat) |
| 342 | #endif |
| 343 | |
| 344 | //----------------------------------------------------------------------------- |
| 345 | // flags section |
| 346 | //----------------------------------------------------------------------------- |
| 347 | |
| 348 | /// @return timeout value for a single SAT/Sub test in milliseconds; 0 means no timeout |
| 349 | unsigned long getSatTimeout ( void ) const { return tBox.testTimeout; } |
| 350 | /// @return true iff semantic branching is used |
| 351 | bool useSemanticBranching ( void ) const { return tBox.useSemanticBranching; } |
| 352 | /// @return true iff lazy blocking is used |
| 353 | bool useLazyBlocking ( void ) const { return tBox.useLazyBlocking; } |
| 354 | |
| 355 | /// reset all session flags |
| 356 | void resetSessionFlags ( void ); |
| 357 | |
| 358 | /// adds C to the labels of NODE; @return true iff clash occurs |
| 359 | bool addToDoEntry ( DlCompletionTree* node, const ConceptWDep& C, const char* reason = nullptr ); |
| 360 | /// synonym to the above |
| 361 | bool addToDoEntry ( DlCompletionTree* node, BipolarPointer c, const DepSet& dep, const char* reason = nullptr ) |
| 362 | { return addToDoEntry ( node, ConceptWDep(c,dep), reason ); } |
| 363 | /// insert C to the label of NODE; do necessary updates; may return Clash in case of data entry C |
| 364 | bool insertToDoEntry ( DlCompletionTree* node, const ConceptWDep& C, DagTag tag, const char* reason ); |
| 365 | /// if something was added to cached node N, un- or re-cache it; @return result of re-cache |
| 366 | bool correctCachedEntry ( DlCompletionTree* n ); |
| 367 | |
| 368 | // CGraph-wide rules support |
| 369 | |
| 370 | /// @return true if node is valid for the reasoning |
| 371 | bool isNodeGloballyUsed ( const DlCompletionTree* node ) const |
| 372 | { return ! ( node->isDataNode() || node->isIBlocked() || node->isPBlocked() ); } |
| 373 | /// @return true if node is valid for the reasoning |
| 374 | bool isObjectNodeUnblocked ( const DlCompletionTree* node ) const |
| 375 | { return isNodeGloballyUsed(node) && !node->isDBlocked(); } |
| 376 | /// add C to a set of session GCIs; init all nodes with (C,dep) |
| 377 | bool addSessionGCI ( BipolarPointer C, const DepSet& dep ); |
| 378 | |
| 379 | |
| 380 | /// put TODO entry for either BP or inverse(BP) in NODE's label |
| 381 | void updateName ( DlCompletionTree* node, BipolarPointer bp ) |
| 382 | { |
| 383 | const CGLabel& lab = node->label(); |
| 384 | CGLabel::const_iterator p; |
| 385 | for ( p = lab.begin_sc(); p != lab.end_sc(); ++p ) |
| 386 | if ( unlikely ( p->bp() == bp || p->bp() == inverse(bp) ) ) |
| 387 | { |
| 388 | addExistingToDoEntry ( node, lab.getSCOffset(p), "sp" ); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | /// re-do every BP or inverse(BP) in labels of CGraph |
| 393 | void updateName ( BipolarPointer bp ) |
| 394 | { |
| 395 | for ( DlCompletionTree* node : CGraph ) |
| 396 | if ( isNodeGloballyUsed(node) ) |
| 397 | updateName ( node, bp ); |
| 398 | } |
| 399 | |
| 400 | // label access interface |
| 401 | |
| 402 | /// try to add a concept to a label given by TAG; ~C can't appear in the label |
| 403 | bool findConcept ( const CWDArray& lab, BipolarPointer bp ); |
| 404 | /// try to add a concept to a label given by TAG; ~C can't appear in the label |
| 405 | bool findConcept ( const CWDArray& lab, const ConceptWDep& C ) { return findConcept ( lab, C.bp() ); } |
| 406 | /// try to add a concept to a label given by TAG; ~C can't appear in the label; setup clash-set if found |
| 407 | bool findConceptClash ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 408 | /// try to add a concept to a label given by TAG; ~C can't appear in the label; setup clash-set if found |
| 409 | bool findConceptClash ( const CWDArray& lab, const ConceptWDep& C ) { return findConceptClash ( lab, C.bp(), C.getDep() ); } |
| 410 | /// check if it is possible to add a concept to a label given by TAG |
| 411 | addConceptResult checkAddedConcept ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 412 | /// check if C or ~C is already in LAB |
| 413 | addConceptResult tryAddConcept ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 414 | |
| 415 | /** Adds ToDo entry which already exists in label of NODE. There is no need |
| 416 | to add entry to label, but it is necessary to provide offset of existing concept. |
| 417 | This is done by providing OFFSET of the concept in NODE's label |
| 418 | */ |
| 419 | void addExistingToDoEntry ( DlCompletionTree* node, int offset, const char* reason = nullptr ) |
| 420 | { |
| 421 | const ConceptWDep& C = node->label().getConcept(offset); |
| 422 | auto bp = C.bp(); |
| 423 | TODO.addEntry ( node, DLHeap[bp].Type(), bp, offset ); |
| 424 | if ( LLM.isWritable(llGTA) ) |
| 425 | logEntry ( node, C, reason ); |
| 426 | } |
| 427 | /// add all elements from NODE label into TODO list |
| 428 | void redoNodeLabel ( DlCompletionTree* node, const char* reason ) |
| 429 | { |
| 430 | const CGLabel& lab = node->label(); |
| 431 | CGLabel::const_iterator p { }; |
| 432 | for ( p = lab.begin_sc(); p != lab.end_sc(); ++p ) |
| 433 | addExistingToDoEntry ( node, lab.getSCOffset(p), reason ); |
| 434 | for ( p = lab.begin_cc(); p != lab.end_cc(); ++p ) |
| 435 | addExistingToDoEntry ( node, lab.getCCOffset(p), reason ); |
| 436 | } |
| 437 | |
| 438 | /// main reasoning function |
| 439 | bool checkSatisfiability ( void ); |
| 440 | /// perform all the actions that should be done once, after all normal rules are not applicable. @return true if the concept is unsat |
| 441 | bool performAfterReasoning ( void ); |
| 442 | /// make sure that the DAG does not grow larger than that was recorded |
| 443 | void ensureDAGSize ( void ) |
| 444 | { |
| 445 | if ( unlikely ( dagSize < DLHeap.size() ) ) |
| 446 | { |
| 447 | dagSize = DLHeap.maxSize(); |
| 448 | pUsed.ensureMaxSetSize(dagSize); |
| 449 | nUsed.ensureMaxSetSize(dagSize); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | //----------------------------------------------------------------------------- |
| 454 | //-- internal cache support |
| 455 | //----------------------------------------------------------------------------- |
| 456 | |
| 457 | /// build cache suitable for classification |
| 458 | void prepareCascadedCache ( BipolarPointer p ); |
| 459 | /// create cache for given DAG node bu building model; @return cache |
| 460 | modelCacheInterface* buildCache ( BipolarPointer p ); |
| 461 | /// return cache of given completion tree (implementation) |
| 462 | modelCacheInterface* createModelCache ( const DlCompletionTree* p ) const |
| 463 | { return new modelCacheIan ( DLHeap, p, encounterNominal, tBox.nC, tBox.nR ); } |
| 464 | |
| 465 | /// check whether given NODE can be cached |
| 466 | bool canBeCached ( DlCompletionTree* node ); |
| 467 | /// build cache of the node (it is known that caching is possible) in newNodeCache |
| 468 | void doCacheNode ( DlCompletionTree* node ); |
| 469 | /// mark NODE (un)cached depending on the joint cache STATUS; @return resulting status |
| 470 | modelCacheState reportNodeCached ( DlCompletionTree* node ); |
| 471 | /// check whether node may be (un)cached; save node if something is changed |
| 472 | modelCacheState tryCacheNode ( DlCompletionTree* node ) |
| 473 | { |
| 474 | modelCacheState ret = canBeCached(node) ? reportNodeCached(node) : csFailed; |
| 475 | // node is cached if RET is csValid |
| 476 | CGraph.saveRareCond(node->setCached(ret == csValid)); |
| 477 | return ret; |
| 478 | } |
| 479 | /// @return true iff cache status is invalid |
| 480 | static bool usageByState ( modelCacheState status ) |
| 481 | { |
| 482 | return status == csInvalid; |
| 483 | } |
| 484 | |
| 485 | //----------------------------------------------------------------------------- |
| 486 | //-- internal nominal reasoning interface |
| 487 | //----------------------------------------------------------------------------- |
| 488 | |
| 489 | /// check whether reasoning with nominals is performed |
| 490 | virtual bool hasNominals ( void ) const { return false; } |
| 491 | |
| 492 | /// check satisfiability of task which is set-up |
| 493 | bool runSat ( void ); |
| 494 | |
| 495 | /* |
| 496 | * Tactics description; |
| 497 | * |
| 498 | * Each tactic represents (possibly combined) expansion rule for |
| 499 | * certain type of concept expression (parameter CUR). |
| 500 | * |
| 501 | * Each tactic returns: |
| 502 | * - true - if expansion of CUR lead to clash |
| 503 | * - false - otherwise |
| 504 | */ |
| 505 | |
| 506 | /// main calling method; wrapper for Body |
| 507 | bool commonTactic ( void ); |
| 508 | /// choose proper tactic based on type of a concept constructor |
| 509 | bool commonTacticBody ( const DLVertex& cur ); |
| 510 | /// expansion rule for (non)primitive concept |
| 511 | bool commonTacticBodyId ( const DLVertex& cur ); |
| 512 | /// expansion rule for (non)primitive singleton concept |
| 513 | bool commonTacticBodySingleton ( const DLVertex& cur ); |
| 514 | /// expansion rule for conjunction |
| 515 | bool commonTacticBodyAnd ( const DLVertex& cur ); |
| 516 | /// expansion rule for disjunction |
| 517 | bool commonTacticBodyOr ( const DLVertex& cur ); |
| 518 | /// expansion rule for general existential quantifier |
| 519 | bool commonTacticBodySome ( const DLVertex& cur ); |
| 520 | /// expansion rule for existential quantifier in the form \ER{nom} |
| 521 | bool commonTacticBodyValue ( const TRole* R, const TIndividual* nom ); |
| 522 | /// expansion rule for the existential quantifier with universal role |
| 523 | bool commonTacticBodySomeUniv ( const DLVertex& cur ); |
| 524 | /// expansion rule for universal restriction |
| 525 | bool commonTacticBodyAll ( const DLVertex& cur ); |
| 526 | /// expansion rule for universal restriction with simple role using RA |
| 527 | bool commonTacticBodyAllSimple ( const DLVertex& cur ); |
| 528 | /// expansion rule for universal restriction with non-simple role using RA |
| 529 | bool commonTacticBodyAllComplex ( const DLVertex& cur ); |
| 530 | /// expansion rule for \E R{Self} |
| 531 | bool commonTacticBodySomeSelf ( const TRole* R ); |
| 532 | /// expansion rule for \neg\E R{Self} |
| 533 | bool commonTacticBodyIrrefl ( const TRole* R ); |
| 534 | /// expansion rule for at-least number restriction |
| 535 | bool commonTacticBodyGE ( const DLVertex& cur ); |
| 536 | /// expansion rule for at-most number restriction |
| 537 | bool commonTacticBodyLE ( const DLVertex& cur ); |
| 538 | /// expansion rule for choose-rule |
| 539 | bool commonTacticBodyChoose ( const TRole* R, BipolarPointer C ); |
| 540 | /// expansion rule for functional restriction |
| 541 | bool commonTacticBodyFunc ( const DLVertex& cur ); |
| 542 | /// expansion rule for at-most restriction in nominal node (NN-rule) |
| 543 | bool commonTacticBodyNN ( const DLVertex& cur ); |
| 544 | /// expansion rule for auxiliary projection-construction |
| 545 | bool commonTacticBodyProj ( const TRole* R, BipolarPointer C, const TRole* ProjR ); |
| 546 | |
| 547 | /// expansion rule for the functional restriction with top role |
| 548 | bool processTopRoleFunc ( const DLVertex& cur ); |
| 549 | /// expansion rule for the at-most restriction with top role |
| 550 | bool processTopRoleLE ( const DLVertex& cur ); |
| 551 | /// expansion rule for the at-least restriction with top role |
| 552 | bool processTopRoleGE ( const DLVertex& cur ); |
| 553 | |
| 554 | // support for inapplicable tactics |
| 555 | |
| 556 | /// @return true iff current node is i-blocked (ie, no expansion necessary) |
| 557 | bool isIBlocked ( void ) const { return curNode->isIBlocked(); } |
| 558 | /// @return true iff NN-rule wrt (<= R.C) is applicable to the curNode |
| 559 | bool isNNApplicable ( const TRole* r, BipolarPointer C, BipolarPointer stopper ) const; |
| 560 | /// apply rule-like actions for the concept P |
| 561 | bool applyExtraRules ( const TConcept* p ); |
| 562 | /// apply rule-like actions for the concept P if necessary |
| 563 | inline |
| 564 | bool applyExtraRulesIf ( const TConcept* p ) |
| 565 | { |
| 566 | if ( !p->hasExtraRules() ) |
| 567 | return false; |
| 568 | fpp_assert ( p->isPrimitive() ); |
| 569 | return applyExtraRules(p); |
| 570 | } |
| 571 | |
| 572 | // support for choose-rule |
| 573 | |
| 574 | /// apply choose-rule for given range of edges |
| 575 | bool applyChooseRule ( DlCompletionTree* node, BipolarPointer C ); |
| 576 | |
| 577 | // support for creating/updating edge methods |
| 578 | |
| 579 | /// check whether current node is blocked |
| 580 | bool isCurNodeBlocked ( void ); |
| 581 | /// apply all the generating rules for the (unblocked) current node |
| 582 | void applyAllGeneratingRules ( DlCompletionTree* node ); |
| 583 | /// add C and T_G with given DEP-set to a NODE; @return DONE/CLASH |
| 584 | bool initNewNode ( DlCompletionTree* node, const DepSet& dep, BipolarPointer C ); |
| 585 | /// apply reflexive roles to the (newly created) NODE with appropriate DEP; @return true for clash |
| 586 | bool applyReflexiveRoles ( DlCompletionTree* node, const DepSet& dep ); |
| 587 | /// add necessary concepts to the NODE of the new edge, labelled with R |
| 588 | bool initHeadOfNewEdge ( DlCompletionTree* node, const TRole* R, const DepSet& dep, const char* reason ); |
| 589 | |
| 590 | // support for existential-like rules |
| 591 | |
| 592 | /// @return true iff there is R-neighbour labelled with C |
| 593 | bool isSomeExists ( const TRole* R, BipolarPointer C ) const |
| 594 | { |
| 595 | if ( !isUsed(C) ) |
| 596 | return false; |
| 597 | const DlCompletionTree* where = curNode->isSomeApplicable ( R, C ); |
| 598 | if ( where != nullptr ) |
| 599 | if ( LLM.isWritable(llGTA) ) |
| 600 | LL << " E(" << R->getName() << "," << where->getId() << "," << C << ")"; |
| 601 | return where != nullptr; |
| 602 | } |
| 603 | /// aux method for setting up new edge PA |
| 604 | bool setupEdge ( DlCompletionTreeArc* pA, const DepSet& curDep, unsigned int redoFlags = 0 ); |
| 605 | /// aux method for creating new edge from curNode with given ROLE edge label and CONCEPT at the final label |
| 606 | bool createNewEdge ( const TRole* Role, BipolarPointer Concept, unsigned int redoFlags ); |
| 607 | /// create new ROLE-neighbour with LEVEL to curNode; return edge to it |
| 608 | DlCompletionTreeArc* createOneNeighbour ( const TRole* Role, const DepSet& dep, |
| 609 | CTNominalLevel level = BlockableLevel ); |
| 610 | |
| 611 | // support for re-applying expansion rules for FORALL-like concepts in node label |
| 612 | |
| 613 | /** |
| 614 | apply AR.C in and <= nR (if needed) in NODE's label where R is label of arcSample. |
| 615 | Set of applicable concepts is defined by redoFlags value. |
| 616 | */ |
| 617 | bool applyUniversalNR ( DlCompletionTree* Node, const DlCompletionTreeArc* arcSample, |
| 618 | const DepSet& dep, unsigned int redoFlags ); |
| 619 | |
| 620 | // support for branching rules |
| 621 | |
| 622 | /// check if branching rule was called for the 1st time |
| 623 | bool isFirstBranchCall ( void ) const { return bContext == nullptr; } |
| 624 | /// init branching context with given rule type |
| 625 | void initBC ( void ) |
| 626 | { |
| 627 | // save reasoning context |
| 628 | bContext->curNode = curNode; |
| 629 | bContext->curConcept = curConcept; |
| 630 | bContext->branchDep = curConcept.getDep(); |
| 631 | bContext->pUsedIndex = pUsed.size(); |
| 632 | bContext->nUsedIndex = nUsed.size(); |
| 633 | bContext->SGsize = SessionGCIs.size(); |
| 634 | } |
| 635 | /// clear branching context |
| 636 | void clearBC ( void ) { bContext = nullptr; } |
| 637 | |
| 638 | /// create BC for Or rule |
| 639 | void createBCOr ( void ) { bContext = Stack.pushOr(); initBC(); } |
| 640 | /// create BC for NN-rule |
| 641 | void createBCNN ( void ) { bContext = Stack.pushNN(); initBC(); } |
| 642 | /// create BC for LE-rule |
| 643 | void createBCLE ( void ) { bContext = Stack.pushLE(); initBC(); } |
| 644 | /// create BC for LE-rule |
| 645 | void createBCTopLE ( void ) { bContext = Stack.pushTopLE(); initBC(); } |
| 646 | /// create BC for Choose-rule |
| 647 | void createBCCh ( void ) { bContext = Stack.pushCh(); initBC(); } |
| 648 | /// create BC for the barrier |
| 649 | void createBCBarrier ( void ) { bContext = Stack.pushBarrier(); initBC(); } |
| 650 | |
| 651 | // support for disjunction |
| 652 | |
| 653 | /// Aux method for locating OR node characteristics; @return true if node is labelled by one of DJs |
| 654 | bool planOrProcessing ( const DLVertex& cur, DepSet& dep ); |
| 655 | /// aux method for disjunction processing |
| 656 | bool processOrEntry ( void ); |
| 657 | |
| 658 | // support for (qualified) number restrictions |
| 659 | |
| 660 | /// create N R-neighbours of curNode with given Nominal LEVEL labelled with C |
| 661 | bool createDifferentNeighbours ( const TRole* R, BipolarPointer C, const DepSet& dep, |
| 662 | unsigned int n, CTNominalLevel level ); |
| 663 | |
| 664 | /// check whether a node represents a functional one |
| 665 | static bool isFunctionalVertex ( const DLVertex& v ) { return ( v.Type() == dtLE && v.getNumberLE() == 1 && v.getC() == bpTOP ); } |
| 666 | /// check if ATLEAST and ATMOST entries are in clash. Both vertex MUST have dtLE type. |
| 667 | bool checkNRclash ( const DLVertex& atleast, const DLVertex& atmost ) const |
| 668 | { // >= n R.C clash with <= m S.D iff... |
| 669 | return (atmost.getC() == bpTOP || atleast.getC() == atmost.getC()) && // either D is TOP or C == D... |
| 670 | atleast.getNumberGE() > atmost.getNumberLE() && // and n is greater than m... |
| 671 | *atleast.getRole() <= *atmost.getRole(); // and R [= S |
| 672 | } |
| 673 | /// check if ATLEAST and ATMOST restrictions are in clash; setup depset from CUR |
| 674 | bool isNRClash ( const DLVertex& atleast, const DLVertex& atmost, const ConceptWDep& reason ); |
| 675 | /// quick check whether CURNODE has a clash with a given ATMOST restriction |
| 676 | bool isQuickClashLE ( const DLVertex& atmost ) |
| 677 | { |
| 678 | for ( DlCompletionTree::const_label_iterator q = curNode->beginl_cc(), q_end = curNode->endl_cc(); q < q_end; ++q ) |
| 679 | if ( isNegative(q->bp()) // need at-least restriction |
| 680 | && isNRClash ( DLHeap[*q], atmost, *q ) ) |
| 681 | return true; |
| 682 | return false; |
| 683 | } |
| 684 | /// quick check whether CURNODE has a clash with a given ATLEAST restriction |
| 685 | bool isQuickClashGE ( const DLVertex& atleast ) |
| 686 | { |
| 687 | for ( DlCompletionTree::const_label_iterator q = curNode->beginl_cc(), q_end = curNode->endl_cc(); q < q_end; ++q ) |
| 688 | if ( isPositive(q->bp()) // need at-most restriction |
| 689 | && isNRClash ( atleast, DLHeap[*q], *q ) ) |
| 690 | return true; |
| 691 | return false; |
| 692 | } |
| 693 | /// init the LE processing (fill in the EdgesToMerge); @return true if the CR is satisfied |
| 694 | bool initLEProcessing ( const DLVertex& cur ); |
| 695 | /// init the LE processing for the TopRole (fill in the NodesToMerge); @return true if the CR is satisfied |
| 696 | bool initTopLEProcessing ( const DLVertex& cur ); |
| 697 | |
| 698 | /// aux method that fills the dep-set for either C or ~C found in the label; @return whether C was found |
| 699 | bool findChooseRuleConcept ( const CWDArray& label, BipolarPointer C, DepSet& Dep ) |
| 700 | { |
| 701 | if ( C == bpTOP ) |
| 702 | return true; |
| 703 | if ( findConceptClash ( label, C, Dep ) ) |
| 704 | { |
| 705 | Dep.add(getClashSet()); |
| 706 | return true; |
| 707 | } |
| 708 | else if ( findConceptClash ( label, inverse(C), Dep ) ) |
| 709 | { |
| 710 | Dep.add(getClashSet()); |
| 711 | return false; |
| 712 | } |
| 713 | else |
| 714 | fpp_unreachable(); |
| 715 | } |
| 716 | /// apply choose-rule for all vertices (necessary for Top role in QCR) |
| 717 | bool applyChooseRuleGlobally ( BipolarPointer C ); |
| 718 | /// aux method which fills EdgesToMerge with *different* ROLE-neighbours of curNode |
| 719 | void findNeighbours ( const TRole* Role, BipolarPointer C, DepSet& Dep ); |
| 720 | /// the same but for universal role |
| 721 | void findCLabelledNodes ( BipolarPointer C, DepSet& Dep ); |
| 722 | /// aux method that checks whether clash occurs during the merge of labels |
| 723 | bool checkMergeClash ( const CGLabel& from, const CGLabel& to, const DepSet& dep, unsigned int nodeId ); |
| 724 | /// aux method that merge FROM label to the TO node with an appropriate dep-set |
| 725 | bool mergeLabels ( const CGLabel& from, DlCompletionTree* to, const DepSet& dep ); |
| 726 | /// merge FROM node into TO node with additional dep-set DEPF |
| 727 | bool Merge ( DlCompletionTree* from, DlCompletionTree* to, const DepSet& depF ); |
| 728 | /// check whether clash occurs due to new edge from FROM to TO labelled with R |
| 729 | bool checkDisjointRoleClash ( const DlCompletionTree* from, const DlCompletionTree* to, |
| 730 | const TRole* R, const DepSet& dep ); |
| 731 | /// check whether clash occurs EDGE to TO labelled with S disjoint with R |
| 732 | bool checkDisjointRoleClash ( const DlCompletionTreeArc* edge, const DlCompletionTree* to, |
| 733 | const TRole* R, const DepSet& dep ) |
| 734 | { // clash found |
| 735 | if ( edge->getArcEnd() == to && edge->getRole()->isDisjoint(R) ) |
| 736 | { |
| 737 | setClashSet(dep); |
| 738 | updateClashSet(edge->getDep()); |
| 739 | return true; |
| 740 | } |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | // support for FORALL expansion |
| 745 | |
| 746 | /** Perform expansion of (\neg \ER.Self).DEP to an EDGE */ |
| 747 | bool checkIrreflexivity ( const DlCompletionTreeArc* edge, |
| 748 | const TRole* R, const DepSet& dep ) |
| 749 | { |
| 750 | // only loops counts here... |
| 751 | if ( edge->getArcEnd() != edge->getReverse()->getArcEnd() ) |
| 752 | return false; |
| 753 | // which are labelled either with R or with R- |
| 754 | if ( !edge->isNeighbour(R) && !edge->isNeighbour(R->inverse()) ) |
| 755 | return false; |
| 756 | |
| 757 | // set up clash |
| 758 | setClashSet(dep); |
| 759 | updateClashSet(edge->getDep()); |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | /** Perform expansion of (C=\AR{0}.X).DEP wrt RST to an EDGE with a given reason */ |
| 764 | bool applyTransitions ( const DlCompletionTreeArc* edge, const RAStateTransitions& RST, |
| 765 | BipolarPointer C, const DepSet& dep, const char* reason = nullptr ); |
| 766 | |
| 767 | // support for the projection |
| 768 | |
| 769 | /// apply projection to given edge if necessary |
| 770 | bool checkProjection ( DlCompletionTreeArc* pA, BipolarPointer C, const TRole* ProjR ); |
| 771 | |
| 772 | // datatype staff |
| 773 | |
| 774 | /// @return true iff given data node contains inconsistent data constraints |
| 775 | bool hasDataClash ( const DlCompletionTree* node ); |
| 776 | /// @return true iff given data node contains inconsistent data constraints |
| 777 | bool checkDataClash ( const DlCompletionTree* node ) |
| 778 | { |
| 779 | if ( hasDataClash(node) ) |
| 780 | { |
| 781 | setClashSet(DTReasoner.getClashSet()); |
| 782 | return true; |
| 783 | } |
| 784 | else |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | // logging actions |
| 789 | |
| 790 | /// log indentation |
| 791 | void logIndentation ( void ) const; |
| 792 | /// log start of processing of a ToDo entry |
| 793 | void logStartEntry ( void ) const; |
| 794 | /// log finish of processing of a ToDo entry |
| 795 | void logFinishEntry ( bool res ) const; |
| 796 | /// log the result of processing ACTION with entry (N,C{DEP})/REASON |
| 797 | void logNCEntry ( const DlCompletionTree* n, const ConceptWDep& C, |
| 798 | const char* action, const char* reason ) const |
| 799 | { |
| 800 | CHECK_LL_RETURN(llGTA); // useless, but safe |
| 801 | |
| 802 | LL << " " << action << "("; |
| 803 | n->logNode(); |
| 804 | LL << "," << C.bp() << C.getDep() << ")"; |
| 805 | if ( reason ) |
| 806 | LL << reason; |
| 807 | } |
| 808 | /// log addition of the entry to ToDo list |
| 809 | void logEntry ( const DlCompletionTree* n, const ConceptWDep& C, const char* reason ) const |
| 810 | { logNCEntry ( n, C, "+", reason ); } |
| 811 | /// log clash happened during node processing |
| 812 | void logClash ( const DlCompletionTree* n, const ConceptWDep& C ) const |
| 813 | { logNCEntry ( n, C, "x", DLHeap[C].getTagName() ); } |
| 814 | /// write root subtree of CG with given LEVEL |
| 815 | void writeRoot ( unsigned int level ) |
| 816 | { |
| 817 | if ( LLM.isWritable(level) ) |
| 818 | CGraph.Print(LL); |
| 819 | } |
| 820 | |
| 821 | /// merge session statistics to the global one |
| 822 | void finaliseStatistic ( void ); |
| 823 | /// write down statistics wrt LOCAL flag |
| 824 | void logStatisticData ( std::ostream& o, bool needLocal ) const; |
| 825 | |
| 826 | // save/restore methods |
| 827 | |
| 828 | /// restore local state from current branching context |
| 829 | void restoreBC ( void ); |
| 830 | /// use this method in ALL dependency stuff (never use tryLevel directly) |
| 831 | unsigned int getCurLevel ( void ) const { return tryLevel; } |
| 832 | /// set new branching level (never use tryLevel directly) |
| 833 | void setCurLevel ( unsigned int level ) { tryLevel = level; } |
| 834 | /// @return true if no branching ops were applied during reasoners; FIXME!! doesn't work properly with a nominal cloud |
| 835 | bool noBranchingOps ( void ) const { return tryLevel == InitBranchingLevelValue + nonDetShift; } |
| 836 | /// Get save/restore level based on either current- or DS level |
| 837 | unsigned int getSaveRestoreLevel ( const DepSet& ds ) const |
| 838 | { |
| 839 | // FIXME!!! see more precise it later |
| 840 | if ( RKG_USE_DYNAMIC_BACKJUMPING ) |
| 841 | return ds.level()+1; |
| 842 | else |
| 843 | return getCurLevel(); |
| 844 | } |
| 845 | /// save current reasoning state |
| 846 | void save ( void ); |
| 847 | /// restore reasoning state to the latest saved position |
| 848 | void restore ( void ) { return restore(getCurLevel()-1); } |
| 849 | /// restore reasoning state to the NEWTRYLEVEL position |
| 850 | void restore ( unsigned int newTryLevel ); |
| 851 | /// update level in N node and save it's state (if necessary) |
| 852 | void updateLevel ( DlCompletionTree* n, const DepSet& ds ) { CGraph.saveNode ( n, getSaveRestoreLevel(ds) ); } |
| 853 | /// finalize branching OP processing making deterministic op |
| 854 | void determiniseBranchingOp ( void ) |
| 855 | { |
| 856 | clearBC(); // clear context for the next branching op |
| 857 | Stack.pop(); // remove unnecessary context from the stack |
| 858 | } |
| 859 | |
| 860 | // access to global clashset, which contains result of clash during label addition |
| 861 | |
| 862 | /// get value of global dep-set |
| 863 | const DepSet& getClashSet ( void ) const { return clashSet; } |
| 864 | /// set value of global dep-set to D |
| 865 | void setClashSet ( const DepSet& d ) { clashSet = d; } |
| 866 | /// add D to global dep-set |
| 867 | void updateClashSet ( const DepSet& d ) { clashSet.add(d); } |
| 868 | /// get dep-set wrt current level |
| 869 | DepSet getCurDepSet ( void ) const { return DepSet(Manager.get(getCurLevel()-1)); } |
| 870 | |
| 871 | /// get RW access to current branching dep-set |
| 872 | DepSet& getBranchDep ( void ) { return bContext->branchDep; } |
| 873 | /// get RO access to current branching dep-set |
| 874 | const DepSet& getBranchDep ( void ) const { return bContext->branchDep; } |
| 875 | /// prepare cumulative dep-set to usage |
| 876 | void prepareBranchDep ( void ) { getBranchDep().restrict(getCurLevel()); } |
| 877 | /// prepare cumulative dep-set and copy it to the general clash-set |
| 878 | void useBranchDep ( void ) |
| 879 | { |
| 880 | prepareBranchDep(); |
| 881 | setClashSet(getBranchDep()); |
| 882 | } |
| 883 | /// update cumulative branch-dep with current clash-set and move options forward |
| 884 | void nextBranchingOption ( void ) |
| 885 | { |
| 886 | getBranchDep().add(getClashSet()); |
| 887 | bContext->nextOption(); |
| 888 | } |
| 889 | |
| 890 | /// restore one level (no backjumping) |
| 891 | bool straightforwardRestore ( void ); |
| 892 | /// restore if backjumping is used |
| 893 | bool backJumpedRestore ( void ); |
| 894 | /// restore state based on usedBackjumping flag |
| 895 | bool tunedRestore ( void ); |
| 896 | |
| 897 | /// check if P was used during current reasoning session |
| 898 | bool isUsed ( BipolarPointer p ) const { return ( isPositive(p) ? pUsed : nUsed ).in(getValue(p)); } |
| 899 | /// set P as a used during current reasoning |
| 900 | void setUsed ( BipolarPointer p ) { ( isPositive(p) ? pUsed : nUsed ).add(getValue(p)); } |
| 901 | |
| 902 | public: // rule's support |
| 903 | /// @return true if the rule is applicable; set the dep-set accordingly |
| 904 | bool applicable ( const TBox::TSimpleRule& rule ); |
| 905 | |
| 906 | public: // blocking support |
| 907 | /// re-apply all the relevant expansion rules to a given unblocked NODE |
| 908 | void repeatUnblockedNode ( DlCompletionTree* node, bool direct ) |
| 909 | { |
| 910 | if ( direct ) // not blocked -- clear blocked cache |
| 911 | applyAllGeneratingRules(node); // re-apply all the generating rules |
| 912 | else |
| 913 | redoNodeLabel ( node, "ubi" ); |
| 914 | } |
| 915 | |
| 916 | /// get access to the DAG associated with it (necessary for the blocking support) |
| 917 | const DLDag& getDAG ( void ) const { return tBox.DLHeap; } |
| 918 | |
| 919 | public: |
| 920 | /// c'tor |
| 921 | explicit DlSatTester ( TBox& tbox ); |
| 922 | /// no copy c'tor |
| 923 | DlSatTester ( const DlSatTester& ) = delete; |
| 924 | /// no assignment |
| 925 | DlSatTester& operator = ( const DlSatTester& ) = delete; |
| 926 | /// d'tor |
| 927 | virtual ~DlSatTester() = default; |
| 928 | |
| 929 | /// prepare reasoner to a new run |
| 930 | virtual void prepareReasoner ( void ); |
| 931 | /// set-up satisfiability task for given pointers and run runSat on it |
| 932 | bool runSat ( BipolarPointer p, BipolarPointer q = bpTOP ); |
| 933 | /// set-up role disjointness task for given roles and run SAT test |
| 934 | bool checkDisjointRoles ( const TRole* R, const TRole* S ); |
| 935 | /// set-up role irreflexivity task for R and run SAT test |
| 936 | bool checkIrreflexivity ( const TRole* R ); |
| 937 | |
| 938 | /// get the ROOT node of the completion graph |
| 939 | const DlCompletionTree* getRootNode ( void ) const { return CGraph.getRoot(); } |
| 940 | |
| 941 | /// set blocking method for a session |
| 942 | void setBlockingMethod ( bool hasInverse, bool hasQCR ) { CGraph.setBlockingMethod ( hasInverse, hasQCR ); } |
| 943 | |
| 944 | /// build cache entry for given DAG node, using cascaded schema; @return cache |
| 945 | const modelCacheInterface* createCache ( BipolarPointer p ); |
| 946 | /// create model cache for the just-classified entry |
| 947 | modelCacheInterface* buildCacheByCGraph ( bool sat ) const |
| 948 | { |
| 949 | if ( sat ) // here we need actual (not a p-blocked) root of the tree |
| 950 | return createModelCache(getRootNode()); |
| 951 | else // unsat => cache is just bottom |
| 952 | return createConstCache(bpBOTTOM); |
| 953 | } |
| 954 | |
| 955 | void writeTotalStatistic ( std::ostream& o ) |
| 956 | { |
| 957 | # ifdef USE_REASONING_STATISTICS |
| 958 | AccumulatedStatistic::accumulateAll(); // ensure that the last reasoning results are in |
| 959 | logStatisticData ( o, /*needLocal=*/false ); |
| 960 | # endif |
| 961 | printBlockingStat (o); |
| 962 | clearBlockingStat(); |
| 963 | o << "\n"; |
| 964 | } |
| 965 | |
| 966 | /// print SAT/SUB timings to O; @return total time spend during reasoning |
| 967 | float printReasoningTime ( std::ostream& o ) const; |
| 968 | }; // DlSatTester |
| 969 | |
| 970 | // implementation |
| 971 | |
| 972 | inline void DlSatTester :: resetSessionFlags ( void ) |
| 973 | { |
| 974 | // reflect possible change of DAG size |
| 975 | ensureDAGSize(); |
| 976 | |
| 977 | setUsed(bpTOP); |
| 978 | setUsed(bpBOTTOM); |
| 979 | |
| 980 | encounterNominal = false; |
| 981 | checkDataNode = true; |
| 982 | } |
| 983 | |
| 984 | inline bool |
| 985 | DlSatTester :: initNewNode ( DlCompletionTree* node, const DepSet& dep, BipolarPointer C ) |
| 986 | { |
| 987 | if ( node->isDataNode() ) // creating new data node -- do data check once in the end |
| 988 | checkDataNode = false; |
| 989 | node->setInit(C); |
| 990 | if ( unlikely ( addToDoEntry ( node, C, dep ) ) ) |
| 991 | return true; |
| 992 | if ( node->isDataNode() ) |
| 993 | return false; |
| 994 | if ( unlikely ( addToDoEntry ( node, tBox.getTG(), dep ) ) ) |
| 995 | return true; |
| 996 | if ( unlikely ( GCIs.isReflexive() && applyReflexiveRoles ( node, dep ) ) ) |
| 997 | return true; |
| 998 | if ( unlikely ( !SessionGCIs.empty() ) ) |
| 999 | for ( std::vector<BipolarPointer>::iterator p = SessionGCIs.begin(), p_end = SessionGCIs.end(); p != p_end; ++p ) |
| 1000 | if ( unlikely ( addToDoEntry ( node, *p, dep, "sg" ) ) ) |
| 1001 | return true; |
| 1002 | |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
| 1006 | inline bool |
| 1007 | DlSatTester :: runSat ( BipolarPointer p, BipolarPointer q ) |
| 1008 | { |
| 1009 | prepareReasoner(); |
| 1010 | |
| 1011 | // use general method to init node with P and add Q then |
| 1012 | if ( initNewNode ( CGraph.getRoot(), DepSet(), p ) || |
| 1013 | addToDoEntry ( CGraph.getRoot(), ConceptWDep(q) ) ) |
| 1014 | return false; // concept[s] unsatisfiable |
| 1015 | |
| 1016 | // check satisfiability explicitly |
| 1017 | TsProcTimer& timer = q == bpTOP ? satTimer : subTimer; |
| 1018 | timer.Start(); |
| 1019 | bool result = runSat(); |
| 1020 | timer.Stop(); |
| 1021 | return result; |
| 1022 | } |
| 1023 | |
| 1024 | inline bool |
| 1025 | DlSatTester :: checkDisjointRoles ( const TRole* R, const TRole* S ) |
| 1026 | { |
| 1027 | prepareReasoner(); |
| 1028 | |
| 1029 | // use general method to init node... |
| 1030 | DepSet dummy; |
| 1031 | if ( initNewNode ( CGraph.getRoot(), dummy, bpTOP ) ) |
| 1032 | return true; |
| 1033 | // ... add edges with R and S... |
| 1034 | curNode = CGraph.getRoot(); |
| 1035 | DlCompletionTreeArc* edgeR = createOneNeighbour ( R, dummy ); |
| 1036 | DlCompletionTreeArc* edgeS = createOneNeighbour ( S, dummy ); |
| 1037 | // init new nodes/edges. No need to apply restrictions, as no reasoning have been done yet. |
| 1038 | if ( initNewNode ( edgeR->getArcEnd(), dummy, bpTOP ) |
| 1039 | || initNewNode ( edgeS->getArcEnd(), dummy, bpTOP ) |
| 1040 | || setupEdge ( edgeR, dummy ) |
| 1041 | || setupEdge ( edgeS, dummy ) |
| 1042 | || Merge ( edgeS->getArcEnd(), edgeR->getArcEnd(), dummy ) ) |
| 1043 | return true; |
| 1044 | |
| 1045 | // 2 roles are disjoint if current setting is unsatisfiable |
| 1046 | curNode = nullptr; |
| 1047 | return !runSat(); |
| 1048 | } |
| 1049 | |
| 1050 | inline bool |
| 1051 | DlSatTester :: checkIrreflexivity ( const TRole* R ) |
| 1052 | { |
| 1053 | prepareReasoner(); |
| 1054 | |
| 1055 | // use general method to init node... |
| 1056 | DepSet dummy; |
| 1057 | if ( initNewNode ( CGraph.getRoot(), dummy, bpTOP ) ) |
| 1058 | return true; |
| 1059 | // ... add an R-loop |
| 1060 | curNode = CGraph.getRoot(); |
| 1061 | DlCompletionTreeArc* edgeR = createOneNeighbour ( R, dummy ); |
| 1062 | // init new nodes/edges. No need to apply restrictions, as no reasoning have been done yet. |
| 1063 | if ( initNewNode ( edgeR->getArcEnd(), dummy, bpTOP ) |
| 1064 | || setupEdge ( edgeR, dummy ) |
| 1065 | || Merge ( edgeR->getArcEnd(), CGraph.getRoot(), dummy ) ) |
| 1066 | return true; |
| 1067 | |
| 1068 | // R is irreflexive if current setting is unsatisfiable |
| 1069 | curNode = nullptr; |
| 1070 | return !runSat(); |
| 1071 | } |
| 1072 | |
| 1073 | // restore implementation |
| 1074 | inline bool DlSatTester :: backJumpedRestore ( void ) |
| 1075 | { |
| 1076 | // if empty clash dep-set -- concept is unsatisfiable |
| 1077 | if ( getClashSet().empty () ) |
| 1078 | return true; |
| 1079 | |
| 1080 | // some non-deterministic choices were done |
| 1081 | restore ( getClashSet().level() ); |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | inline bool DlSatTester :: straightforwardRestore ( void ) |
| 1086 | { |
| 1087 | if ( noBranchingOps() ) // no non-deterministic choices was made |
| 1088 | return true; // ... the concept is unsatisfiable |
| 1089 | else |
| 1090 | { // restoring the state |
| 1091 | restore (); |
| 1092 | return false; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | inline bool DlSatTester :: tunedRestore ( void ) |
| 1097 | { |
| 1098 | if ( tBox.useBackjumping ) |
| 1099 | return backJumpedRestore (); |
| 1100 | else |
| 1101 | return straightforwardRestore (); |
| 1102 | } |
| 1103 | |
| 1104 | inline bool DlSatTester :: commonTacticBodyAll ( const DLVertex& cur ) |
| 1105 | { |
| 1106 | #ifdef ENABLE_CHECKING |
| 1107 | fpp_assert ( isPositive(curConcept.bp()) && cur.Type() == dtForall ); |
| 1108 | #endif |
| 1109 | |
| 1110 | if ( unlikely(cur.getRole()->isTop()) ) |
| 1111 | { |
| 1112 | incStat(nAllCalls); |
| 1113 | return addSessionGCI ( cur.getC(), curConcept.getDep() ); |
| 1114 | } |
| 1115 | // can't skip singleton models for complex roles due to empty transitions |
| 1116 | if ( cur.getRole()->isSimple() ) |
| 1117 | return commonTacticBodyAllSimple(cur); |
| 1118 | else |
| 1119 | return commonTacticBodyAllComplex(cur); |
| 1120 | } |
| 1121 | |
| 1122 | //----------------------------------------------------------------------------- |
| 1123 | //-- implementation of reasoner-related parts of TBox |
| 1124 | //----------------------------------------------------------------------------- |
| 1125 | |
| 1126 | inline const modelCacheInterface* |
| 1127 | TBox :: initCache ( const TConcept* pConcept, bool sub ) |
| 1128 | { |
| 1129 | BipolarPointer bp = sub ? inverse(pConcept->pName) : pConcept->pName; |
| 1130 | const modelCacheInterface* cache = DLHeap.getCache(bp); |
| 1131 | |
| 1132 | if ( cache == nullptr ) |
| 1133 | { |
| 1134 | if ( sub ) |
| 1135 | prepareFeatures ( nullptr, pConcept ); |
| 1136 | else |
| 1137 | prepareFeatures ( pConcept, nullptr ); |
| 1138 | cache = getReasoner()->createCache(bp); |
| 1139 | clearFeatures(); |
| 1140 | } |
| 1141 | |
| 1142 | return cache; |
| 1143 | } |
| 1144 | |
| 1145 | /// test if 2 concept non-subsumption can be determined by cache merging |
| 1146 | inline enum modelCacheState |
| 1147 | TBox :: testCachedNonSubsumption ( const TConcept* p, const TConcept* q ) |
| 1148 | { |
| 1149 | const modelCacheInterface* pCache = initCache ( p, /*sub=*/false ); |
| 1150 | const modelCacheInterface* nCache = initCache ( q, /*sub=*/true ); |
| 1151 | return pCache->canMerge(nCache); |
| 1152 | } |
| 1153 | |
| 1154 | #endif |